Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable injecting sidecar container via helm configuration. #749

Merged
merged 11 commits into from
Sep 29, 2021
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +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 injecting sidecar container via helm configuration. [[GH-749](https://github.com/hashicorp/consul-k8s/pull/749)]
NicoletaPopoviciu marked this conversation as resolved.
Show resolved Hide resolved
## 0.34.1 (September 17, 2021)

BUG FIXES:
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
80 changes: 80 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,83 @@ rollingUpdate:
[ "$status" -eq 1 ]
[[ "$output" =~ "global.adminPartitions.name has to be \"default\" in the server cluster" ]]
}

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

@test "client/DaemonSet: adds extra client container" {
NicoletaPopoviciu marked this conversation as resolved.
Show resolved Hide resolved
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: adds two extra client containers" {
NicoletaPopoviciu marked this conversation as resolved.
Show resolved Hide resolved
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' | tee /dev/stderr)

local containers_count=$(echo $object |
yq -r 'length' | tee /dev/stderr)
[ "${containers_count}" = 3 ]

}

@test "client/DaemonSet: no extra client containers added" {
NicoletaPopoviciu marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@test "client/DaemonSet: no extra client containers added" {
@test "client/DaemonSet: no extra 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' | tee /dev/stderr)

local containers_count=$(echo $object |
yq -r 'length' | tee /dev/stderr)
[ "${containers_count}" = 1 ]
NicoletaPopoviciu marked this conversation as resolved.
Show resolved Hide resolved
}
81 changes: 80 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,83 @@ 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' | tee /dev/stderr)

local containers_count=$(echo $object |
yq -r 'length' | tee /dev/stderr)
[ "${containers_count}" = 3 ]

}

@test "server/StatefulSet: no extra containers added" {
NicoletaPopoviciu marked this conversation as resolved.
Show resolved Hide resolved
cd `chart_dir`

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

local containers_count=$(echo $object |
yq -r 'length' | tee /dev/stderr)
[ "${containers_count}" = 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.
# @type: array<string>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is an array of objects/maps

Suggested change
# @type: array<string>
# @type: array<map>

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the @type line needs to be the last in the comment. For example:

  # Example:
  #
  # ```yaml
  # imagePullSecrets:
  #   - name: pull-secret-name
  #   - name: pull-secret-name-2
  # ```
  # @type: array<map>

# Example:
#
# ```yaml
# extraContainers:
# - name: extra-container
# image: example-image:latest
# command:
# - ...
# ```
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.
# @type: array<string>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# @type: array<string>
# @type: array<map>

# Example:
#
# ```yaml
# extraContainers:
# - name: extra-container
# image: example-image:latest
# command:
# - ...
# ```
extraContainers: []

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