{{- if .Values.notary.enabled }}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "harbor.notary-server" . }}
  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: notary-server
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  replicas: {{ .Values.notary.server.replicas }}
  {{- if .Values.notary.server.updateStrategy }}
  strategy: {{- toYaml .Values.notary.server.updateStrategy | nindent 4 }}
  {{- end }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" . | nindent 6 }}
      app.kubernetes.io/component: notary-server
  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.notary.server.podLabels }}
        {{- include "common.tplvalues.render" (dict "value" .Values.notary.server.podLabels "context" $) | nindent 8 }}
        {{- end }}
        app.kubernetes.io/component: notary-server
      annotations:
        checksum/secret: {{ include (print $.Template.BasePath "/notary/notary-secret.yaml") . | sha256sum }}
        checksum/secret-core: {{ include (print $.Template.BasePath "/core/core-secret.yaml") . | sha256sum }}
        {{- if .Values.notary.server.podAnnotations }}
        annotations: {{- include "common.tplvalues.render" (dict "value" .Values.notary.server.podAnnotations "context" $) | nindent 8 }}
        {{- end }}
    spec:
      {{- include "harbor.imagePullSecrets" . | nindent 6 }}
      {{- if .Values.notary.server.affinity }}
      affinity: {{- include "common.tplvalues.render" (dict "value" .Values.notary.server.affinity "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.notary.server.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" (dict "value" .Values.notary.server.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.notary.server.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.notary.server.tolerations "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.podSecurityContext }}
      securityContext: {{- include "common.tplvalues.render" (dict "value" .Values.podSecurityContext "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.notary.server.initContainers }}
      initContainers: {{- include "common.tplvalues.render" (dict "value" .Values.notary.server.initContainers "context" $) | nindent 8 }}
      {{- end }}
      containers:
        - name: notary-server
          image: {{ include "harbor.notaryServerImage" . | quote }}
          imagePullPolicy: {{ .Values.notaryServerImage.pullPolicy | quote }}
          {{- if .Values.containerSecurityContext }}
          securityContext: {{- include "common.tplvalues.render" (dict "value" .Values.containerSecurityContext "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.notary.server.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.notary.server.command "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.notary.server.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.notary.server.args "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.notary.server.resources }}
          resources: {{- toYaml .Values.notary.server.resources | nindent 12 }}
          {{- end }}
          {{- if .Values.notary.server.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.notary.server.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          env:
            - name: BITNAMI_DEBUG
              value: {{ ternary "true" "false" .Values.notaryServerImage.debug | quote }}
            - name: MIGRATIONS_PATH
              value: migrations/server/postgresql
            - name: DB_URL
              value: {{ include "harbor.database.notaryServer" . }}
            {{- if .Values.notary.server.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.notary.server.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          envFrom:
            {{- if .Values.notary.server.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.notary.server.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.notary.server.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.notary.server.extraEnvVarsSecret "context" $) }}
            {{- end }}
          volumeMounts:
            - name: notary-config
              mountPath: /etc/notary
            - name: root-certificate
              mountPath: /root.crt
              subPath: tls.crt
            {{- if .Values.notary.secretName }}
            - name: notary-ca
              mountPath: /etc/ssl/notary/cert/notary-signer-ca.crt
              subPath: notary-signer-ca.crt
            {{- end }}
            {{- if .Values.notary.server.extraVolumeMounts }}
            {{- include "common.tplvalues.render" (dict "value" .Values.notary.server.extraVolumeMounts "context" $) | nindent 12 }}
            {{- end }}
          ports:
            - containerPort: 4443
              name: notary-server
          {{- if .Values.notary.server.livenessProbe.enabled }}
          livenessProbe:
            tcpSocket:
              port: notary-server
            initialDelaySeconds: {{ .Values.notary.server.livenessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.notary.server.livenessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.notary.server.livenessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.notary.server.livenessProbe.successThreshold }}
            failureThreshold: {{ .Values.notary.server.livenessProbe.failureThreshold }}
          {{- else if .Values.notary.server.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.notary.server.customLivenessProbe "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.notary.server.readinessProbe.enabled }}
          readinessProbe:
            tcpSocket:
              port: notary-server
            initialDelaySeconds: {{ .Values.notary.server.readinessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.notary.server.readinessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.notary.server.readinessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.notary.server.readinessProbe.successThreshold }}
            failureThreshold: {{ .Values.notary.server.readinessProbe.failureThreshold }}
          {{- else if .Values.notary.server.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.notary.server.customReadinessProbe "context" $) | nindent 12 }}
          {{- end }}
        {{- if .Values.notary.server.sidecars }}
        {{- include "common.tplvalues.render" (dict "value" .Values.notary.server.sidecars "context" $) | nindent 8 }}
        {{- end }}
      volumes:
        - name: notary-config
          secret:
            secretName: {{ include "harbor.notary-server" . }}
        - name: root-certificate
          secret:
            {{- if .Values.core.secretName }}
            secretName: {{ .Values.core.secretName }}
            {{- else }}
            secretName: {{ include "harbor.core" . }}
            {{- end }}
        {{- if .Values.notary.secretName }}
        - name: notary-ca
          secret:
            secretName: {{ .Values.notary.secretName }}
        {{- end }}
      {{- if .Values.notary.server.extraVolumes }}
      {{- include "common.tplvalues.render" (dict "value" .Values.notary.server.extraVolumes "context" $) | nindent 6 }}
      {{- end }}
{{- end }}
