diff --git a/charts/clusterpedia/templates/_helpers.tpl b/charts/clusterpedia/templates/_helpers.tpl index 2ea543c54..d46aba780 100644 --- a/charts/clusterpedia/templates/_helpers.tpl +++ b/charts/clusterpedia/templates/_helpers.tpl @@ -188,7 +188,7 @@ Return the proper Docker Image Registry Secret Names {{- if empty .Values.externalStorage.database }} {{ required "Please set correct storage database!" "" }} {{- else -}} - {{- .Values.externalStorage.database | quote -}} + {{- .Values.externalStorage.database }} {{- end -}} {{- else -}} {{- "clusterpedia" -}} @@ -306,3 +306,11 @@ We truncate at 63 chars because some Kubernetes name fields are limited to this {{- .Values.mysql.primary.persistence.selector.matchLabels -}} {{- end -}} {{- end -}} + +{{- define "clusterpedia.storage.password.envKey" -}} +{{- if eq (include "clusterpedia.storage.type" .) "postgres" }} + {{- "PGPASSWORD" -}} +{{- else if eq (include "clusterpedia.storage.type" .) "mysql" -}} + {{- "DB_PASSWORD" -}} +{{- end -}} +{{- end -}} diff --git a/charts/clusterpedia/templates/apiserver-deployment.yaml b/charts/clusterpedia/templates/apiserver-deployment.yaml index 938fe91ca..18b90d048 100644 --- a/charts/clusterpedia/templates/apiserver-deployment.yaml +++ b/charts/clusterpedia/templates/apiserver-deployment.yaml @@ -30,6 +30,32 @@ spec: spec: {{- include "clusterpedia.apiserver.imagePullSecrets" . | nindent 6 }} initContainers: + {{- if .Values.externalStorage.createDatabase }} + - name: ensure-database + image: {{ include "clusterpedia.storage.image" . }} + command: + - /bin/sh + - -ec + {{- if eq (include "clusterpedia.storage.type" .) "postgres" }} + - | + until psql -U {{ include "clusterpedia.storage.user" . }} -h {{ include "clusterpedia.storage.host" . }} -p {{ include "clusterpedia.storage.port" . }} postgres -c "SELECT 1 FROM pg_database WHERE datname = '{{ include "clusterpedia.storage.database" . }}'" | grep -q 1 || psql -U {{ include "clusterpedia.storage.user" . }} -h {{ include "clusterpedia.storage.host" . }} -p {{ include "clusterpedia.storage.port" . }} postgres -c "CREATE DATABASE {{ include "clusterpedia.storage.database" . }} owner {{ include "clusterpedia.storage.user" . }} " -c "GRANT ALL PRIVILEGES ON DATABASE {{ include "clusterpedia.storage.database" . }} to {{ include "clusterpedia.storage.user" . }}"; do + echo waiting for database check && sleep 1; + done; + echo 'DataBase OK ✓' + {{- else if eq (include "clusterpedia.storage.type" .) "mysql" }} + - | + until mysql -u{{ include "clusterpedia.storage.user" . }} -p"${DB_PASSWORD}" --host={{ include "clusterpedia.storage.host" . }} --port={{ include "clusterpedia.storage.port" . }} -e 'CREATE DATABASE IF NOT EXISTS {{ include "clusterpedia.storage.database" . }}'; do + echo waiting for database check && sleep 1; + done; + echo 'DataBase OK ✓' + {{- end }} + env: + - name: {{ include "clusterpedia.storage.password.envKey" . }} + valueFrom: + secretKeyRef: + name: {{ include "clusterpedia.internalstorage.fullname" . }} + key: password + {{- else }} - name: check-storage image: {{ include "clusterpedia.storage.image" . }} command: @@ -48,6 +74,7 @@ spec: name: {{ include "clusterpedia.internalstorage.fullname" . }} key: password {{- end }} + {{- end }} containers: - name: {{ include "clusterpedia.apiserver.fullname" . }} image: {{ template "clusterpedia.apiserver.image" . }} diff --git a/charts/clusterpedia/templates/clustersynchro-manager-deployment.yaml b/charts/clusterpedia/templates/clustersynchro-manager-deployment.yaml index 827a00767..09dc9a303 100644 --- a/charts/clusterpedia/templates/clustersynchro-manager-deployment.yaml +++ b/charts/clusterpedia/templates/clustersynchro-manager-deployment.yaml @@ -30,6 +30,32 @@ spec: spec: {{- include "clusterpedia.clustersynchroManager.imagePullSecrets" . | nindent 6 }} initContainers: + {{- if .Values.externalStorage.createDatabase }} + - name: ensure-database + image: {{ include "clusterpedia.storage.image" . }} + command: + - /bin/sh + - -ec + {{- if eq (include "clusterpedia.storage.type" .) "postgres" }} + - | + until psql -U {{ include "clusterpedia.storage.user" . }} -h {{ include "clusterpedia.storage.host" . }} -p {{ include "clusterpedia.storage.port" . }} postgres -c "SELECT 1 FROM pg_database WHERE datname = '{{ include "clusterpedia.storage.database" . }}'" | grep -q 1 || psql -U {{ include "clusterpedia.storage.user" . }} -h {{ include "clusterpedia.storage.host" . }} -p {{ include "clusterpedia.storage.port" . }} postgres -c "CREATE DATABASE {{ include "clusterpedia.storage.database" . }} owner {{ include "clusterpedia.storage.user" . }} " -c "GRANT ALL PRIVILEGES ON DATABASE {{ include "clusterpedia.storage.database" . }} to {{ include "clusterpedia.storage.user" . }}"; do + echo waiting for database check && sleep 1; + done; + echo 'DataBase OK ✓' + {{- else if eq (include "clusterpedia.storage.type" .) "mysql" }} + - | + until mysql -u{{ include "clusterpedia.storage.user" . }} -p"${DB_PASSWORD}" --host={{ include "clusterpedia.storage.host" . }} --port={{ include "clusterpedia.storage.port" . }} -e 'CREATE DATABASE IF NOT EXISTS {{ include "clusterpedia.storage.database" . }}'; do + echo waiting for database check && sleep 1; + done; + echo 'DataBase OK ✓' + {{- end }} + env: + - name: {{ include "clusterpedia.storage.password.envKey" . }} + valueFrom: + secretKeyRef: + name: {{ include "clusterpedia.internalstorage.fullname" . }} + key: password + {{- else }} - name: check-storage image: {{ include "clusterpedia.storage.image" . }} command: @@ -48,6 +74,7 @@ spec: name: {{ include "clusterpedia.internalstorage.fullname" . }} key: password {{- end }} + {{- end }} containers: - name: {{ include "clusterpedia.clustersynchroManager.fullname" . }} image: {{ template "clusterpedia.clustersynchroManager.image" . }} diff --git a/charts/clusterpedia/values.yaml b/charts/clusterpedia/values.yaml index 750373b25..004d696cf 100644 --- a/charts/clusterpedia/values.yaml +++ b/charts/clusterpedia/values.yaml @@ -71,24 +71,27 @@ storageConfig: ## @param external define the auth param of external database ## if set the storageInstallMode to "external", the param must be set. externalStorage: - ## @param database Select which database backend Clusterpedia will use. Can be 'postgres' or 'mysql' + ## @param externalStorage.type Select which database backend Clusterpedia will use. Can be 'postgres' or 'mysql' ## type: "" - ## @param auth.host for a custom host + ## @param externalStorage.host for a custom host ## host: "" - ## @param auth.port for a custom port + ## @param externalStorage.port for a custom port ## port: - ## @param auth.user Name for a custom user + ## @param externalStorage.user Name for a custom user ## user: "" - ## @param auth.password Password for a custom password + ## @param externalStorage.password Password for a custom password ## password: "" - ## @param auth.database Name for a custom database + ## @param externalStorage.database Name for a custom database ## database: "" + ## @param externalStorage.createDatabase whether to create the .Values.externalStorage.database or not + ## + createDatabase: false ## clusterpedia apiserver config apiserver: