From dcd5828fbbd1c073b6211cbd10f14c590b5510be Mon Sep 17 00:00:00 2001 From: Flavio Percoco Date: Thu, 19 Mar 2020 16:29:24 +0000 Subject: [PATCH] Accept a string as extraInitContainers value for filebeat This makes the filebeat chart consistent with the other beats, while preserving backwards compatibility. Fixes #490 --- filebeat/README.md | 2 +- filebeat/templates/daemonset.yaml | 10 ++++++++++ filebeat/tests/filebeat_test.py | 18 +++++++++++++++++- 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/filebeat/README.md b/filebeat/README.md index 22d4d3924..148111a67 100644 --- a/filebeat/README.md +++ b/filebeat/README.md @@ -61,7 +61,7 @@ helm install --name filebeat elastic/filebeat --set imageTag=7.6.1 | `filebeatConfig` | Allows you to add any config files in `/usr/share/filebeat` such as `filebeat.yml`. See [values.yaml](https://github.com/elastic/helm-charts/tree/master/filebeat/values.yaml) for an example of the formatting with the default configuration. | see [values.yaml](https://github.com/elastic/helm-charts/tree/master/filebeat/values.yaml) | | `extraContainers` | List of additional init containers to be added at the Daemonset | `""` | | `extraEnvs` | Extra [environment variables](https://kubernetes.io/docs/tasks/inject-data-application/define-environment-variable-container/#using-environment-variables-inside-of-your-config) which will be appended to the `env:` definition for the container | `[]` | -| `extraInitContainers` | List of additional init containers to be added at the Daemonset | `[]` | +| `extraInitContainers` | List of additional init containers to be added at the Daemonset. It also accepts a templatable string of additional containers to be passed to the `tpl` function | `[]` | | `extraVolumeMounts` | List of additional volumeMounts to be mounted on the Daemonset | `[]` | | `extraVolumes` | List of additional volumes to be mounted on the Daemonset | `[]` | | `envFrom` | Templatable string of envFrom to be passed to the [environment from variables](https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/#configure-all-key-value-pairs-in-a-configmap-as-container-environment-variables) which will be appended to the `envFrom:` definition for the container | `[]` | diff --git a/filebeat/templates/daemonset.yaml b/filebeat/templates/daemonset.yaml index d2f5a234f..5c0063f67 100644 --- a/filebeat/templates/daemonset.yaml +++ b/filebeat/templates/daemonset.yaml @@ -87,7 +87,17 @@ spec: {{- end }} {{- if .Values.extraInitContainers }} initContainers: + # All the other beats accept a string here while + # filebeat accepts a valid yaml array. We're keeping + # this as a backwards compatible change, while adding + # also a way to pass a string as other templates to + # make these implementations consistent. + # https://github.com/elastic/helm-charts/issues/490 + {{- if eq "string" (printf "%T" .Values.extraInitContainers) }} +{{ tpl .Values.extraInitContainers . | indent 8 }} + {{- else }} {{ toYaml .Values.extraInitContainers | indent 8 }} + {{- end }} {{- end }} containers: - name: "filebeat" diff --git a/filebeat/tests/filebeat_test.py b/filebeat/tests/filebeat_test.py index dbb03497d..a3eaff7c4 100644 --- a/filebeat/tests/filebeat_test.py +++ b/filebeat/tests/filebeat_test.py @@ -74,7 +74,7 @@ def test_adding_a_extra_container(): } in extraContainer -def test_adding_init_containers(): +def test_adding_init_containers_as_yaml(): config = """ extraInitContainers: - name: dummy-init @@ -90,6 +90,22 @@ def test_adding_init_containers(): } in initContainers +def test_adding_init_containers(): + config = """ +extraInitContainers: | + - name: dummy-init + image: busybox + command: ['echo', 'hey'] +""" + r = helm_template(config) + initContainers = r["daemonset"][name]["spec"]["template"]["spec"]["initContainers"] + assert { + "name": "dummy-init", + "image": "busybox", + "command": ["echo", "hey"], + } in initContainers + + def test_adding_image_pull_secrets(): config = """ imagePullSecrets: