@@ -105,3 +105,66 @@ data:
105105 echo "$response"
106106 exit 1
107107 fi
108+ prestop-redis-cluster.sh : |-
109+ #!/bin/bash
110+ set -e
111+
112+ # redis-cli automatically consumes credentials from the REDISCLI_AUTH variable
113+ [[ -n "$REDIS_PASSWORD" ]] && export REDISCLI_AUTH="$REDIS_PASSWORD"
114+ [[ -f "$REDIS_PASSWORD_FILE" ]] && export REDISCLI_AUTH="$(< "${REDIS_PASSWORD_FILE}")"
115+
116+ run_redis_command() {
117+ local args=("-h" "$1")
118+ shift
119+ {{- if .Values.tls.enabled }}
120+ args+=("--tls" "--cert" "{{ template "redis-cluster.tlsCert" . }}" "--key" "{{ template "redis-cluster.tlsCertKey" . }}" "--cacert" "{{ template "redis-cluster.tlsCACert" . }}" "-p" $REDIS_TLS_PORT_NUMBER)
121+ {{- else }}
122+ args+=("-p" $REDIS_PORT_NUMBER)
123+ {{- end }}
124+ redis-cli "${args[@]}" "$@"
125+ }
126+
127+ is_master() {
128+ REDIS_ROLE=$(run_redis_command 127.0.0.1 ROLE | head -1)
129+ [[ "$REDIS_ROLE" == "master" ]]
130+ }
131+
132+ get_cluster_node_id() {
133+ CLUSTER_ID=$(run_redis_command 127.0.0.1 CLUSTER MYID | head -1)
134+ echo $CLUSTER_ID
135+ }
136+
137+ get_replica_ips() {
138+ CLUSTER_NODE_ID=$(get_cluster_node_id)
139+
140+ # Get a list of replica IP addresses that could be promoted
141+ # Use shuf to randomize the order
142+ REPLICAS="$(run_redis_command 127.0.0.1 CLUSTER REPLICAS $CLUSTER_NODE_ID | grep -Ev "(disconnected|nofailover|noaddr|handshake)" | cut -d " " -f 2 | cut -d ":" -f 1 | shuf)"
143+ echo "$REPLICAS"
144+ }
145+
146+ if is_master; then
147+ # Get list of replicas of the current master
148+ mapfile -t REPLICA_IPS < <( get_replica_ips )
149+
150+ NUM_REPLICAS=${#REPLICA_IPS[@]}
151+ echo "Found $NUM_REPLICAS available replicas"
152+
153+ if (( $NUM_REPLICAS > 0 )); then
154+ # Iterate over replicas, attempting to promote one
155+ for REPLICA_IP in "${REPLICA_IPS[@]}"; do
156+ echo "Going to fail over to replica at $REPLICA_IP"
157+ result=$(run_redis_command $REPLICA_IP CLUSTER FAILOVER)
158+
159+ if [[ "$result" == "OK" ]]; then
160+ {{- if .Values.cluster.redisShutdownWaitFailover }}
161+ # Wait for clients to update their topology
162+ sleep 10
163+ break
164+ {{- end }}
165+ fi
166+ done
167+ fi
168+ else
169+ exit 0
170+ fi
0 commit comments