apiVersion: apps/v1
kind: StatefulSet

metadata:
  labels:
    app: redis
  name: redis-master

spec:
  podManagementPolicy: OrderedReady
  replicas: 1
  selector:
    matchLabels:
      app: redis
      role: master
  serviceName: redis-headless
  template:
    metadata:
      annotations: {}
      labels:
        app: redis
        role: master
    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 [[ ! -f /opt/bitnami/redis/etc/master.conf ]];then
                cp /opt/bitnami/redis/mounted-etc/master.conf /opt/bitnami/redis/etc/master.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+=("--protected-mode" "no")
              ARGS+=("--include" "/opt/bitnami/redis/etc/redis.conf")
              ARGS+=("--include" "/opt/bitnami/redis/etc/master.conf")
              /run.sh ${ARGS[@]}
          envFrom:
            - configMapRef:
                name: redis-master
            - secretRef:
                name: redis
          env:
            - name: REDIS_REPLICATION_MODE
              value: master
            - name: ALLOW_EMPTY_PASSWORD
              value: "yes"
            - name: REDIS_PORT
              value: "6379"
          livenessProbe:
            exec:
              command:
                - sh
                - -c
                - /health/ping_liveness_local.sh 5
            failureThreshold: 5
            initialDelaySeconds: 5
            periodSeconds: 5
            successThreshold: 1
            timeoutSeconds: 5
          readinessProbe:
            exec:
              command:
                - sh
                - -c
                - /health/ping_readiness_local.sh 5
            failureThreshold: 5
            initialDelaySeconds: 5
            periodSeconds: 5
            successThreshold: 1
            timeoutSeconds: 1
          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-conf
          name: config
        - emptyDir: {}
          name: redis-tmp-conf
  volumeClaimTemplates:
    - metadata:
        labels:
          app: redis
          component: master
        name: redis-data
      spec:
        accessModes:
          - ReadWriteOnce
        resources:
          requests:
            storage: 8Gi
        volumeMode: Filesystem
