{{- if .Values.replicaSet.enabled }}
apiVersion: apps/v1
kind: StatefulSet
metadata:
  labels:
    app: {{ template "mongodb.name" . }}
    chart: {{ template "mongodb.chart" . }}
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
  name: {{ template "mongodb.fullname" . }}-secondary
spec:
  selector:
    matchLabels:
      app: {{ template "mongodb.name" . }}
      release: {{ .Release.Name }}
      chart: {{ template "mongodb.chart" . }}
      component: secondary
  podManagementPolicy: "Parallel"
  serviceName: {{ template "mongodb.fullname" . }}-headless
  replicas: {{ .Values.replicaSet.replicas.secondary }}
  template:
    metadata:
      labels:
        app: {{ template "mongodb.name" . }}
        chart: {{ template "mongodb.chart" . }}
        release: {{ .Release.Name }}
        component: secondary
    spec:
      {{- if .Values.securityContext.enabled }}
      securityContext:
        fsGroup: {{ .Values.securityContext.fsGroup }}
        runAsUser: {{ .Values.securityContext.runAsUser }}
      {{- end }}
      {{- if .Values.nodeSelector }}
      nodeSelector:
{{ toYaml .Values.nodeSelector | indent 8 }}
      {{- end }}
      {{- if .Values.image.pullSecrets }}
      imagePullSecrets:
      {{- range .Values.image.pullSecrets }}
        - name: {{ . }}
      {{- end}}
      {{- end }}
      containers:
        - name: {{ template "mongodb.name" . }}-secondary
          image: {{ template "mongodb.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          ports:
          - containerPort: {{ .Values.service.port }}
            name: mongodb
          env:
          - name: MONGODB_POD_NAME
            valueFrom:
              fieldRef:
                fieldPath: metadata.name
          - name: MONGODB_REPLICA_SET_MODE
            value: "secondary"
          - name: MONGODB_PRIMARY_HOST
            value: {{ template "mongodb.fullname" . }}
          - name: MONGODB_REPLICA_SET_NAME
            value: {{ .Values.replicaSet.name | quote }}
            {{- if .Values.replicaSet.useHostnames }}
          - name: MONGODB_ADVERTISED_HOSTNAME
            value: "$(MONGODB_POD_NAME).{{ template "mongodb.fullname" . }}-headless.{{ .Release.Namespace }}"
            {{- end }}
            {{- if .Values.usePassword }}
          - name: MONGODB_PRIMARY_ROOT_PASSWORD
            valueFrom:
              secretKeyRef:
                name: {{ if .Values.existingSecret }}{{ .Values.existingSecret }}{{- else }}{{ template "mongodb.fullname" . }}{{- end }}
                key: mongodb-root-password
          - name: MONGODB_REPLICA_SET_KEY
            valueFrom:
              secretKeyRef:
                name: {{ if .Values.existingSecret }}{{ .Values.existingSecret }}{{- else }}{{ template "mongodb.fullname" . }}{{- end }}
                key: mongodb-replica-set-key
            {{- end }}
          - name: MONGODB_ENABLE_IPV6
          {{- if .Values.mongodbEnableIPv6 }}
            value: "yes"
          {{- else }}
            value: "no"
          {{- end }}
          - name: MONGODB_EXTRA_FLAGS
            value: {{ default "" .Values.mongodbExtraFlags | join " " }}
          {{- if .Values.livenessProbe.enabled }}
          livenessProbe:
            exec:
              command:
                - mongo
                - --eval
                - "db.adminCommand('ping')"
            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
                - --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 }}
          volumeMounts:
            - name: datadir
              mountPath: /bitnami/mongodb
            {{- if .Values.configmap }}
            - name: config
              mountPath: /opt/bitnami/mongodb/conf/mongodb.conf
              subPath: mongodb.conf
            {{- end }}
      volumes:
        {{- if .Values.configmap }}
        - name: config
          configMap:
            name: {{ template "mongodb.fullname" . }}
        {{- end }}
{{- if .Values.persistence.enabled }}
  volumeClaimTemplates:
    - metadata:
        name: datadir
        annotations:
        {{- range $key, $value := .Values.persistence.annotations }}
          {{ $key }}: {{ $value }}
        {{- end }}
      spec:
        accessModes:
        {{- range .Values.persistence.accessModes }}
          - {{ . | quote }}
        {{- end }}
        resources:
          requests:
            storage: {{ .Values.persistence.size | quote }}
      {{- if .Values.persistence.storageClass }}
      {{- if (eq "-" .Values.persistence.storageClass) }}
        storageClassName: ""
      {{- else }}
        storageClassName: "{{ .Values.persistence.storageClass }}"
      {{- end }}
      {{- end }}
{{- else }}
        - name: datadir
          emptyDir: {}
{{- end }}
{{- end }}
