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

apiVersion: {{ template "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ include "harbor.portal" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  {{- $versionLabel := dict "app.kubernetes.io/version" ( include "common.images.version" ( dict "imageRoot" .Values.portal.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: portal
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  replicas: {{ .Values.portal.replicaCount }}
  {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.portal.podLabels .Values.commonLabels $versionLabel ) "context" . ) }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 6 }}
      app.kubernetes.io/component: portal
  {{- if .Values.portal.updateStrategy }}
  strategy: {{- toYaml .Values.portal.updateStrategy | nindent 4 }}
  {{- end }}
  template:
    metadata:
      labels: {{- include "common.labels.standard" ( dict "customLabels" $podLabels "context" $ ) | nindent 8 }}
        app.kubernetes.io/component: portal
      annotations:
        {{- if and .Values.internalTLS.enabled (not .Values.portal.tls.existingSecret) }}
        checksum/tls: {{ include (print $.Template.BasePath "/internal/internal-crt-secret.yaml") . | sha256sum }}
        {{- end }}
        {{- if .Values.portal.podAnnotations }}
        {{- include "common.tplvalues.render" (dict "value" .Values.portal.podAnnotations "context" $) | nindent 8 }}
        {{- end }}
    spec:
      {{- include "harbor.imagePullSecrets" . | nindent 6 }}
      automountServiceAccountToken: {{ .Values.portal.automountServiceAccountToken }}
      {{- if .Values.portal.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.portal.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.portal.affinity }}
      affinity: {{- include "common.tplvalues.render" (dict "value" .Values.portal.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.portal.podAffinityPreset "component" "portal" "customLabels" $podLabels "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.portal.podAntiAffinityPreset "component" "portal" "customLabels" $podLabels "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.portal.nodeAffinityPreset.type "key" .Values.portal.nodeAffinityPreset.key "values" .Values.portal.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.portal.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" (dict "value" .Values.portal.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.portal.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.portal.tolerations "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.portal.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.portal.topologySpreadConstraints "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.portal.priorityClassName }}
      priorityClassName: {{ .Values.portal.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.portal.schedulerName }}
      schedulerName: {{ .Values.portal.schedulerName }}
      {{- end }}
      serviceAccountName: {{ include "harbor.portal.serviceAccountName" . }}
      {{- if .Values.portal.podSecurityContext.enabled }}
      securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.portal.podSecurityContext "context" $) | nindent 8 }}
      {{- end }}
      initContainers:
        {{- if .Values.portal.initContainers }}
        {{- include "common.tplvalues.render" (dict "value" .Values.portal.initContainers "context" $) | nindent 8 }}
        {{- end }}
        - name: certificate-volume
          image: {{ include "harbor.portal.image" . }}
          imagePullPolicy: {{ .Values.portal.image.pullPolicy | quote }}
          command:
            - /bin/bash
          args:
            - -ec
            - cp -r /etc/ssl/certs/* /certs
          {{- if .Values.portal.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.portal.containerSecurityContext "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.certificateVolume.resources }}
          resources: {{- toYaml .Values.certificateVolume.resources | nindent 12 }}
          {{- else if ne .Values.certificateVolume.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.certificateVolume.resourcesPreset) | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: empty-dir
              mountPath: /certs
              subPath: etc-ssl-certs
      containers:
        - name: portal
          image: {{ include "harbor.portal.image" . }}
          imagePullPolicy: {{ .Values.portal.image.pullPolicy | quote }}
          {{- if .Values.portal.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.portal.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.portal.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.portal.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.portal.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.portal.args "context" $) | nindent 12 }}
          {{- end }}
          env:
            - name: BITNAMI_DEBUG
              value: {{ ternary "true" "false" .Values.portal.image.debug | quote }}
            {{- if .Values.portal.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.portal.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          envFrom:
            {{- if .Values.portal.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.portal.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.portal.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.portal.extraEnvVarsSecret "context" $) }}
            {{- end }}
          ports:
            - containerPort: {{ ternary .Values.portal.containerPorts.https .Values.portal.containerPorts.http .Values.internalTLS.enabled }}
              name: {{ ternary "https" "http" .Values.internalTLS.enabled }}
          {{- if not .Values.diagnosticMode.enabled }}
          {{- if .Values.portal.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.portal.customStartupProbe "context" $) | nindent 12 }}
          {{- else if .Values.portal.startupProbe.enabled }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.portal.startupProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: {{ ternary "https" "http" .Values.internalTLS.enabled }}
          {{- end }}
          {{- if .Values.portal.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.portal.customLivenessProbe "context" $) | nindent 12 }}
          {{- else if .Values.portal.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.portal.livenessProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: {{ ternary "https" "http" .Values.internalTLS.enabled }}
          {{- end }}
          {{- if .Values.portal.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.portal.customReadinessProbe "context" $) | nindent 12 }}
          {{- else if .Values.portal.readinessProbe.enabled }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.portal.readinessProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /
              port: {{ ternary "https" "http" .Values.internalTLS.enabled }}
              scheme: {{ ternary "https" "http" .Values.internalTLS.enabled | upper }}
          {{- end }}
          {{- end }}
          {{- if .Values.portal.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.portal.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.portal.resources }}
          resources: {{- toYaml .Values.portal.resources | nindent 12 }}
          {{- else if ne .Values.portal.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.portal.resourcesPreset) | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: empty-dir
              mountPath: /tmp
              subPath: tmp-dir
            - name: empty-dir
              mountPath: /etc/ssl/certs
              subPath: etc-ssl-certs
            - name: portal-config
              mountPath: /opt/bitnami/nginx/conf/nginx.conf
              subPath: nginx.conf
            {{- if .Values.internalTLS.enabled }}
            - name: internal-tls-certs
              mountPath: /etc/harbor/ssl/portal
            {{- end }}
          {{- if .Values.portal.extraVolumeMounts }}
            {{- include "common.tplvalues.render" (dict "value" .Values.portal.extraVolumeMounts "context" $) | nindent 12 }}
          {{- end }}
        {{- if .Values.portal.sidecars }}
        {{- include "common.tplvalues.render" (dict "value" .Values.portal.sidecars "context" $) | nindent 8 }}
        {{- end }}
      volumes:
        - name: empty-dir
          emptyDir: {}
        - name: portal-config
          configMap:
            name: {{ include "harbor.portal" . }}
        {{- if .Values.internalTLS.enabled }}
        - name: internal-tls-certs
          secret:
            secretName: {{ include "harbor.portal.tls.secretName" . }}
        {{- end }}
      {{- if .Values.portal.extraVolumes }}
      {{- include "common.tplvalues.render" (dict "value" .Values.portal.extraVolumes "context" $) | nindent 8 }}
      {{- end }}
