apiVersion: {{ include "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ include "common.names.fullname" . }}
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    {{- if .Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" . | nindent 6 }}
  template:
    metadata:
      labels: {{- include "common.labels.standard" . | nindent 8 }}
        {{- if .Values.podLabels }}
        {{- include "common.tplvalues.render" (dict "value" .Values.podLabels "context" $) | nindent 8 }}
        {{- end }}
      {{- if or .Values.podAnnotations (and .Values.metrics.enabled .Values.metrics.podAnnotations) (and .Values.serverBlock (not .Values.existingServerBlockConfigmap)) }}
      annotations:
        {{- if .Values.podAnnotations }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.podAnnotations "context" $) | nindent 8 }}
        {{- end }}
        {{- if and .Values.metrics.enabled .Values.metrics.podAnnotations }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.metrics.podAnnotations "context" $) | nindent 8 }}
        {{- end }}
        {{- if and .Values.serverBlock (not .Values.existingServerBlockConfigmap) }}
        checksum/server-block-configuration: {{ include (print $.Template.BasePath "/server-block-configmap.yaml") . | sha256sum }}
        {{- end }}
      {{- end }}
    spec:
      {{- include "nginx.imagePullSecrets" . | nindent 6 }}
      {{- if .Values.affinity }}
      affinity: {{- include "common.tplvalues.render" (dict "value" .Values.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.podAffinityPreset "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.podAntiAffinityPreset "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.nodeAffinityPreset.type "key" .Values.nodeAffinityPreset.key "values" .Values.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" (dict "value" .Values.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.tolerations "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.podSecurityContext.enabled }}
      securityContext:
        fsGroup: {{ .Values.podSecurityContext.fsGroup }}
      {{- end }}
      {{- if .Values.cloneStaticSiteFromGit.enabled }}
      initContainers:
        - name: git-clone-repository
          image: {{ include "nginx.cloneStaticSiteFromGit.image" . }}
          imagePullPolicy: {{ .Values.cloneStaticSiteFromGit.image.pullPolicy | quote }}
          command:
            - /bin/bash
            - -ec
            - |
              git clone {{ .Values.cloneStaticSiteFromGit.repository }} --branch {{ .Values.cloneStaticSiteFromGit.branch }} /app
          volumeMounts:
            - name: staticsite
              mountPath: /app
      containers:
        - name: git-repo-syncer
          image: {{ include "nginx.cloneStaticSiteFromGit.image" . }}
          imagePullPolicy: {{ .Values.cloneStaticSiteFromGit.image.pullPolicy | quote }}
          command:
            - /bin/bash
            - -ec
            - |
              while true; do
                  cd /app && git pull origin {{ .Values.cloneStaticSiteFromGit.branch }}
                  sleep {{ .Values.cloneStaticSiteFromGit.interval }}
              done
          volumeMounts:
            - name: staticsite
              mountPath: /app
      {{- else }}
      containers:
      {{- end }}
        - name: nginx
          image: {{ include "nginx.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
          {{- if .Values.containerSecurityContext.enabled }}
          securityContext:
            runAsUser: {{ .Values.containerSecurityContext.runAsUser }}
            runAsNonRoot: {{ .Values.containerSecurityContext.runAsNonRoot }}
          {{- end }}
          {{- if .Values.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.command "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.args "context" $) | nindent 12 }}
          {{- end }}
          env:
            - name: BITNAMI_DEBUG
              value: {{ ternary "true" "false" .Values.image.debug | quote }}
            {{- if .Values.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          {{- if or .Values.extraEnvVarsCM .Values.extraEnvVarsSecret }}
          envFrom:
            {{- if .Values.extraEnvVarsCM }}
            - configMapRef:
                name: {{ tpl .Values.extraEnvVarsCM . | quote }}
            {{- end }}
            {{- if .Values.extraEnvVarsSecret }}
            - secretRef:
                name: {{ tpl .Values.extraEnvVarsSecret . | quote }}
            {{- end }}
          {{- end }}
          ports:
            - name: http
              containerPort: {{ .Values.containerPorts.http }}
            {{- if .Values.containerPorts.https }}
            - name: https
              containerPort: {{ .Values.containerPorts.https }}
            {{- end }}
          {{- if .Values.livenessProbe.enabled }}
          livenessProbe:
            tcpSocket:
              port: http
            periodSeconds: {{ .Values.livenessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.livenessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.livenessProbe.successThreshold }}
            failureThreshold: {{ .Values.livenessProbe.failureThreshold }}
          {{- else if .Values.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.customLivenessProbe "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.readinessProbe.enabled }}
          readinessProbe:
            tcpSocket:
              port: http
            initialDelaySeconds: {{ .Values.readinessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.readinessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.readinessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.readinessProbe.successThreshold }}
            failureThreshold: {{ .Values.readinessProbe.failureThreshold }}
          {{- else if .Values.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.customReadinessProbe "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.resources }}
          resources: {{- toYaml .Values.resources | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: nginx-server-block-paths
              mountPath: /opt/bitnami/nginx/conf/server_blocks
            {{- if or .Values.serverBlock .Values.existingServerBlockConfigmap }}
            - name: nginx-server-block
              mountPath: /opt/bitnami/nginx/conf/server_blocks/common
            {{- end }}
            {{- if .Values.ldapDaemon.enabled }}
            - name: nginx-server-block-ldap
              mountPath: /opt/bitnami/nginx/conf/server_blocks/ldap
            {{- end }}
            {{- if (include "nginx.useStaticSite" .) }}
            - name: staticsite
              mountPath: /app
            {{- end }}
            {{- if .Values.extraVolumeMounts }}
            {{- include "common.tplvalues.render" ( dict "value" .Values.extraVolumeMounts "context" $) | nindent 12 }}
            {{- end }}
        {{- if .Values.ldapDaemon.enabled }}
        - name: ldap-daemon
          image: {{ include "nginx.ldapDaemon.image" . }}
          imagePullPolicy: {{ .Values.ldapDaemon.image.pullPolicy | quote }}
          env:
            - name: NGINXLDAP_PORT_NUMBER
              value: {{ .Values.ldapDaemon.port | quote}}
            - name: NGINXLDAP_LDAP_URI
              value: {{ .Values.ldapDaemon.ldapConfig.uri | quote }}
            - name: NGINXLDAP_LDAP_BASE_DN
              value: {{ .Values.ldapDaemon.ldapConfig.baseDN | quote }}
            - name: NGINXLDAP_LDAP_BIND_DN
              value: {{ .Values.ldapDaemon.ldapConfig.bindDN | quote }}
            - name: NGINXLDAP_LDAP_BIND_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ template "common.names.fullname" . }}-ldap-daemon
                  key: ldap-daemon-ldap-bind-password
            - name: NGINXLDAP_LDAP_FILTER
              value: {{ .Values.ldapDaemon.ldapConfig.filter | quote }}
            - name: NGINXLDAP_HTTP_REALM
              value: {{ .Values.ldapDaemon.ldapConfig.httpRealm | quote }}
            - name: NGINXLDAP_HTTP_COOKIE_NAME
              value: {{ .Values.ldapDaemon.ldapConfig.httpCookieName | quote }}
          ports:
            - name: ldap-daemon
              containerPort: {{ .Values.ldapDaemon.port }}
          {{- if .Values.ldapDaemon.livenessProbe.enabled }}
          livenessProbe:
            tcpSocket:
              port: ldap-daemon
            periodSeconds: {{ .Values.ldapDaemon.livenessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.ldapDaemon.livenessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.ldapDaemon.livenessProbe.successThreshold }}
            failureThreshold: {{ .Values.ldapDaemon.livenessProbe.failureThreshold }}
          {{- else if .Values.ldapDaemon.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.ldapDaemon.customLivenessProbe "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.ldapDaemon.readinessProbe.enabled }}
          readinessProbe:
            tcpSocket:
              port: ldap-daemon
            initialDelaySeconds: {{ .Values.ldapDaemon.readinessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.ldapDaemon.readinessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.ldapDaemon.readinessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.ldapDaemon.readinessProbe.successThreshold }}
            failureThreshold: {{ .Values.ldapDaemon.readinessProbe.failureThreshold }}
          {{- else if .Values.ldapDaemon.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.ldapDaemon.customReadinessProbe "context" $) | nindent 12 }}
          {{- end }}
        {{- end }}
        {{- if .Values.metrics.enabled }}
        - name: metrics
          image: {{ include "nginx.metrics.image" . }}
          imagePullPolicy: {{ .Values.metrics.image.pullPolicy | quote }}
          command: ['/usr/bin/exporter', '-nginx.scrape-uri', 'http://127.0.0.1:8080/status']
          ports:
            - name: metrics
              containerPort: 9113
          livenessProbe:
            httpGet:
              path: /metrics
              port: metrics
            initialDelaySeconds: 15
            timeoutSeconds: 5
          readinessProbe:
            httpGet:
              path: /metrics
              port: metrics
            initialDelaySeconds: 5
            timeoutSeconds: 1
          {{- if .Values.metrics.resources }}
          resources: {{- toYaml .Values.metrics.resources | nindent 12 }}
          {{- end }}
        {{- end }}
      volumes:
        - name: nginx-server-block-paths
          configMap:
            name: {{ template "common.names.fullname" . }}-server-block
            items:
              - key: server-blocks-paths.conf
                path: server-blocks-paths.conf
        {{- if or .Values.serverBlock .Values.existingServerBlockConfigmap .Values.extraVolumes (include "nginx.useStaticSite" .) }}
        {{- if or .Values.serverBlock .Values.existingServerBlockConfigmap }}
        - name: nginx-server-block
          configMap:
            name: {{ include "nginx.serverBlockConfigmapName" . }}
            {{- if or .Values.serverBlock }}
            items:
              - key: server-block.conf
                path: server-block.conf
            {{- end }}
        {{- end }}
        {{- if (include "nginx.useStaticSite" .) }}
        - name: staticsite
          {{- include "nginx.staticSiteVolume" . | nindent 10 }}
        {{- end }}
        {{- if .Values.extraVolumes }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
        {{- end }}
        {{- if .Values.ldapDaemon.enabled }}
        - name: nginx-server-block-ldap
          secret:
            secretName: {{ include "ldap.nginxServerBlockSecret" . }}
        {{- end }}
