-
Notifications
You must be signed in to change notification settings - Fork 1.9k
[elasticsearch] add master pre stop hook using voting_config_exclusion #108
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -191,6 +191,48 @@ spec: | |
- name: node.{{ $role }} | ||
value: "{{ $enabled }}" | ||
{{- end }} | ||
{{- if and (eq .Values.roles.master "true") (ge (int .Values.esMajorVersion) 7) }} | ||
lifecycle: | ||
preStop: | ||
exec: | ||
command: | ||
- sh | ||
- -c | ||
- | | ||
#!/usr/bin/env bash -e | ||
# Exclude node from voting config to prevent https://github.com/elastic/helm-charts/issues/63 | ||
# reference: https://www.elastic.co/guide/en/elasticsearch/reference/7.0/modules-discovery-adding-removing-nodes.html#modules-discovery-removing-nodes | ||
http () { | ||
local path="${1}" | ||
local method="${2:-GET}" | ||
if [ -n "${ELASTIC_USERNAME}" ] && [ -n "${ELASTIC_PASSWORD}" ]; then | ||
BASIC_AUTH="-u ${ELASTIC_USERNAME}:${ELASTIC_PASSWORD}" | ||
else | ||
BASIC_AUTH='' | ||
fi | ||
curl -X${method} -s -k --fail ${BASIC_AUTH} {{ .Values.protocol }}://127.0.0.1:{{ .Values.httpPort }}${path} | ||
} | ||
|
||
NODE_NAME=$(awk 'BEGIN {print ENVIRON["node.name"]}') | ||
echo "Exclude the node ${NODE_NAME} from voting config" | ||
|
||
http "/_cluster/voting_config_exclusions/${NODE_NAME}" "POST" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this should be inside the |
||
|
||
while true ; do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Being an infinite loop seems to be ok here because the termination of pods waits for the graceful timeout for the preStop condition to finish: https://kubernetes.io/docs/concepts/workloads/pods/pod/#termination-of-pods. |
||
echo -e "Wait for new master node to be elected" | ||
if [[ "$(http "/_cat/master")" != *"${NODE_NAME}"* ]]; then | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This if statement can be improved to be safer. Right now this is if statement will be matched if the curl command fails in anyway.
A safer option would be to only break if |
||
break | ||
fi | ||
sleep 1 | ||
done | ||
|
||
# Node won't be actually removed from cluster, | ||
# so node should be deleted from voting config exclusions | ||
echo "Node ${NODE_NAME} is now being deleted from voting config exclusions" | ||
http "/_cluster/voting_config_exclusions?wait_for_removal=false" "DELETE" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should also be inside the |
||
|
||
echo "Node ${NODE_NAME} is ready to shutdown" | ||
{{- end }} | ||
{{- if .Values.extraEnvs }} | ||
{{ toYaml .Values.extraEnvs | indent 10 }} | ||
{{- end }} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was originally confused about why awk was needed here. But it looks like bash can't access environment variables with dots in the name.