# helm_charts_keeper-statefulset.yaml
apiVersion: apps/v1
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
{{- if .Values.keeper.fsGroup }}
      securityContext:
        fsGroup: {{ .Values.keeper.fsGroup }}
{{- end }}
{{- if .Values.image.pullSecrets }}
      imagePullSecrets:
{{ toYaml .Values.image.pullSecrets | indent 8 }}
{{- end }}
      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="{{ .Values.keeper.uid_prefix }}${ADDR[-1]}"
              export POD_IP=$(hostname -i)
              export STKEEPER_PG_ADVERTISE_ADDRESS=$POD_IP
              export STKEEPER_PG_LISTEN_ADDRESS=${STKEEPER_PG_LISTEN_ADDRESS:-$POD_IP}
              export STOLON_DATA=/stolon-data
              chown stolon:stolon $STOLON_DATA
              {{- if .Values.shmVolume.enabled }}
              chmod -R 777 /dev/shm
              {{- end }}
              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.clusterName" . }}
            - 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
{{ if not (empty .Values.replicationSecret.name) }}
              valueFrom:
                secretKeyRef:
                  name: {{ .Values.replicationSecret.name }}
                  key: {{ .Values.replicationSecret.usernameKey }}
{{ else }}
              value: {{ .Values.replicationUsername | quote}}
{{ end }}
            - name: STKEEPER_PG_REPL_PASSWORDFILE
{{ if not (empty .Values.replicationSecret.name) }}
              value: /etc/secrets/stolon-{{ .Values.replicationSecret.name }}/{{ .Values.replicationSecret.passwordKey }}
{{ else if not (empty .Values.replicationPasswordFile) }}
              value: {{ .Values.replicationPasswordFile }}
{{ else }}
              value: "/etc/secrets/stolon/pg_repl_password"
{{ end }}
            - name: STKEEPER_PG_SU_USERNAME
{{ if not (empty .Values.superuserSecret.name) }}
              valueFrom:
                secretKeyRef:
                  name: {{ .Values.superuserSecret.name }}
                  key: {{ .Values.superuserSecret.usernameKey }}
{{ else }}
              value: {{ .Values.superuserUsername | quote }}
{{ end }}
            - name: STKEEPER_PG_SU_PASSWORDFILE
{{ if not (empty .Values.superuserSecret.name) }}
              value: /etc/secrets/stolon-{{ .Values.superuserSecret.name }}/{{ .Values.superuserSecret.passwordKey }}
{{ else if not (empty .Values.superuserPasswordFile) }}
              value: {{ .Values.superuserPasswordFile }}
{{ else }}
              value: "/etc/secrets/stolon/pg_su_password"
{{ end }}
            - name: STKEEPER_METRICS_LISTEN_ADDRESS
              value: "0.0.0.0:{{ .Values.ports.metrics.containerPort }}"
            - name: STKEEPER_DEBUG
              value: {{ .Values.debug | quote}}
{{- if .Values.keeper.extraEnv }}
{{ toYaml .Values.keeper.extraEnv | indent 12 }}
{{- end }}
          ports:
{{- range $key, $value := .Values.ports }}
            - name: {{ $key }}
{{ toYaml $value | indent 14 }}
{{- end }}
          resources:
{{ toYaml .Values.keeper.resources | indent 12 }}
          volumeMounts:
{{- if .Values.shmVolume.enabled }}
          - name: dshm
            mountPath: /dev/shm
{{- end }}
          - name: data
            mountPath: /stolon-data
{{- if .Values.tls.enabled }}
          - name: certs
            mountPath: /certs
{{- end }}
{{ if and (or (empty .Values.superuserSecret.name) (empty .Values.replicationSecret.name)) (or (empty .Values.superuserPasswordFile) (empty .Values.replicationPasswordFile)) }}
          - name: stolon-secrets
            mountPath: /etc/secrets/stolon
{{ end }}
{{ if not (empty .Values.superuserSecret.name) }}
          - name: stolon-secret-{{ .Values.superuserSecret.name }}
            mountPath: /etc/secrets/stolon-{{ .Values.superuserSecret.name }}
{{ end }}
{{ if and (not (empty .Values.replicationSecret.name)) (not (eq .Values.superuserSecret.name .Values.replicationSecret.name)) }}
          - name: stolon-secret-{{ .Values.replicationSecret.name }}
            mountPath: /etc/secrets/stolon-{{ .Values.replicationSecret.name }}
{{ end }}
{{- range $key, $value := .Values.keeper.volumeMounts }}
          - name: {{ $key }}
{{ toYaml $value | indent 12 }}
{{- end }}
{{- if .Values.keeper.hooks.failKeeper.enabled }}
          - name: config
            mountPath: /pre-stop-hook.sh
            subPath: pre-stop-hook.sh
{{- end }}
{{- if .Values.nodePostStartScript }}
          - name: config
            mountPath: /postStartScript.sh
            subPath: postStartScript.sh
{{- end }}
          lifecycle:
{{- if .Values.keeper.hooks.failKeeper.enabled }}
            preStop:
              exec:
                command: ["/bin/bash", "-e", "/pre-stop-hook.sh"]
{{- end }}
{{- if .Values.nodePostStartScript }}
            postStart:
              exec:
                command: ["/bin/bash", "-e", "/postStartScript.sh"]
{{- end }}
{{- 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:
{{- if .Values.shmVolume.enabled }}
        - name: dshm
          emptyDir:
            medium: Memory
{{- end }}
{{- if .Values.tls.enabled }}
        - name: certs
          secret:
            defaultMode: 0600
            secretName: {{ if .Values.tls.existingSecret }}{{ .Values.tls.existingSecret }}{{ else }}{{ template "stolon.fullname" . }}-certs{{end}}
{{ end }}
        - name: config
          configMap:
            name: {{ template "stolon.fullname" . }}
{{ if or (empty .Values.superuserSecret.name) (empty .Values.replicationSecret.name) }}
        - name: stolon-secrets
          secret:
            secretName: {{ template "stolon.fullname" . }}
{{ end }}
{{ if not (empty .Values.superuserSecret.name) }}
        - name: stolon-secret-{{ .Values.superuserSecret.name }}
          secret:
            secretName: {{ .Values.superuserSecret.name }}
{{ end }}
{{ if and (not (empty .Values.replicationSecret.name)) (not (eq .Values.superuserSecret.name .Values.replicationSecret.name)) }}
        - name: stolon-secret-{{ .Values.replicationSecret.name }}
          secret:
            secretName: {{ .Values.replicationSecret.name }}
{{ end }}
{{- range $key, $value := .Values.keeper.volumes }}
        - name: {{ $key }}
{{ toYaml $value | indent 10 }}
{{- end }}
{{- 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 }}