---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "redis-commander.fullname" . }}
  annotations:
    container.apparmor.security.beta.kubernetes.io/redis-commander: {{ .Values.image.apparmorProfile }}
    container.security.alpha.kubernetes.io/redis-commander: {{ .Values.image.seccompProfile }}
  labels:
    {{- include "redis-commander.labels" . | nindent 4 }}
spec:
{{- if not .Values.autoscaling.enabled }}
  replicas: {{ .Values.replicaCount }}
{{- end }}
  selector:
    matchLabels:
      {{- include "redis-commander.selectorLabels" . | nindent 6 }}
  template:
    metadata:
    {{- with .Values.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
    {{- end }}
      labels:
        {{- include "redis-commander.selectorLabels" . | nindent 8 }}
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- if .Values.serviceAccount.create }}
      serviceAccountName: {{ include "redis-commander.serviceAccountName" . }}
      {{- else }}
      automountServiceAccountToken: false
      {{- end }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      containers:
        - name: {{ .Chart.Name }}
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          ports:
            - name: http
              containerPort: 8081
              protocol: TCP
          env:
          {{- $envLength:= len .Values.env -}}
          {{- if not (eq $envLength 0) }}
          {{- toYaml .Values.env | nindent 10 }}
          {{- end }}
          {{- with .Values.redis.host }}
          - name: REDIS_HOST
            value: {{ . }}
          {{- end }}
          {{- with .Values.redis.password }}
          - name: REDIS_PASSWORD
            value: {{ . }}
          {{- end }}
          {{- with .Values.redis.username }}
          - name: REDIS_USERNAME
            value: {{ . }}
          {{- end }}
          {{- with .Values.redis.hosts }}
          - name: REDIS_HOSTS
            value: {{ . }}
          {{- end }}
          {{- with .Values.httpAuth.username }}
          - name: HTTP_USER
            value: {{ . }}
          {{- end }}
          {{- with .Values.httpAuth.password }}
          - name: HTTP_PASSWORD
            value: {{ . }}
          {{- end }}
          - name: K8S_SIGTERM
            value: "1"
          volumeMounts:
            {{- if .Values.connections.local_production_json }}
            - name: production-local
              mountPath: /redis-commander/config/local-production-docker.json # This should be your final destination
              subPath: local-production-docker.json
            {{- end }}
            {{- with .Values.volumeMounts }}
            {{- toYaml . | default "" | nindent 12 }}
            {{- end }}
          livenessProbe:
            httpGet:
              path: /favicon.png
              port: http
            initialDelaySeconds: 10
            timeoutSeconds: 5
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
      volumes:
        {{- if .Values.connections.local_production_json }}
        - name: production-local
          configMap:
            name: {{ .Release.Name }}-configmap
            items:
              - key: local-production-docker.json
                path: local-production-docker.json
        {{- end }}
        {{- with .Values.volumes }}
        {{- toYaml . | default "" | nindent 8 }}
        {{- end }}
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
