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

{{- if .Values.supervisor.enabled }}
apiVersion: {{ include "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ template "pinniped.supervisor.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: pinniped
    app.kubernetes.io/component: supervisor
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  replicas: {{ .Values.supervisor.replicaCount }}
  {{- if .Values.supervisor.updateStrategy }}
  strategy: {{- toYaml .Values.supervisor.updateStrategy | nindent 4 }}
  {{- end }}
  {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.supervisor.podLabels .Values.commonLabels ) "context" . ) }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 6 }}
      app.kubernetes.io/component: supervisor
  template:
    metadata:
      annotations:
        {{- if .Values.supervisor.podAnnotations }}
        {{- include "common.tplvalues.render" (dict "value" .Values.supervisor.podAnnotations "context" $) | nindent 8 }}
        {{- end }}
      labels: {{- include "common.labels.standard" ( dict "customLabels" $podLabels "context" $ ) | nindent 8 }}
        app.kubernetes.io/component: supervisor
        app.kubernetes.io/part-of: pinniped
    spec:
      serviceAccountName: {{ template "pinniped.supervisor.serviceAccountName" . }}
      {{- include "pinniped.imagePullSecrets" . | nindent 6 }}
      automountServiceAccountToken: {{ .Values.supervisor.automountServiceAccountToken }}
      {{- if .Values.supervisor.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.supervisor.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.supervisor.affinity }}
      affinity: {{- include "common.tplvalues.render" ( dict "value" .Values.supervisor.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.supervisor.podAffinityPreset "component" "supervisor" "customLabels" $podLabels "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.supervisor.podAntiAffinityPreset "component" "supervisor" "customLabels" $podLabels "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.supervisor.nodeAffinityPreset.type "key" .Values.supervisor.nodeAffinityPreset.key "values" .Values.supervisor.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.supervisor.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" ( dict "value" .Values.supervisor.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.supervisor.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.supervisor.tolerations "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.supervisor.priorityClassName }}
      priorityClassName: {{ .Values.supervisor.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.supervisor.schedulerName }}
      schedulerName: {{ .Values.supervisor.schedulerName | quote }}
      {{- end }}
      {{- if .Values.supervisor.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.supervisor.topologySpreadConstraints "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.supervisor.podSecurityContext.enabled }}
      securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.supervisor.podSecurityContext "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.supervisor.terminationGracePeriodSeconds }}
      terminationGracePeriodSeconds: {{ .Values.supervisor.terminationGracePeriodSeconds }}
      {{- end }}
      initContainers:
        {{- if .Values.supervisor.initContainers }}
          {{- include "common.tplvalues.render" (dict "value" .Values.supervisor.initContainers "context" $) | nindent 8 }}
        {{- end }}
      containers:
        - name: supervisor
          image: {{ template "pinniped.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          {{- if .Values.supervisor.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.supervisor.containerSecurityContext "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.supervisor.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.supervisor.command "context" $) | nindent 12 }}
          {{- else }}
          command:
            - pinniped-supervisor
          {{- end }}
          {{- if .Values.supervisor.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.supervisor.args "context" $) | nindent 12 }}
          {{- else }}
          args:
            - /etc/podinfo
            - /bitnami/pinniped/conf/pinniped.yaml
          {{- end }}
          env:
            {{- if .Values.supervisor.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.supervisor.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          envFrom:
            {{- if .Values.supervisor.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.supervisor.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.supervisor.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.supervisor.extraEnvVarsSecret "context" $) }}
            {{- end }}
          {{- if .Values.supervisor.resources }}
          resources: {{- toYaml .Values.supervisor.resources | nindent 12 }}
          {{- else if ne .Values.supervisor.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.supervisor.resourcesPreset) | nindent 12 }}
          {{- end }}
          ports:
            - name: https
              containerPort: {{ .Values.supervisor.containerPorts.https }}
            - name: https-api
              containerPort: {{ .Values.supervisor.containerPorts.api }}
          {{- if .Values.supervisor.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.supervisor.customLivenessProbe "context" $) | nindent 12 }}
          {{- else if .Values.supervisor.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.supervisor.livenessProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: https
          {{- end }}
          {{- if .Values.supervisor.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.supervisor.customReadinessProbe "context" $) | nindent 12 }}
          {{- else if .Values.supervisor.readinessProbe.enabled }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.supervisor.readinessProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /healthz
              port: https
              scheme: HTTPS
          {{- end }}
          {{- if .Values.supervisor.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.supervisor.customStartupProbe "context" $) | nindent 12 }}
          {{- else if .Values.supervisor.startupProbe.enabled }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.supervisor.startupProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /healthz
              port: https
              scheme: HTTPS
          {{- end }}
          {{- if .Values.supervisor.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.supervisor.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: empty-dir
              mountPath: /tmp
            - name: config-volume
              mountPath: /bitnami/pinniped/conf
              readOnly: true
            - name: podinfo
              mountPath: /etc/podinfo
              readOnly: true
          {{- if .Values.supervisor.extraVolumeMounts }}
          {{- include "common.tplvalues.render" (dict "value" .Values.supervisor.extraVolumeMounts "context" $) | nindent 12 }}
          {{- end }}
        {{- if .Values.supervisor.sidecars }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.supervisor.sidecars "context" $) | nindent 8 }}
        {{- end }}
      volumes:
        - name: empty-dir
          emptyDir: {}
        - name: config-volume
          configMap:
            name: {{ template "pinniped.supervisor.configmapName" . }}
        - name: podinfo
          downwardAPI:
            items:
              - path: labels
                fieldRef:
                  fieldPath: metadata.labels
              - path: name
                fieldRef:
                  fieldPath: metadata.name
              - path: namespace
                fieldRef:
                  fieldPath: metadata.namespace
        {{- if .Values.supervisor.extraVolumes }}
        {{- include "common.tplvalues.render" (dict "value" .Values.supervisor.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
{{- end }}
