{{- $envVars := include "impress.common.env" (list . .Values.frontend) -}}
{{- $fullName := include "impress.frontend.fullname" . -}}
{{- $component := "frontend" -}}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ $fullName }}
  namespace: {{ .Release.Namespace | quote }}
  annotations:
    {{- with .Values.backend.dpAnnotations }}
    {{- toYaml . | nindent 4 }}
    {{- end }}
  labels:
    {{- include "impress.common.labels" (list . $component) | nindent 4 }}
spec:
  replicas: {{ .Values.frontend.replicas }}
  selector:
    matchLabels:
      {{- include "impress.common.selectorLabels" (list . $component) | nindent 6 }}
  template:
    metadata:
      annotations:
        {{- with .Values.frontend.podAnnotations }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
      labels:
        {{- include "impress.common.selectorLabels" (list . $component) | nindent 8 }}
    spec:
      {{- if $.Values.image.credentials }}
      imagePullSecrets:
        - name: {{ include "impress.secret.dockerconfigjson.name" (dict "fullname" (include "impress.fullname" .) "imageCredentials" $.Values.image.credentials) }}
      {{- end}}
      shareProcessNamespace: {{ .Values.frontend.shareProcessNamespace }}
      containers:
        {{- with .Values.frontend.sidecars }}
          {{- toYaml . | nindent 8 }}
        {{- end }}
        - name: {{ .Chart.Name }}
          image: "{{ (.Values.frontend.image | default dict).repository | default .Values.image.repository }}:{{ (.Values.frontend.image | default dict).tag | default .Values.image.tag }}"
          imagePullPolicy: {{ (.Values.frontend.image | default dict).pullPolicy | default .Values.image.pullPolicy }}
          {{- with .Values.frontend.command }}
          command:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- with .Values.frontend.args }}
          args:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          env:
            {{- if $envVars}}
            {{- $envVars | indent 12 }}
            {{- end }}
          {{- with .Values.frontend.securityContext }}
          securityContext:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          ports:
            - name: http
              containerPort: {{ .Values.frontend.service.targetPort }}
              protocol: TCP
          {{- if .Values.frontend.probes.liveness }}
          livenessProbe:
            {{- include "impress.probes.abstract" (merge .Values.frontend.probes.liveness (dict "targetPort" .Values.frontend.service.targetPort )) | nindent 12 }}
          {{- end }}
          {{- if .Values.frontend.probes.readiness }}
          readinessProbe:
            {{- include "impress.probes.abstract" (merge .Values.frontend.probes.readiness (dict "targetPort" .Values.frontend.service.targetPort )) | nindent 12 }}
          {{- end }}
          {{- if .Values.frontend.probes.startup }}
          startupProbe:
            {{- include "impress.probes.abstract" (merge .Values.frontend.probes.startup (dict "targetPort" .Values.frontend.service.targetPort )) | nindent 12 }}
          {{- end }}
          {{- with .Values.frontend.resources }}
          resources:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          volumeMounts:
            {{- range $index, $value := .Values.mountFiles }}
            - name: "files-{{ $index }}"
              mountPath: {{ $value.path }}
              subPath: content
            {{- end }}
            {{- range $name, $volume := .Values.frontend.persistence }}
            - name: "{{ $name }}"
              mountPath: "{{ $volume.mountPath }}"
            {{- end }}
            {{- range .Values.frontend.extraVolumeMounts }}
            - name: {{ .name }}
              mountPath: {{ .mountPath }}
              subPath: {{ .subPath | default "" }}
              readOnly: {{ .readOnly }}
            {{- end }}
      {{- with .Values.frontend.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.frontend.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.frontend.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      volumes:
        {{- range $index, $value := .Values.mountFiles }}
        - name: "files-{{ $index }}"
          configMap:
            name: "{{ include "impress.fullname" $ }}-files-{{ $index }}"
        {{- end }}
        {{- range $name, $volume := .Values.frontend.persistence }}
        - name: "{{ $name }}"
          {{- if eq $volume.type "emptyDir" }}
          emptyDir: {}
          {{- else }}
          persistentVolumeClaim:
            claimName: "{{ $fullName }}-{{ $name }}"
          {{- end }}
        {{- end }}
        {{- range .Values.frontend.extraVolumes }}
        - name: {{ .name }}
          {{- if .existingClaim }}
          persistentVolumeClaim:
            claimName: {{ .existingClaim }}
          {{- else if .hostPath }}
          hostPath:
            {{ toYaml .hostPath | nindent 12 }}
          {{- else if .csi }}
          csi:
            {{- toYaml .csi | nindent 12 }}
          {{- else if .configMap }}
          configMap:
            {{- toYaml .configMap | nindent 12 }}
          {{- else if .emptyDir }}
          emptyDir:
            {{- toYaml .emptyDir | nindent 12 }}
          {{- else }}
          emptyDir: {}
          {{- end }}
        {{- end }}
