apiVersion: {{ template "deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ include "harbor.registry" . }}
  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: registry
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  replicas: {{ .Values.registry.replicas }}
  {{- if .Values.registry.updateStrategy }}
  strategy: {{- toYaml .Values.registry.updateStrategy | nindent 4 }}
  {{- end }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" . | nindent 6 }}
      app.kubernetes.io/component: registry
  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.registry.podLabels }}
        {{- include "common.tplvalues.render" (dict "value" .Values.registry.podLabels "context" $) | nindent 8 }}
        {{- end }}
        app.kubernetes.io/component: registry
      annotations:
        checksum/configmap: {{ include (print $.Template.BasePath "/registry/registry-cm.yaml") . | sha256sum }}
        checksum/secret: {{ include (print $.Template.BasePath "/registry/registry-secret.yaml") . | sha256sum }}
        checksum/secret-jobservice: {{ include (print $.Template.BasePath "/jobservice/jobservice-secrets.yaml") . | sha256sum }}
        checksum/secret-core: {{ include (print $.Template.BasePath "/core/core-secret.yaml") . | sha256sum }}
        {{- if and .Values.internalTLS.enabled (not .Values.registry.tls.existingSecret) }}
        checksum/tls: {{ include (print $.Template.BasePath "/internal/internal-crt-secret.yaml") . | sha256sum }}
        {{- end }}
        {{- if .Values.registry.podAnnotations }}
        {{- include "common.tplvalues.render" (dict "value" .Values.registry.podAnnotations "context" $) | nindent 8 }}
        {{- end }}
    spec:
      {{- include "harbor.imagePullSecrets" . | nindent 6 }}
      {{- if .Values.registry.affinity }}
      affinity: {{- include "common.tplvalues.render" (dict "value" .Values.registry.affinity "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.registry.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" (dict "value" .Values.registry.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.registry.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.registry.tolerations "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.podSecurityContext }}
      securityContext: {{- include "common.tplvalues.render" (dict "value" .Values.podSecurityContext "context" $) | nindent 8 }}
      {{- end }}
      initContainers:
      {{- if and .Values.volumePermissions.enabled .Values.persistence.enabled }}
        - name: volume-permissions
          image: {{ include "harbor.volumePermissions.image" . }}
          imagePullPolicy: {{ .Values.volumePermissions.image.pullPolicy | quote }}
          command:
            - sh
            - -c
            - |
              mkdir -p "{{ .Values.persistence.imageChartStorage.filesystem.rootdirectory }}"
              chown -R "{{ .Values.containerSecurityContext.runAsUser }}:{{ .Values.podSecurityContext.fsGroup }}" "{{ .Values.persistence.imageChartStorage.filesystem.rootdirectory }}"
          securityContext:
            runAsUser: 0
          {{- if .Values.volumePermissions.resources }}
          resources: {{- toYaml .Values.volumePermissions.resources | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: registry-data
              mountPath: {{ .Values.persistence.imageChartStorage.filesystem.rootdirectory }}
              subPath: {{ .Values.persistence.persistentVolumeClaim.registry.subPath }}
      {{- end }}
      {{- if .Values.registry.initContainers }}
      {{- include "common.tplvalues.render" (dict "value" .Values.registry.initContainers "context" $) | nindent 8 }}
      {{- end }}
      containers:
        - name: registry
          image: {{ include "harbor.registryImage" . }}
          imagePullPolicy: {{ .Values.registryImage.pullPolicy | quote }}
          {{- if .Values.registry.server.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.registry.server.command "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.registry.server.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.registry.server.args "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.containerSecurityContext }}
          securityContext: {{- include "common.tplvalues.render" (dict "value" .Values.containerSecurityContext "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.registry.server.livenessProbe.enabled }}
          livenessProbe:
            httpGet:
              path: /
              scheme: {{ include "harbor.component.scheme" . | upper }}
              port: registry
            initialDelaySeconds: {{ .Values.registry.server.livenessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.registry.server.livenessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.registry.server.livenessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.registry.server.livenessProbe.successThreshold }}
            failureThreshold: {{ .Values.registry.server.livenessProbe.failureThreshold }}
          {{- else if .Values.registry.server.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.registry.server.customLivenessProbe "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.registry.server.readinessProbe.enabled }}
          readinessProbe:
            httpGet:
              path: /
              scheme: {{ include "harbor.component.scheme" . | upper }}
              port: registry
            initialDelaySeconds: {{ .Values.registry.server.readinessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.registry.server.readinessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.registry.server.readinessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.registry.server.readinessProbe.successThreshold }}
            failureThreshold: {{ .Values.registry.server.readinessProbe.failureThreshold }}
          {{- else if .Values.registry.server.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.registry.server.customReadinessProbe "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.registry.server.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.registry.server.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.registry.server.resources }}
          resources: {{- toYaml .Values.registry.server.resources | nindent 12 }}
          {{- end }}
          env:
            - name: BITNAMI_DEBUG
              value: {{ ternary "true" "false" .Values.registryImage.debug | quote }}
            {{- if .Values.internalTLS.enabled }}
            - name: INTERNAL_TLS_ENABLED
              value: "true"
            - name: INTERNAL_TLS_KEY_PATH
              value: /etc/harbor/ssl/registry/tls.key
            - name: INTERNAL_TLS_CERT_PATH
              value: /etc/harbor/ssl/registry/tls.crt
            - name: INTERNAL_TLS_TRUST_CA_PATH
              value: /etc/harbor/ssl/registry/ca.crt
            {{- end }}
            {{- if has "chartmuseum" .Values.proxy.components }}
            - name: HTTP_PROXY
              value: {{ .Values.proxy.httpProxy | quote  }}
            - name: HTTPS_PROXY
              value: {{ .Values.proxy.httpsProxy | quote }}
            - name: NO_PROXY
              value: {{ include "harbor.noProxy" . | quote }}
            {{- end }}
            {{- if .Values.registry.server.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.registry.server.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          envFrom:
            - secretRef:
                name: {{ include "harbor.registry" . }}
            {{- if .Values.registry.server.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.registry.server.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.registry.server.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.registry.server.extraEnvVarsSecret "context" $) }}
            {{- end }}
          ports:
            - containerPort: {{ include "harbor.registry.containerPort" . }}
              name: registry
            - containerPort: 5001
              name: debug
          volumeMounts:
            - name: registry-data
              mountPath: {{ .Values.persistence.imageChartStorage.filesystem.rootdirectory }}
              subPath: {{ .Values.persistence.persistentVolumeClaim.registry.subPath }}
            - name: registry-root-certificate
              mountPath: /etc/registry/root.crt
              subPath: tls.crt
            - name: registry-htpasswd
              mountPath: /etc/registry/passwd
              subPath: passwd
            - name: registry-config
              mountPath: /etc/registry/config.yml
              subPath: config.yml
            {{- if .Values.internalTLS.enabled }}
            - name: internal-tls-certs
              mountPath: /etc/harbor/ssl/registry
            {{- end }}
            {{- if and .Values.persistence.enabled (eq .Values.persistence.imageChartStorage.type "gcs") }}
            - name: gcs-key
              mountPath: /etc/registry/gcs-key.json
              subPath: gcs-key.json
            {{- end }}
            {{- if .Values.persistence.imageChartStorage.caBundleSecretName }}
            - name: storage-service-ca
              mountPath: /harbor_cust_cert/custom-ca-bundle.crt
              subPath: ca.crt
            {{- end }}
            {{- if .Values.registry.middleware.enabled }}
            {{- if eq .Values.registry.middleware.type "cloudFront" }}
            - name: cloudfront-key
              mountPath: /etc/registry/pk.pem
              subPath: pk.pem
            {{- end }}
            {{- end }}
            {{- if .Values.registry.server.extraVolumeMounts }}
            {{- include "common.tplvalues.render" (dict "value" .Values.registry.server.extraVolumeMounts "context" $) | nindent 12 }}
            {{- end }}
        - name: registryctl
          image: {{ include "harbor.registryctlImage" . }}
          imagePullPolicy: {{ .Values.registryctlImage.pullPolicy | quote }}
          {{- if .Values.registry.controller.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.registry.controller.command "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.registry.controller.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.registry.controller.args "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.registry.controller.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.registry.controller.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.containerSecurityContext }}
          securityContext: {{- include "common.tplvalues.render" (dict "value" .Values.containerSecurityContext "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.registry.controller.livenessProbe.enabled }}
          livenessProbe:
            httpGet:
              path: /api/health
              scheme: {{ include "harbor.component.scheme" . | upper }}
              port: registryctl
            initialDelaySeconds: {{ .Values.registry.controller.livenessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.registry.controller.livenessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.registry.controller.livenessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.registry.controller.livenessProbe.successThreshold }}
            failureThreshold: {{ .Values.registry.controller.livenessProbe.failureThreshold }}
          {{- else if .Values.registry.controller.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.registry.controller.customLivenessProbe "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.registry.controller.readinessProbe.enabled }}
          readinessProbe:
            httpGet:
              path: /api/health
              scheme: {{ include "harbor.component.scheme" . | upper }}
              port: registryctl
            initialDelaySeconds: {{ .Values.registry.controller.readinessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.registry.controller.readinessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.registry.controller.readinessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.registry.controller.readinessProbe.successThreshold }}
            failureThreshold: {{ .Values.registry.controller.readinessProbe.failureThreshold }}
          {{- else if .Values.registry.controller.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.registry.controller.customReadinessProbe "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.registry.controller.resources }}
          resources: {{- toYaml .Values.registry.controller.resources | nindent 12 }}
          {{- end }}
          envFrom:
            - secretRef:
                name: {{ include "harbor.registry" . }}
            {{- if .Values.registry.controller.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.registry.controller.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.registry.controller.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.registry.controller.extraEnvVarsSecret "context" $) }}
            {{- end }}
          env:
            - name: BITNAMI_DEBUG
              value: {{ ternary "true" "false" .Values.registryctlImage.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 .Values.internalTLS.enabled }}
            - name: INTERNAL_TLS_ENABLED
              value: "true"
            - name: INTERNAL_TLS_KEY_PATH
              value: /etc/harbor/ssl/registry/tls.key
            - name: INTERNAL_TLS_CERT_PATH
              value: /etc/harbor/ssl/registry/tls.crt
            - name: INTERNAL_TLS_TRUST_CA_PATH
              value: /etc/harbor/ssl/registry/ca.crt
            {{- end }}
            {{- if .Values.registry.controller.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.registry.controller.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          ports:
            - containerPort: {{ include "harbor.registryCtl.containerPort" . }}
              name: registryctl
          volumeMounts:
            - name: registry-data
              mountPath: {{ .Values.persistence.imageChartStorage.filesystem.rootdirectory }}
              subPath: {{ .Values.persistence.persistentVolumeClaim.registry.subPath }}
            {{- if .Values.internalTLS.enabled }}
            - name: internal-tls-certs
              mountPath: /etc/harbor/ssl/registry
            {{- end }}
            - name: registry-config
              mountPath: /etc/registry/config.yml
              subPath: config.yml
            - name: registry-config
              mountPath: /etc/registryctl/config.yml
              subPath: ctl-config.yml
            {{- if .Values.persistence.imageChartStorage.caBundleSecretName }}
            - name: storage-service-ca
              mountPath: /harbor_cust_cert/custom-ca-bundle.crt
              subPath: ca.crt
            {{- end }}
            {{- if and .Values.persistence.enabled (eq .Values.persistence.imageChartStorage.type "gcs") }}
            - name: gcs-key
              mountPath: /etc/registry/gcs-key.json
              subPath: gcs-key.json
            {{- end }}
            {{- if .Values.registry.controller.extraVolumeMounts }}
            {{- include "common.tplvalues.render" (dict "value" .Values.registry.controller.extraVolumeMounts "context" $) | nindent 12 }}
            {{- end }}
        {{- if .Values.registry.sidecars }}
        {{- include "common.tplvalues.render" (dict "value" .Values.registry.sidecars "context" $) | nindent 8 }}
        {{- end }}
      volumes:
        - name: registry-htpasswd
          secret:
            secretName: {{ template "harbor.registry" . }}
            items:
              - key: REGISTRY_HTPASSWD
                path: passwd
        - name: registry-root-certificate
          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.registry.tls.secretName" . }}
        {{- end }}
        - name: registry-config
          configMap:
            name: {{ include "harbor.registry" . }}
        - name: registry-data
        {{- if and .Values.persistence.enabled (eq .Values.persistence.imageChartStorage.type "filesystem") }}
          persistentVolumeClaim:
            claimName: {{ .Values.persistence.persistentVolumeClaim.registry.existingClaim | default (include "harbor.registry" .) }}
        {{- else }}
          emptyDir: {}
        {{- end }}
        {{- if and .Values.persistence.enabled (eq .Values.persistence.imageChartStorage.type "gcs") }}
        - name: gcs-key
          secret:
            secretName: {{ include "harbor.registry" . }}
            items:
              - key: GCS_KEY_DATA
                path: gcs-key.json
        {{- end }}
        {{- if .Values.persistence.imageChartStorage.caBundleSecretName }}
        - name: storage-service-ca
          secret:
            secretName: {{ .Values.persistence.imageChartStorage.caBundleSecretName }}
        {{- end }}
        {{- if .Values.registry.middleware.enabled }}
        {{- if eq .Values.registry.middleware.type "cloudFront" }}
        - name: cloudfront-key
          secret:
            secretName: {{ .Values.registry.middleware.cloudFront.privateKeySecret }}
            items:
              - key: CLOUDFRONT_KEY_DATA
                path: pk.pem
        {{- end }}
        {{- end }}
      {{- if .Values.registry.extraVolumes }}
      {{- include "common.tplvalues.render" (dict "value" .Values.registry.extraVolumes "context" $) | nindent 6 }}
      {{- end }}
