apiVersion: apps/v1
kind: StatefulSet
metadata:
  labels:
    app: redis
  name: redis-slave
spec:
  podManagementPolicy: OrderedReady
  replicas: 2
  selector:
    matchLabels:
      app: redis
      role: slave
  serviceName: redis-headless
  template:
    metadata:
      labels:
        app: redis
        role: slave
    spec:
      containers:
      - name: redis
        image: docker.io/bitnami/redis:5.0.5-debian-9-r141
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 6379
          name: redis
          protocol: TCP
        command:
        - /bin/bash
        - -c
        - |
          if [[ -n $REDIS_PASSWORD_FILE ]]; then
            password_aux=`cat ${REDIS_PASSWORD_FILE}`
            export REDIS_PASSWORD=$password_aux
          fi
          if [[ -n $REDIS_MASTER_PASSWORD_FILE ]]; then
            password_aux=`cat ${REDIS_MASTER_PASSWORD_FILE}`
            export REDIS_MASTER_PASSWORD=$password_aux
          fi
          if [[ ! -f /opt/bitnami/redis/etc/replica.conf ]];then
            cp /opt/bitnami/redis/mounted-etc/replica.conf /opt/bitnami/redis/etc/replica.conf
          fi
          if [[ ! -f /opt/bitnami/redis/etc/redis.conf ]];then
            cp /opt/bitnami/redis/mounted-etc/redis.conf /opt/bitnami/redis/etc/redis.conf
          fi
          ARGS=("--port" "${REDIS_PORT}")
          ARGS+=("--slaveof" "${REDIS_MASTER_HOST}" "${REDIS_MASTER_PORT_NUMBER}")
          ARGS+=("--protected-mode" "no")
          ARGS+=("--include" "/opt/bitnami/redis/etc/redis.conf")
          ARGS+=("--include" "/opt/bitnami/redis/etc/replica.conf")
          /run.sh "${ARGS[@]}"
        envFrom:
        - configMapRef:
            name: redis-slave
        - secretRef:
            name: redis
        env:
        - name: REDIS_REPLICATION_MODE
          value: slave
        - name: REDIS_PORT
          value: "6379"
        - name: ALLOW_EMPTY_PASSWORD
          value: "NO"
        livenessProbe:
          exec:
            command:
            - sh
            - -c
            - /health/ping_liveness_local_and_master.sh 5
          failureThreshold: 5
          initialDelaySeconds: 30
          periodSeconds: 10
          successThreshold: 1
          timeoutSeconds: 5
        readinessProbe:
          exec:
            command:
            - sh
            - -c
            - /health/ping_readiness_local_and_master.sh 5
          failureThreshold: 5
          initialDelaySeconds: 5
          periodSeconds: 10
          successThreshold: 1
          timeoutSeconds: 10
        securityContext:
          runAsUser: 1001
        volumeMounts:
        - mountPath: /health
          name: health
        - mountPath: /data
          name: redis-data
        - mountPath: /opt/bitnami/redis/mounted-etc
          name: config
        - mountPath: /opt/bitnami/redis/etc
          name: redis-tmp-conf
      restartPolicy: Always
      securityContext:
        fsGroup: 1001
      volumes:
      - configMap:
          defaultMode: 493
          name: redis-health
        name: health
      - configMap:
          defaultMode: 420
          name: redis
        name: config
      - emptyDir: {}
        name: sentinel-tmp-conf
      - emptyDir: {}
        name: redis-tmp-conf
  updateStrategy:
    type: RollingUpdate
  volumeClaimTemplates:
  - metadata:
      labels:
        app: redis
        component: slave
      name: redis-data
    spec:
      accessModes:
      - ReadWriteOnce
      resources:
        requests:
          storage: 8Gi
      volumeMode: Filesystem
