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

{{- if .Values.shards }}
{{- $replicas := .Values.shards | int }}
{{- range $i, $e := until $replicas }}
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ printf "%s-shard%d-data" (include "common.names.fullname" $ ) $i }}
  namespace: {{ include "common.names.namespace" $ | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" $.Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: shardsvr
  {{- if $.Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" $.Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list $.Values.shardsvr.dataNode.podLabels $.Values.common.podLabels $.Values.common.podLabels ) "context" $ ) }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 6 }}
      app.kubernetes.io/component: shardsvr
  podManagementPolicy: {{ $.Values.shardsvr.dataNode.podManagementPolicy }}
  updateStrategy: {{- toYaml $.Values.shardsvr.dataNode.updateStrategy | nindent 4 }}
  serviceName: {{ printf "%s-headless" (include "common.names.fullname" $) }}
  replicas: {{ $.Values.shardsvr.dataNode.replicaCount }}
  template:
    metadata:
      labels: {{- include "common.labels.standard" ( dict "customLabels" $podLabels "context" $ ) | nindent 8 }}
        app.kubernetes.io/component: shardsvr
        shard: {{ $i | quote }}
      {{- if or $.Values.common.podAnnotations $.Values.shardsvr.dataNode.podAnnotations $.Values.metrics.enabled }}
      annotations:
        {{- if or $.Values.common.podAnnotations $.Values.shardsvr.dataNode.podAnnotations }}
        {{- $annotations := include "common.tplvalues.merge" ( dict "values" ( list $.Values.shardsvr.dataNode.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.shardsvr.dataNode.schedulerName }}
      schedulerName: {{ $.Values.shardsvr.dataNode.schedulerName | quote }}
      {{- end }}
      {{- if $.Values.shardsvr.dataNode.affinity }}
      affinity: {{- include "common.tplvalues.render" (dict "value" $.Values.shardsvr.dataNode.affinity "context" (set $ "dataNodeLoopId" $i)) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" $.Values.shardsvr.dataNode.podAffinityPreset "component" "shardsvr" "customLabels" $podLabels "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" $.Values.shardsvr.dataNode.podAntiAffinityPreset "component" "shardsvr" "customLabels" $podLabels "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" $.Values.shardsvr.dataNode.nodeAffinityPreset.type "key" $.Values.shardsvr.dataNode.nodeAffinityPreset.key "values" $.Values.shardsvr.dataNode.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      automountServiceAccountToken: {{ $.Values.shardsvr.dataNode.automountServiceAccountToken }}
      {{- if $.Values.shardsvr.dataNode.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" $.Values.shardsvr.dataNode.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      {{- if $.Values.shardsvr.dataNode.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" (dict "value" $.Values.shardsvr.dataNode.nodeSelector "context" (set $ "dataNodeLoopId" $i)) | nindent 8 }}
      {{- end }}
      {{- if $.Values.shardsvr.dataNode.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" $.Values.shardsvr.dataNode.tolerations "context" (set $ "dataNodeLoopId" $i)) | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ include "mongodb-sharded.serviceAccountName" (dict "component" "shardsvr" "value" $.Values.shardsvr.dataNode.serviceAccount "context" $) }}
      {{- if $.Values.shardsvr.dataNode.priorityClassName }}
      priorityClassName: {{ $.Values.shardsvr.dataNode.priorityClassName | quote }}
      {{- end }}
      {{- if $.Values.shardsvr.dataNode.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" $.Values.shardsvr.dataNode.topologySpreadConstraints "context" $) | nindent 8 }}
      {{- end }}
      {{- if $.Values.shardsvr.dataNode.podSecurityContext.enabled }}
      securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" $.Values.shardsvr.dataNode.podSecurityContext "context" $) | nindent 8 }}
      {{- end }}
      {{- if $.Values.shardsvr.dataNode.terminationGracePeriodSeconds }}
      terminationGracePeriodSeconds: {{ $.Values.shardsvr.dataNode.terminationGracePeriodSeconds }}
      {{- end }}
      {{- include "mongodb-sharded.imagePullSecrets" $ | nindent 6 }}
      initContainers:
        {{- if and $.Values.volumePermissions.enabled $.Values.shardsvr.persistence.enabled }}
        - name: volume-permissions
          image: {{ include "mongodb-sharded.volumePermissions.image" $ }}
          imagePullPolicy: {{ $.Values.volumePermissions.image.pullPolicy | quote }}
          command:
            - /bin/bash
          args:
            - -ec
            - |
              mkdir -p {{ $.Values.shardsvr.persistence.mountPath }}{{- if $.Values.shardsvr.persistence.subPath }}/{{ include "common.tplvalues.render" (dict "value" $.Values.shardsvr.persistence.subPath "context" $) }}{{- end }}
              chown {{ $.Values.shardsvr.dataNode.containerSecurityContext.runAsUser }}:{{ $.Values.shardsvr.dataNode.podSecurityContext.fsGroup }} {{ $.Values.shardsvr.persistence.mountPath }}{{- if $.Values.shardsvr.persistence.subPath }}/{{ include "common.tplvalues.render" (dict "value" $.Values.shardsvr.persistence.subPath "context" $) }}{{- end }}
              find {{ $.Values.shardsvr.persistence.mountPath }} -mindepth 1 -maxdepth 1 -not -name ".snapshot" -not -name "lost+found" | xargs -r chown -R {{ $.Values.shardsvr.dataNode.containerSecurityContext.runAsUser }}:{{ $.Values.shardsvr.dataNode.podSecurityContext.fsGroup }}
          securityContext:
            runAsUser: 0
          {{- if $.Values.volumePermissions.resources }}
          resources: {{ toYaml $.Values.volumePermissions.resources | nindent 12 }}
          {{- else if ne $.Values.volumePermissions.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" $.Values.volumePermissions.resourcesPreset) | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: empty-dir
              mountPath: /tmp
              subPath: tmp-dir
            - name: datadir
              mountPath: {{ $.Values.shardsvr.persistence.mountPath }}
              {{- if $.Values.shardsvr.persistence.subPath }}
              subPath: {{ include "common.tplvalues.render" (dict "value" $.Values.shardsvr.persistence.subPath "context" $) }}
              {{- end }}
        {{- end }}
        {{- with $.Values.shardsvr.dataNode.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.shardsvr.dataNode.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" $.Values.shardsvr.dataNode.containerSecurityContext "context" $) | nindent 12 }}
          {{- end }}
          ports:
            - containerPort: {{ $.Values.common.containerPorts.mongodb }}
              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_INIT_RETRY_ATTEMPTS
              value: {{ $.Values.common.mongodbInitRetryAttempts | quote }}
            - name: MONGODB_INIT_RETRY_DELAY
              value: {{ $.Values.common.mongodbInitRetryDelay | quote }}
            - name: MONGODB_DISABLE_SYSTEM_LOG
              {{- if $.Values.common.mongodbDisableSystemLog }}
              value: "yes"
              {{- else }}
              value: "no"
              {{- end }}
            - name: MONGODB_PORT_NUMBER
              value: {{ $.Values.common.containerPorts.mongodb | quote }}
            - name: MONGODB_SHARDING_MODE
              value: "shardsvr"
            - name: MONGODB_POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: MONGODB_MONGOS_HOST
              value: {{ include "mongodb-sharded.serviceName" $ }}
            - name: MONGODB_MONGOS_PORT_NUMBER
              value: {{ $.Values.service.ports.mongodb | quote }}
            - name: MONGODB_INITIAL_PRIMARY_HOST
              value: {{ printf "%s-shard%d-data-0.%s-headless.%s.svc.%s" (include "common.names.fullname" $ ) $i (include "common.names.fullname" $ ) $.Release.Namespace $.Values.clusterDomain }}
            - name: MONGODB_REPLICA_SET_NAME
              value: {{ printf "%s-shard-%d" ( include "common.names.fullname" $ ) $i }}
              {{- if $.Values.common.useHostnames }}
            - name: MONGODB_ADVERTISED_HOSTNAME
              value: {{ printf "$(MONGODB_POD_NAME).%s-headless.%s.svc.%s" (include "common.names.fullname" $) (include "common.names.namespace" $) $.Values.clusterDomain }}
              {{- end }}
            {{- 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_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.shardsvr.dataNode.mongodbExtraFlags }}
            - name: MONGODB_EXTRA_FLAGS
              value: {{ $.Values.shardsvr.dataNode.mongodbExtraFlags | join " " | quote }}
            {{- end }}
            {{- if $.Values.common.extraEnvVars }}
              {{- include "common.tplvalues.render" ( dict "value" $.Values.common.extraEnvVars "context" $ ) | nindent 12 }}
            {{- end }}
            {{- if $.Values.shardsvr.dataNode.extraEnvVars }}
              {{- include "common.tplvalues.render" ( dict "value" $.Values.shardsvr.dataNode.extraEnvVars "context" $ ) | nindent 12 }}
            {{- end }}
          {{- if or $.Values.common.extraEnvVarsCM $.Values.common.extraEnvVarsSecret $.Values.shardsvr.dataNode.extraEnvVarsCM $.Values.shardsvr.dataNode.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.shardsvr.dataNode.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" ( dict "value" $.Values.shardsvr.dataNode.extraEnvVarsCM "context" $ ) }}
            {{- end }}
            {{- if $.Values.shardsvr.dataNode.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" ( dict "value" $.Values.shardsvr.dataNode.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 }}
          {{- if $.Values.shardsvr.dataNode.command }}
          command: {{- include "common.tplvalues.render" (dict "value" $.Values.shardsvr.dataNode.command "context" $) | nindent 12 }}
          {{- end }}
          {{- if $.Values.shardsvr.dataNode.args }}
          args: {{- include "common.tplvalues.render" (dict "value" $.Values.shardsvr.dataNode.args "context" $) | nindent 12 }}
          {{- end }}
          {{- if $.Values.shardsvr.dataNode.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" $.Values.shardsvr.dataNode.customLivenessProbe "context" $) | nindent 12 }}
          {{- else if $.Values.shardsvr.dataNode.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit $.Values.shardsvr.dataNode.livenessProbe "enabled") "context" $) | nindent 12 }}
            exec:
              command:
                - pgrep
                - mongod
          {{- end }}
          {{- if $.Values.shardsvr.dataNode.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" $.Values.shardsvr.dataNode.customReadinessProbe "context" $) | nindent 12 }}
          {{- else if $.Values.shardsvr.dataNode.readinessProbe.enabled }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" (omit $.Values.shardsvr.dataNode.readinessProbe "enabled") "context" $) | nindent 12 }}
            exec:
              command:
                - /bin/sh
                - -c
                - mongosh --port $MONGODB_PORT_NUMBER --eval "db.adminCommand('ping')"
          {{- end }}
          {{- if $.Values.shardsvr.dataNode.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" $.Values.shardsvr.dataNode.customStartupProbe "context" $) | nindent 12 }}
          {{- else if $.Values.shardsvr.dataNode.startupProbe.enabled }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" (omit $.Values.shardsvr.dataNode.startupProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: mongodb
          {{- end }}
          {{- end }}
          {{- if $.Values.shardsvr.dataNode.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" $.Values.shardsvr.dataNode.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
            - name: replicaset-entrypoint-configmap
              mountPath: /entrypoint
            - name: datadir
              mountPath: {{ $.Values.shardsvr.persistence.mountPath }}
              {{- if $.Values.shardsvr.persistence.subPath }}
              subPath: {{ include "common.tplvalues.render" (dict "value" $.Values.shardsvr.persistence.subPath "context" $) }}
              {{- end }}
            {{- if or $.Values.shardsvr.dataNode.config $.Values.shardsvr.dataNode.configCM }}
            - name: config
              mountPath: /bitnami/mongodb/conf/
            {{- end }}
            {{- if $.Values.auth.usePasswordFiles }}
            - 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.shardsvr.dataNode.extraVolumeMounts }}
              {{- include "common.tplvalues.render" ( dict "value" $.Values.shardsvr.dataNode.extraVolumeMounts "context" $ ) | nindent 12 }}
            {{- end }}
          {{- if $.Values.shardsvr.dataNode.resources }}
          resources: {{- toYaml $.Values.shardsvr.dataNode.resources | nindent 12 }}
          {{- else if ne $.Values.shardsvr.dataNode.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" $.Values.shardsvr.dataNode.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.shardsvr.dataNode.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: {}
        - name: replicaset-entrypoint-configmap
          configMap:
            name: {{ printf "%s-replicaset-entrypoint" (include "common.names.fullname" $) }}
        {{- if $.Values.auth.usePasswordFiles }}
        - name: secrets
          secret:
            secretName: {{ include "mongodb-sharded.secret" $ }}
        {{- 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 or $.Values.shardsvr.dataNode.config $.Values.shardsvr.dataNode.configCM }}
        - name: config
          configMap:
            name: {{ include "mongodb-sharded.shardsvr.dataNode.configCM" $ }}
        {{- end }}
        {{- if $.Values.common.extraVolumes }}
          {{- include "common.tplvalues.render" ( dict "value" $.Values.common.extraVolumes "context" $ ) | nindent 8 }}
        {{- end }}
        {{- if $.Values.shardsvr.dataNode.extraVolumes }}
          {{- include "common.tplvalues.render" ( dict "value" $.Values.shardsvr.dataNode.extraVolumes "context" $ ) | nindent 8 }}
        {{- end }}
  {{- if not $.Values.shardsvr.persistence.enabled }}
        - name: datadir
          emptyDir: {}
  {{- else }}
  {{- if $.Values.shardsvr.persistentVolumeClaimRetentionPolicy.enabled }}
  persistentVolumeClaimRetentionPolicy:
    whenDeleted: {{ $.Values.shardsvr.persistentVolumeClaimRetentionPolicy.whenDeleted }}
    whenScaled: {{ $.Values.shardsvr.persistentVolumeClaimRetentionPolicy.whenScaled }}
  {{- end }}
  volumeClaimTemplates:
    - apiVersion: v1
      kind: PersistentVolumeClaim
      metadata:
        name: datadir
        {{- if or $.Values.shardsvr.persistence.annotations $.Values.commonAnnotations $.Values.shardsvr.persistence.resourcePolicy }}
        {{- if or $.Values.commonAnnotations $.Values.shardsvr.persistence.annotations $.Values.shardsvr.persistence.resourcePolicy }}
        annotations:
          {{- if $.Values.commonAnnotations }}
          {{- include "common.tplvalues.render" ( dict "value" $.Values.commonAnnotations "context" $ ) | nindent 10 }}
          {{- end }}
          {{- if $.Values.shardsvr.persistence.annotations }}
          {{- include "common.tplvalues.render" ( dict "value" $.Values.shardsvr.persistence.annotations "context" $) | nindent 10 }}
          {{- end }}
        {{- end }}
        {{- end }}
      spec:
        accessModes:
        {{- range $.Values.shardsvr.persistence.accessModes }}
          - {{ . | quote }}
        {{- end }}
        resources:
          requests:
            storage: {{ $.Values.shardsvr.persistence.size | quote }}
        {{- with $.Values.shardsvr.persistence.selector }}
        selector:
          {{- include "common.tplvalues.render" (dict "value" $.Values.shardsvr.persistence.selector "context" (merge $ (dict "Index" $i))) | nindent 10 }}
        {{- end }}
        {{- include "common.storage.class" (dict "persistence" $.Values.shardsvr.persistence "global" $.Values.global) | nindent 8 }}
  {{- end }}
{{- if lt $i (sub $replicas 1) }}
---
{{- end }}
{{- end }}
{{- end }}
