apiVersion: apps/v1beta2
kind: StatefulSet
metadata:
  name: {{ template "couchdb.fullname" . }}
  labels:
    app: {{ template "couchdb.name" . }}
    chart: {{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
spec:
  replicas: {{ .Values.clusterSize }}
  serviceName: {{ template "couchdb.fullname" . }}
  podManagementPolicy: {{ .Values.podManagementPolicy }}
  selector:
    matchLabels:
      app: {{ template "couchdb.name" . }}
      release: {{ .Release.Name }}
  template:
    metadata:
      labels:
        app: {{ template "couchdb.name" . }}
        release: {{ .Release.Name }}
    spec:
      {{- if .Values.schedulerName }}
      schedulerName: "{{ .Values.schedulerName }}"
      {{- end }}
      initContainers:
        - name: init-copy
          image: "{{ .Values.initImage.repository }}:{{ .Values.initImage.tag }}"
          imagePullPolicy: {{ .Values.initImage.pullPolicy }}
          command: ['sh','-c','cp /tmp/chart.ini /default.d; ls -lrt /default.d;']
          volumeMounts:
          - name: config
            mountPath: /tmp/
          - name: config-storage
            mountPath: /default.d
      containers:
        - name: couchdb
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          ports:
            - name: couchdb
              containerPort: 5984
            - name: epmd
              containerPort: 4369
            - containerPort: 9100
          env:
{{- if not .Values.allowAdminParty }}
            - name: COUCHDB_USER
              valueFrom:
                secretKeyRef:
                  name: {{ template "couchdb.fullname" . }}
                  key: adminUsername
            - name: COUCHDB_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ template "couchdb.fullname" . }}
                  key: adminPassword
            - name: COUCHDB_SECRET
              valueFrom:
                secretKeyRef:
                  name: {{ template "couchdb.fullname" . }}
                  key: cookieAuthSecret
{{- end }}
            - name: ERL_FLAGS
              value: "{{ range $k, $v := .Values.erlangFlags }} -{{ $k }} {{ $v }} {{ end }}"
          livenessProbe:
{{- if .Values.couchdbConfig.chttpd.require_valid_user }}
            exec:
              command:
                - sh
                - -c
                - curl -G --silent --fail -u ${COUCHDB_USER}:${COUCHDB_PASSWORD} http://localhost:5984/
{{- else }}
            httpGet:
              path: /
              port: 5984
{{- end }}
          readinessProbe:
{{- if .Values.couchdbConfig.chttpd.require_valid_user }}
            exec:
              command:
                - sh
                - -c
                - curl -G --silent --fail -u ${COUCHDB_USER}:${COUCHDB_PASSWORD} http://localhost:5984/_up
{{- else }}
            httpGet:
              path: /_up
              port: 5984
{{- end }}
          resources:
{{ toYaml .Values.resources | indent 12 }}
          volumeMounts:
          - name: config-storage
            mountPath: /opt/couchdb/etc/default.d
          - name: database-storage
            mountPath: /opt/couchdb/data
        - name: couchdb-statefulset-assembler
          image: "{{ .Values.helperImage.repository }}:{{ .Values.helperImage.tag }}"
          imagePullPolicy: {{ .Values.helperImage.pullPolicy }}
          env:
            - name: COUCHDB_CLUSTER_SIZE
              valueFrom:
                configMapKeyRef:
                  name: {{ template "couchdb.fullname" . }}
                  key: clusterSize
{{- if not .Values.allowAdminParty }}
            - name: COUCHDB_USER
              valueFrom:
                secretKeyRef:
                  name: {{ template "couchdb.fullname" . }}
                  key: adminUsername
            - name: COUCHDB_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ template "couchdb.fullname" . }}
                  key: adminPassword
{{- end }}
{{- if .Values.enableSearch }}
        - name: clouseau
          image: "{{ .Values.searchImage.repository }}:{{ .Values.searchImage.tag }}"
          imagePullPolicy: {{ .Values.searchImage.pullPolicy }}
{{- end }}
{{- if .Values.nodeSelector }}
      nodeSelector:
{{ toYaml .Values.nodeSelector | indent 8 }}
{{- end }}
{{- with .Values.affinity }}
      affinity:
{{ toYaml . | indent 8 }}
{{- end }}
      volumes:
        - name: config-storage
          emptyDir: {}
        - name: config
          configMap:
            name: {{ template "couchdb.fullname" . }}
            items:
              - key: inifile
                path: chart.ini
{{- if not .Values.persistentVolume.enabled }}
        - name: database-storage
          emptyDir: {}
{{- else }}
  volumeClaimTemplates:
    - metadata:
        name: database-storage
        labels:
          app: {{ template "couchdb.name" . }}
          release: {{ .Release.Name }}
      spec:
        accessModes:
        {{- range .Values.persistentVolume.accessModes }}
          - {{ . | quote }}
        {{- end }}
        resources:
          requests:
            storage: {{ .Values.persistentVolume.size | quote }}
      {{- if .Values.persistentVolume.storageClass }}
      {{- if (eq "-" .Values.persistentVolume.storageClass) }}
        storageClassName: ""
      {{- else }}
        storageClassName: "{{ .Values.persistentVolume.storageClass }}"
      {{- end }}
      {{- end }}
{{- end }}
