apiVersion: {{ template "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ include "harbor.core" . }}
  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: core
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  replicas: {{ .Values.core.replicaCount }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" . | 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.matchLabels" . | nindent 8 }}
        {{- if .Values.commonLabels }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 8 }}
        {{- end }}
        {{- if .Values.core.podLabels }}
        {{- include "common.tplvalues.render" (dict "value" .Values.core.podLabels "context" $) | nindent 8 }}
        {{- end }}
        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" "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.core.podAntiAffinityPreset "component" "core" "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 }}
      {{- if .Values.core.podSecurityContext.enabled }}
      securityContext: {{- omit .Values.core.podSecurityContext "enabled" | toYaml | nindent 8 }}
      {{- end }}
      {{- if .Values.core.initContainers }}
      initContainers: {{- include "common.tplvalues.render" (dict "value" .Values.core.initContainers "context" $) | nindent 8 }}
      {{- end }}
      containers:
        - name: core
          image: {{ include "harbor.core.image" . }}
          imagePullPolicy: {{ .Values.core.image.pullPolicy | quote }}
          {{- if .Values.core.containerSecurityContext.enabled }}
          securityContext: {{- omit .Values.core.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.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: {{ include "harbor.core" . }}
                  key: secret
            - name: JOBSERVICE_SECRET
              valueFrom:
                secretKeyRef:
                  name: {{ include "harbor.jobservice" . }}
                  key: secret
            {{- if or .Values.core.configOverwriteJson .Values.core.configOverwriteJsonSecret }}
            - name: CONFIG_OVERWRITE_JSON
              valueFrom:
                secretKeyRef:
                  name: {{ include "harbor.coreOverridesJsonSecret" . }}
                  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: {{ printf "%s-envvars" (include "harbor.core" .) }}
            {{- 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 }}
            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.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 }}
          {{- end }}
          volumeMounts:
            - name: config
              mountPath: /etc/core/app.conf
              subPath: app.conf
            - name: secret-key
              mountPath: /etc/core/key
              subPath: key
            - name: token-service-private-key
              mountPath: /etc/core/private_key.pem
              subPath: tls.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 }}
            - name: psc
              mountPath: /etc/core/token
            {{- 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: config
          configMap:
            name: {{ include "harbor.core" . }}
            items:
              - key: app.conf
                path: app.conf
        - name: secret-key
          secret:
            secretName: {{ include "harbor.core" . }}
            items:
              - key: secretKey
                path: key
        {{- if .Values.internalTLS.caBundleSecret }}
        {{- include "harbor.caBundleVolume" . | nindent 8 }}
        {{- end }}
        - name: token-service-private-key
          secret:
            {{- if .Values.core.secretName }}
            secretName: {{ .Values.core.secretName }}
            {{- else }}
            secretName: {{ include "harbor.core" . }}
            {{- 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 }}
        - name: psc
          emptyDir: {}
      {{- if .Values.core.extraVolumes }}
      {{- include "common.tplvalues.render" (dict "value" .Values.core.extraVolumes "context" $) | nindent 8 }}
      {{- end }}
