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

{{- if and .Values.shards .Values.shardsvr.arbiter.replicaCount }}
{{- $replicas := $.Values.shards | int }}
{{- range $i, $e := until $replicas }}
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ printf "%s-shard%d-arbiter" (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-arbiter
  {{- 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.arbiter.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-arbiter
  podManagementPolicy: {{ $.Values.shardsvr.arbiter.podManagementPolicy }}
  updateStrategy: {{- toYaml $.Values.shardsvr.arbiter.updateStrategy | nindent 4 }}
  serviceName: {{ printf "%s-headless" (include "common.names.fullname" $) }}
  replicas: {{ $.Values.shardsvr.arbiter.replicaCount }}
  template:
    metadata:
      labels: {{- include "common.labels.standard" ( dict "customLabels" $podLabels "context" $ ) | nindent 8 }}
        app.kubernetes.io/component: shardsvr-arbiter
        shard: {{ $i | quote }}
      {{- if or $.Values.common.podAnnotations $.Values.shardsvr.arbiter.podAnnotations $.Values.metrics.enabled }}
      annotations:
        {{- if or $.Values.common.podAnnotations $.Values.shardsvr.arbiter.podAnnotations }}
        {{- $annotations := include "common.tplvalues.merge" ( dict "values" ( list $.Values.shardsvr.arbiter.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:
      serviceAccountName: {{ include "mongodb-sharded.serviceAccountName" (dict "component" "shardsvr-arbiter" "value" $.Values.shardsvr.arbiter.serviceAccount "context" $) }}
      {{- if $.Values.shardsvr.arbiter.schedulerName }}
      schedulerName: {{ $.Values.shardsvr.arbiter.schedulerName | quote }}
      {{- end }}
      automountServiceAccountToken: {{ $.Values.shardsvr.arbiter.automountServiceAccountToken }}
      {{- if $.Values.shardsvr.arbiter.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" $.Values.shardsvr.arbiter.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      {{- if $.Values.shardsvr.arbiter.affinity }}
      affinity: {{- include "common.tplvalues.render" (dict "value" $.Values.shardsvr.arbiter.affinity "context" (set $ "arbiterLoopId" $i)) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" $.Values.shardsvr.arbiter.podAffinityPreset "component" "shardsvr-arbiter" "customLabels" $podLabels "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" $.Values.shardsvr.arbiter.podAntiAffinityPreset "component" "shardsvr-arbiter" "customLabels" $podLabels "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" $.Values.shardsvr.arbiter.nodeAffinityPreset.type "key" $.Values.shardsvr.arbiter.nodeAffinityPreset.key "values" $.Values.shardsvr.arbiter.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if $.Values.shardsvr.arbiter.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" (dict "value" $.Values.shardsvr.arbiter.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if $.Values.shardsvr.arbiter.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" $.Values.shardsvr.arbiter.tolerations "context" $) | nindent 8 }}
      {{- end }}
      {{- if $.Values.shardsvr.arbiter.priorityClassName }}
      priorityClassName: {{ $.Values.shardsvr.arbiter.priorityClassName | quote }}
      {{- end }}
      {{- if $.Values.shardsvr.arbiter.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" $.Values.shardsvr.arbiter.topologySpreadConstraints "context" $) | nindent 8 }}
      {{- end }}
      {{- if $.Values.shardsvr.arbiter.podSecurityContext.enabled }}
      securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" $.Values.shardsvr.arbiter.podSecurityContext "context" $) | nindent 8 }}
      {{- end }}
      {{- if $.Values.shardsvr.arbiter.terminationGracePeriodSeconds }}
      terminationGracePeriodSeconds: {{ $.Values.shardsvr.arbiter.terminationGracePeriodSeconds }}
      {{- end }}
      {{- include "mongodb-sharded.imagePullSecrets" $ | nindent 6 }}
      {{- if or $.Values.shardsvr.arbiter.initContainers $.Values.common.initContainers }}
      initContainers:
        {{- with $.Values.shardsvr.arbiter.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: {{ printf "%s-arbiter" (include "common.names.fullname" $) }}
          image: {{ include "mongodb-sharded.image" $ }}
          imagePullPolicy: {{ $.Values.image.pullPolicy }}
          {{- if $.Values.shardsvr.arbiter.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" $.Values.shardsvr.arbiter.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_DISABLE_SYSTEM_LOG
              {{- if $.Values.common.mongodbDisableSystemLog }}
              value: "yes"
              {{- else }}
              value: "no"
              {{- end }}
            - name: MONGODB_POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: MONGODB_INIT_RETRY_ATTEMPTS
              value: {{ $.Values.common.mongodbInitRetryAttempts | quote }}
            - name: MONGODB_INIT_RETRY_DELAY
              value: {{ $.Values.common.mongodbInitRetryDelay | quote }}
            - name: MONGODB_SHARDING_MODE
              value: "shardsvr"
            - name: MONGODB_REPLICA_SET_MODE
              value: "arbiter"
            - name: MONGODB_PORT_NUMBER
              value: {{ $.Values.common.containerPorts.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 }}
            - 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.auth.enabled }}
            - name: MONGODB_INITIAL_PRIMARY_ROOT_USER
              value: {{ $.Values.auth.rootUser | quote }}
            {{- if $.Values.auth.usePasswordFiles }}
            - name: MONGODB_INITIAL_PRIMARY_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_INITIAL_PRIMARY_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 }}
            {{- if $.Values.shardsvr.arbiter.mongodbExtraFlags }}
            - name: MONGODB_EXTRA_FLAGS
              value: {{ $.Values.shardsvr.arbiter.mongodbExtraFlags | join " " | quote }}
            {{- end }}
            {{- if $.Values.common.extraEnvVars }}
              {{- include "common.tplvalues.render" ( dict "value" $.Values.common.extraEnvVars "context" $ ) | nindent 12 }}
            {{- end }}
            {{- if $.Values.shardsvr.arbiter.extraEnvVars }}
              {{- include "common.tplvalues.render" ( dict "value" $.Values.shardsvr.arbiter.extraEnvVars "context" $ ) | nindent 12 }}
            {{- end }}
          {{- if or $.Values.common.extraEnvVarsCM $.Values.common.extraEnvVarsSecret $.Values.shardsvr.arbiter.extraEnvVarsCM $.Values.shardsvr.arbiter.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.arbiter.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" ( dict "value" $.Values.shardsvr.arbiter.extraEnvVarsCM "context" $ ) }}
            {{- end }}
            {{- if $.Values.shardsvr.arbiter.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" ( dict "value" $.Values.shardsvr.arbiter.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.arbiter.command }}
          command: {{- include "common.tplvalues.render" (dict "value" $.Values.shardsvr.arbiter.command "context" $) | nindent 12 }}
          {{- end }}
          {{- if $.Values.shardsvr.arbiter.args }}
          args: {{- include "common.tplvalues.render" (dict "value" $.Values.shardsvr.arbiter.args "context" $) | nindent 12 }}
          {{- end }}
          {{- if $.Values.shardsvr.arbiter.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" $.Values.shardsvr.arbiter.customLivenessProbe "context" $) | nindent 12 }}
          {{- else if $.Values.shardsvr.arbiter.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit $.Values.shardsvr.arbiter.livenessProbe "enabled") "context" $) | nindent 12 }}
            exec:
              command:
                - pgrep
                - -f
                - mongodb
          {{- end }}
          {{- if $.Values.shardsvr.arbiter.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" $.Values.shardsvr.arbiter.customReadinessProbe "context" $) | nindent 12 }}
          {{- else if $.Values.shardsvr.arbiter.readinessProbe.enabled }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" (omit $.Values.shardsvr.arbiter.readinessProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: mongodb
          {{- end }}
          {{- if $.Values.shardsvr.arbiter.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" $.Values.shardsvr.arbiter.customStartupProbe "context" $) | nindent 12 }}
          {{- else if $.Values.shardsvr.arbiter.startupProbe.enabled }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" (omit $.Values.shardsvr.arbiter.startupProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: mongodb
          {{- end }}
          {{- end }}
          {{- if $.Values.shardsvr.arbiter.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" $.Values.shardsvr.arbiter.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 or $.Values.shardsvr.arbiter.config $.Values.shardsvr.arbiter.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.arbiter.extraVolumeMounts }}
              {{- include "common.tplvalues.render" ( dict "value" $.Values.shardsvr.arbiter.extraVolumeMounts "context" $ ) | nindent 12 }}
            {{- end }}
          {{- if $.Values.shardsvr.arbiter.resources }}
          resources: {{- toYaml $.Values.shardsvr.arbiter.resources | nindent 12 }}
          {{- else if ne $.Values.shardsvr.arbiter.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" $.Values.shardsvr.arbiter.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.arbiter.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 or $.Values.shardsvr.arbiter.config $.Values.shardsvr.arbiter.configCM }}
        - name: config
          configMap:
            name: {{ include "mongodb-sharded.shardsvr.arbiter.configCM" $ }}
        {{- end }}
        {{- 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 $.Values.common.extraVolumes }}
        {{- include "common.tplvalues.render" ( dict "value" $.Values.common.extraVolumes "context" $ ) | nindent 8 }}
      {{- end }}
      {{- if $.Values.shardsvr.arbiter.extraVolumes }}
        {{- include "common.tplvalues.render" ( dict "value" $.Values.shardsvr.arbiter.extraVolumes "context" $ ) | nindent 8 }}
      {{- end }}
{{- if lt $i (sub $replicas 1) }}
---
{{- end }}
{{- end }}
{{- end }}
