{{- /*
Copyright Broadcom, Inc. All Rights Reserved.
SPDX-License-Identifier: APACHE-2.0
*/}}

{{- if .Values.dex.enabled }}
apiVersion: {{ include "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ include "argocd.dex" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  {{- $versionLabel := dict "app.kubernetes.io/version" ( include "common.images.version" ( dict "imageRoot" .Values.dex.image "chart" .Chart ) ) }}
  {{- $labels := include "common.tplvalues.merge" ( dict "values" ( list .Values.commonLabels $versionLabel ) "context" . ) }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" $labels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: dex
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  replicas: {{ .Values.dex.replicaCount }}
  {{- if .Values.dex.updateStrategy }}
  strategy: {{- toYaml .Values.dex.updateStrategy | nindent 4 }}
  {{- end }}
  {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.dex.podLabels .Values.commonLabels $versionLabel ) "context" . ) }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 6 }}
      app.kubernetes.io/component: dex
  template:
    metadata:
      {{- if .Values.dex.podAnnotations }}
      annotations: {{- include "common.tplvalues.render" (dict "value" .Values.dex.podAnnotations "context" $) | nindent 8 }}
      {{- end }}
      labels: {{- include "common.labels.standard" ( dict "customLabels" $podLabels "context" $ ) | nindent 8 }}
        app.kubernetes.io/component: dex
    spec:
      serviceAccountName: {{ include "argocd.dex.serviceAccountName" . }}
      {{- include "argocd.imagePullSecrets" . | nindent 6 }}
      automountServiceAccountToken: {{ .Values.dex.automountServiceAccountToken }}
      {{- if .Values.dex.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.dex.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.dex.affinity }}
      affinity: {{- include "common.tplvalues.render" ( dict "value" .Values.dex.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.dex.podAffinityPreset "component" "dex" "customLabels" $podLabels "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.dex.podAntiAffinityPreset "component" "dex" "customLabels" $podLabels "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.dex.nodeAffinityPreset.type "key" .Values.dex.nodeAffinityPreset.key "values" .Values.dex.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.dex.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" ( dict "value" .Values.dex.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.dex.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.dex.tolerations "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.dex.schedulerName }}
      schedulerName: {{ .Values.dex.schedulerName }}
      {{- end }}
      {{- if .Values.dex.shareProcessNamespace }}
      shareProcessNamespace: {{ .Values.dex.shareProcessNamespace }}
      {{- end }}
      {{- if .Values.dex.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.controller.topologySpreadConstraints "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.dex.priorityClassName }}
      priorityClassName: {{ .Values.dex.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.dex.runtimeClassName }}
      runtimeClassName: {{ .Values.dex.runtimeClassName }}
      {{- end }}
      {{- if .Values.dex.podSecurityContext.enabled }}
      securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.dex.podSecurityContext "context" $) | nindent 8 }}
      {{- end }}
      initContainers:
        {{- if .Values.volumePermissions.enabled }}
        - name: volume-permissions
          image: {{ include "argocd.volumePermissions.image" . }}
          imagePullPolicy: {{ .Values.volumePermissions.image.pullPolicy | quote }}
          command:
            - /bin/bash
            - -ec
            - |
              chown -R {{ .Values.dex.containerSecurityContext.runAsUser }}:{{ .Values.dex.podSecurityContext.fsGroup }} /shared
              chown -R {{ .Values.dex.containerSecurityContext.runAsUser }}:{{ .Values.dex.podSecurityContext.fsGroup }} /tmp
          securityContext:
            runAsUser: {{ .Values.volumePermissions.containerSecurityContext.runAsUser }}
          {{- if .Values.volumePermissions.resources }}
          resources: {{- toYaml .Values.volumePermissions.resources | nindent 12 }}
          {{- else if ne .Values.volumePermissions.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.volumePermissions.resourcesPreset) | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: empty-dir
              mountPath: /shared
              subPath: app-static-dir
            - name: empty-dir
              mountPath: /tmp
              subPath: tmp-dir
        {{- end }}
        - name: copyutil
          image: {{ include "argocd.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          {{- if .Values.dex.resources }}
          resources:  {{- toYaml .Values.dex.resources | nindent 12 }}
          {{- end }}
          {{- if .Values.dex.containerSecurityContext }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.dex.containerSecurityContext "context" $) | nindent 12 }}
          {{- end }}
          command:
            - cp
            - -n
            - /opt/bitnami/argo-cd/bin/argocd
            - /shared/argocd-dex
          volumeMounts:
            - name: empty-dir
              mountPath: /shared
              subPath: app-static-dir
            - name: empty-dir
              mountPath: /tmp
              subPath: tmp-dir
        {{- if .Values.dex.initContainers }}
          {{- include "common.tplvalues.render" (dict "value" .Values.dex.initContainers "context" $) | nindent 8 }}
        {{- end }}
      containers:
        - name: dex
          image: {{ include "argocd.dex.image" . }}
          imagePullPolicy: {{ .Values.dex.image.pullPolicy }}
          {{- if .Values.dex.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.dex.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.dex.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.dex.containerSecurityContext "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.dex.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.dex.command "context" $) | nindent 12 }}
          {{- else }}
          command:
            - /shared/argocd-dex
          {{- end }}
          {{- if .Values.dex.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.dex.args "context" $) | nindent 12 }}
          {{- else }}
          args:
            - rundex
            {{- if .Values.dex.extraArgs }}
            {{- include "common.tplvalues.render" (dict "value" .Values.dex.extraArgs "context" $) | nindent 12 }}
            {{- end }}
          {{- end }}
          ports:
            - name: http
              containerPort: {{ .Values.dex.containerPorts.http }}
              protocol: TCP
            - name: grpc
              containerPort: {{ .Values.dex.containerPorts.grpc }}
              protocol: TCP
            - name: metrics
              containerPort: {{ .Values.dex.containerPorts.metrics }}
              protocol: TCP
          env:
            {{- if .Values.dex.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.dex.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          envFrom:
            {{- if .Values.dex.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.dex.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.dex.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.dex.extraEnvVarsSecret "context" $) }}
            {{- end }}
          {{- if .Values.dex.resources }}
          resources: {{- toYaml .Values.dex.resources | nindent 12 }}
          {{- else if ne .Values.dex.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.dex.resourcesPreset) | nindent 12 }}
          {{- end }}
          {{- if .Values.dex.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.dex.customStartupProbe "context" $) | nindent 12 }}
          {{- else if .Values.dex.startupProbe.enabled }}
          startupProbe:
            httpGet:
              path: /healthz/live
              port: metrics
            initialDelaySeconds: {{ .Values.dex.startupProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.dex.startupProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.dex.startupProbe.timeoutSeconds }}
            successThreshold: {{ .Values.dex.startupProbe.successThreshold }}
            failureThreshold: {{ .Values.dex.startupProbe.failureThreshold }}
          {{- end }}
          {{- if .Values.dex.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.dex.customLivenessProbe "context" $) | nindent 12 }}
          {{- else if .Values.dex.livenessProbe.enabled }}
          livenessProbe:
            httpGet:
              path: /healthz/live
              port: metrics
            initialDelaySeconds: {{ .Values.dex.livenessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.dex.livenessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.dex.livenessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.dex.livenessProbe.successThreshold }}
            failureThreshold: {{ .Values.dex.livenessProbe.failureThreshold }}
          {{- end }}
          {{- if .Values.dex.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.dex.customReadinessProbe "context" $) | nindent 12 }}
          {{- else if .Values.dex.readinessProbe.enabled }}
          readinessProbe:
            httpGet:
              path: /healthz/ready
              port: metrics
            initialDelaySeconds: {{ .Values.dex.readinessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.dex.readinessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.dex.readinessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.dex.readinessProbe.successThreshold }}
            failureThreshold: {{ .Values.dex.readinessProbe.failureThreshold }}
          {{- end }}
          volumeMounts:
            - name: empty-dir
              mountPath: /shared
              subPath: app-static-dir
            - name: empty-dir
              mountPath: /tmp
              subPath: tmp-dir
          {{- if .Values.dex.extraVolumeMounts }}
          {{- include "common.tplvalues.render" (dict "value" .Values.dex.extraVolumeMounts "context" $) | nindent 12 }}
          {{- end }}
        {{- if .Values.dex.sidecars }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.dex.sidecars "context" $) | nindent 8 }}
        {{- end }}
      volumes:
        - name: empty-dir
          emptyDir: {}
        {{- if .Values.dex.extraVolumes }}
        {{- include "common.tplvalues.render" (dict "value" .Values.dex.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
{{- end }}
