{{- /*
Copyright Broadcom, Inc. All Rights Reserved.
SPDX-License-Identifier: APACHE-2.0
*/}}

apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ include "common.names.fullname" . }}-scripts
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
data:
  ping_readiness_local.sh: |-
    #!/bin/bash
    set -e

    REDIS_STATUS_FILE=/tmp/.redis_cluster_check

    {{- if .Values.usePassword }}
    export REDISCLI_AUTH={{ ternary "`cat $REDIS_PASSWORD_FILE`" "$REDIS_PASSWORD" .Values.usePasswordFiles }}
    {{- end }}
    response=$(
      timeout -s 15 $1 \
      redis-cli \
        -h localhost \
{{- if .Values.tls.enabled }}
        -p $REDIS_TLS_PORT_NUMBER \
        --tls \
        --cert {{ template "redis-cluster.tlsCert" . }} \
        --key {{ template "redis-cluster.tlsCertKey" . }} \
        --cacert {{ template "redis-cluster.tlsCACert" . }} \
{{- else }}
        -p $REDIS_PORT_NUMBER \
{{- end }}
        ping
    )
    if [ "$?" -eq "124" ]; then
      echo "Timed out"
      exit 1
    fi
    if [ "$response" != "PONG" ]; then
      echo "$response"
      exit 1
    fi
{{- if not .Values.cluster.externalAccess.enabled }}
    if [ ! -f "$REDIS_STATUS_FILE" ]; then
      response=$(
        timeout -s 15 $1 \
        redis-cli \
          -h localhost \
  {{- if .Values.tls.enabled }}
          -p $REDIS_TLS_PORT_NUMBER \
          --tls \
          --cert {{ template "redis-cluster.tlsCert" . }} \
          --key {{ template "redis-cluster.tlsCertKey" . }} \
          --cacert {{ template "redis-cluster.tlsCACert" . }} \
  {{- else }}
          -p $REDIS_PORT_NUMBER \
  {{- end }}
          CLUSTER INFO | grep cluster_state | tr -d '[:space:]'
      )
      if [ "$?" -eq "124" ]; then
        echo "Timed out"
        exit 1
      fi
      if [ "$response" != "cluster_state:ok" ]; then
        echo "$response"
        exit 1
      else
        touch "$REDIS_STATUS_FILE"
      fi
    fi
{{- end }}
  ping_liveness_local.sh: |-
    #!/bin/bash
    set -e

    {{- if .Values.usePassword }}
    export REDISCLI_AUTH={{ ternary "`cat $REDIS_PASSWORD_FILE`" "$REDIS_PASSWORD" .Values.usePasswordFiles }}
    {{- end }}
    response=$(
      timeout -s 15 $1 \
      redis-cli \
        -h localhost \
{{- if .Values.tls.enabled }}
        -p $REDIS_TLS_PORT_NUMBER \
        --tls \
        --cert {{ template "redis-cluster.tlsCert" . }} \
        --key {{ template "redis-cluster.tlsCertKey" . }} \
        --cacert {{ template "redis-cluster.tlsCACert" . }} \
{{- else }}
        -p $REDIS_PORT_NUMBER \
{{- end }}
        ping
    )
    if [ "$?" -eq "124" ]; then
      echo "Timed out"
      exit 1
    fi
    responseFirstWord=$(echo $response | head -n1 | awk '{print $1;}')
    if [ "$response" != "PONG" ] && [ "$responseFirstWord" != "LOADING" ] && [ "$responseFirstWord" != "MASTERDOWN" ]; then
      echo "$response"
      exit 1
    fi
