Skip to content

Commit

Permalink
Enable adding extra containers to clients and servers via helm config…
Browse files Browse the repository at this point in the history
…uration. (#749)


Co-authored-by: Iryna Shustava <ishustava@users.noreply.github.com>
Co-authored-by: Luke Kysow <1034429+lkysow@users.noreply.github.com>
  • Loading branch information
3 people authored Sep 29, 2021
1 parent d87f2e6 commit 28128f6
Show file tree
Hide file tree
Showing 6 changed files with 186 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
IMPROVEMENTS:
* Control Plane
* Upgrade Docker image Alpine version from 3.13 to 3.14. [[GH-737](https://github.com/hashicorp/consul-k8s/pull/737)]
* Helm Chart
* Enable adding extra containers to server and client Pods. [[GH-749](https://github.com/hashicorp/consul-k8s/pull/749)]

## 0.34.1 (September 17, 2021)

Expand Down
3 changes: 3 additions & 0 deletions charts/consul/templates/client-daemonset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,9 @@ spec:
securityContext:
{{- toYaml .Values.client.containerSecurityContext.client | nindent 12 }}
{{- end }}
{{- if .Values.client.extraContainers }}
{{ toYaml .Values.client.extraContainers | nindent 8 }}
{{- end }}
{{- if (or .Values.global.acls.manageSystemACLs (and .Values.global.tls.enabled (not .Values.global.tls.enableAutoEncrypt))) }}
initContainers:
{{- if .Values.global.acls.manageSystemACLs }}
Expand Down
3 changes: 3 additions & 0 deletions charts/consul/templates/server-statefulset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,9 @@ spec:
securityContext:
{{- toYaml .Values.server.containerSecurityContext.server | nindent 12 }}
{{- end }}
{{- if .Values.server.extraContainers }}
{{ toYaml .Values.server.extraContainers | nindent 8 }}
{{- end }}
{{- if .Values.server.nodeSelector }}
nodeSelector:
{{ tpl .Values.server.nodeSelector . | indent 8 | trim }}
Expand Down
76 changes: 76 additions & 0 deletions charts/consul/test/unit/client-daemonset.bats
Original file line number Diff line number Diff line change
Expand Up @@ -1419,3 +1419,79 @@ rollingUpdate:
[ "$status" -eq 1 ]
[[ "$output" =~ "global.adminPartitions.name has to be \"default\" in the server cluster" ]]
}

#--------------------------------------------------------------------
# extraContainers

@test "client/DaemonSet: extraContainers adds extra container" {
cd `chart_dir`

# Test that it defines the extra container
local object=$(helm template \
-s templates/client-daemonset.yaml \
--set 'client.extraContainers[0].image=test-image' \
--set 'client.extraContainers[0].name=test-container' \
--set 'client.extraContainers[0].ports[0].name=test-port' \
--set 'client.extraContainers[0].ports[0].containerPort=9410' \
--set 'client.extraContainers[0].ports[0].protocol=TCP' \
--set 'client.extraContainers[0].env[0].name=TEST_ENV' \
--set 'client.extraContainers[0].env[0].value=test_env_value' \
. | tee /dev/stderr |
yq -r '.spec.template.spec.containers[] | select(.name == "test-container")' | tee /dev/stderr)

local actual=$(echo $object |
yq -r '.name' | tee /dev/stderr)
[ "${actual}" = "test-container" ]

local actual=$(echo $object |
yq -r '.image' | tee /dev/stderr)
[ "${actual}" = "test-image" ]

local actual=$(echo $object |
yq -r '.ports[0].name' | tee /dev/stderr)
[ "${actual}" = "test-port" ]

local actual=$(echo $object |
yq -r '.ports[0].containerPort' | tee /dev/stderr)
[ "${actual}" = "9410" ]

local actual=$(echo $object |
yq -r '.ports[0].protocol' | tee /dev/stderr)
[ "${actual}" = "TCP" ]

local actual=$(echo $object |
yq -r '.env[0].name' | tee /dev/stderr)
[ "${actual}" = "TEST_ENV" ]

local actual=$(echo $object |
yq -r '.env[0].value' | tee /dev/stderr)
[ "${actual}" = "test_env_value" ]

}

@test "client/DaemonSet: extraContainers supports adding two containers" {
cd `chart_dir`

local object=$(helm template \
-s templates/client-daemonset.yaml \
--set 'client.extraContainers[0].image=test-image' \
--set 'client.extraContainers[0].name=test-container' \
--set 'client.extraContainers[1].image=test-image' \
--set 'client.extraContainers[1].name=test-container-2' \
. | tee /dev/stderr |
yq -r '.spec.template.spec.containers | length' | tee /dev/stderr)

[ "${object}" = 3 ]

}

@test "client/DaemonSet: no extra client containers added by default" {
cd `chart_dir`

local object=$(helm template \
-s templates/client-daemonset.yaml \
. | tee /dev/stderr |
yq -r '.spec.template.spec.containers | length' | tee /dev/stderr)

[ "${object}" = 1 ]
}
77 changes: 76 additions & 1 deletion charts/consul/test/unit/server-statefulset.bats
Original file line number Diff line number Diff line change
Expand Up @@ -1308,7 +1308,6 @@ load _helpers
[ "${actual}" = '{"name":"CONSUL_LICENSE_PATH","value":"/consul/license/bar"}' ]
}


@test "server/StatefulSet: -recursor can be set by global.recursors" {
cd `chart_dir`
local actual=$(helm template \
Expand All @@ -1318,3 +1317,79 @@ load _helpers
yq -r -c '.spec.template.spec.containers[0].command | join(" ") | contains("-recursor=\"1.2.3.4\"")' | tee /dev/stderr)
[ "${actual}" = "true" ]
}

#--------------------------------------------------------------------
# extraContainers

@test "server/StatefulSet: adds extra container" {
cd `chart_dir`

# Test that it defines the extra container
local object=$(helm template \
-s templates/server-statefulset.yaml \
--set 'server.extraContainers[0].image=test-image' \
--set 'server.extraContainers[0].name=test-container' \
--set 'server.extraContainers[0].ports[0].name=test-port' \
--set 'server.extraContainers[0].ports[0].containerPort=9410' \
--set 'server.extraContainers[0].ports[0].protocol=TCP' \
--set 'server.extraContainers[0].env[0].name=TEST_ENV' \
--set 'server.extraContainers[0].env[0].value=test_env_value' \
. | tee /dev/stderr |
yq -r '.spec.template.spec.containers[] | select(.name == "test-container")' | tee /dev/stderr)

local actual=$(echo $object |
yq -r '.name' | tee /dev/stderr)
[ "${actual}" = "test-container" ]

local actual=$(echo $object |
yq -r '.image' | tee /dev/stderr)
[ "${actual}" = "test-image" ]

local actual=$(echo $object |
yq -r '.ports[0].name' | tee /dev/stderr)
[ "${actual}" = "test-port" ]

local actual=$(echo $object |
yq -r '.ports[0].containerPort' | tee /dev/stderr)
[ "${actual}" = "9410" ]

local actual=$(echo $object |
yq -r '.ports[0].protocol' | tee /dev/stderr)
[ "${actual}" = "TCP" ]

local actual=$(echo $object |
yq -r '.env[0].name' | tee /dev/stderr)
[ "${actual}" = "TEST_ENV" ]

local actual=$(echo $object |
yq -r '.env[0].value' | tee /dev/stderr)
[ "${actual}" = "test_env_value" ]

}

@test "server/StatefulSet: adds two extra containers" {
cd `chart_dir`

local object=$(helm template \
-s templates/server-statefulset.yaml \
--set 'server.extraContainers[0].image=test-image' \
--set 'server.extraContainers[0].name=test-container' \
--set 'server.extraContainers[1].image=test-image' \
--set 'server.extraContainers[1].name=test-container-2' \
. | tee /dev/stderr |
yq -r '.spec.template.spec.containers | length' | tee /dev/stderr)

[ "${object}" = 3 ]

}

@test "server/StatefulSet: no extra containers added by default" {
cd `chart_dir`

local object=$(helm template \
-s templates/server-statefulset.yaml \
. | tee /dev/stderr |
yq -r '.spec.template.spec.containers | length' | tee /dev/stderr)

[ "${object}" = 1 ]
}
26 changes: 26 additions & 0 deletions charts/consul/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,19 @@ server:
# @type: array<map>
extraVolumes: []

# A list of sidecar containers.
# Example:
#
# ```yaml
# extraContainers:
# - name: extra-container
# image: example-image:latest
# command:
# - ...
# ```
# @type: array<map>
extraContainers: []

# This value defines the affinity (https://kubernetes.io/docs/concepts/configuration/assign-pod-node/#affinity-and-anti-affinity)
# for server pods. It defaults to allowing only a single server pod on each node, which
# minimizes risk of the cluster becoming unusable if a node is lost. If you need
Expand Down Expand Up @@ -925,6 +938,19 @@ client:
# @type: array<map>
extraVolumes: []

# A list of sidecar containers.
# Example:
#
# ```yaml
# extraContainers:
# - name: extra-container
# image: example-image:latest
# command:
# - ...
# ```
# @type: array<map>
extraContainers: []

# Toleration Settings for Client pods
# This should be a multi-line string matching the Toleration array
# in a PodSpec.
Expand Down

0 comments on commit 28128f6

Please sign in to comment.