Skip to content

Commit

Permalink
Support Create Database to External Storage
Browse files Browse the repository at this point in the history
Signed-off-by: RuliXu <lily.xu@daocloud.io>
  • Loading branch information
RuliXu committed Sep 16, 2022
1 parent 4441b06 commit 6290190
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 7 deletions.
10 changes: 9 additions & 1 deletion charts/clusterpedia/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -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" -}}
Expand Down Expand Up @@ -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 -}}
27 changes: 27 additions & 0 deletions charts/clusterpedia/templates/apiserver-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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" . }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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" . }}
Expand Down
15 changes: 9 additions & 6 deletions charts/clusterpedia/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 6290190

Please sign in to comment.