{{- 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.cluster.slaveCount }}
  replicas: {{ .Values.cluster.slaveCount }}
{{- end }}
  template:
    metadata:
      labels:
        release: "{{ .Release.Name }}"
        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 }}
      {{- if (.Values.slave.podAnnotations | default .Values.master.podAnnotations) }}
      annotations:
{{ 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 if slave securityContext not defined */ -}}
      {{ include "redis.slave.securityContext" . | indent 6 }}
      serviceAccountName: "{{ template "redis.serviceAccountName" . }}"
      {{- 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:
{{ toYaml . | indent 8 }}
    {{- end }}
      containers:
      - name: {{ template "redis.fullname" . }}
        image: {{ template "redis.image" . }}
        imagePullPolicy: {{ .Values.image.pullPolicy | default "" | quote }}
        {{- if (.Values.slave.args | default .Values.master.args) }}
        args:
{{ toYaml (.Values.slave.args | default .Values.master.args) | indent 10 }}
        {{- 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.port | quote }}
        {{- if .Values.usePassword }}
        - name: REDIS_PASSWORD
          valueFrom:
            secretKeyRef:
              {{- if .Values.existingSecret }}
              name: {{ .Values.existingSecret }}
              {{- else }}
              name: {{ template "redis.fullname" . }}
              {{- end }}
              key: redis-password
        - name: REDIS_MASTER_PASSWORD
          valueFrom:
            secretKeyRef:
              {{- if .Values.existingSecret }}
              name: {{ .Values.existingSecret }}
              {{- else }}
              name: {{ template "redis.fullname" . }}
              {{- end }}
              key: redis-password
        {{- else }}
        - name: ALLOW_EMPTY_PASSWORD
          value: "yes"
        {{- end }}
        - name: REDIS_DISABLE_COMMANDS
          value: {{ .Values.slave.disableCommands | default .Values.master.disableCommands }}
        {{- if (.Values.slave.extraFlags | default .Values.master.extraFlags) }}
        - name: REDIS_EXTRA_FLAGS
          value: {{ .Values.slave.extraFlags | default .Values.master.extraFlags | join " " }}
        {{- 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 }}
{{- end }}
