{{- if .Values.haproxy.enabled }}
kind: Deployment
apiVersion: apps/v1
metadata:
  name: {{ template "redis-ha.fullname" . }}-haproxy
  namespace: {{ .Release.Namespace | quote }}
  labels:
{{ include "labels.standard" . | indent 4 }}
spec:
  strategy:
    type: RollingUpdate
  revisionHistoryLimit: 1
  replicas: {{ .Values.haproxy.replicas }}
  selector:
    matchLabels:
      app: {{ template "redis-ha.name" . }}-haproxy
      release: {{ .Release.Name }}
  template:
    metadata:
      name: {{ template "redis-ha.fullname" . }}-haproxy
      labels:
        app: {{ template "redis-ha.name" . }}-haproxy
        release: {{ .Release.Name }}
        revision: "{{ .Release.Revision }}"
        {{- range $key, $value := .Values.haproxy.labels }}
        {{ $key }}: {{ $value | toString }}
        {{- end }}
      annotations:
      {{- if and (.Values.haproxy.metrics.enabled) (not .Values.haproxy.metrics.serviceMonitor.enabled)  }}
        prometheus.io/port: "{{ .Values.haproxy.metrics.port }}"
        prometheus.io/scrape: "true"
        prometheus.io/path: "{{ .Values.haproxy.metrics.scrapePath }}"
      {{- end }}
        checksum/config: {{ print (include "config-haproxy.cfg" .) (include "config-haproxy_init.sh" .) | sha256sum }}
      {{- if .Values.haproxy.annotations }}
{{ toYaml .Values.haproxy.annotations | indent 8 }}
      {{- end }}
    spec:
      # Needed when using unmodified rbac-setup.yml
      {{ if .Values.haproxy.serviceAccount.create }}
      serviceAccountName: {{ template "redis-ha.serviceAccountName" . }}-haproxy
      {{ end }}
      nodeSelector:
{{ toYaml .Values.nodeSelector | indent 8 }}
      tolerations:
{{ toYaml .Values.tolerations | indent 8 }}
      affinity:
    {{- if .Values.haproxy.affinity }}
    {{- with .Values.haproxy.affinity }}
{{ tpl . $ | indent 8 }}
    {{- end }}
    {{- else }}
    {{- if .Values.haproxy.additionalAffinities }}
{{ toYaml .Values.haproxy.additionalAffinities | indent 8 }}
    {{- end }}
        podAntiAffinity:
    {{- if .Values.haproxy.hardAntiAffinity }}
          requiredDuringSchedulingIgnoredDuringExecution:
            - labelSelector:
                matchLabels:
                  app: {{ template "redis-ha.name" . }}-haproxy
                  release: {{ .Release.Name }}
                  revision: "{{ .Release.Revision }}"
              topologyKey: kubernetes.io/hostname
    {{- else }}
          preferredDuringSchedulingIgnoredDuringExecution:
            - weight: 100
              podAffinityTerm:
                labelSelector:
                  matchLabels:
                    app: {{ template "redis-ha.name" . }}-haproxy
                    release: {{ .Release.Name }}
                    revision: "{{ .Release.Revision }}"
                topologyKey: kubernetes.io/hostname
    {{- end }}
    {{- end }}
      initContainers:
      - name: config-init
        image: {{ .Values.haproxy.image.repository }}:{{ .Values.haproxy.image.tag }}
        imagePullPolicy: {{ .Values.haproxy.image.pullPolicy }}
        resources:
{{ toYaml .Values.haproxy.init.resources | indent 10 }}
        command:
        - sh
        args:
        - /readonly/haproxy_init.sh
{{- if .Values.auth }}
        env:
        - name: AUTH
          valueFrom:
            secretKeyRef:
            {{- if .Values.existingSecret }}
              name: {{ .Values.existingSecret }}
            {{- else }}
              name: {{ template "redis-ha.fullname" . }}
            {{- end }}
              key: {{ .Values.authKey }}
{{- end }}
        volumeMounts:
        - name: config-volume
          mountPath: /readonly
          readOnly: true
        - name: data
          mountPath: /data
      {{- if .Values.haproxy.imagePullSecrets }}
      imagePullSecrets: {{ toYaml .Values.haproxy.imagePullSecrets | nindent 8 }}
      {{- end }}
      securityContext:
{{ toYaml .Values.haproxy.securityContext | indent 8 }}
      containers:
      - name: haproxy
        image: {{ .Values.haproxy.image.repository }}:{{ .Values.haproxy.image.tag }}
        imagePullPolicy: {{ .Values.haproxy.image.pullPolicy }}
        livenessProbe:
          httpGet:
            path: /healthz
            port: 8888
          initialDelaySeconds: 5
          periodSeconds: 3
        readinessProbe:
          httpGet:
            path: /healthz
            port: 8888
          initialDelaySeconds: 5
          periodSeconds: 3
        ports:
        - name: redis
          containerPort: {{ default "6379" .Values.redis.port }}
        {{- if .Values.haproxy.readOnly.enabled }}
        - name: readonlyport
          containerPort: {{ default "6380" .Values.haproxy.readOnly.port }}
        {{- end }}
        {{- if .Values.haproxy.metrics.enabled }}
        - name: metrics-port
          containerPort: {{ default "9101" .Values.haproxy.metrics.port }}
        {{- end }}
        resources:
{{ toYaml .Values.haproxy.resources | indent 10 }}
        volumeMounts:
        - name: data
          mountPath: /usr/local/etc/haproxy
        - name: shared-socket
          mountPath: /run/haproxy
        lifecycle:
{{ toYaml .Values.haproxy.lifecycle | indent 10 }}
{{- if .Values.haproxy.priorityClassName }}
      priorityClassName: {{ .Values.haproxy.priorityClassName }}
{{- end }}
      volumes:
      - name: config-volume
        configMap:
          name: {{ template "redis-ha.fullname" . }}-configmap
      - name: shared-socket
        emptyDir:
{{ toYaml .Values.haproxy.emptyDir | indent 10 }}
      - name: data
        emptyDir:
{{ toYaml .Values.haproxy.emptyDir | indent 10 }}
{{- end }}
