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

apiVersion: {{ if .Values.mongos.useStatefulSet }}{{ include "common.capabilities.statefulset.apiVersion" . }}{{- else }}{{ include "common.capabilities.deployment.apiVersion" . }}{{- end }}
kind: {{ if .Values.mongos.useStatefulSet }}StatefulSet{{- else }}Deployment{{- end }}
metadata:
  name: {{ printf "%s-mongos" (include "common.names.fullname" .) }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: mongos
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  {{- if .Values.mongos.useStatefulSet }}
  serviceName: {{ include "mongodb-sharded.serviceName" . }}
  podManagementPolicy: {{ .Values.mongos.podManagementPolicy }}
  updateStrategy:
  {{- else }}
  strategy:
  {{- end }}
    {{- toYaml .Values.mongos.updateStrategy | nindent 4 }}
  replicas: {{ .Values.mongos.replicaCount }}
  {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.mongos.podLabels .Values.common.podLabels .Values.commonLabels ) "context" . ) }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 6 }}
      app.kubernetes.io/component: mongos
  template:
    metadata:
      labels: {{- include "common.labels.standard" ( dict "customLabels" $podLabels "context" $ ) | nindent 8 }}
        app.kubernetes.io/component: mongos
      {{- if or .Values.common.podAnnotations .Values.mongos.podAnnotations .Values.metrics.enabled }}
      annotations:
        {{- if or .Values.common.podAnnotations .Values.mongos.podAnnotations }}
        {{- $annotations := include "common.tplvalues.merge" ( dict "values" ( list .Values.mongos.podAnnotations .Values.common.podAnnotations ) "context" . ) }}
        {{- include "common.tplvalues.render" ( dict "value" $annotations  "context" $ ) | nindent 8 }}
        {{- end }}
        {{- if .Values.metrics.enabled }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.metrics.podAnnotations "context" $ ) | nindent 8 }}
        {{- end }}
      {{- end }}
    spec:
      {{- if .Values.mongos.schedulerName }}
      schedulerName: {{ .Values.mongos.schedulerName | quote }}
      {{- end }}
      serviceAccountName: {{ include "mongodb-sharded.serviceAccountName" (dict "component" "mongos" "value" $.Values.mongos.serviceAccount "context" $) }}
      automountServiceAccountToken: {{ .Values.mongos.automountServiceAccountToken }}
      {{- if .Values.mongos.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.mongos.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.mongos.affinity }}
      affinity: {{- include "common.tplvalues.render" (dict "value" .Values.mongos.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.mongos.podAffinityPreset "component" "mongos" "customLabels" $podLabels "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.mongos.podAntiAffinityPreset "component" "mongos" "customLabels" $podLabels "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.mongos.nodeAffinityPreset.type "key" .Values.mongos.nodeAffinityPreset.key "values" .Values.mongos.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.mongos.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" (dict "value" .Values.mongos.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.mongos.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.mongos.tolerations "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.mongos.priorityClassName }}
      priorityClassName: {{ .Values.mongos.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.mongos.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.mongos.topologySpreadConstraints "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.mongos.podSecurityContext.enabled }}
      securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.mongos.podSecurityContext "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.mongos.terminationGracePeriodSeconds }}
      terminationGracePeriodSeconds: {{ .Values.mongos.terminationGracePeriodSeconds }}
      {{- end }}
      {{- include "mongodb-sharded.imagePullSecrets" . | nindent 6 }}
      {{- if or $.Values.mongos.initContainers $.Values.common.initContainers }}
      initContainers:
        {{- with $.Values.mongos.initContainers }}
        {{- include "common.tplvalues.render" ( dict "value" . "context" $ ) | nindent 8 }}
        {{- end }}
        {{- with $.Values.common.initContainers }}
        {{- include "common.tplvalues.render" ( dict "value" . "context" $ ) | nindent 8 }}
        {{- end }}
      {{- end }}
      containers:
        - name: mongos
          image: {{ include "mongodb-sharded.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
          {{- if .Values.mongos.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.mongos.containerSecurityContext "context" $) | nindent 12 }}
          {{- end }}
          env:
            - name: MONGODB_ENABLE_NUMACTL
              value: {{ ternary "yes" "no" $.Values.common.mongodbEnableNumactl | quote }}
            - name: BITNAMI_DEBUG
              value: {{ ternary "true" "false" (or .Values.image.debug .Values.diagnosticMode.enabled) | quote }}
            - name: MONGODB_SHARDING_MODE
              value: "mongos"
            - name: MONGODB_INIT_RETRY_ATTEMPTS
              value: {{ .Values.common.mongodbInitRetryAttempts | quote }}
            - name: MONGODB_INIT_RETRY_DELAY
              value: {{ .Values.common.mongodbInitRetryDelay | quote }}
            {{- if $.Values.auth.enabled }}
            - name: MONGODB_ROOT_USER
              value: {{ .Values.auth.rootUser | quote }}
            {{- if .Values.auth.usePasswordFiles }}
            - name: MONGODB_ROOT_PASSWORD_FILE
              value: "/bitnami/mongodb/secrets/mongodb-root-password"
            - name: MONGODB_REPLICA_SET_KEY_FILE
              value: "/bitnami/mongodb/secrets/mongodb-replica-set-key"
            {{- else }}
            - name: MONGODB_ROOT_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "mongodb-sharded.secret" . }}
                  key: mongodb-root-password
            - name: MONGODB_REPLICA_SET_KEY
              valueFrom:
                secretKeyRef:
                  name: {{ include "mongodb-sharded.secret" . }}
                  key: mongodb-replica-set-key
            {{- end }}
            {{- end }}
            - name: MONGODB_POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            {{- if .Values.common.useHostnames }}
            - name: MONGODB_ADVERTISED_HOSTNAME
              value: "$(MONGODB_POD_NAME)"
            {{- end }}
            - name: MONGODB_PORT_NUMBER
              value: {{ $.Values.common.containerPorts.mongodb | quote }}
            - name: MONGODB_CFG_PRIMARY_HOST
              value: {{ include "mongodb-sharded.configServer.primaryHost" . }}
            - name: MONGODB_CFG_REPLICA_SET_NAME
              value: {{ include "mongodb-sharded.configServer.rsName" . }}
            - name: MONGODB_SYSTEM_LOG_VERBOSITY
              value: {{ .Values.common.mongodbSystemLogVerbosity | quote }}
            - name: MONGODB_DISABLE_SYSTEM_LOG
              {{- if .Values.common.mongodbDisableSystemLog }}
              value: "yes"
              {{- else }}
              value: "no"
              {{- end }}
            - name: MONGODB_ENABLE_IPV6
            {{- if .Values.common.mongodbEnableIPv6 }}
              value: "yes"
            {{- else }}
              value: "no"
            {{- end }}
            - name: MONGODB_ENABLE_DIRECTORY_PER_DB
            {{- if .Values.common.mongodbDirectoryPerDB }}
              value: "yes"
            {{- else }}
              value: "no"
            {{- end }}
            {{- if .Values.mongos.mongodbExtraFlags }}
            - name: MONGODB_EXTRA_FLAGS
              value: {{ .Values.mongos.mongodbExtraFlags | join " " | quote }}
            {{- end }}
            {{- if .Values.common.extraEnvVars }}
              {{- include "common.tplvalues.render" ( dict "value" $.Values.common.extraEnvVars "context" $ ) | nindent 12 }}
            {{- end }}
            {{- if .Values.mongos.extraEnvVars }}
              {{- include "common.tplvalues.render" ( dict "value" $.Values.mongos.extraEnvVars "context" $ ) | nindent 12 }}
            {{- end }}
          {{- if or .Values.common.extraEnvVarsCM .Values.common.extraEnvVarsSecret .Values.mongos.extraEnvVarsCM .Values.mongos.extraEnvVarsSecret }}
          envFrom:
            {{- if .Values.common.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" ( dict "value" .Values.common.extraEnvVarsCM "context" $ ) }}
            {{- end }}
            {{- if .Values.common.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" ( dict "value" .Values.common.extraEnvVarsSecret "context" $ ) }}
            {{- end }}
            {{- if .Values.configsvr.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" ( dict "value" .Values.mongos.extraEnvVarsCM "context" $ ) }}
            {{- end }}
            {{- if .Values.mongos.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" ( dict "value" .Values.mongos.extraEnvVarsSecret "context" $ ) }}
            {{- end }}
          {{- end }}
          ports:
            - name: mongodb
              containerPort: {{ $.Values.common.containerPorts.mongodb }}
          {{- if .Values.diagnosticMode.enabled }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.command "context" $) | nindent 12 }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.args "context" $) | nindent 12 }}
          {{- else }}
          {{- if .Values.mongos.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.mongos.command "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.mongos.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.mongos.args "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.mongos.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.mongos.customLivenessProbe "context" $) | nindent 12 }}
          {{- else if .Values.mongos.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.mongos.livenessProbe "enabled") "context" $) | nindent 12 }}
            exec:
              command:
                - pgrep
                - mongos
          {{- end }}
          {{- if .Values.mongos.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.mongos.customReadinessProbe "context" $) | nindent 12 }}
          {{- else if .Values.mongos.readinessProbe.enabled }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.mongos.readinessProbe "enabled") "context" $) | nindent 12 }}
            exec:
              command:
                - /bin/sh
                - -c
                - mongosh --port $MONGODB_PORT_NUMBER --eval "db.adminCommand('ping')"
          {{- end }}
          {{- if .Values.mongos.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.mongos.customStartupProbe "context" $) | nindent 12 }}
          {{- else if .Values.mongos.startupProbe.enabled }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.mongos.startupProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: mongodb
          {{- end }}
          {{- end }}
          {{- if .Values.mongos.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.mongos.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: empty-dir
              mountPath: /tmp
              subPath: tmp-dir
            - name: empty-dir
              mountPath: /opt/bitnami/mongodb/conf
              subPath: app-conf-dir
            - name: empty-dir
              mountPath: /opt/bitnami/mongodb/tmp
              subPath: app-tmp-dir
            - name: empty-dir
              mountPath: /opt/bitnami/mongodb/logs
              subPath: app-logs-dir
            - name: empty-dir
              mountPath: /.mongodb
              subPath: app-mongosh-dir
            {{- if .Values.auth.usePasswordFiles }}
            - name: secrets
              mountPath: /bitnami/mongodb/secrets/
            {{- end }}
            {{- if or .Values.mongos.config .Values.mongos.configCM }}
            - name: config
              mountPath: /bitnami/mongodb/conf/
            {{- end }}
            {{- if $.Values.common.extraVolumeMounts }}
              {{- include "common.tplvalues.render" ( dict "value" $.Values.common.extraVolumeMounts "context" $ ) | nindent 12 }}
            {{- end }}
            {{- if $.Values.mongos.extraVolumeMounts }}
              {{- include "common.tplvalues.render" ( dict "value" $.Values.mongos.extraVolumeMounts "context" $ ) | nindent 12 }}
            {{- end }}
          {{- if .Values.mongos.resources }}
          resources: {{- toYaml .Values.mongos.resources | nindent 12 }}
          {{- else if ne .Values.mongos.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.mongos.resourcesPreset) | nindent 12 }}
          {{- end }}
        {{- if .Values.metrics.enabled }}
        - name: metrics
          image: {{ include "mongodb-sharded.metrics.image" . }}
          imagePullPolicy: {{ .Values.metrics.image.pullPolicy | quote }}
          {{- if .Values.metrics.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.metrics.containerSecurityContext "context" $) | nindent 12 }}
          {{- end }}
          env:
          {{- if $.Values.auth.enabled }}
            - name: MONGODB_ROOT_USER
              value: {{ .Values.auth.rootUser | quote }}
          {{- if .Values.auth.usePasswordFiles }}
            - name: MONGODB_ROOT_PASSWORD_FILE
              value: "/bitnami/mongodb/secrets/mongodb-root-password"
          {{- else }}
            - name: MONGODB_ROOT_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "mongodb-sharded.secret" . }}
                  key: mongodb-root-password
          {{- end }}
          {{- end }}
          {{- if .Values.diagnosticMode.enabled }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.command "context" $) | nindent 12 }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.args "context" $) | nindent 12 }}
          {{- else }}
          command:
            - sh
            - -ec
            - |-
              #!/bin/sh
              {{- if .Values.auth.usePasswordFiles }}
              export MONGODB_ROOT_PASSWORD="$(cat "${MONGODB_ROOT_PASSWORD_FILE}")"
              {{- end }}
              /bin/mongodb_exporter --collect-all --compatible-mode --web.listen-address ":{{ .Values.metrics.containerPorts.metrics }}" --mongodb.uri mongodb://$(echo $MONGODB_ROOT_USER):$(echo $MONGODB_ROOT_PASSWORD | sed -r "s/@/%40/g;s/:/%3A/g")@localhost:{{ .Values.common.containerPorts.mongodb }}/admin{{ ternary "?ssl=true" "" $.Values.metrics.useTLS }} {{ .Values.metrics.extraArgs }}
          {{- end }}
          volumeMounts:
            - name: empty-dir
              mountPath: /tmp
              subPath: tmp-dir
            {{- if .Values.auth.usePasswordFiles }}
            - name: secrets
              mountPath: /bitnami/mongodb/secrets/
            {{- end }}
          ports:
            - name: metrics
              containerPort: {{ .Values.metrics.containerPorts.metrics }}
          {{- if not .Values.diagnosticMode.enabled }}
          {{- if .Values.metrics.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.metrics.customLivenessProbe "context" $) | nindent 12 }}
          {{- else if .Values.metrics.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.metrics.livenessProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: metrics
          {{- end }}
          {{- if .Values.metrics.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.metrics.customReadinessProbe "context" $) | nindent 12 }}
          {{- else if .Values.metrics.readinessProbe.enabled }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.metrics.readinessProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /metrics
              port: metrics
          {{- end }}
          {{- if .Values.metrics.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.metrics.customStartupProbe "context" $) | nindent 12 }}
          {{- else if .Values.metrics.startupProbe.enabled }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.metrics.startupProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: metrics
          {{- end }}
          {{- end }}
          {{- if .Values.metrics.resources }}
          resources: {{- toYaml .Values.metrics.resources | nindent 12 }}
          {{- else if ne .Values.metrics.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.metrics.resourcesPreset) | nindent 12 }}
          {{- end }}
        {{- end }}
        {{- with .Values.mongos.sidecars }}
        {{- include "common.tplvalues.render" ( dict "value" . "context" $ ) | nindent 8 }}
        {{- end }}
        {{- with .Values.common.sidecars }}
        {{- include "common.tplvalues.render" ( dict "value" . "context" $ ) | nindent 8 }}
        {{- end }}
      volumes:
        - name: empty-dir
          emptyDir: {}
        {{- if .Values.auth.usePasswordFiles }}
        - name: secrets
          secret:
            secretName: {{ include "mongodb-sharded.secret" . }}
        {{- end }}
        {{- if or .Values.mongos.config .Values.mongos.configCM }}
        - name: config
          configMap:
            name: {{ include "mongodb-sharded.mongos.configCM" . }}
        {{- end }}
        {{- if $.Values.common.extraVolumes }}
          {{- include "common.tplvalues.render" ( dict "value" $.Values.common.extraVolumes "context" $ ) | nindent 8 }}
        {{- end }}
        {{- if $.Values.mongos.extraVolumes }}
          {{- include "common.tplvalues.render" ( dict "value" $.Values.mongos.extraVolumes "context" $ ) | nindent 8 }}
        {{- end }}
