{{- if .Values.concierge.enabled }}
apiVersion: {{ include "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ template "pinniped.concierge.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    app.kubernetes.io/part-of: pinniped
    app.kubernetes.io/component: concierge
    {{- if .Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  replicas: {{ .Values.concierge.replicaCount }}
  {{- if .Values.concierge.updateStrategy }}
  strategy: {{- toYaml .Values.concierge.updateStrategy | nindent 4 }}
  {{- end }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" . | nindent 6 }}
      app.kubernetes.io/component: concierge
  template:
    metadata:
      annotations:
        {{- if .Values.concierge.podAnnotations }}
        {{- include "common.tplvalues.render" (dict "value" .Values.concierge.podAnnotations "context" $) | nindent 8 }}
        {{- end }}
      labels: {{- include "common.labels.standard" . | nindent 8 }}
        app.kubernetes.io/component: concierge
        {{- if .Values.concierge.podLabels }}
        {{- include "common.tplvalues.render" (dict "value" .Values.concierge.podLabels "context" $) | nindent 8 }}
        {{- end }}
    spec:
      serviceAccountName: {{ template "pinniped.concierge.serviceAccountName" . }}
      {{- include "pinniped.imagePullSecrets" . | nindent 6 }}
      {{- if .Values.concierge.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.concierge.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.concierge.affinity }}
      affinity: {{- include "common.tplvalues.render" ( dict "value" .Values.concierge.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.concierge.podAffinityPreset "component" "concierge" "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.concierge.podAntiAffinityPreset "component" "concierge" "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.concierge.nodeAffinityPreset.type "key" .Values.concierge.nodeAffinityPreset.key "values" .Values.concierge.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.concierge.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" ( dict "value" .Values.concierge.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.concierge.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.concierge.tolerations "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.concierge.priorityClassName }}
      priorityClassName: {{ .Values.concierge.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.concierge.schedulerName }}
      schedulerName: {{ .Values.concierge.schedulerName | quote }}
      {{- end }}
      {{- if .Values.concierge.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.concierge.topologySpreadConstraints "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.concierge.podSecurityContext.enabled }}
      securityContext: {{- omit .Values.concierge.podSecurityContext "enabled" | toYaml | nindent 8 }}
      {{- end }}
      {{- if .Values.concierge.terminationGracePeriodSeconds }}
      terminationGracePeriodSeconds: {{ .Values.concierge.terminationGracePeriodSeconds }}
      {{- end }}
      initContainers:
        {{- if .Values.concierge.initContainers }}
          {{- include "common.tplvalues.render" (dict "value" .Values.concierge.initContainers "context" $) | nindent 8 }}
        {{- end }}
      {{- if .Values.concierge.hostNetwork }}
      hostNetwork: {{ .Values.concierge.hostNetwork }}
      {{- end }}
      {{- if .Values.concierge.dnsPolicy }}
      dnsPolicy: {{ .Values.concierge.dnsPolicy }}
      {{- end }}
      containers:
        - name: concierge
          image: {{ template "pinniped.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          {{- if .Values.concierge.containerSecurityContext.enabled }}
          securityContext: {{- omit .Values.concierge.containerSecurityContext "enabled" | toYaml | nindent 12 }}
          {{- end }}
          {{- if .Values.concierge.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.concierge.command "context" $) | nindent 12 }}
          {{- else }}
          command:
            - pinniped-concierge
          {{- end }}
          {{- if .Values.concierge.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.concierge.args "context" $) | nindent 12 }}
          {{- else }}
          args:
            - --config=/bitnami/pinniped/conf/pinniped.yaml
            - --downward-api-path=/etc/podinfo
          {{- end }}
          env:
            {{- if .Values.concierge.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.concierge.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          envFrom:
            {{- if .Values.concierge.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.concierge.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.concierge.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.concierge.extraEnvVarsSecret "context" $) }}
            {{- end }}
          {{- if .Values.concierge.resources }}
          resources: {{- toYaml .Values.concierge.resources | nindent 12 }}
          {{- end }}
          ports:
            - name: https-proxy
              containerPort: {{ .Values.concierge.containerPorts.proxy }}
            - name: https-api
              containerPort: {{ .Values.concierge.containerPorts.api }}
          {{- if .Values.concierge.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.concierge.customLivenessProbe "context" $) | nindent 12 }}
          {{- else if .Values.concierge.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.concierge.livenessProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /healthz
              port: https-api
              scheme: HTTPS
          {{- end }}
          {{- if .Values.concierge.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.concierge.customReadinessProbe "context" $) | nindent 12 }}
          {{- else if .Values.concierge.readinessProbe.enabled }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.concierge.readinessProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /healthz
              port: https-api
              scheme: HTTPS
          {{- end }}
          {{- if .Values.concierge.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.concierge.customStartupProbe "context" $) | nindent 12 }}
          {{- else if .Values.concierge.startupProbe.enabled }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.concierge.startupProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /healthz
              port: https-api
              scheme: HTTPS
          {{- end }}
          {{- if .Values.concierge.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.concierge.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: tmp
              mountPath: /tmp
            - name: config-volume
              mountPath: /bitnami/pinniped/conf
              readOnly: true
            - name: podinfo
              mountPath: /etc/podinfo
              readOnly: true
            - name: impersonation-proxy
              # This mount path is hardcoded in the pinniped source code
              mountPath: /var/run/secrets/impersonation-proxy.concierge.pinniped.dev/serviceaccount
              readOnly: true
          {{- if .Values.concierge.extraVolumeMounts }}
          {{- include "common.tplvalues.render" (dict "value" .Values.concierge.extraVolumeMounts "context" $) | nindent 12 }}
          {{- end }}
        {{- if .Values.concierge.sidecars }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.concierge.sidecars "context" $) | nindent 8 }}
        {{- end }}
      volumes:
        - name: tmp
          emptyDir:
            medium: Memory
            sizeLimit: 100Mi
        - name: config-volume
          configMap:
            name: {{ template "pinniped.concierge.configmapName" . }}
        - name: impersonation-proxy
          secret:
            secretName: {{ template "pinniped.concierge.impersonation-proxy.fullname" . }}
            items:
              - key: token
                path: token
        - name: podinfo
          downwardAPI:
            items:
              - path: labels
                fieldRef:
                  fieldPath: metadata.labels
              - path: name
                fieldRef:
                  fieldPath: metadata.name
              - path: namespace
                fieldRef:
                  fieldPath: metadata.namespace
        {{- if .Values.concierge.extraVolumes }}
        {{- include "common.tplvalues.render" (dict "value" .Values.concierge.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
{{- end }}
