apiVersion: apps/v1
kind: Deployment
metadata:
  name: "{{ template "harbor.registry" . }}"
  labels:
{{ include "harbor.labels" . | indent 4 }}
    component: registry
spec:
  replicas: {{ .Values.registry.replicas }}
  selector:
    matchLabels:
{{ include "harbor.matchLabels" . | indent 6 }}
      component: registry
  template:
    metadata:
      labels:
{{ include "harbor.labels" . | indent 8 }}
        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 .Values.registry.podAnnotations }}
{{ toYaml .Values.registry.podAnnotations | indent 8 }}
{{- end }}
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      containers:
      - name: registry
        image: {{ .Values.registry.registry.image.repository }}:{{ .Values.registry.registry.image.tag }}
        imagePullPolicy: {{ .Values.imagePullPolicy }}
        livenessProbe:
          httpGet:
            path: /
            port: 5000
          initialDelaySeconds: 1
          periodSeconds: 10
        readinessProbe:
          httpGet:
            path: /
            port: 5000
          initialDelaySeconds: 1
          periodSeconds: 10
{{- if .Values.registry.registry.resources }}
        resources:
{{ toYaml .Values.registry.registry.resources | indent 10 }}
{{- end }}
        args: ["serve", "/etc/registry/config.yml"]
        envFrom:
        - secretRef:
            name: "{{ template "harbor.registry" . }}"
        ports:
        - containerPort: 5000
        - containerPort: 5001
        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-config
          mountPath: /etc/registry/config.yml
          subPath: config.yml
        {{- 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 }}
      - name: registryctl
        image: {{ .Values.registry.controller.image.repository }}:{{ .Values.registry.controller.image.tag }}
        imagePullPolicy: {{ .Values.imagePullPolicy }}
        livenessProbe:
          httpGet:
            path: /api/health
            port: 8080
          initialDelaySeconds: 1
          periodSeconds: 10
        readinessProbe:
          httpGet:
            path: /api/health
            port: 8080
          initialDelaySeconds: 1
          periodSeconds: 10
{{- if .Values.registry.controller.resources }}
        resources:
{{ toYaml .Values.registry.controller.resources | indent 10 }}
{{- end }}
        args: ["serve", "/etc/registry/config.yml"]
        envFrom:
        - secretRef:
            name: "{{ template "harbor.registry" . }}"
        env:
          - name: CORE_SECRET
            valueFrom:
              secretKeyRef:
                name: {{ template "harbor.core" . }}
                key: secret
          - name: JOBSERVICE_SECRET
            valueFrom:
              secretKeyRef:
                name: {{ template "harbor.jobservice" . }}
                key: secret
        ports:
        - containerPort: 8080
        volumeMounts:
        - name: registry-data
          mountPath: {{ .Values.persistence.imageChartStorage.filesystem.rootdirectory }}
          subPath: {{ .Values.persistence.persistentVolumeClaim.registry.subPath }}
        - 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 }}
      volumes:
      - name: registry-root-certificate
        secret:
          {{- if .Values.core.secretName }}
          secretName: {{ .Values.core.secretName }}
          {{- else }}
          secretName: {{ template "harbor.core" . }}
          {{- end }}
      - name: registry-config
        configMap:
          name: "{{ template "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: {{ template "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 }}
    {{- with .Values.registry.nodeSelector }}
      nodeSelector:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.registry.affinity }}
      affinity:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.registry.tolerations }}
      tolerations:
{{ toYaml . | indent 8 }}
    {{- end }}
