apiVersion: v1
kind: ConfigMap
metadata:
  labels:
    app: {{ template "redis.name" . }}
    chart: {{ template "redis.chart" . }}
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
  name: {{ template "redis.fullname" . }}-health
data:
  ping_local.sh: |-
{{- if .Values.usePasswordFile }}
    password_aux=`cat ${REDIS_PASSWORD_FILE}`
    export REDIS_PASSWORD=$password_aux
{{- end }}
    response=$(
      timeout -s 9 $1 \
      redis-cli \
{{- if .Values.usePassword }}
        -a $REDIS_PASSWORD \
{{- end }}
        -h localhost \
        -p $REDIS_PORT \
        ping
    )
    if [ "$response" != "PONG" ]; then
      echo "$response"
      exit 1
    fi
  ping_master.sh: |-
{{- if .Values.usePasswordFile }}
    password_aux=`cat ${REDIS_MASTER_PASSWORD_FILE}`
    export REDIS_MASTER_PASSWORD=$password_aux
{{- end }}
    response=$(
      timeout -s 9 $1 \
      redis-cli \
{{- if .Values.usePassword }}
        -a $REDIS_MASTER_PASSWORD \
{{- end }}
        -h $REDIS_MASTER_HOST \
        -p $REDIS_MASTER_PORT_NUMBER \
        ping
    )
    if [ "$response" != "PONG" ]; then
      echo "$response"
      exit 1
    fi
  ping_local_and_master.sh: |-
    script_dir="$(dirname "$0")"
    exit_status=0
    "$script_dir/ping_local.sh" $1 || exit_status=$?
    "$script_dir/ping_master.sh" $1 || exit_status=$?
    exit $exit_status
