# This file is derived from https://github.com/Mailu/Mailu/blob/master/docs/kubernetes/mailu/redis.yaml

{{- /* Define persistantVolume claimName */}}
{{- $claimName := .Values.persistence.single_pvc | ternary (include "mailu.claimName" .) .Values.redis.persistence.claimNameOverride | default (printf "%s-redis" (include "mailu.fullname" .)) }}

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "mailu.fullname" . }}-redis
spec:
  selector:
    matchLabels:
      app: {{ include "mailu.fullname" . }}
      component: redis
  replicas: 1
  template:
    metadata:
      labels:
        app: {{ include "mailu.fullname" . }}
        component: redis
    spec:
      serviceAccount: {{ include "mailu.serviceAccountName" . }}
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      containers:
      - name: redis
        image: {{ template "imageRegistry" . -}} {{ .Values.redis.image.repository }}:{{ .Values.redis.image.tag }}
        imagePullPolicy: Always
        volumeMounts:
          - name: data
            subPath: redis
            mountPath: /data
          {{- if .Values.timezone }}
          - name: zoneinfo
            mountPath: /usr/share/zoneinfo
            readOnly: true
          - name: zoneinfo
            subPath: {{ .Values.timezone }}
            mountPath: /etc/localtime
            readOnly: true
          {{- end }}
        ports:
          - containerPort: 6379
            name: redis
            protocol: TCP
        {{- with .Values.redis.resources }}
        resources:
        {{- .|toYaml|nindent 10}}
        {{- end }}
        {{- if .Values.redis.startupProbe}}
        startupProbe:
          exec:
            command:
              - /usr/local/bin/redis-cli
              - info
              - status
          periodSeconds:  {{ default 10 .Values.redis.startupProbe.periodSeconds }} 
          failureThreshold: {{ default 30 .Values.redis.startupProbe.failureThreshold }} 
          timeoutSeconds: {{ default 5 .Values.redis.startupProbe.timeoutSeconds }} 
        {{- end }}
        {{- if .Values.redis.livenessProbe}}
        livenessProbe:
          exec:
            command:
              - /usr/local/bin/redis-cli
              - info
              - status
          periodSeconds:  {{ default 10 .Values.redis.livenessProbe.periodSeconds }} 
          failureThreshold: {{ default 3 .Values.redis.livenessProbe.failureThreshold }} 
          timeoutSeconds: {{ default 5 .Values.redis.livenessProbe.timeoutSeconds }} 
        {{- end }}
        {{- if .Values.redis.readinessProbe}}
        readinessProbe:
          exec:
            command:
              - /usr/local/bin/redis-cli
              - info
              - status
          periodSeconds:  {{ default 10 .Values.redis.readinessProbe.periodSeconds }} 
          failureThreshold: {{ default 1 .Values.redis.readinessProbe.failureThreshold }} 
          timeoutSeconds: {{ default 5 .Values.redis.readinessProbe.timeoutSeconds }} 
        {{- end }}
      volumes:
        - name: data
          persistentVolumeClaim:
            claimName: {{ $claimName }}
        {{- if .Values.timezone }}
        - name: zoneinfo
          hostPath:
            path: /usr/share/zoneinfo
            type: Directory
        {{- end }}
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 0
      maxSurge: 1


{{- if not .Values.persistence.single_pvc }}
---

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name:  {{ $claimName }}
{{- if .Values.redis.persistence.annotations }}
  annotations:
{{ toYaml .Values.redis.persistence.annotations | indent 4 }}
{{- end }}
spec:
  accessModes:
    - {{ .Values.redis.persistence.accessMode }}
  resources:
    requests:
      storage: {{ .Values.redis.persistence.size }}
  {{- if .Values.redis.persistence.storageClass }}
  storageClassName: {{ .Values.redis.persistence.storageClass }}
  {{- end }}
{{- end }}

---

apiVersion: v1
kind: Service
metadata:
  name: {{ include "mailu.fullname" . }}-redis
  labels:
    app: {{ include "mailu.fullname" . }}
    component: redis
spec:
  selector:
    app: {{ include "mailu.fullname" . }}
    component: redis
  ports:
  - name: redis
    port: 6379
    protocol: TCP
