From 19601be66ccfbc3fbe70fb093f7b8aef11401836 Mon Sep 17 00:00:00 2001 From: Fiore Mario Vitale Date: Sat, 28 Sep 2019 23:43:38 +0200 Subject: [PATCH] [stable/stolon] Init script support (#15990) * Add initScript and postStartScript feature Signed-off-by: Fiore Mario Vitale * Increase chart version Signed-off-by: Fiore Mario Vitale * Add new line at the end of file Signed-off-by: Fiore Mario Vitale * Remove trailing whitespace Signed-off-by: Fiore Mario Vitale * Bump version to 1.2.0 Minor syntax fix Signed-off-by: Fiore Mario Vitale * Fix minor syntax issues Signed-off-by: Fiore Mario Vitale --- stable/stolon/Chart.yaml | 2 +- stable/stolon/README.md | 2 + stable/stolon/templates/configmap.yaml | 3 + .../stolon/templates/hooks/init-db-job.yaml | 58 +++++++++++++++++++ .../stolon/templates/keeper-statefulset.yaml | 14 ++++- stable/stolon/values.yaml | 18 ++++++ 6 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 stable/stolon/templates/hooks/init-db-job.yaml diff --git a/stable/stolon/Chart.yaml b/stable/stolon/Chart.yaml index 10d053517f..cc8f6210db 100644 --- a/stable/stolon/Chart.yaml +++ b/stable/stolon/Chart.yaml @@ -1,6 +1,6 @@ apiVersion: v1 name: stolon -version: 1.1.2 +version: 1.2.0 appVersion: 0.13.0 description: Stolon - PostgreSQL cloud native High Availability. home: https://github.com/sorintlab/stolon diff --git a/stable/stolon/README.md b/stable/stolon/README.md index de1444d88f..9ab6e5ff7d 100644 --- a/stable/stolon/README.md +++ b/stable/stolon/README.md @@ -89,6 +89,8 @@ Kubernetes is the default store backend. `consul`, `etcdv2` or `etcdv3` can also | `sentinel.podDisruptionBudget.enabled` | If true, create a pod disruption budget for sentinel pods. | `false` | | `sentinel.podDisruptionBudget.minAvailable` | Minimum number / percentage of pods that should remain scheduled | `""` | | `sentinel.podDisruptionBudget.maxUnavailable` | Maximum number / percentage of pods that may be made unavailable | `""` | +| `initdbScripts` | Dictionary of scripts. Executed after cluster startup | `nil` | +| `nodePostStartScript` | Dictionary of scripts. Executed after the node startup | `nil` | [pgconf]: https://github.com/postgres/postgres/blob/master/src/backend/utils/misc/postgresql.conf.sample diff --git a/stable/stolon/templates/configmap.yaml b/stable/stolon/templates/configmap.yaml index e81e8e938a..3230be281f 100644 --- a/stable/stolon/templates/configmap.yaml +++ b/stable/stolon/templates/configmap.yaml @@ -30,3 +30,6 @@ data: echo "Node ${NODE_NAME} is ready to shutdown" {{- end }} +{{- with .Values.nodePostStartScript }} +{{ toYaml . | indent 2 }} +{{- end }} diff --git a/stable/stolon/templates/hooks/init-db-job.yaml b/stable/stolon/templates/hooks/init-db-job.yaml new file mode 100644 index 0000000000..dc9b6e0d1a --- /dev/null +++ b/stable/stolon/templates/hooks/init-db-job.yaml @@ -0,0 +1,58 @@ +--- +apiVersion: v1 +kind: ConfigMap +metadata: + name: init-scripts +data: +{{- with .Values.initdbScripts }} +{{ toYaml . | indent 2 }} +{{- end }} +--- +{{- if .Values.initdbScripts }} +apiVersion: batch/v1 +kind: Job +metadata: + name: '{{template "stolon.clusterName" .}}-createdb' + labels: + heritage: {{.Release.Service | quote }} + release: {{.Release.Name | quote }} + chart: "{{.Chart.Name}}-{{.Chart.Version}}" + annotations: + "helm.sh/hook": post-install + "helm.sh/hook-weight": "2" + "helm.sh/hook-delete-policy": hook-succeeded +spec: + template: + metadata: + name: "{{.Release.Name}}" + labels: + heritage: {{.Release.Service | quote }} + release: {{.Release.Name | quote }} + chart: "{{.Chart.Name}}-{{.Chart.Version}}" + spec: + restartPolicy: Never + containers: + - name: create-database-job + image: 'postgres' + env: + - name: USERNAME + value: {{ .Values.superuserUsername }} + - name: PGPASSWORD + value: {{ .Values.superuserPassword }} + - name: HOST + value: {{ template "stolon.fullname" . }}-proxy + command: ["/bin/bash", "-e", "/tmp/sql-script/create_script.sh"] + volumeMounts: + - mountPath: "/tmp/sql-script" + readOnly: true + name: sql-script + volumes: + - name: sql-script + configMap: + name: init-scripts + defaultMode: 420 + initContainers: + - name: wait-for-database + image: jwilder/dockerize + command: ['dockerize', '-timeout', '30s', '-wait', 'tcp://{{ template "stolon.fullname" . }}-proxy:5432'] +{{- end }} \ No newline at end of file diff --git a/stable/stolon/templates/keeper-statefulset.yaml b/stable/stolon/templates/keeper-statefulset.yaml index d9a514b9f2..fe0b159df4 100644 --- a/stable/stolon/templates/keeper-statefulset.yaml +++ b/stable/stolon/templates/keeper-statefulset.yaml @@ -128,11 +128,23 @@ spec: - 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 }} +{{- end }} +{{- if .Values.nodePostStartScript }} + postStart: + exec: + command: ["/bin/bash", "-e", "/postStartScript.sh"] +{{- end }} {{- with .Values.keeper.nodeSelector }} nodeSelector: {{ toYaml . | indent 8 }} diff --git a/stable/stolon/values.yaml b/stable/stolon/values.yaml index b871671a27..a4a567596c 100644 --- a/stable/stolon/values.yaml +++ b/stable/stolon/values.yaml @@ -132,3 +132,21 @@ sentinel: podDisruptionBudget: # minAvailable: 1 # maxUnavailable: 1 + +## initdb scripts +## Specify dictionary of scripts to be run at first boot, the entry point script is create_script.sh +## i.e. you can use pgsql to run sql script on the cluster. +## +# initdbScripts: +# create_script.sh: | +# #!/bin/sh +# echo "Do something." + +## nodePostStart scripts +## Specify dictionary of scripts to be run at first boot, the entry point script is postStartScript.sh +## i.e. you can create tablespace directory here. +## +# nodePostStartScript: +# postStartScript.sh: | +# #!/bin/bash +# echo "Do something."