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

apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ printf "%s-scripts" (include "common.names.fullname" . ) | replace "+" "_" | trunc 63 | trimSuffix "-" }}
  namespace: {{ .Release.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/sh
    set -e

    VALKEY_STATUS_FILE=/tmp/.valkey_cluster_check

    {{- if and .Values.usePassword .Values.usePasswordFiles }}
    password_aux=`cat ${VALKEY_PASSWORD_FILE}`
    export REDISCLI_AUTH=$password_aux
    {{- else }}
    if [ ! -z "$VALKEY_PASSWORD" ]; then export REDISCLI_AUTH=$VALKEY_PASSWORD; fi;
    {{- end }}
    response=$(
      timeout -s 15 $1 \
      valkey-cli \
        -h localhost \
{{- if .Values.tls.enabled }}
        -p $VALKEY_TLS_PORT_NUMBER \
        --tls \
        --cert {{ template "valkey-cluster.tlsCert" . }} \
        --key {{ template "valkey-cluster.tlsCertKey" . }} \
        --cacert {{ template "valkey-cluster.tlsCACert" . }} \
{{- else }}
        -p $VALKEY_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 "$VALKEY_STATUS_FILE" ]; then
      response=$(
        timeout -s 15 $1 \
        valkey-cli \
          -h localhost \
  {{- if .Values.tls.enabled }}
          -p $VALKEY_TLS_PORT_NUMBER \
          --tls \
          --cert {{ template "valkey-cluster.tlsCert" . }} \
          --key {{ template "valkey-cluster.tlsCertKey" . }} \
          --cacert {{ template "valkey-cluster.tlsCACert" . }} \
  {{- else }}
          -p $VALKEY_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 "$VALKEY_STATUS_FILE"
      fi
    fi
{{- end }}
  ping_liveness_local.sh: |-
    #!/bin/sh
    set -e

    {{- if and .Values.usePassword .Values.usePasswordFiles }}
    password_aux=`cat ${VALKEY_PASSWORD_FILE}`
    export REDISCLI_AUTH=$password_aux
    {{- else }}
    if [ ! -z "$VALKEY_PASSWORD" ]; then export REDISCLI_AUTH=$VALKEY_PASSWORD; fi;
    {{- end }}
    response=$(
      timeout -s 15 $1 \
      valkey-cli \
        -h localhost \
{{- if .Values.tls.enabled }}
        -p $VALKEY_TLS_PORT_NUMBER \
        --tls \
        --cert {{ template "valkey-cluster.tlsCert" . }} \
        --key {{ template "valkey-cluster.tlsCertKey" . }} \
        --cacert {{ template "valkey-cluster.tlsCACert" . }} \
{{- else }}
        -p $VALKEY_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
