{{- if not .Values.configsvr.external.host }}
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ include "common.names.fullname" . }}-configsvr
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    app.kubernetes.io/component: configsvr
spec:
  selector:
    matchLabels: {{- include "common.labels.matchLabels" . | nindent 6 }}
      app.kubernetes.io/component: configsvr
  serviceName: {{ include "common.names.fullname" . }}-headless
  replicas: {{ .Values.configsvr.replicas }}
  podManagementPolicy: {{ .Values.configsvr.podManagementPolicy }}
  updateStrategy: {{- toYaml .Values.configsvr.updateStrategy | nindent 4 }}
  template:
    metadata:
      labels: {{- include "common.labels.standard" . | nindent 8 }}
        app.kubernetes.io/component: configsvr
        {{- if .Values.configsvr.podLabels }}
          {{- include "common.tplvalues.render" ( dict "value" .Values.configsvr.podLabels "context" $ ) | nindent 8 }}
        {{- end }}
      {{- if or .Values.common.podAnnotations .Values.configsvr.podAnnotations .Values.metrics.enabled }}
      annotations:
        {{- if .Values.common.podAnnotations }}
           {{- include "common.tplvalues.render" ( dict "value" .Values.common.podAnnotations "context" $ ) | nindent 8 }}
        {{- end }}
        {{- if .Values.configsvr.podAnnotations }}
          {{- include "common.tplvalues.render" ( dict "value" .Values.configsvr.podAnnotations "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.common.schedulerName }}
      schedulerName: {{ .Values.common.schedulerName | quote }}
      {{- end }}
      serviceAccountName: {{ include "mongodb-sharded.serviceAccountName" (dict "value" .Values.configsvr.serviceAccount "context" $) }}
      {{- if .Values.configsvr.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.configsvr.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.configsvr.affinity }}
      affinity: {{- include "common.tplvalues.render" (dict "value" .Values.configsvr.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.configsvr.podAffinityPreset "component" "configsvr" "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.configsvr.podAntiAffinityPreset "component" "configsvr" "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.configsvr.nodeAffinityPreset.type "key" .Values.configsvr.nodeAffinityPreset.key "values" .Values.configsvr.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.configsvr.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" (dict "value" .Values.configsvr.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.configsvr.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.configsvr.tolerations "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.configsvr.priorityClassName }}
      priorityClassName: {{ .Values.configsvr.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.securityContext.enabled }}
      securityContext:
        fsGroup: {{ .Values.securityContext.fsGroup }}
      {{- end }}
      {{- include "mongodb-sharded.imagePullSecrets" . | nindent 6 }}
      initContainers:
        {{- if and .Values.volumePermissions.enabled .Values.configsvr.persistence.enabled }}
        - name: volume-permissions
          image: {{ include "mongodb-sharded.volumePermissions.image" . }}
          imagePullPolicy: {{ .Values.volumePermissions.image.pullPolicy | quote }}
          command: ["chown", "-R", "{{ .Values.securityContext.runAsUser }}:{{ .Values.securityContext.fsGroup }}", "{{ .Values.configsvr.persistence.mountPath }}"]
          securityContext:
            runAsUser: 0
          resources: {{ toYaml .Values.volumePermissions.resources | nindent 12 }}
          volumeMounts:
            - name: datadir
              mountPath: {{ .Values.configsvr.persistence.mountPath }}
        {{- end }}
        {{- with .Values.configsvr.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 }}
      containers:
        - name: mongodb
          image: {{ include "mongodb-sharded.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          {{- if .Values.securityContext.enabled }}
          securityContext:
            runAsNonRoot: {{ .Values.securityContext.runAsNonRoot }}
            runAsUser: {{ .Values.securityContext.runAsUser }}
          {{- end }}
          ports:
            - containerPort: {{ .Values.common.containerPorts.mongo }}
              name: mongodb
          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_SYSTEM_LOG_VERBOSITY
              value: {{ .Values.common.mongodbSystemLogVerbosity | quote }}
            - name: MONGODB_DISABLE_SYSTEM_LOG
              {{- if .Values.mongodbDisableSystemLog }}
              value: "yes"
              {{- else }}
              value: "no"
              {{- end }}
            - name: MONGODB_MAX_TIMEOUT
              value: {{ .Values.common.mongodbMaxWaitTimeout | quote }}
            - name: MONGODB_SHARDING_MODE
              value: "configsvr"
            - name: MONGODB_POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: MONGODB_PORT_NUMBER
              value: {{ .Values.common.containerPorts.mongo | quote }}
            - name: MONGODB_INITIAL_PRIMARY_HOST
              value: {{ include "mongodb-sharded.configServer.primaryHost" . }}
            - name: MONGODB_REPLICA_SET_NAME
              value: {{ printf "%s-configsvr" ( include "common.names.fullname" . ) }}
              {{- if .Values.common.useHostnames }}
            - name: MONGODB_ADVERTISED_HOSTNAME
              value: "$(MONGODB_POD_NAME).{{ include "common.names.fullname" . }}-headless.{{ .Release.Namespace }}.svc.{{ .Values.clusterDomain }}"
              {{- end }}
            {{- if .Values.usePasswordFile }}
            - 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 }}
            - 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.configsvr.mongodbExtraFlags }}
            - name: MONGODB_EXTRA_FLAGS
              value: {{ .Values.configsvr.mongodbExtraFlags | join " " | quote }}
            {{- end }}
            {{- if .Values.common.extraEnvVars }}
              {{- include "common.tplvalues.render" ( dict "value" .Values.common.extraEnvVars "context" $ ) | nindent 12 }}
            {{- end }}
            {{- if .Values.configsvr.extraEnvVars }}
              {{- include "common.tplvalues.render" ( dict "value" .Values.configsvr.extraEnvVars "context" $ ) | nindent 12 }}
            {{- end }}
          {{- if or .Values.common.extraEnvVarsCM .Values.common.extraEnvVarsSecret .Values.configsvr.extraEnvVarsCM .Values.configsvr.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.configsvr.extraEnvVarsCM "context" $ ) }}
            {{- end }}
            {{- if .Values.configsvr.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" ( dict "value" .Values.configsvr.extraEnvVarsSecret "context" $ ) }}
            {{- 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:
            - /bin/bash
            - /entrypoint/replicaset-entrypoint.sh
          {{- end }}
          {{- if not .Values.diagnosticMode.enabled }}
          {{- if .Values.livenessProbe.enabled }}
          livenessProbe:
            exec:
              command:
                - pgrep
                - mongod
            initialDelaySeconds: {{ .Values.livenessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.livenessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.livenessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.livenessProbe.successThreshold }}
            failureThreshold: {{ .Values.livenessProbe.failureThreshold }}
          {{- end }}
          {{- if .Values.readinessProbe.enabled }}
          readinessProbe:
            exec:
              command:
                - mongo
                - --disableImplicitSessions
                - --eval
                - "db.adminCommand('ping')"
            initialDelaySeconds: {{ .Values.readinessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.readinessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.readinessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.readinessProbe.successThreshold }}
            failureThreshold: {{ .Values.readinessProbe.failureThreshold }}
          {{- end }}
          {{- end }}
          volumeMounts:
            - name: replicaset-entrypoint-configmap
              mountPath: /entrypoint
            - name: datadir
              mountPath: {{ .Values.configsvr.persistence.mountPath }}
            {{- if or .Values.configsvr.config .Values.configsvr.configCM }}
            - name: config
              mountPath: /bitnami/mongodb/conf/
            {{- end }}
            {{- if .Values.usePasswordFile }}
            - name: secrets
              mountPath: /bitnami/mongodb/secrets/
            {{- end }}
            {{- if .Values.common.initScriptsCM }}
            - name: custom-init-scripts-cm
              mountPath: /docker-entrypoint-initdb.d/cm
            {{- end }}
            {{- if .Values.common.initScriptsSecret }}
            - name: custom-init-scripts-secret
              mountPath: /docker-entrypoint-initdb.d/secret
            {{- end }}
            {{- if .Values.common.extraVolumeMounts }}
              {{- include "common.tplvalues.render" ( dict "value" .Values.common.extraVolumeMounts "context" $ ) | nindent 12 }}
            {{- end }}
            {{- if .Values.configsvr.extraVolumeMounts }}
              {{- include "common.tplvalues.render" ( dict "value" .Values.configsvr.extraVolumeMounts "context" $ ) | nindent 12 }}
            {{- end }}
          resources: {{- toYaml .Values.configsvr.resources | nindent 12 }}
        {{- if .Values.metrics.enabled }}
        - name: metrics
          image: {{ include "mongodb-sharded.metrics.image" . }}
          imagePullPolicy: {{ .Values.metrics.image.pullPolicy | quote }}
          {{- if .Values.securityContext.enabled }}
          securityContext:
            runAsNonRoot: {{ .Values.securityContext.runAsNonRoot }}
            runAsUser: {{ .Values.securityContext.runAsUser }}
          {{- end }}
          env:
          {{- if .Values.usePasswordFile }}
            - 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 }}
          {{- 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.usePasswordFile }}
              export MONGODB_ROOT_PASSWORD="$(cat "${MONGODB_ROOT_PASSWORD_FILE}")"
              {{- end }}
              /bin/mongodb_exporter --web.listen-address ":{{ .Values.metrics.containerPort }}" --mongodb.uri mongodb://root:`echo $MONGODB_ROOT_PASSWORD | sed -r "s/@/%40/g;s/:/%3A/g"`@localhost:{{ .Values.service.port }}/admin{{ ternary "?ssl=true" "" $.Values.metrics.useTLS }} {{ .Values.metrics.extraArgs }}
          {{- end }}
          {{- if .Values.usePasswordFile }}
          volumeMounts:
            - name: secrets
              mountPath: /bitnami/mongodb/secrets/
          {{- end }}
          ports:
            - name: metrics
              containerPort: {{ .Values.metrics.containerPort }}
          {{- if not .Values.diagnosticMode.enabled }}
          {{- if .Values.metrics.livenessProbe.enabled }}
          livenessProbe:
            httpGet:
              path: /metrics
              port: metrics
            initialDelaySeconds: {{ .Values.metrics.livenessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.metrics.livenessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.metrics.livenessProbe.timeoutSeconds }}
            failureThreshold: {{ .Values.metrics.livenessProbe.failureThreshold }}
            successThreshold: {{ .Values.metrics.livenessProbe.successThreshold }}
          {{- end }}
          {{- if .Values.metrics.readinessProbe.enabled }}
          readinessProbe:
            httpGet:
              path: /metrics
              port: metrics
            initialDelaySeconds: {{ .Values.metrics.readinessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.metrics.readinessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.metrics.readinessProbe.timeoutSeconds }}
            failureThreshold: {{ .Values.metrics.readinessProbe.failureThreshold }}
            successThreshold: {{ .Values.metrics.readinessProbe.successThreshold }}
          {{- end }}
          {{- end }}
          resources: {{ toYaml .Values.metrics.resources | nindent 12 }}
        {{- end }}
        {{- with .Values.configsvr.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: replicaset-entrypoint-configmap
          configMap:
            name: {{ include "common.names.fullname" . }}-replicaset-entrypoint
        {{- if .Values.usePasswordFile }}
        - name: secrets
          secret:
            secretName: {{ include "mongodb-sharded.secret" . }}
        {{- end }}
        {{- if or .Values.configsvr.config .Values.configsvr.configCM }}
        - name: config
          configMap:
            name: {{ include "mongodb-sharded.configsvr.configCM" . }}
        {{- end }}
        {{- if .Values.common.initScriptsCM }}
        - name: custom-init-scripts-cm
          configMap:
            name: {{ include "mongodb-sharded.initScriptsCM" . }}
            defaultMode: 0755
        {{- end }}
        {{- if .Values.common.initScriptsSecret }}
        - name: custom-init-scripts-secret
          secret:
            secretName: {{ include "mongodb-sharded.initScriptsSecret" . }}
            defaultMode: 0755
        {{- end }}
        {{- if .Values.common.extraVolumes }}
          {{- include "common.tplvalues.render" ( dict "value" .Values.common.extraVolumes "context" $ ) | nindent 8 }}
        {{- end }}
        {{- if .Values.configsvr.extraVolumes }}
          {{- include "common.tplvalues.render" ( dict "value" .Values.configsvr.extraVolumes "context" $ ) | nindent 8 }}
        {{- end }}
  {{- if .Values.configsvr.persistence.enabled }}
  volumeClaimTemplates:
    - metadata:
        name: datadir
        annotations:
        {{- range $key, $value := .Values.configsvr.persistence.annotations }}
          {{ $key }}: "{{ $value }}"
        {{- end }}
      spec:
        accessModes:
        {{- range .Values.configsvr.persistence.accessModes }}
          - {{ . | quote }}
        {{- end }}
        resources:
          requests:
            storage: {{ .Values.configsvr.persistence.size | quote }}
        {{- include "common.storage.class" (dict "persistence" .Values.configsvr.persistence "global" .Values.global) | nindent 8 }}
  {{- else }}
        - name: datadir
          emptyDir: {}
  {{- end }}
{{- end }}
