{{- if .Values.redis.enabled }}
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ .Release.Name }}-redis
  namespace: {{ .Release.Namespace }}
  labels:
    app: {{ .Release.Name }}-redis
    app.kubernetes.io/part-of: oneuptime
    app.kubernetes.io/managed-by: Helm
    app.kubernetes.io/component: redis
    appname: oneuptime
spec:
  serviceName: {{ .Release.Name }}-redis-headless
  replicas: 1
  selector:
    matchLabels:
      app: {{ .Release.Name }}-redis
      app.kubernetes.io/component: redis
  template:
    metadata:
      labels:
        app: {{ .Release.Name }}-redis
        app.kubernetes.io/part-of: oneuptime
        app.kubernetes.io/managed-by: Helm
        app.kubernetes.io/component: redis
        appname: oneuptime
    spec:
      {{- with .Values.redis.master.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.redis.master.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.redis.master.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- if .Values.redis.master.podSecurityContext }}
      {{- with .Values.redis.master.podSecurityContext }}
      securityContext:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- else if .Values.podSecurityContext }}
      {{- with .Values.podSecurityContext }}
      securityContext:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- end }}
      containers:
      - name: redis
        image: "{{ .Values.redis.image.repository }}:{{ .Values.redis.image.tag }}"
        imagePullPolicy: {{ .Values.redis.image.pullPolicy }}
        {{- if .Values.redis.master.containerSecurityContext }}
        securityContext:
          {{- with .Values.redis.master.containerSecurityContext }}
          {{- toYaml . | nindent 10 }}
          {{- end }}
        {{- else if .Values.containerSecurityContext }}
        securityContext:
          {{- with .Values.containerSecurityContext }}
          {{- toYaml . | nindent 10 }}
          {{- end }}
        {{- end }}
        command:
          - sh
          - -c
          - |
            # Replace ${REDIS_PASSWORD} with actual password in config file
            sed "s/\${REDIS_PASSWORD}/${REDIS_PASSWORD}/g" /etc/redis/redis.conf.template > /tmp/redis.conf
            exec redis-server /tmp/redis.conf
        ports:
        - name: redis
          containerPort: 6379
          protocol: TCP
        env:
        - name: REDIS_PASSWORD
          valueFrom:
            secretKeyRef:
              name: {{ .Release.Name }}-redis
              key: redis-password
        livenessProbe:
          exec:
            command:
            - redis-cli
            - -a
            - $(REDIS_PASSWORD)
            - ping
          initialDelaySeconds: 30
          timeoutSeconds: 5
          periodSeconds: 10
          failureThreshold: 5
        readinessProbe:
          exec:
            command:
            - redis-cli
            - -a
            - $(REDIS_PASSWORD)
            - ping
          initialDelaySeconds: 5
          timeoutSeconds: 1
          periodSeconds: 5
          failureThreshold: 5
        {{- with .Values.redis.master.resources }}
        resources:
          {{- toYaml . | nindent 10 }}
        {{- end }}
        volumeMounts:
        - name: redis-config
          mountPath: /etc/redis
        {{- if .Values.redis.master.persistence.enabled }}
        - name: data
          mountPath: /data
        {{- end }}
      volumes:
      - name: redis-config
        configMap:
          name: {{ .Release.Name }}-redis-config
      {{- if not .Values.redis.master.persistence.enabled }}
      - name: data
        emptyDir: {}
      {{- end }}
  {{- if .Values.redis.master.persistence.enabled }}
  volumeClaimTemplates:
  - metadata:
      name: data
      labels:
        app: {{ .Release.Name }}-redis
        app.kubernetes.io/part-of: oneuptime
        app.kubernetes.io/managed-by: Helm
        app.kubernetes.io/component: redis
        appname: oneuptime
    spec:
      accessModes:
        - ReadWriteOnce
      {{- if .Values.redis.master.persistence.storageClass }}
      {{- if (eq "-" .Values.redis.master.persistence.storageClass) }}
      storageClassName: ""
      {{- else }}
      storageClassName: {{ .Values.redis.master.persistence.storageClass }}
      {{- end }}
      {{- end }}
      resources:
        requests:
          storage: {{ .Values.redis.master.persistence.size }}
  {{- end }}
{{- end }}
