Skip to content

Commit 628c207

Browse files
authored
add support for hostAliases in pod templates (#153)
1 parent 60b626c commit 628c207

File tree

4 files changed

+67
-6
lines changed

4 files changed

+67
-6
lines changed

charts/k8s-service/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,10 +1034,10 @@ To configure a canary deployment, set `canary.enabled = true` and define the `co
10341034

10351035
```yaml
10361036
canary:
1037-
enabled: true
1037+
enabled: true
10381038
containerImage:
1039-
repository: nginx
1040-
tag: 1.15.9
1039+
repository: nginx
1040+
tag: 1.15.9
10411041
```
10421042
Once deployed, your service will route traffic across both your stable and canary deployments, allowing you to monitor for and catch any issues early.
10431043

charts/k8s-service/templates/_deployment_spec.tpl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ spec:
140140
securityContext:
141141
{{ toYaml .Values.podSecurityContext | indent 8 }}
142142
{{- end}}
143+
{{- if .Values.hostAliases }}
144+
hostAliases:
145+
{{ toYaml .Values.hostAliases | indent 8 }}
146+
{{- end }}
143147
144148
containers:
145149
{{- if .isCanary }}

charts/k8s-service/values.yaml

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,16 +110,40 @@ livenessProbe: {}
110110
# port: http
111111
readinessProbe: {}
112112

113-
# securityContext is a map that specified the privillege and access control settings for a Pod of Container. Security Context
113+
# hostAliases is a list of maps that specify additional entries to be added to the pod's `/etc/hosts` file. This is useful
114+
# for adding custom DNS entries to the pod. The items in the list are maps with the following keys:
115+
# - ip (string) (required) : The IP address of the host.
116+
# - hostnames (list[string]) (required) : A list of hostnames that should be resolved to the IP address.
117+
#
118+
# Refer to https://kubernetes.io/docs/tasks/network/customize-hosts-file-for-pods/ for more details.
119+
#
120+
# EXAMPLE:
121+
#
122+
# The following example specifies two aliases to be added to the pod's /etc/hosts file in a new section at the bottom:
123+
#
124+
# hostAliases:
125+
# - ip: 127.0.0.1
126+
# hostnames:
127+
# - foo.local
128+
# - bar.local
129+
# - ip: 10.1.2.3
130+
# hostnames:
131+
# - foo.remote
132+
# - bar.remote
133+
#
134+
# NOTE: This variable is injected directly into the deployment spec.
135+
hostAliases: []
136+
137+
# securityContext is a map that specified the privilege and access control settings for a Pod of Container. Security Context
114138
# can be specified when the application requires additional access control permissions. More details on securityContext and supported
115139
# settings can be found at https://kubernetes.io/docs/tasks/configure-pod-container/security-context/
116140
# similar to the podSecurityContext {} however, this sets security attributes at the container level rather than at the pod level scope.
117141

118142
#
119143
# EXAMPLE:
120-
# 1) To run a container in privilleged mode
144+
# 1) To run a container in privileged mode
121145
# securityContext:
122-
# privilleged: true
146+
# privileged: true
123147
#
124148
# 2) To run a container as a specific user
125149
# securityContext:

test/k8s_service_template_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,39 @@ func TestK8SServiceWithContainerArgsHasArgsSpec(t *testing.T) {
628628
assert.Equal(t, appContainer.Args, []string{"echo", "Hello world"})
629629
}
630630

631+
// Test that omitting hostAliases does not set hostAliases attribute on the Deployment container spec.
632+
func TestK8SServiceDefaultHasNullHostAliasesSpec(t *testing.T) {
633+
t.Parallel()
634+
635+
deployment := renderK8SServiceDeploymentWithSetValues(t, map[string]string{})
636+
renderedPodSpec := deployment.Spec.Template.Spec
637+
assert.Nil(t, renderedPodSpec.HostAliases)
638+
}
639+
640+
// Test that setting hostAliases sets the hostAliases attribute on the Deployment container spec.
641+
func TestK8SServiceWithHostAliasesHasHostAliasesSpec(t *testing.T) {
642+
t.Parallel()
643+
644+
deployment := renderK8SServiceDeploymentWithSetValues(
645+
t,
646+
map[string]string{
647+
"hostAliases[0].ip": "127.0.0.1",
648+
"hostAliases[0].hostnames[0]": "foo.local",
649+
"hostAliases[0].hostnames[1]": "bar.local",
650+
"hostAliases[1].ip": "10.1.2.3",
651+
"hostAliases[1].hostnames[0]": "foo.remote",
652+
"hostAliases[1].hostnames[1]": "bar.remote",
653+
},
654+
)
655+
renderedPodSpec := deployment.Spec.Template.Spec
656+
assert.Equal(t, len(renderedPodSpec.HostAliases), 2)
657+
// order should be preserved, since order is important for /etc/hosts
658+
assert.Equal(t, renderedPodSpec.HostAliases[0].IP, "127.0.0.1")
659+
assert.Equal(t, renderedPodSpec.HostAliases[0].Hostnames, []string{"foo.local", "bar.local"})
660+
assert.Equal(t, renderedPodSpec.HostAliases[1].IP, "10.1.2.3")
661+
assert.Equal(t, renderedPodSpec.HostAliases[1].Hostnames, []string{"foo.remote", "bar.remote"})
662+
}
663+
631664
// Test that providing tls configuration to Ingress renders correctly
632665
func TestK8SServiceIngressMultiCert(t *testing.T) {
633666
t.Parallel()

0 commit comments

Comments
 (0)