{{- if .Values.notary.enabled }}
apiVersion: {{ include "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ include "harbor.notary-server" . }}
  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: notary-server
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  replicas: {{ .Values.notary.server.replicaCount }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" . | nindent 6 }}
      app.kubernetes.io/component: notary-server
  {{- if .Values.notary.server.updateStrategy }}
  strategy: {{- toYaml .Values.notary.server.updateStrategy | nindent 4 }}
  {{- end }}
  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 }}
      automountServiceAccountToken: {{ .Values.notary.server.automountServiceAccountToken }}
      {{- if .Values.notary.server.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.notary.server.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.notary.server.affinity }}
      affinity: {{- include "common.tplvalues.render" (dict "value" .Values.notary.server.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.notary.server.podAffinityPreset "component" "notary-server" "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.notary.server.podAntiAffinityPreset "component" "notary-server" "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.notary.server.nodeAffinityPreset.type "key" .Values.notary.server.nodeAffinityPreset.key "values" .Values.notary.server.nodeAffinityPreset.values) | nindent 10 }}
      {{- 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.notary.server.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.notary.server.topologySpreadConstraints "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.notary.server.priorityClassName }}
      priorityClassName: {{ .Values.notary.server.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.notary.server.schedulerName }}
      schedulerName: {{ .Values.notary.server.schedulerName }}
      {{- end }}
      {{- if .Values.notary.server.podSecurityContext.enabled }}
      securityContext: {{- omit .Values.notary.server.podSecurityContext "enabled" | toYaml | 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.notary.server.image" . | quote }}
          imagePullPolicy: {{ .Values.notary.server.image.pullPolicy | quote }}
          {{- if .Values.notary.server.containerSecurityContext.enabled }}
          securityContext: {{- omit .Values.notary.server.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.notary.server.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.notary.server.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.notary.server.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.notary.server.args "context" $) | nindent 12 }}
          {{- end }}
          env:
            - name: BITNAMI_DEBUG
              value: {{ ternary "true" "false" .Values.notary.server.image.debug | quote }}
            - name: MIGRATIONS_PATH
              value: migrations/server/postgresql
            - name: DB_URL
              valueFrom:
                secretKeyRef:
                  name: {{ printf "%s-envvars" (include "harbor.notary-server" .) }}
                  key: server_db_url
            {{- 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 }}
          ports:
            - containerPort: {{ .Values.notary.server.containerPorts.server }}
              name: notary-server
          {{- if not .Values.diagnosticMode.enabled }}
          {{- if .Values.notary.server.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.notary.server.customStartupProbe "context" $) | nindent 12 }}
          {{- else if .Values.notary.server.startupProbe.enabled }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.notary.server.startupProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: notary-server
          {{- end }}
          {{- if .Values.notary.server.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.notary.server.customLivenessProbe "context" $) | nindent 12 }}
          {{- else if .Values.notary.server.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.notary.server.livenessProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: notary-server
          {{- end }}
          {{- if .Values.notary.server.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.notary.server.customReadinessProbe "context" $) | nindent 12 }}
          {{- else if .Values.notary.server.readinessProbe.enabled }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.notary.server.readinessProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: notary-server
          {{- end }}
          {{- end }}
          {{- if .Values.notary.server.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.notary.server.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.notary.server.resources }}
          resources: {{- toYaml .Values.notary.server.resources | nindent 12 }}
          {{- 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 }}
        {{- 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 8 }}
      {{- end }}
{{- end }}
