apiVersion: {{ template "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ include "harbor.portal" . }}
  namespace: {{ .Release.Namespace | quote }}
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    {{- if .Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
    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 }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" . | 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" . | nindent 8 }}
        {{- if .Values.commonLabels }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 8 }}
        {{- end }}
        {{- if .Values.portal.podLabels }}
        {{- include "common.tplvalues.render" (dict "value" .Values.portal.podLabels "context" $) | nindent 8 }}
        {{- end }}
        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" "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.portal.podAntiAffinityPreset "component" "portal" "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 }}
      {{- if .Values.portal.podSecurityContext.enabled }}
      securityContext: {{- omit .Values.portal.podSecurityContext "enabled" | toYaml | nindent 8 }}
      {{- end }}
      {{- if .Values.portal.initContainers }}
      initContainers: {{- include "common.tplvalues.render" (dict "value" .Values.portal.initContainers "context" $) | nindent 8 }}
      {{- end }}
      containers:
        - name: portal
          image: {{ include "harbor.portal.image" . }}
          imagePullPolicy: {{ .Values.portal.image.pullPolicy | quote }}
          {{- if .Values.portal.containerSecurityContext.enabled }}
          securityContext: {{- omit .Values.portal.containerSecurityContext "enabled" | toYaml | 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 }}
            httpGet:
              path: /
              port: {{ ternary "https" "http" .Values.internalTLS.enabled }}
              scheme: {{ ternary "https" "http" .Values.internalTLS.enabled | upper }}
          {{- 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 }}
          {{- end }}
          volumeMounts:
            - 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: 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 }}
