{{ if .Values.job.autoUpdateClusterSpec }}
apiVersion: batch/v1
kind: Job
metadata:
  name: {{ template "stolon.fullname" . }}-update-cluster-spec
  labels:
    app: {{ template "stolon.name" . }}
    chart: {{ template "stolon.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
  annotations:
    "helm.sh/hook": post-upgrade
    "helm.sh/hook-delete-policy": before-hook-creation
spec:
  template:
    metadata:
      labels:
        app: {{ template "stolon.fullname" . }}
        release: {{ .Release.Name }}
{{- if .Values.job.annotations }}
      annotations:
{{ toYaml .Values.job.annotations | indent 8 }}
{{- end }}
    spec:
      restartPolicy: OnFailure
      serviceAccountName: {{ template "stolon.serviceAccountName" . }}
  {{- if eq .Values.store.backend "etcdv2" "etcdv3" }}
      initContainers:
        - name: {{ .Chart.Name }}-etcd-wait
          image: "{{ .Values.etcdImage.repository }}:{{ .Values.etcdImage.tag }}"
          imagePullPolicy: {{ .Values.etcdImage.pullPolicy }}
          command: ["sh", "-c", "while ! etcdctl --endpoints {{ .Values.store.endpoints }} cluster-health; do sleep 1 && echo -n .; done"]
  {{- end }}
      containers:
        - name: {{ template "stolon.fullname" . }}-update-cluster-spec
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          command: ["/usr/local/bin/stolonctl"]
          args:
            - update
            - --cluster-name={{ template "stolon.clusterName" . }}
            - --store-backend={{ .Values.store.backend }}
            {{- if eq .Values.store.backend "kubernetes" }}
            - --kube-resource-kind={{ .Values.store.kubeResourceKind }}
            {{- else }}
            - --store-endpoints={{ .Values.store.endpoints }}
            {{- end }}
            - -p
            - '{ {{- range $key, $value := .Values.clusterSpec }} {{ $key | quote }}: {{ if typeIs "string" $value }} {{ $value | quote }} {{ else }} {{ $value }} {{ end }}, {{- end }} {{ if .Values.tls.enabled }}"pgParameters": {{- $pgParameters := .Values.pgParameters -}}{{ $all := set $pgParameters "ssl" "on" }}{{ $all := set $all "ssl_cert_file" "/certs/serverCrt.crt" }} {{ $all := set $all "ssl_key_file" "/certs/serverKey.key" }}{{ $all := set $all "ssl_ca_file" "/certs/rootCa.crt" }}{{ toJson $all }} {{ else }}"pgParameters": {{- $pgParameters := .Values.pgParameters -}}{{ $all := set $pgParameters "ssl" "off" }}{{ $all := set $all "ssl_cert_file" nil }} {{ $all := set $all "ssl_key_file" nil }}{{ $all := set $all "ssl_ca_file" nil }}{{ toJson $all }}{{ end}}}'
{{ end }}
