apiVersion: {{ include "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ include "mongo-express.fullname" . }}
  labels:
    {{- include "mongo-express.labels" . | nindent 4 }}
    {{- if .Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
  {{- if or .Values.deploymentAnnotations .Values.commonAnnotations }}
  {{- $annotations := include "common.tplvalues.merge" ( dict "values" ( list .Values.deploymentAnnotations .Values.commonAnnotations ) "context" . ) }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" $annotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      {{- include "mongo-express.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      labels:
        {{- include "mongo-express.selectorLabels" . | nindent 8 }}
        {{- if .Values.podLabels }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.podLabels "context" $ ) | nindent 8 }}
        {{- end }}
        {{- if .Values.commonLabels }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 8 }}
        {{- end }}
      annotations:
        {{- if not .Values.existingSecret }}
        checksum/secret: {{ include (print $.Template.BasePath "/secret.yaml") . | sha256sum }}
        {{- end }}
        {{- if .Values.podAnnotations }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.podAnnotations "context" $ ) | nindent 8 }}
        {{- end }}
        {{- if .Values.commonAnnotations }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 8 }}
        {{- end }}
    spec:
      {{- include "common.images.pullSecrets" ( dict "images" ( list .Values.image ) "global" .Values.global ) | nindent 6 }}
      {{- if .Values.priorityClassName }}
      priorityClassName: {{ .Values.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.runtimeClassName }}
      runtimeClassName: {{ .Values.runtimeClassName | quote }}
      {{- end }}
      serviceAccountName: {{ include "mongo-express.serviceAccountName" . }}
      {{- if .Values.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.topologySpreadConstraints "context" .) | nindent 8 }}
      {{- end }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      containers:
        - name: mongo-express
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          image: {{ include "common.images.image" ( dict "imageRoot" .Values.image "global" .Values.global ) }}
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          {{- if .Values.extraArgs }}
          args:
            {{- range $key, $value := .Values.extraArgs }}
            - --{{ $key }}={{ $value }}
            {{- end }}
          {{- end }}
          env:
            - name: PORT
              value: {{ .Values.containerPorts.http | quote }}
            - name: MONGODB_SERVER
              value: {{ .Values.mongodbServer | quote }}
            - name: MONGODB_PORT
              value: {{ .Values.mongodbPort | quote }}
            - name: ME_CONFIG_MONGODB_ENABLE_ADMIN
              value: {{ .Values.mongodbEnableAdmin | quote }}
            {{- if .Values.mongodbEnableAdmin }}
            {{- if and .Values.mongodbAdminUsername .Values.mongodbAdminPassword}}
            - name: MONGODB_USERNAME
              value: {{ .Values.mongodbAdminUsername | quote }}
            - name: MONGODB_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "mongo-express.secretName" . }}
                  key: {{ include "mongo-express.secretKeyMongodbAdminPassword" . }}
            - name: ME_CONFIG_MONGODB_URL
              value: "mongodb://$(MONGODB_USERNAME):$(MONGODB_PASSWORD)@$(MONGODB_SERVER):$(MONGODB_PORT)/"
            {{- else }}
            - name: ME_CONFIG_MONGODB_URL
              value: "mongodb://$(MONGODB_SERVER):$(MONGODB_PORT)/"
            {{- end }}
            {{- else }}
            - name: MONGODB_DATABASE
              value: {{ .Values.mongodbAuthDatabase | quote }}
            {{- if and .Values.mongodbAuthUsername .Values.mongodbAuthPassword}}
            - name: MONGODB_USERNAME
              value: {{ .Values.mongodbAuthUsername | quote }}
            - name: MONGODB_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "mongo-express.secretName" . }}
                  key: {{ include "mongo-express.secretKeyMongodbAuthPassword" . }}
            - name: ME_CONFIG_MONGODB_URL
              value: "mongodb://$(MONGODB_USERNAME):$(MONGODB_PASSWORD)@$(MONGODB_SERVER):$(MONGODB_PORT)/$(MONGODB_DATABASE)"
            {{- else }}
            - name: ME_CONFIG_MONGODB_URL
              value: "mongodb://$(MONGODB_SERVER):$(MONGODB_PORT)/$(MONGODB_DATABASE)"
            {{- end }}
            {{- end }}
            - name: ME_CONFIG_SITE_BASEURL
              value: {{ .Values.siteBaseUrl | quote }}
            - name: ME_CONFIG_SITE_COOKIESECRET
              valueFrom:
                secretKeyRef:
                  name: {{ include "mongo-express.secretName" . }}
                  key: {{ include "mongo-express.secretKeySiteCookieSecret" . }}
            - name: ME_CONFIG_SITE_SESSIONSECRET
              valueFrom:
                secretKeyRef:
                  name: {{ include "mongo-express.secretName" . }}
                  key: {{ include "mongo-express.secretKeySiteSessionSecret" . }}
            {{- if and .Values.basicAuthUsername .Values.basicAuthPassword }}
            - name: ME_CONFIG_BASICAUTH
              value: "true"
            - name: ME_CONFIG_BASICAUTH_USERNAME
              value: {{ .Values.basicAuthUsername | quote }}
            - name: ME_CONFIG_BASICAUTH_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "mongo-express.secretName" . }}
                  key: {{ include "mongo-express.secretKeyBasicAuthPassword" . }}
            {{- else }}
            - name: ME_CONFIG_BASICAUTH
              value: "false"
            {{- end }}
            {{- 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: {{ include "common.tplvalues.render" (dict "value" .Values.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.extraEnvVarsSecret "context" $) }}
            {{- end }}
          {{- end }}
          ports:
            - name: http
              containerPort: {{ .Values.containerPorts.http }}
              protocol: TCP
          {{- if .Values.livenessProbe.enabled }}
          livenessProbe:
            httpGet:
              path: {{ .Values.siteBaseUrl | trimSuffix "/" }}/status
              port: http
            initialDelaySeconds: {{ .Values.livenessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.livenessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.livenessProbe.timeoutSeconds }}
            failureThreshold: {{ .Values.livenessProbe.failureThreshold }}
            successThreshold: {{ .Values.livenessProbe.successThreshold }}
          {{- end }}
          {{- if .Values.readinessProbe.enabled }}
          readinessProbe:
            httpGet:
              path: {{ .Values.siteBaseUrl | trimSuffix "/" }}/status
              port: http
            initialDelaySeconds: {{ .Values.readinessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.readinessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.readinessProbe.timeoutSeconds }}
            failureThreshold: {{ .Values.readinessProbe.failureThreshold }}
            successThreshold: {{ .Values.readinessProbe.successThreshold }}
          {{- end }}
          {{- if .Values.startupProbe.enabled }}
          startupProbe:
            httpGet:
              path: {{ .Values.siteBaseUrl | trimSuffix "/" }}/status
              port: http
            initialDelaySeconds: {{ .Values.startupProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.startupProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.startupProbe.timeoutSeconds }}
            failureThreshold: {{ .Values.startupProbe.failureThreshold }}
            successThreshold: {{ .Values.startupProbe.successThreshold }}
          {{- end }}
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
    {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
    {{- end }}
    {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
    {{- end }}
