apiVersion: apps/v1beta2
kind: StatefulSet
metadata:
  name: {{ template "redis.fullname" . }}-master
  labels:
    app: {{ template "redis.name" . }}
    chart: {{ template "redis.chart" . }}
    release: "{{ .Release.Name }}"
    heritage: "{{ .Release.Service }}"
spec:
  selector:
    matchLabels:
      release: "{{ .Release.Name }}"
      role: master
      app: {{ template "redis.name" . }}
  serviceName: {{ template "redis.fullname" . }}-master
  template:
    metadata:
      labels:
        release: "{{ .Release.Name }}"
        chart: {{ template "redis.chart" . }}
        role: master
        app: {{ template "redis.name" . }}
{{- if .Values.master.podLabels }}
{{ toYaml .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.master.podAnnotations }}
{{ toYaml .Values.master.podAnnotations | indent 8 }}
      {{- end }}
    spec:
      {{- if .Values.image.pullSecrets }}
      imagePullSecrets:
        {{- range .Values.image.pullSecrets }}
        - name: {{ . }}
        {{- end}}
      {{- end}}
      {{- if .Values.master.securityContext.enabled }}
      securityContext:
        fsGroup: {{ .Values.master.securityContext.fsGroup }}
      {{- end }}
      serviceAccountName: "{{ template "redis.serviceAccountName" . }}"
      {{- if .Values.master.priorityClassName }}
      priorityClassName: "{{ .Values.master.priorityClassName }}"
      {{- end }}
      {{- with .Values.master.affinity }}
      affinity:
{{ tpl (toYaml .) $ | indent 8 }}
      {{- end }}
      {{- if .Values.master.nodeSelector }}
      nodeSelector:
{{ toYaml .Values.master.nodeSelector | indent 8 }}
      {{- end }}
      {{- if .Values.master.tolerations }}
      tolerations:
{{ toYaml .Values.master.tolerations | indent 8 }}
      {{- end }}
      {{- if .Values.master.schedulerName }}
      schedulerName: "{{ .Values.master.schedulerName }}"
      {{- end }}
      containers:
      - name: {{ template "redis.fullname" . }}
        image: "{{ template "redis.image" . }}"
        imagePullPolicy: {{ default "" .Values.image.pullPolicy | quote }}
        {{- if .Values.master.securityContext.enabled }}
        securityContext:
          runAsUser: {{ .Values.master.securityContext.runAsUser }}
        {{- end }}
        command:
        - /bin/bash
        - -c
        - |
          if [[ -n $REDIS_PASSWORD_FILE ]]; then
            password_aux=`cat ${REDIS_PASSWORD_FILE}`
            export REDIS_PASSWORD=$password_aux
          fi
          ARGS=("--port" "${REDIS_PORT}")
          {{- if .Values.usePassword }}
          ARGS+=("--requirepass" "${REDIS_PASSWORD}")
          {{- else }}
          ARGS+=("--protected-mode" "no")
          {{- end }}
          ARGS+=("--include" "/opt/bitnami/redis/etc/redis.conf")
          ARGS+=("--include" "/opt/bitnami/redis/etc/master.conf")
          {{- if .Values.master.command }}
          {{ .Values.master.command }} ${ARGS[@]}
          {{- else }}
          redis-server "${ARGS[@]}"
          {{- end }}
        env:
        - name: REDIS_REPLICATION_MODE
          value: master
        {{- if .Values.usePassword }}
        {{- if .Values.usePasswordFile }}
        - name: REDIS_PASSWORD_FILE
          value: "/opt/bitnami/redis/secrets/redis-password"
        {{- else }}
        - name: REDIS_PASSWORD
          valueFrom:
            secretKeyRef:
              name: {{ template "redis.secretName" . }}
              key: redis-password
        {{- end }}
        {{- else }}
        - name: ALLOW_EMPTY_PASSWORD
          value: "yes"
        {{- end }}
        - name: REDIS_PORT
          value: {{ .Values.master.port | quote }}
        ports:
        - name: redis
          containerPort: {{ .Values.master.port }}
        {{- if .Values.master.livenessProbe.enabled }}
        livenessProbe:
          initialDelaySeconds: {{ .Values.master.livenessProbe.initialDelaySeconds }}
          periodSeconds: {{ .Values.master.livenessProbe.periodSeconds }}
          timeoutSeconds: {{ .Values.master.livenessProbe.timeoutSeconds }}
          successThreshold: {{ .Values.master.livenessProbe.successThreshold }}
          failureThreshold: {{ .Values.master.livenessProbe.failureThreshold }}
          exec:
            command:
            - sh
            - -c
            - /health/ping_local.sh {{ .Values.master.livenessProbe.timeoutSeconds }}
        {{- end }}
        {{- if .Values.master.readinessProbe.enabled}}
        readinessProbe:
          initialDelaySeconds: {{ .Values.master.readinessProbe.initialDelaySeconds }}
          periodSeconds: {{ .Values.master.readinessProbe.periodSeconds }}
          timeoutSeconds: {{ .Values.master.readinessProbe.timeoutSeconds }}
          successThreshold: {{ .Values.master.readinessProbe.successThreshold }}
          failureThreshold: {{ .Values.master.readinessProbe.failureThreshold }}
          exec:
            command:
            - sh
            - -c
            - /health/ping_local.sh {{ .Values.master.livenessProbe.timeoutSeconds }}
        {{- end }}
        resources:
{{ toYaml .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: {{ .Values.master.persistence.path }}
          subPath: {{ .Values.master.persistence.subPath }}
        {{- if .Values.configmap }}
        - name: config
          mountPath: /opt/bitnami/redis/etc
        {{- end }}
      {{- $needsVolumePermissions := and .Values.volumePermissions.enabled (and ( and .Values.master.persistence.enabled (not .Values.persistence.existingClaim) ) .Values.master.securityContext.enabled) }}
      {{- if or $needsVolumePermissions .Values.sysctlImage.enabled }}
      initContainers:
      {{- if $needsVolumePermissions }}
      - name: volume-permissions
        image: "{{ template "volumePermissions.image" . }}"
        imagePullPolicy: {{ default "" .Values.volumePermissions.image.pullPolicy | quote }}
        command: ["/bin/chown", "-R", "{{ .Values.master.securityContext.runAsUser }}:{{ .Values.master.securityContext.fsGroup }}", "{{ .Values.master.persistence.path }}"]
        securityContext:
          runAsUser: 0
        resources:
{{ toYaml .Values.volumePermissions.resources | indent 10 }}
        volumeMounts:
        - name: redis-data
          mountPath: {{ .Values.master.persistence.path }}
          subPath: {{ .Values.master.persistence.subPath }}
      {{- end }}
      {{- if .Values.sysctlImage.enabled }}
      - name: init-sysctl
        image: {{ template "redis.sysctl.image" . }}
        resources:
{{ toYaml .Values.sysctlImage.resources | indent 10 }}
        {{- 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 }}
      {{- 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 }}
      {{- if not .Values.master.persistence.enabled }}
      - name: "redis-data"
        emptyDir: {}
      {{- else }}
      {{- if .Values.persistence.existingClaim }}
      - name: "redis-data"
        persistentVolumeClaim:
          claimName: {{ .Values.persistence.existingClaim }}
      {{- end }}
      {{- end }}
      {{- if .Values.sysctlImage.mountHostSys }}
      - name: host-sys
        hostPath:
          path: /sys
      {{- end }}
  {{- if and .Values.master.persistence.enabled (not .Values.persistence.existingClaim) }}
  volumeClaimTemplates:
    - metadata:
        name: redis-data
        labels:
          app: "{{ template "redis.name" . }}"
          component: "master"
          release: {{ .Release.Name | quote }}
          heritage: {{ .Release.Service | quote }}
      spec:
        accessModes:
        {{- range .Values.master.persistence.accessModes }}
          - {{ . | quote }}
        {{- end }}
        resources:
          requests:
            storage: {{ .Values.master.persistence.size | quote }}
      {{- if .Values.master.persistence.storageClass }}
      {{- if (eq "-" .Values.master.persistence.storageClass) }}
        storageClassName: ""
      {{- else }}
        storageClassName: {{ .Values.master.persistence.storageClass | quote }}
      {{- end }}
      {{- end }}
  {{- end }}
  updateStrategy:
    type: {{ .Values.master.statefulset.updateStrategy }}
    {{- if .Values.master.statefulset.rollingUpdatePartition }}
    rollingUpdate:
      partition: {{ .Values.master.statefulset.rollingUpdatePartition }}
    {{- end }}
