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

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