{{- if .Values.enabled }}
{{- $namespace := .Release.Namespace -}}
{{- $applicationName := .Values.appName -}}
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ $applicationName }}
  namespace: {{ $namespace }}
  labels:
    {{- include "uavcloud-middleware.mongo.labels.standard" . | nindent 4 }}
spec:
  serviceName: {{ $applicationName }}
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      {{- include "uavcloud-middleware.mongo.labels.matchLabels" . | nindent 6 }}
  template:
    metadata:
      labels:
        {{- include "uavcloud-middleware.mongo.labels.standard" . | nindent 8 }}
      annotations:
        pod.alpha.kubernetes.io/initialized: "true"
    spec:
      {{- if .Values.global.affinity }}
      affinity: {{- include "common.tplvalues.render" (dict "value" .Values.global.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
{{- /*        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.global.podAffinityPreset "context" $) | nindent 10 -}}*/}}
{{- /*        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.global.podAntiAffinityPreset "context" $) | nindent 10 }}*/}}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.global.nodeAffinityPreset.type "key" .Values.global.nodeAffinityPreset.key "values" .Values.global.nodeAffinityPreset.values) | nindent 10 -}}
      {{- end }}
      containers:
        - name: {{ $applicationName }}
          image: "{{ .Values.global.image.repository | default .Values.image.repository }}/mongo:{{ .Values.image.tag }}"
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
          ports:
            - containerPort: {{ .Values.containerPort.mongo }}
              name: mongo27017
              protocol: TCP
          env:
            - name: MONGO_INITDB_ROOT_USERNAME
              value: {{ .Values.auth.username }}
            - name: MONGO_INITDB_ROOT_PASSWORD
              value: {{ .Values.auth.password }}
          volumeMounts:
            - name: mongo-data
              mountPath: /data/db
              readOnly: false
              subPath: {{ $namespace }}/{{ $applicationName }}/data/db
      volumes:
        - name: mongo-data
          persistentVolumeClaim:
            claimName: helm-mongo
---
{{- end }}
