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

apiVersion: {{ template "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ include "harbor.core" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  {{- $versionLabel := dict "app.kubernetes.io/version" ( include "common.images.version" ( dict "imageRoot" .Values.core.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: core
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  replicas: {{ .Values.core.replicaCount }}
  {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.core.podLabels .Values.commonLabels $versionLabel ) "context" . ) }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 6 }}
      app.kubernetes.io/component: core
  {{- if .Values.core.updateStrategy }}
  strategy: {{- toYaml .Values.core.updateStrategy | nindent 4 }}
  {{- end }}
  template:
    metadata:
      labels: {{- include "common.labels.standard" ( dict "customLabels" $podLabels "context" $ ) | nindent 8 }}
        app.kubernetes.io/component: core
      annotations:
        checksum/configmap: {{ include (print $.Template.BasePath "/core/core-cm.yaml") . | sha256sum }}
        checksum/configmap-envvars: {{ include (print $.Template.BasePath "/core/core-cm-envvars.yaml") . | sha256sum }}
        checksum/secret: {{ include (print $.Template.BasePath "/core/core-secret.yaml") . | sha256sum }}
        checksum/secret-envvars: {{ include (print $.Template.BasePath "/core/core-secret-envvars.yaml") . | sha256sum }}
        checksum/secret-jobservice: {{ include (print $.Template.BasePath "/jobservice/jobservice-secrets.yaml") . | sha256sum }}
        {{- if and .Values.internalTLS.enabled (not .Values.core.tls.existingSecret) }}
        checksum/tls: {{ include (print $.Template.BasePath "/internal/internal-crt-secret.yaml") . | sha256sum }}
        {{- end }}
        {{- if .Values.core.podAnnotations }}
        {{- include "common.tplvalues.render" (dict "value" .Values.core.podAnnotations "context" $) | nindent 8 }}
        {{- end }}
    spec:
      {{- include "harbor.imagePullSecrets" . | nindent 6 }}
      automountServiceAccountToken: {{ .Values.core.automountServiceAccountToken }}
      {{- if .Values.core.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.core.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.core.affinity }}
      affinity: {{- include "common.tplvalues.render" (dict "value" .Values.core.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.core.podAffinityPreset "component" "core" "customLabels" $podLabels "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.core.podAntiAffinityPreset "component" "core" "customLabels" $podLabels "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.core.nodeAffinityPreset.type "key" .Values.core.nodeAffinityPreset.key "values" .Values.core.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.core.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" (dict "value" .Values.core.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.core.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.core.tolerations "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.core.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.core.topologySpreadConstraints "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.core.priorityClassName }}
      priorityClassName: {{ .Values.core.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.core.schedulerName }}
      schedulerName: {{ .Values.core.schedulerName }}
      {{- end }}
      serviceAccountName: {{ include "harbor.core.serviceAccountName" . }}
      {{- if .Values.core.podSecurityContext.enabled }}
      securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.core.podSecurityContext "context" $) | nindent 8 }}
      {{- end }}
      initContainers:
        {{- if .Values.core.initContainers }}
        {{- include "common.tplvalues.render" (dict "value" .Values.core.initContainers "context" $) | nindent 8 }}
        {{- end }}
        - name: certificate-volume
          image: {{ include "harbor.core.image" . }}
          imagePullPolicy: {{ .Values.core.image.pullPolicy | quote }}
          command:
            - /bin/bash
          args:
            - -ec
            - cp -r /etc/ssl/certs/* /certs
          {{- if .Values.core.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.core.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: core
          image: {{ include "harbor.core.image" . }}
          imagePullPolicy: {{ .Values.core.image.pullPolicy | quote }}
          {{- if .Values.core.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.core.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.core.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.core.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.core.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.core.args "context" $) | nindent 12 }}
          {{- end }}
          env:
            - name: BITNAMI_DEBUG
              value: {{ ternary "true" "false" .Values.core.image.debug | quote }}
            - name: CORE_SECRET
              valueFrom:
                secretKeyRef:
                  name: {{ template "harbor.core.secretName" . }}
                  key: secret
            - name: JOBSERVICE_SECRET
              valueFrom:
                secretKeyRef:
                  name: {{ template "harbor.jobservice.secretName" . }}
                  key: secret
            {{- if .Values.existingSecret }}
            - name: HARBOR_ADMIN_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ tpl .Values.existingSecret . }}
                  key: {{ template "harbor.secret.adminPasswordKey" . }}
            {{- end }}
            {{- if and (not .Values.postgresql.enabled) .Values.externalDatabase.existingSecret }}
            - name: POSTGRESQL_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ tpl .Values.externalDatabase.existingSecret . }}
                  key: {{ .Values.externalDatabase.existingSecretPasswordKey }}
            {{- end }}
            {{- if or .Values.core.configOverwriteJson .Values.core.configOverwriteJsonSecret }}
            - name: CONFIG_OVERWRITE_JSON
              valueFrom:
                secretKeyRef:
                  name: {{ include "harbor.core.overridesJsonSecret" . }}
                  key: overrides.json
            {{- end }}
            {{- if .Values.internalTLS.enabled }}
            - name: INTERNAL_TLS_ENABLED
              value: "true"
            - name: INTERNAL_TLS_KEY_PATH
              value: /etc/harbor/ssl/core/tls.key
            - name: INTERNAL_TLS_CERT_PATH
              value: /etc/harbor/ssl/core/tls.crt
            - name: INTERNAL_TLS_TRUST_CA_PATH
              value: /etc/harbor/ssl/core/ca.crt
            {{- end }}
            {{- if .Values.core.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.core.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          envFrom:
            - configMapRef:
                name: {{ printf "%s-envvars" (include "harbor.core" .) }}
            - secretRef:
                name: {{ template "harbor.core.envvars.secretName" . }}
            {{- if .Values.core.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.core.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.core.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.core.extraEnvVarsSecret "context" $) }}
            {{- end }}
          ports:
            - containerPort: {{ ternary .Values.core.containerPorts.https .Values.core.containerPorts.http .Values.internalTLS.enabled }}
              name: {{ ternary "https" "http" .Values.internalTLS.enabled }}
            {{- if .Values.metrics.enabled }}
            - containerPort: {{ .Values.core.containerPorts.metrics }}
              name: metrics
            {{- end }}
          {{- if not .Values.diagnosticMode.enabled }}
          {{- if .Values.core.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.core.customStartupProbe "context" $) | nindent 12 }}
          {{- else if .Values.core.startupProbe.enabled }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.core.startupProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /api/v2.0/ping
              scheme: {{ ternary "https" "http" .Values.internalTLS.enabled | upper }}
              port: {{ ternary "https" "http" .Values.internalTLS.enabled }}
          {{- end }}
          {{- if .Values.core.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.core.customLivenessProbe "context" $) | nindent 12 }}
          {{- else if .Values.core.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.core.livenessProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: {{ ternary "https" "http" .Values.internalTLS.enabled }}
          {{- end }}
          {{- if .Values.core.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.core.customReadinessProbe "context" $) | nindent 12 }}
          {{- else if .Values.core.readinessProbe.enabled }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.core.readinessProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /api/v2.0/ping
              scheme: {{ ternary "https" "http" .Values.internalTLS.enabled | upper }}
              port: {{ ternary "https" "http" .Values.internalTLS.enabled }}
          {{- end }}
          {{- end }}
          {{- if .Values.core.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.core.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.core.resources }}
          resources: {{- toYaml .Values.core.resources | nindent 12 }}
          {{- else if ne .Values.core.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.core.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: config
              mountPath: /etc/core/app.conf
              subPath: app.conf
            - name: token-service-private-key
              mountPath: /etc/core/private_key.pem
              subPath: tls.key
            - name: secret-key
              mountPath: /etc/core/key
              subPath: key
            {{- if .Values.internalTLS.enabled }}
            - name: internal-tls-certs
              mountPath: /etc/harbor/ssl/core
            {{- end }}
            {{- if or (and (eq .Values.exposureType "proxy") .Values.nginx.tls.enabled) (and (eq .Values.exposureType "ingress") .Values.ingress.core.tls) }}
            - name: ca-download
              mountPath: /etc/core/ca
            {{- end }}
            {{- if .Values.core.uaaSecret }}
            - name: auth-ca-cert
              mountPath: /etc/core/auth-ca/auth-ca.crt
              subPath: auth-ca.crt
            {{- end }}
            {{- include "harbor.redis.caVolumeMount" ( dict "context" . "component" "core" ) | nindent 12 }}
            {{- if .Values.internalTLS.caBundleSecret }}
            {{- include "harbor.caBundleVolumeMount" . | nindent 12 }}
            {{- end }}
            {{- if .Values.core.extraVolumeMounts }}
            {{- include "common.tplvalues.render" (dict "value" .Values.core.extraVolumeMounts "context" $) | nindent 12 }}
            {{- end }}
        {{- if .Values.core.sidecars }}
        {{- include "common.tplvalues.render" (dict "value" .Values.core.sidecars "context" $) | nindent 8 }}
        {{- end }}
      volumes:
        - name: empty-dir
          emptyDir: {}
        - name: config
          configMap:
            name: {{ include "harbor.core" . }}
            items:
              - key: app.conf
                path: app.conf
        - name: token-service-private-key
          secret:
            secretName: {{ template "harbor.core.token.secretName" . }}
        - name: secret-key
          secret:
            secretName: {{ template "harbor.core.secretName" . }}
            items:
              - key: secretKey
                path: key
        {{- if .Values.internalTLS.caBundleSecret }}
        {{- include "harbor.caBundleVolume" . | nindent 8 }}
        {{- end }}
        {{- if .Values.internalTLS.enabled }}
        - name: internal-tls-certs
          secret:
            secretName: {{ include "harbor.core.tls.secretName" . }}
        {{- end }}
        {{- if or (and (eq .Values.exposureType "proxy") .Values.nginx.tls.enabled) (and (eq .Values.exposureType "ingress") .Values.ingress.core.tls) }}
        - name: ca-download
          secret:
          {{- if and (eq .Values.exposureType "proxy") .Values.nginx.tls.enabled }}
            {{- if (include "harbor.autoGenCertForNginx" .) }}
            secretName: {{ include "harbor.nginx" . }}
            {{- else }}
            secretName: {{ .Values.nginx.tls.existingSecret }}
            {{- end }}
          {{- else if and (eq .Values.exposureType "ingress") .Values.ingress.core.tls }}
            secretName: {{ printf "%s-tls" .Values.ingress.core.hostname }}
          {{- end }}
        {{- end }}
        {{- if .Values.core.uaaSecret }}
        - name: auth-ca-cert
          secret:
            secretName: {{ .Values.core.uaaSecret }}
            items:
              - key: ca.crt
                path: auth-ca.crt
        {{- end }}
      {{- include "harbor.redis.caVolume" ( dict "context" . "component" "core" ) | nindent 8 }}
      {{- if .Values.core.extraVolumes }}
      {{- include "common.tplvalues.render" (dict "value" .Values.core.extraVolumes "context" $) | nindent 8 }}
      {{- end }}
