apiVersion: apps/v1beta2
kind: StatefulSet
metadata:
  name: {{ template "stolon.fullname" . }}-keeper
  labels:
    app: {{ template "stolon.name" . }}
    chart: {{ template "stolon.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
spec:
  serviceName: {{ template "stolon.fullname" . }}-keeper-headless
  replicas: {{ .Values.keeper.replicaCount }}
  selector:
    matchLabels:
      app: {{ template "stolon.name" . }}
      release: {{ .Release.Name }}
      component: stolon-keeper
  template:
    metadata:
      labels:
        app: {{ template "stolon.name" . }}
        release: {{ .Release.Name }}
        component: stolon-keeper
        stolon-cluster: {{ template "stolon.fullname" . }}
      annotations:
{{- with .Values.keeper.annotations }}
{{ toYaml . | indent 8 }}
{{- end }}
    spec:
{{- if .Values.keeper.priorityClassName }}
      priorityClassName: "{{ .Values.keeper.priorityClassName }}"
{{- end }}
      serviceAccountName: {{ template "stolon.serviceAccountName" . }}
      terminationGracePeriodSeconds: 10
      containers:
        - name: {{ .Chart.Name }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          command:
            - "/bin/bash"
            - "-ec"
            - |
              # Generate our keeper uid using the pod index
              IFS='-' read -ra ADDR <<< "$(hostname)"
              export STKEEPER_UID="keeper${ADDR[-1]}"
              export POD_IP=$(hostname -i)
              export STKEEPER_PG_LISTEN_ADDRESS=$POD_IP
              export STOLON_DATA=/stolon-data
              chown stolon:stolon $STOLON_DATA
              exec gosu stolon stolon-keeper --data-dir $STOLON_DATA
          env:
            - name: POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: STKEEPER_CLUSTER_NAME
              value: {{ template "stolon.fullname" . }}
            - name: STKEEPER_STORE_BACKEND
              value: {{ .Values.store.backend | quote}}
            {{- if eq .Values.store.backend "kubernetes" }}
            - name: STKEEPER_KUBE_RESOURCE_KIND
              value: {{ .Values.store.kubeResourceKind  | quote}}
            {{- else }}
            - name: STKEEPER_STORE_ENDPOINTS
              value: {{ .Values.store.endpoints | quote}}
            {{- end }}
            - name: STKEEPER_PG_REPL_USERNAME
              value: {{ .Values.replicationUsername | quote}}
            - name: STKEEPER_PG_REPL_PASSWORDFILE
              value: "/etc/secrets/stolon/pg_repl_password"
            - name: STKEEPER_PG_SU_USERNAME
              value: {{ .Values.superuserUsername | quote}}
            - name: STKEEPER_PG_SU_PASSWORDFILE
              value: "/etc/secrets/stolon/pg_su_password"
            - name: STKEEPER_METRICS_LISTEN_ADDRESS
              value: "0.0.0.0:8080"
            - name: STKEEPER_DEBUG
              value: {{ .Values.debug | quote}}
          ports:
{{- range $key, $value := .Values.ports }}
            - name: {{ $key }}
{{ toYaml $value | indent 14 }}
{{- end }}
          resources:
{{ toYaml .Values.keeper.resources | indent 12 }}
          volumeMounts:
          - name: data
            mountPath: /stolon-data
          - name: stolon-secrets
            mountPath: /etc/secrets/stolon
{{- with .Values.keeper.nodeSelector }}
      nodeSelector:
{{ toYaml . | indent 8 }}
{{- end }}
{{- with .Values.keeper.affinity }}
      affinity:
{{ toYaml . | indent 8 }}
{{- end }}
{{- with .Values.keeper.tolerations }}
      tolerations:
{{ toYaml . | indent 8 }}
{{- end }}
      volumes:
        - name: stolon-secrets
          secret:
            secretName: {{ template "stolon.fullname" . }}
{{- if .Values.persistence.enabled }}
  volumeClaimTemplates:
    - metadata:
        name: data
      spec:
        accessModes:
  {{- range .Values.persistence.accessModes }}
          - {{ . | quote }}
  {{- end }}
        resources:
          requests:
            storage: {{ .Values.persistence.size | quote }}
  {{- if .Values.persistence.storageClassName }}
    {{- if (eq "-" .Values.persistence.storageClassName) }}
        storageClassName: ""
    {{- else }}
        storageClassName: "{{ .Values.persistence.storageClassName }}"
    {{- end }}
  {{- end }}
{{- else }}
        - name: data
          emptyDir: {}
{{- end }}
