From 7bbad50437c6ec034dc2dedc27ebf7abbdcd431e Mon Sep 17 00:00:00 2001 From: Tetiana Kravchenko Date: Tue, 2 Jul 2019 11:01:11 +0200 Subject: [PATCH 1/7] Add option to provide custom start/stop hooks Signed-off-by: Tetiana Kravchenko --- elasticsearch/README.md | 2 + elasticsearch/templates/configmap.yaml | 9 ++- elasticsearch/templates/statefulset.yaml | 75 +++++------------------- elasticsearch/values.yaml | 16 +++++ 4 files changed, 41 insertions(+), 61 deletions(-) diff --git a/elasticsearch/README.md b/elasticsearch/README.md index f4df573bf..c65405b56 100644 --- a/elasticsearch/README.md +++ b/elasticsearch/README.md @@ -65,6 +65,7 @@ helm install --name elasticsearch elastic/elasticsearch --version 7.2.0 --set im | `minimumMasterNodes` | The value for [discovery.zen.minimum_master_nodes](https://www.elastic.co/guide/en/elasticsearch/reference/6.7/discovery-settings.html#minimum_master_nodes). Should be set to `(master_eligible_nodes / 2) + 1`. Ignored in Elasticsearch versions >= 7. | `2` | | `esMajorVersion` | Used to set major version specific configuration | `7` | | `esConfig` | Allows you to add any config files in `/usr/share/elasticsearch/config/` such as `elasticsearch.yml` and `log4j2.properties`. See [values.yaml](./values.yaml) for an example of the formatting. | `{}` | +| `hooks` | Allows you to add lifecycle preStop, postStart hooks. See [values.yaml](./values.yaml) for an example of the formatting. | `{}` | | `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 | `[]` | | `extraVolumes` | Additional volumes to be passed to the `tpl` function | | | `extraVolumeMounts` | Additional volumeMounts to be passed to the `tpl` function | | @@ -106,6 +107,7 @@ helm install --name elasticsearch elastic/elasticsearch --version 7.2.0 --set im | `ingress` | Configurable [ingress](https://kubernetes.io/docs/concepts/services-networking/ingress/) to expose the Elasticsearch service. See [`values.yaml`](./values.yaml) for an example | `enabled: false` | | `schedulerName` | Name of the [alternate scheduler](https://kubernetes.io/docs/tasks/administer-cluster/configure-multiple-schedulers/#specify-schedulers-for-pods) | `nil` | | `masterTerminationFix` | A workaround needed for Elasticsearch < 7.2.0 to prevent master status being lost during restarts [#63](https://github.com/elastic/helm-charts/issues/63) | `false` | +| `lifecycle` | Allows you to add lifecycle configuration. See [values.yaml](./values.yaml) for an example of the formatting. | `{}` | ## Try it out diff --git a/elasticsearch/templates/configmap.yaml b/elasticsearch/templates/configmap.yaml index fe5f0c3b8..89760ec01 100644 --- a/elasticsearch/templates/configmap.yaml +++ b/elasticsearch/templates/configmap.yaml @@ -1,5 +1,3 @@ -{{- if .Values.esConfig }} ---- apiVersion: v1 kind: ConfigMap metadata: @@ -10,8 +8,15 @@ metadata: chart: "{{ .Chart.Name }}-{{ .Chart.Version }}" app: "{{ template "uname" . }}" data: +{{- if .Values.esConfig }} {{- range $path, $config := .Values.esConfig }} {{ $path }}: | {{ $config | indent 4 -}} {{- end -}} {{- end -}} +{{- if .Values.hooks }} +{{- range $path, $config := .Values.hooks }} + {{ $path }}: | +{{ $config | indent 4 -}} +{{- end -}} +{{- end }} diff --git a/elasticsearch/templates/statefulset.yaml b/elasticsearch/templates/statefulset.yaml index 8f3a7d53f..cc936f171 100644 --- a/elasticsearch/templates/statefulset.yaml +++ b/elasticsearch/templates/statefulset.yaml @@ -23,7 +23,7 @@ spec: name: {{ template "uname" . }} {{- with .Values.persistence.annotations }} annotations: -{{ toYaml . | indent 8 }} + {{ toYaml . | indent 4 }} {{- end }} spec: {{ toYaml .Values.volumeClaimTemplate | indent 6 }} @@ -96,10 +96,10 @@ spec: {{- range .Values.secretMounts }} - name: {{ .name }} secret: - secretName: {{ .secretName }} + secretName: {{ .name }} {{- end }} - {{- if .Values.esConfig }} - - name: esconfig + {{- if or .Values.esConfig .Values.hooks }} + - name: config configMap: name: {{ template "uname" . }}-config {{- end }} @@ -215,64 +215,21 @@ spec: {{- end }} {{- end }} {{- range $path, $config := .Values.esConfig }} - - name: esconfig + - name: config mountPath: /usr/share/elasticsearch/config/{{ $path }} subPath: {{ $path }} {{- end -}} + {{- if .Values.hooks }} + {{- range $path, $config := .Values.hooks }} + - name: config + mountPath: /{{ $path }} + subPath: {{ $path }} + {{- end -}} + {{- end }} {{- if .Values.extraVolumeMounts }} {{ tpl .Values.extraVolumeMounts . | indent 10 }} {{- end }} - {{- if .Values.masterTerminationFix }} - {{- if eq .Values.roles.master "true" }} - # This sidecar will prevent slow master re-election - # https://github.com/elastic/helm-charts/issues/63 - - name: elasticsearch-master-graceful-termination-handler - image: "{{ .Values.image }}:{{ .Values.imageTag }}" - imagePullPolicy: "{{ .Values.imagePullPolicy }}" - command: - - "sh" - - -c - - | - #!/usr/bin/env bash - set -eo pipefail - - http () { - local path="${1}" - if [ -n "${ELASTIC_USERNAME}" ] && [ -n "${ELASTIC_PASSWORD}" ]; then - BASIC_AUTH="-u ${ELASTIC_USERNAME}:${ELASTIC_PASSWORD}" - else - BASIC_AUTH='' - fi - curl -XGET -s -k --fail ${BASIC_AUTH} {{ .Values.protocol }}://{{ template "masterService" . }}:{{ .Values.httpPort }}${path} - } - - cleanup () { - while true ; do - local master="$(http "/_cat/master?h=node" || echo "")" - if [[ $master == "{{ template "masterService" . }}"* && $master != "${NODE_NAME}" ]]; then - echo "This node is not master." - break - fi - echo "This node is still master, waiting gracefully for it to step down" - sleep 1 - done - - exit 0 - } - - trap cleanup SIGTERM - - sleep infinity & - wait $! - resources: -{{ toYaml .Values.sidecarResources | indent 10 }} - env: - - name: NODE_NAME - valueFrom: - fieldRef: - fieldPath: metadata.name - {{- if .Values.extraEnvs }} -{{ toYaml .Values.extraEnvs | indent 10 }} - {{- end }} - {{- end }} - {{- end }} +{{- if .Values.lifecycle }} + lifecycle: +{{ toYaml .Values.lifecycle | indent 10 }} +{{- end }} diff --git a/elasticsearch/values.yaml b/elasticsearch/values.yaml index 45846eae4..75507647f 100755 --- a/elasticsearch/values.yaml +++ b/elasticsearch/values.yaml @@ -27,6 +27,14 @@ esConfig: {} # log4j2.properties: | # key = value +# Add custom preStop, postStart lifecycle hooks +hooks: {} + ## (string) Script to execute prior the pod stops. + # preStop: |- + + ## (string) Script to execute after the pod starts. + # postStart: |- + # Extra environment variables to append to this nodeGroup # This will be appended to the current 'env:' key. You can use any of the kubernetes env # syntax here @@ -184,3 +192,11 @@ fullnameOverride: "" # https://github.com/elastic/helm-charts/issues/63 masterTerminationFix: false + +lifecycle: {} + # preStop: + # exec: + # command: ["/bin/bash","/preStop"] + # preStop: + # exec: + # command: ["/bin/bash","/preStop"] From 3aa3071a26ac3f49034dd56b53239a092be59ffd Mon Sep 17 00:00:00 2001 From: Tetiana Kravchenko Date: Tue, 2 Jul 2019 11:09:26 +0200 Subject: [PATCH 2/7] revert some changes Signed-off-by: Tetiana Kravchenko --- elasticsearch/templates/statefulset.yaml | 53 +++++++++++++++++++++++- 1 file changed, 51 insertions(+), 2 deletions(-) diff --git a/elasticsearch/templates/statefulset.yaml b/elasticsearch/templates/statefulset.yaml index cc936f171..5ab92ad1d 100644 --- a/elasticsearch/templates/statefulset.yaml +++ b/elasticsearch/templates/statefulset.yaml @@ -23,7 +23,7 @@ spec: name: {{ template "uname" . }} {{- with .Values.persistence.annotations }} annotations: - {{ toYaml . | indent 4 }} +{{ toYaml . | indent 8 }} {{- end }} spec: {{ toYaml .Values.volumeClaimTemplate | indent 6 }} @@ -96,7 +96,7 @@ spec: {{- range .Values.secretMounts }} - name: {{ .name }} secret: - secretName: {{ .name }} + secretName: {{ .secretName }} {{- end }} {{- if or .Values.esConfig .Values.hooks }} - name: config @@ -229,6 +229,55 @@ spec: {{- if .Values.extraVolumeMounts }} {{ tpl .Values.extraVolumeMounts . | indent 10 }} {{- end }} + {{- if .Values.masterTerminationFix }} + {{- if eq .Values.roles.master "true" }} + # This sidecar will prevent slow master re-election + # https://github.com/elastic/helm-charts/issues/63 + - name: elasticsearch-master-graceful-termination-handler + image: "{{ .Values.image }}:{{ .Values.imageTag }}" + imagePullPolicy: "{{ .Values.imagePullPolicy }}" + command: + - "sh" + - -c + - | + #!/usr/bin/env bash + set -eo pipefail + http () { + local path="${1}" + if [ -n "${ELASTIC_USERNAME}" ] && [ -n "${ELASTIC_PASSWORD}" ]; then + BASIC_AUTH="-u ${ELASTIC_USERNAME}:${ELASTIC_PASSWORD}" + else + BASIC_AUTH='' + fi + curl -XGET -s -k --fail ${BASIC_AUTH} {{ .Values.protocol }}://{{ template "masterService" . }}:{{ .Values.httpPort }}${path} + } + cleanup () { + while true ; do + local master="$(http "/_cat/master?h=node" || echo "")" + if [[ $master == "{{ template "masterService" . }}"* && $master != "${NODE_NAME}" ]]; then + echo "This node is not master." + break + fi + echo "This node is still master, waiting gracefully for it to step down" + sleep 1 + done + exit 0 + } + trap cleanup SIGTERM + sleep infinity & + wait $! + resources: +{{ toYaml .Values.sidecarResources | indent 10 }} + env: + - name: NODE_NAME + valueFrom: + fieldRef: + fieldPath: metadata.name + {{- if .Values.extraEnvs }} +{{ toYaml .Values.extraEnvs | indent 10 }} + {{- end }} + {{- end }} + {{- end }} {{- if .Values.lifecycle }} lifecycle: {{ toYaml .Values.lifecycle | indent 10 }} From 6ca958260d0a6d8e5416603f8703b19627bf23ac Mon Sep 17 00:00:00 2001 From: Tetiana Kravchenko Date: Tue, 2 Jul 2019 11:11:30 +0200 Subject: [PATCH 3/7] revert some changes Signed-off-by: Tetiana Kravchenko --- elasticsearch/templates/statefulset.yaml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/elasticsearch/templates/statefulset.yaml b/elasticsearch/templates/statefulset.yaml index 5ab92ad1d..b47680ded 100644 --- a/elasticsearch/templates/statefulset.yaml +++ b/elasticsearch/templates/statefulset.yaml @@ -242,6 +242,7 @@ spec: - | #!/usr/bin/env bash set -eo pipefail + http () { local path="${1}" if [ -n "${ELASTIC_USERNAME}" ] && [ -n "${ELASTIC_PASSWORD}" ]; then @@ -251,6 +252,7 @@ spec: fi curl -XGET -s -k --fail ${BASIC_AUTH} {{ .Values.protocol }}://{{ template "masterService" . }}:{{ .Values.httpPort }}${path} } + cleanup () { while true ; do local master="$(http "/_cat/master?h=node" || echo "")" @@ -261,9 +263,12 @@ spec: echo "This node is still master, waiting gracefully for it to step down" sleep 1 done + exit 0 } + trap cleanup SIGTERM + sleep infinity & wait $! resources: From db535e2ae6c6e4d7642ab395b79f65779ed8935f Mon Sep 17 00:00:00 2001 From: Tetiana Kravchenko Date: Wed, 3 Jul 2019 11:15:15 +0200 Subject: [PATCH 4/7] values: fix duplication, move hooks closer to the lifecycle; tests: add sugested test Signed-off-by: Tetiana Kravchenko --- elasticsearch/tests/elasticsearch_test.py | 17 +++++++++++++++++ elasticsearch/values.yaml | 20 ++++++++++---------- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/elasticsearch/tests/elasticsearch_test.py b/elasticsearch/tests/elasticsearch_test.py index 79c681112..a2b555f98 100755 --- a/elasticsearch/tests/elasticsearch_test.py +++ b/elasticsearch/tests/elasticsearch_test.py @@ -655,3 +655,20 @@ def test_master_termination_fixed_enabled(): c = r['statefulset'][uname]['spec']['template']['spec']['containers'][1] assert c['name'] == 'elasticsearch-master-graceful-termination-handler' + +def test_lifecycle_hooks(): + config = '' + r = helm_template(config) + c = r['statefulset'][uname]['spec']['template']['spec']['containers'][0] + assert 'lifecycle' not in c + + config = ''' + lifecycle: + preStop: + exec: + command: ["/bin/bash","/preStop"] + ''' + r = helm_template(config) + c = r['statefulset'][uname]['spec']['template']['spec']['containers'][0] + + assert c['lifecycle']['preStop']['exec']['command'] == ["/bin/bash","/preStop"] diff --git a/elasticsearch/values.yaml b/elasticsearch/values.yaml index 75507647f..598a7ed2d 100755 --- a/elasticsearch/values.yaml +++ b/elasticsearch/values.yaml @@ -27,14 +27,6 @@ esConfig: {} # log4j2.properties: | # key = value -# Add custom preStop, postStart lifecycle hooks -hooks: {} - ## (string) Script to execute prior the pod stops. - # preStop: |- - - ## (string) Script to execute after the pod starts. - # postStart: |- - # Extra environment variables to append to this nodeGroup # This will be appended to the current 'env:' key. You can use any of the kubernetes env # syntax here @@ -197,6 +189,14 @@ lifecycle: {} # preStop: # exec: # command: ["/bin/bash","/preStop"] - # preStop: + # postStart: # exec: - # command: ["/bin/bash","/preStop"] + # command: ["/bin/bash","/postStart"] + +# Add custom preStop, postStart lifecycle hooks +hooks: {} + ## (string) Script to execute prior the pod stops. + # preStop: |- + + ## (string) Script to execute after the pod starts. + # postStart: |- From a5c334e7c143902e08f17d254d13b613fea8d955 Mon Sep 17 00:00:00 2001 From: Tetiana Kravchenko Date: Fri, 5 Jul 2019 10:17:02 +0200 Subject: [PATCH 5/7] revertchanges: remove hooks variable Signed-off-by: Tetiana Kravchenko --- elasticsearch/README.md | 1 - elasticsearch/templates/configmap.yaml | 8 +------- elasticsearch/templates/statefulset.yaml | 13 +++---------- elasticsearch/values.yaml | 6 ------ 4 files changed, 4 insertions(+), 24 deletions(-) diff --git a/elasticsearch/README.md b/elasticsearch/README.md index c65405b56..cecddb86c 100644 --- a/elasticsearch/README.md +++ b/elasticsearch/README.md @@ -65,7 +65,6 @@ helm install --name elasticsearch elastic/elasticsearch --version 7.2.0 --set im | `minimumMasterNodes` | The value for [discovery.zen.minimum_master_nodes](https://www.elastic.co/guide/en/elasticsearch/reference/6.7/discovery-settings.html#minimum_master_nodes). Should be set to `(master_eligible_nodes / 2) + 1`. Ignored in Elasticsearch versions >= 7. | `2` | | `esMajorVersion` | Used to set major version specific configuration | `7` | | `esConfig` | Allows you to add any config files in `/usr/share/elasticsearch/config/` such as `elasticsearch.yml` and `log4j2.properties`. See [values.yaml](./values.yaml) for an example of the formatting. | `{}` | -| `hooks` | Allows you to add lifecycle preStop, postStart hooks. See [values.yaml](./values.yaml) for an example of the formatting. | `{}` | | `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 | `[]` | | `extraVolumes` | Additional volumes to be passed to the `tpl` function | | | `extraVolumeMounts` | Additional volumeMounts to be passed to the `tpl` function | | diff --git a/elasticsearch/templates/configmap.yaml b/elasticsearch/templates/configmap.yaml index 89760ec01..78074a804 100644 --- a/elasticsearch/templates/configmap.yaml +++ b/elasticsearch/templates/configmap.yaml @@ -1,3 +1,4 @@ +{{- if .Values.esConfig }} apiVersion: v1 kind: ConfigMap metadata: @@ -8,15 +9,8 @@ metadata: chart: "{{ .Chart.Name }}-{{ .Chart.Version }}" app: "{{ template "uname" . }}" data: -{{- if .Values.esConfig }} {{- range $path, $config := .Values.esConfig }} {{ $path }}: | {{ $config | indent 4 -}} {{- end -}} {{- end -}} -{{- if .Values.hooks }} -{{- range $path, $config := .Values.hooks }} - {{ $path }}: | -{{ $config | indent 4 -}} -{{- end -}} -{{- end }} diff --git a/elasticsearch/templates/statefulset.yaml b/elasticsearch/templates/statefulset.yaml index b47680ded..1f268e795 100644 --- a/elasticsearch/templates/statefulset.yaml +++ b/elasticsearch/templates/statefulset.yaml @@ -98,8 +98,8 @@ spec: secret: secretName: {{ .secretName }} {{- end }} - {{- if or .Values.esConfig .Values.hooks }} - - name: config + {{- if .Values.esConfig }} + - name: esconfig configMap: name: {{ template "uname" . }}-config {{- end }} @@ -215,17 +215,10 @@ spec: {{- end }} {{- end }} {{- range $path, $config := .Values.esConfig }} - - name: config + - name: esconfig mountPath: /usr/share/elasticsearch/config/{{ $path }} subPath: {{ $path }} {{- end -}} - {{- if .Values.hooks }} - {{- range $path, $config := .Values.hooks }} - - name: config - mountPath: /{{ $path }} - subPath: {{ $path }} - {{- end -}} - {{- end }} {{- if .Values.extraVolumeMounts }} {{ tpl .Values.extraVolumeMounts . | indent 10 }} {{- end }} diff --git a/elasticsearch/values.yaml b/elasticsearch/values.yaml index 598a7ed2d..4da57116a 100755 --- a/elasticsearch/values.yaml +++ b/elasticsearch/values.yaml @@ -194,9 +194,3 @@ lifecycle: {} # command: ["/bin/bash","/postStart"] # Add custom preStop, postStart lifecycle hooks -hooks: {} - ## (string) Script to execute prior the pod stops. - # preStop: |- - - ## (string) Script to execute after the pod starts. - # postStart: |- From dd4856535f35f4dc482c117cab95576eb27fe7e9 Mon Sep 17 00:00:00 2001 From: Tetiana Kravchenko Date: Fri, 5 Jul 2019 10:18:22 +0200 Subject: [PATCH 6/7] remove not needed comment Signed-off-by: Tetiana Kravchenko --- elasticsearch/values.yaml | 2 -- 1 file changed, 2 deletions(-) diff --git a/elasticsearch/values.yaml b/elasticsearch/values.yaml index 4da57116a..f0329da9c 100755 --- a/elasticsearch/values.yaml +++ b/elasticsearch/values.yaml @@ -192,5 +192,3 @@ lifecycle: {} # postStart: # exec: # command: ["/bin/bash","/postStart"] - -# Add custom preStop, postStart lifecycle hooks From 58142e46b59d435e7463be551fc61722e6b514a9 Mon Sep 17 00:00:00 2001 From: Tetiana Kravchenko Date: Fri, 5 Jul 2019 10:27:48 +0200 Subject: [PATCH 7/7] default values: update example to make it more generic Signed-off-by: Tetiana Kravchenko --- elasticsearch/values.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/elasticsearch/values.yaml b/elasticsearch/values.yaml index f0329da9c..0b51349be 100755 --- a/elasticsearch/values.yaml +++ b/elasticsearch/values.yaml @@ -188,7 +188,7 @@ masterTerminationFix: false lifecycle: {} # preStop: # exec: - # command: ["/bin/bash","/preStop"] + # command: ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"] # postStart: # exec: - # command: ["/bin/bash","/postStart"] + # command: ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"]