{{- /*
Copyright Broadcom, Inc. All Rights Reserved.
SPDX-License-Identifier: APACHE-2.0
*/}}

{{- if eq .Values.exposureType "proxy" }}
apiVersion: {{ template "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ include "harbor.nginx" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  {{- $versionLabel := dict "app.kubernetes.io/version" ( include "common.images.version" ( dict "imageRoot" .Values.nginx.image "chart" .Chart ) ) }}
  {{- $labels := include "common.tplvalues.merge" ( dict "values" ( list .Values.commonLabels $versionLabel ) "context" . ) }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" $labels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: nginx
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  replicas: {{ .Values.nginx.replicaCount }}
  {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.nginx.podLabels .Values.commonLabels $versionLabel ) "context" . ) }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 6 }}
      app.kubernetes.io/component: nginx
  {{- if .Values.nginx.updateStrategy }}
  strategy: {{- toYaml .Values.nginx.updateStrategy | nindent 4 }}
  {{- end }}
  template:
    metadata:
      labels: {{- include "common.labels.standard" ( dict "customLabels" $podLabels "context" $ ) | nindent 8 }}
        app.kubernetes.io/component: nginx
      annotations:
        {{- if not .Values.nginx.tls.enabled }}
        checksum/configmap: {{ include (print $.Template.BasePath "/nginx/configmap-http.yaml") . | sha256sum }}
        {{- else }}
        checksum/configmap: {{ include (print $.Template.BasePath "/nginx/configmap-https.yaml") . | sha256sum }}
        {{- end }}
        {{- if (include "harbor.autoGenCertForNginx" .) }}
        checksum/tls-secret: {{ include (print $.Template.BasePath "/nginx/tls-secret.yaml") . | sha256sum }}
        {{- end }}
        {{- if .Values.nginx.podAnnotations }}
        {{- include "common.tplvalues.render" (dict "value" .Values.nginx.podAnnotations "context" $) | nindent 8 }}
        {{- end }}
    spec:
      {{- include "harbor.imagePullSecrets" . | nindent 6 }}
      automountServiceAccountToken: {{ .Values.nginx.automountServiceAccountToken }}
      {{- if .Values.nginx.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.nginx.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.nginx.affinity }}
      affinity: {{- include "common.tplvalues.render" (dict "value" .Values.nginx.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.nginx.podAffinityPreset "component" "nginx" "customLabels" $podLabels "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.nginx.podAntiAffinityPreset "component" "nginx" "customLabels" $podLabels "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.nginx.nodeAffinityPreset.type "key" .Values.nginx.nodeAffinityPreset.key "values" .Values.nginx.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.nginx.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" (dict "value" .Values.nginx.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.nginx.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.nginx.tolerations "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.nginx.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.nginx.topologySpreadConstraints "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.nginx.priorityClassName }}
      priorityClassName: {{ .Values.nginx.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.nginx.schedulerName }}
      schedulerName: {{ .Values.nginx.schedulerName }}
      {{- end }}
      serviceAccountName: {{ include "harbor.nginx.serviceAccountName" . }}
      {{- if .Values.nginx.podSecurityContext.enabled }}
      securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.nginx.podSecurityContext "context" $) | nindent 8 }}
      {{- end }}
      initContainers:
        - name: preserve-logs-symlinks
          image: {{ include "harbor.nginx.image" . }}
          imagePullPolicy: {{ .Values.nginx.image.pullPolicy | quote }}
          {{- if .Values.nginx.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.nginx.containerSecurityContext "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.nginx.resources }}
          resources: {{- toYaml .Values.nginx.resources | nindent 12 }}
          {{- else if ne .Values.nginx.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.nginx.resourcesPreset) | nindent 12 }}
          {{- end }}
          command:
            - /bin/bash
          args:
            - -ec
            - |
              #!/bin/bash
              . /opt/bitnami/scripts/libfs.sh
              # We copy the logs folder because it has symlinks to stdout and stderr
              if ! is_dir_empty /opt/bitnami/nginx/logs; then
                cp -r /opt/bitnami/nginx/logs /emptydir/app-logs-dir
              fi
          volumeMounts:
            - name: empty-dir
              mountPath: /emptydir
      {{- if .Values.nginx.initContainers }}
      {{- include "common.tplvalues.render" (dict "value" .Values.nginx.initContainers "context" $) | nindent 8 }}
      {{- end }}
      containers:
        - name: nginx
          image: {{ include "harbor.nginx.image" . }}
          imagePullPolicy: {{ .Values.nginx.image.pullPolicy | quote }}
          {{- if .Values.nginx.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.nginx.containerSecurityContext "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.diagnosticMode.enabled }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.command "context" $) | nindent 12 }}
          {{- else if .Values.nginx.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.nginx.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.nginx.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.nginx.args "context" $) | nindent 12 }}
          {{- end }}
          env:
            - name: BITNAMI_DEBUG
              value: {{ ternary "true" "false" .Values.nginx.image.debug | quote }}
            {{- if .Values.nginx.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.nginx.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          envFrom:
            {{- if .Values.nginx.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.nginx.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.nginx.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.nginx.extraEnvVarsSecret "context" $) }}
            {{- end }}
          {{- if .Values.nginx.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.nginx.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          ports:
            - containerPort: {{ .Values.nginx.containerPorts.http }}
              name: http
            - containerPort: {{ .Values.nginx.containerPorts.https }}
              name: https
          {{- if not .Values.diagnosticMode.enabled }}
          {{- if .Values.nginx.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.nginx.customStartupProbe "context" $) | nindent 12 }}
          {{- else if .Values.nginx.startupProbe.enabled }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.nginx.startupProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: {{ ternary "https" "http" .Values.nginx.tls.enabled }}
          {{- end }}
          {{- if .Values.nginx.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.nginx.customLivenessProbe "context" $) | nindent 12 }}
          {{- else if .Values.nginx.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.nginx.livenessProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: {{ ternary "https" "http" .Values.nginx.tls.enabled }}
          {{- end }}
          {{- if .Values.nginx.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.nginx.customReadinessProbe "context" $) | nindent 12 }}
          {{- else if .Values.nginx.readinessProbe.enabled }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.nginx.readinessProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /
              port: {{ ternary "https" "http" .Values.nginx.tls.enabled }}
              scheme: {{ ternary "https" "http" .Values.nginx.tls.enabled  | upper }}
          {{- end }}
          {{- end }}
          {{- if .Values.nginx.resources }}
          resources: {{- toYaml .Values.nginx.resources | nindent 12 }}
          {{- else if ne .Values.nginx.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.nginx.resourcesPreset) | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: empty-dir
              mountPath: /tmp
              subPath: tmp-dir
            - name: empty-dir
              mountPath: /opt/bitnami/nginx/conf
              subPath: app-conf-dir
            - name: empty-dir
              mountPath: /opt/bitnami/nginx/tmp
              subPath: app-tmp-dir
            - name: config
              mountPath: /opt/bitnami/nginx/conf/nginx.conf
              subPath: nginx.conf
            {{- if .Values.nginx.tls.enabled }}
            - name: certificate
              mountPath: /etc/nginx/cert
            {{- end }}
            {{- if .Values.nginx.extraVolumeMounts }}
            {{- include "common.tplvalues.render" (dict "value" .Values.nginx.extraVolumeMounts "context" $) | nindent 12 }}
            {{- end }}
        {{- if .Values.nginx.sidecars }}
        {{- include "common.tplvalues.render" (dict "value" .Values.nginx.sidecars "context" $) | nindent 8 }}
        {{- end }}
      volumes:
        - name: empty-dir
          emptyDir: {}
        - name: config
          configMap:
            name: {{ include "harbor.nginx" . }}
        {{- if .Values.nginx.tls.enabled }}
        - name: certificate
          secret:
            {{- if .Values.nginx.tls.existingSecret }}
            secretName: {{ .Values.nginx.tls.existingSecret }}
            {{- else }}
            secretName: {{ include "harbor.nginx" . }}
            {{- end }}
        {{- end }}
      {{- if .Values.nginx.extraVolumes }}
      {{- include "common.tplvalues.render" (dict "value" .Values.nginx.extraVolumes "context" $) | nindent 8 }}
      {{- end }}
{{- end }}
