{{- if .Values.redis.enabled }}
{{- if and .Values.redis.persistence.enabled (not .Values.redis.persistence.existingClaim) }}
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: {{ include "clawith.fullname" . }}-redis-data
  namespace: {{ .Values.global.namespace }}
  labels:
    {{- include "clawith.labels" . | nindent 4 }}
    app.kubernetes.io/component: redis
spec:
  accessModes:
    - {{ .Values.redis.persistence.accessMode }}
  {{- if .Values.redis.persistence.storageClass }}
  storageClassName: {{ .Values.redis.persistence.storageClass }}
  {{- end }}
  resources:
    requests:
      storage: {{ .Values.redis.persistence.size }}
{{- end }}
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "clawith.fullname" . }}-redis
  namespace: {{ .Values.global.namespace }}
  labels:
    {{- include "clawith.labels" . | nindent 4 }}
    app.kubernetes.io/component: redis
spec:
  replicas: 1
  strategy:
    type: Recreate
  selector:
    matchLabels:
      {{- include "clawith.selectorLabels" . | nindent 6 }}
      app.kubernetes.io/component: redis
  template:
    metadata:
      labels:
        {{- include "clawith.selectorLabels" . | nindent 8 }}
        app.kubernetes.io/component: redis
    spec:
      containers:
      - name: redis
        image: "{{ .Values.redis.image.registry }}/{{ .Values.redis.image.repository }}:{{ .Values.redis.image.tag }}"
        imagePullPolicy: {{ .Values.redis.image.pullPolicy }}
        ports:
        - name: redis
          containerPort: {{ .Values.redis.service.port }}
        livenessProbe:
          exec:
            command:
              - redis-cli
              - ping
          periodSeconds: 5
          timeoutSeconds: 5
          failureThreshold: 5
        {{- with .Values.redis.resources }}
        resources:
          {{- toYaml . | nindent 10 }}
        {{- end }}
        {{- if .Values.redis.persistence.enabled }}
        volumeMounts:
        - name: data
          mountPath: /data
        {{- end }}
      {{- if .Values.redis.persistence.enabled }}
      volumes:
      - name: data
        persistentVolumeClaim:
          claimName: {{ .Values.redis.persistence.existingClaim | default (printf "%s-redis-data" (include "clawith.fullname" .)) }}
      {{- end }}
---
apiVersion: v1
kind: Service
metadata:
  name: {{ include "clawith.fullname" . }}-redis
  namespace: {{ .Values.global.namespace }}
  labels:
    {{- include "clawith.labels" . | nindent 4 }}
    app.kubernetes.io/component: redis
spec:
  type: {{ .Values.redis.service.type }}
  selector:
    {{- include "clawith.selectorLabels" . | nindent 4 }}
    app.kubernetes.io/component: redis
  ports:
  - name: tcp-redis
    port: {{ .Values.redis.service.port }}
    targetPort: redis
{{- end }}

