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

{{- if .Values.imageAutomationController.enabled }}
apiVersion: {{ include "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ template "flux.image-automation-controller.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  {{- $versionLabel := dict "app.kubernetes.io/version" ( include "common.images.version" ( dict "imageRoot" .Values.imageAutomationController.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/part-of: flux
    app.kubernetes.io/component: image-automation-controller
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  {{- if not .Values.imageAutomationController.autoscaling.enabled }}
  replicas: {{ .Values.imageAutomationController.replicaCount }}
  {{- end }}
  {{- if .Values.imageAutomationController.updateStrategy }}
  strategy: {{- toYaml .Values.imageAutomationController.updateStrategy | nindent 4 }}
  {{- end }}
  {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.imageAutomationController.podLabels .Values.commonLabels $versionLabel ) "context" . ) }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 6 }}
      app.kubernetes.io/component: image-automation-controller
  template:
    metadata:
      {{- if .Values.imageAutomationController.podAnnotations }}
      annotations: {{- include "common.tplvalues.render" (dict "value" .Values.imageAutomationController.podAnnotations "context" $) | nindent 8 }}
      {{- end }}
      labels: {{- include "common.labels.standard" ( dict "customLabels" $podLabels "context" $ ) | nindent 8 }}
        app.kubernetes.io/part-of: flux
        app.kubernetes.io/component: image-automation-controller
    spec:
      serviceAccountName: {{ template "flux.image-automation-controller.serviceAccountName" . }}
      {{- include "flux.imagePullSecrets" . | nindent 6 }}
      automountServiceAccountToken: {{ .Values.imageAutomationController.automountServiceAccountToken }}
      {{- if .Values.imageAutomationController.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.imageAutomationController.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.imageAutomationController.affinity }}
      affinity: {{- include "common.tplvalues.render" ( dict "value" .Values.imageAutomationController.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.imageAutomationController.podAffinityPreset "component" "image-automation-controller" "customLabels" $podLabels "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.imageAutomationController.podAntiAffinityPreset "component" "image-automation-controller" "customLabels" $podLabels "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.imageAutomationController.nodeAffinityPreset.type "key" .Values.imageAutomationController.nodeAffinityPreset.key "values" .Values.imageAutomationController.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.imageAutomationController.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" ( dict "value" .Values.imageAutomationController.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.imageAutomationController.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.imageAutomationController.tolerations "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.imageAutomationController.priorityClassName }}
      priorityClassName: {{ .Values.imageAutomationController.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.imageAutomationController.schedulerName }}
      schedulerName: {{ .Values.imageAutomationController.schedulerName | quote }}
      {{- end }}
      {{- if .Values.imageAutomationController.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.imageAutomationController.topologySpreadConstraints "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.imageAutomationController.podSecurityContext.enabled }}
      securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.imageAutomationController.podSecurityContext "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.imageAutomationController.terminationGracePeriodSeconds }}
      terminationGracePeriodSeconds: {{ .Values.imageAutomationController.terminationGracePeriodSeconds }}
      {{- end }}
      initContainers:
        {{- if .Values.imageAutomationController.initContainers }}
          {{- include "common.tplvalues.render" (dict "value" .Values.imageAutomationController.initContainers "context" $) | nindent 8 }}
        {{- end }}
      containers:
        - name: image-automation-controller
          image: {{ template "flux.image-automation-controller.image" . }}
          imagePullPolicy: {{ .Values.imageAutomationController.image.pullPolicy }}
          {{- if .Values.imageAutomationController.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.imageAutomationController.containerSecurityContext "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.diagnosticMode.enabled }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.command "context" $) | nindent 12 }}
          {{- else if .Values.imageAutomationController.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.imageAutomationController.command "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.diagnosticMode.enabled }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.args "context" $) | nindent 12 }}
          {{- else if .Values.imageAutomationController.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.imageAutomationController.args "context" $) | nindent 12 }}
          {{- else }}
          args:
            - --watch-all-namespaces={{ .Values.imageAutomationController.watchAllNamespaces }}
            {{- if .Values.imageAutomationController.image.debug }}
            - --log-level=debug
            {{- else }}
            - --log-level=info
            {{- end }}
            - --metrics-addr=:{{ .Values.imageAutomationController.containerPorts.metrics }}
            - --health-addr=:{{ .Values.imageAutomationController.containerPorts.health }}
            - --log-encoding=json
            - --enable-leader-election
          {{- end }}
          env:
            - name: RUNTIME_NAMESPACE
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace
            {{- if .Values.imageAutomationController.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.imageAutomationController.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          envFrom:
            {{- if .Values.imageAutomationController.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.imageAutomationController.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.imageAutomationController.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.imageAutomationController.extraEnvVarsSecret "context" $) }}
            {{- end }}
          {{- if .Values.imageAutomationController.resources }}
          resources: {{- toYaml .Values.imageAutomationController.resources | nindent 12 }}
          {{- else if ne .Values.imageAutomationController.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.imageAutomationController.resourcesPreset) | nindent 12 }}
          {{- end }}
          ports:
            - name: http-metrics
              containerPort: {{ .Values.imageAutomationController.containerPorts.metrics }}
            - name: http-health
              containerPort: {{ .Values.imageAutomationController.containerPorts.health }}
          {{- if not .Values.diagnosticMode.enabled }}
          {{- if .Values.imageAutomationController.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.imageAutomationController.customLivenessProbe "context" $) | nindent 12 }}
          {{- else if .Values.imageAutomationController.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.imageAutomationController.livenessProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /healthz
              port: http-health
          {{- end }}
          {{- if .Values.imageAutomationController.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.imageAutomationController.customReadinessProbe "context" $) | nindent 12 }}
          {{- else if .Values.imageAutomationController.readinessProbe.enabled }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.imageAutomationController.readinessProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /readyz
              port: http-health
          {{- end }}
          {{- if .Values.imageAutomationController.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.imageAutomationController.customStartupProbe "context" $) | nindent 12 }}
          {{- else if .Values.imageAutomationController.startupProbe.enabled }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.imageAutomationController.startupProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /healthz
              port: http-health
          {{- end }}
          {{- end }}
          {{- if .Values.imageAutomationController.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.imageAutomationController.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: empty-dir
              mountPath: /tmp
              subPath: tmp-dir
          {{- if .Values.imageAutomationController.extraVolumeMounts }}
          {{- include "common.tplvalues.render" (dict "value" .Values.imageAutomationController.extraVolumeMounts "context" $) | nindent 12 }}
          {{- end }}
        {{- if .Values.imageAutomationController.sidecars }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.imageAutomationController.sidecars "context" $) | nindent 8 }}
        {{- end }}
      volumes:
        - name: empty-dir
          emptyDir: {}
        {{- if .Values.imageAutomationController.extraVolumes }}
        {{- include "common.tplvalues.render" (dict "value" .Values.imageAutomationController.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
{{- end }}
