{{- if .Values.cluster.enabled }}
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: {{ template "redis.fullname" . }}-slave
  labels:
    app: {{ template "redis.name" . }}
    chart: {{ template "redis.chart" . }}
    release: "{{ .Release.Name }}"
    heritage: "{{ .Release.Service }}"
spec:
{{- if .Values.slave.updateStrategy }}
  strategy:
{{ toYaml .Values.slave.updateStrategy | indent 4 }}
{{- end }}
{{- if .Values.cluster.slaveCount }}
  replicas: {{ .Values.cluster.slaveCount }}
{{- end }}
  selector:
    matchLabels:
        release: "{{ .Release.Name }}"
        role: slave
        app: {{ template "redis.name" . }}
  template:
    metadata:
      labels:
        release: "{{ .Release.Name }}"
        chart: {{ template "redis.chart" . }}
        role: slave
        app: {{ template "redis.name" . }}
      {{- if (.Values.slave.podLabels | default .Values.master.podLabels) }}
{{ toYaml (.Values.slave.podLabels | default .Values.master.podLabels) | indent 8 }}
      {{- end }}
      annotations:
        checksum/health: {{ include (print $.Template.BasePath "/health-configmap.yaml") . | sha256sum }}
        checksum/configmap: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}
        checksum/secret: {{ include (print $.Template.BasePath "/secret.yaml") . | sha256sum }}
      {{- if (.Values.slave.podAnnotations | default .Values.master.podAnnotations) }}
{{ toYaml (.Values.slave.podAnnotations | default .Values.master.podAnnotations) | indent 8 }}
      {{- end }}
    spec:
      {{- if .Values.image.pullSecrets }}
      imagePullSecrets:
        {{- range .Values.image.pullSecrets }}
        - name: {{ . }}
        {{- end}}
      {{- end}}
      {{- /* Include master securityContext.fsGroup if slave securityContext.fsGroup not defined */ -}}
      {{- if (.Values.slave.securityContext | default .Values.master.securityContext) }}
      securityContext:
        {{- if .Values.slave.securityContext }}
        fsGroup: {{ .Values.slave.securityContext.fsGroup }}
        {{- else }}
        fsGroup: {{ .Values.master.securityContext.fsGroup }}
        {{- end }}
      {{- end }}
      serviceAccountName: "{{ template "redis.serviceAccountName" . }}"
      {{- if .Values.slave.priorityClassName }}
      priorityClassName: "{{ .Values.slave.priorityClassName }}"
      {{- end }}
      {{- if (.Values.slave.nodeSelector | default .Values.master.nodeSelector) }}
      nodeSelector:
{{ toYaml (.Values.slave.nodeSelector | default .Values.master.nodeSelector) | indent 8 }}
      {{- end }}
      {{- if (.Values.slave.tolerations | default .Values.master.tolerations) }}
      tolerations:
{{ toYaml (.Values.slave.tolerations | default .Values.master.tolerations) | indent 8 }}
      {{- end }}
      {{- if .Values.slave.schedulerName }}
      schedulerName: "{{ .Values.slave.schedulerName }}"
      {{- end }}
    {{- with .Values.slave.affinity }}
      affinity:
{{ tpl (toYaml .) $ | indent 8 }}
    {{- end }}
      containers:
      - name: {{ template "redis.fullname" . }}
        image: {{ template "redis.image" . }}
        imagePullPolicy: {{ .Values.image.pullPolicy | default "" | quote }}
{{- $command := default .Values.master.command .Values.slave.command }}
        {{- /* Include master securityContext.runAsUser if slave securityContext.runAsUser not defined */ -}}
        {{- if (.Values.slave.securityContext | default .Values.master.securityContext) }}
        securityContext:
          {{- if .Values.slave.securityContext }}
          runAsUser: {{ .Values.slave.securityContext.runAsUser }}
          {{- else }}
          runAsUser: {{ .Values.master.securityContext.runAsUser }}
          {{- end }}
        {{- end }}
        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
          ARGS=("--port" "${REDIS_PORT}")
          {{- if .Values.usePassword }}
          ARGS+=("--requirepass" "${REDIS_PASSWORD}")
          ARGS+=("--slaveof" "${REDIS_MASTER_HOST}" "${REDIS_MASTER_PORT_NUMBER}")
          ARGS+=("--masterauth" "${REDIS_MASTER_PASSWORD}")
          {{- else }}
          ARGS+=("--protected-mode" "no")
          {{- end }}
          ARGS+=("--include" "/opt/bitnami/redis/etc/redis.conf")
          ARGS+=("--include" "/opt/bitnami/redis/etc/replica.conf")
          {{- if .Values.master.command }}
          {{ .Values.master.command }} "${ARGS[@]}"
          {{- else }}
          redis-server "${ARGS[@]}"
          {{- end }}
        env:
        - name: REDIS_REPLICATION_MODE
          value: slave
        - name: REDIS_MASTER_HOST
          value: {{ template "redis.fullname" . }}-master
        - name: REDIS_PORT
          value: {{ .Values.slave.port | default .Values.master.port | quote }}
        - name: REDIS_MASTER_PORT_NUMBER
          value: {{ .Values.master.service.port | quote }}
        {{- if .Values.usePassword }}
        {{- if .Values.usePasswordFile }}
        - name: REDIS_PASSWORD_FILE
          value: "/opt/bitnami/redis/secrets/redis-password"
        - name: REDIS_MASTER_PASSWORD_FILE
          value: "/opt/bitnami/redis/secrets/redis-password"
        {{- else }}
        - name: REDIS_PASSWORD
          valueFrom:
            secretKeyRef:
              name: {{ template "redis.secretName" . }}
              key: redis-password
        - name: REDIS_MASTER_PASSWORD
          valueFrom:
            secretKeyRef:
              name: {{ template "redis.secretName" . }}
              key: redis-password
        {{- end }}
        {{- else }}
        - name: ALLOW_EMPTY_PASSWORD
          value: "yes"
        {{- end }}
        ports:
        - name: redis
          containerPort: {{ .Values.slave.port | default .Values.master.port }}
        {{- /* Include master livenessProbe if slave livenessProbe not defined */ -}}
        {{ include "redis.slave.livenessProbe" . | indent 8 }}
        {{- /* Include master readinessProbe if slave readinessProbe not defined */ -}}
        {{ include "redis.slave.readinessProbe" . | indent 8 }}
        resources:
{{ toYaml (.Values.slave.resources | default .Values.master.resources) | indent 10 }}
        volumeMounts:
        - name: health
          mountPath: /health
        {{- if .Values.usePasswordFile }}
        - name: redis-password
          mountPath: /opt/bitnami/redis/secrets/
        {{- end }}
        - name: redis-data
          mountPath: /data
        {{- if .Values.configmap }}
        - name: config
          mountPath: /opt/bitnami/redis/etc
        {{- end }}
      {{- if .Values.sysctlImage.enabled }}
      initContainers:
      - name: init-sysctl
        image: {{ template "redis.sysctl.image" . }}
        {{- if .Values.sysctlImage.mountHostSys }}
        volumeMounts:
        - name: host-sys
          mountPath: /host-sys
        {{- end }}
        command:
{{ toYaml .Values.sysctlImage.command | indent 10 }}
        securityContext:
          privileged: true
          runAsUser: 0
      {{- end }}
      volumes:
      - name: health
        configMap:
          name: {{ template "redis.fullname" . }}-health
          defaultMode: 0755
      {{- if .Values.usePasswordFile }}
      - name: redis-password
        secret:
          secretName: {{ template "redis.secretName" . }}
      {{- end }}
      {{- if .Values.configmap }}
      - name: config
        configMap:
          name: {{ template "redis.fullname" . }}
      {{- end }}
      - name: redis-data
        emptyDir: {}
      {{- if .Values.sysctlImage.mountHostSys }}
      - name: host-sys
        hostPath:
          path: /sys
      {{- end }}
{{- end }}
