apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: {{ template "mongodb.fullname" . }}
  labels:
    app: {{ template "mongodb.name" . }}
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    release: "{{ .Release.Name }}"
    heritage: "{{ .Release.Service }}"
spec:
  template:
    metadata:
      labels:
        app: {{ template "mongodb.name" . }}
    spec:
      {{- 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.fullname" . }}
        image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
        imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
        env:
        {{ if and .Values.auth.enabled .Values.auth.rootPassword }}
        - name: MONGO_INITDB_ROOT_USERNAME
          valueFrom:
            secretKeyRef:
              name: {{ template "mongodb.fullname" . }}
              key: mongodb-root-username
        - name: MONGO_INITDB_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: {{ template "mongodb.fullname" . }}
              key: mongodb-root-password
        {{ end }}
        - name: MONGODB_EXTRA_FLAGS
          value: {{ default "" .Values.mongodbExtraFlags | join " " }}
        ports:
        - name: mongodb
          containerPort: 27017
        livenessProbe:
          exec:
            command:
            - mongo
            - --eval
            - "db.adminCommand('ping')"
{{ toYaml .Values.livenessProbe | indent 10 }}
        readinessProbe:
          exec:
            command:
            - mongo
            - --eval
            - "db.adminCommand('ping')"
{{ toYaml .Values.readinessProbe | indent 10 }}
        volumeMounts:
        - name: data
          mountPath: {{ .Values.persistence.mountPath }}
        - name: config
          mountPath: /etc/mongo/mongod.conf
        resources:
{{ toYaml .Values.resources | indent 10 }}
      volumes:
      - name: config
        configMap:
          name: {{ template "mongodb.fullname" . }}
      - name: data
      {{- if .Values.persistence.enabled }}
        persistentVolumeClaim:
          claimName: {{ if .Values.persistence.existingClaim }}{{ .Values.persistence.existingClaim }}{{- else }}{{ template "mongodb.fullname" . }}{{- end }}

      {{- else }}
        emptyDir: {}
      {{- end -}}
