Skip to content

Commit

Permalink
Merge pull request #41 from Iceber/storage_dsn
Browse files Browse the repository at this point in the history
support for setting the external storage with dsn
  • Loading branch information
Iceber authored Apr 25, 2023
2 parents f4b2da7 + fdbadf5 commit eb18ea8
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 13 deletions.
2 changes: 1 addition & 1 deletion charts/clusterpedia/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type: application
# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
# Versions are expected to follow Semantic Versioning (https://semver.org/)
version: 1.5.0
version: 1.6.0

# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application. Versions are not expected to
Expand Down
42 changes: 35 additions & 7 deletions charts/clusterpedia/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,25 @@ Return the proper Docker Image Registry Secret Names
{{- end -}}
{{- end -}}

{{- define "clusterpedia.storage.dsn" -}}
{{- if eq .Values.storageInstallMode "external" }}
{{- if not (eq .Values.externalStorage.type "mysql") }}
{{ required "storage dsn not support mysql" "" }}
{{- else -}}
{{- .Values.externalStorage.dsn }}
{{- end -}}
{{- end -}}
{{- end -}}



{{- define "clusterpedia.storage.user" -}}
{{- if eq .Values.storageInstallMode "external" }}
{{- required "Please set correct storage user!" .Values.externalStorage.user -}}
{{- if empty (include "clusterpedia.storage.dsn" .) -}}
{{- required "Please set correct storage user!" .Values.externalStorage.user -}}
{{- else -}}
{{- .Values.externalStorage.user -}}
{{- end -}}
{{- else -}}
{{- if eq (include "clusterpedia.storage.type" .) "postgres" -}}
{{- if not (empty .Values.global.postgresql.auth.username) -}}
Expand All @@ -144,7 +160,11 @@ Return the proper Docker Image Registry Secret Names

{{- define "clusterpedia.storage.password" -}}
{{- if eq .Values.storageInstallMode "external" }}
{{- required "Please set correct storage password!" .Values.externalStorage.password | b64enc -}}
{{- if empty (include "clusterpedia.storage.dsn" .) -}}
{{- required "Please set correct storage password!" .Values.externalStorage.password | b64enc -}}
{{- else -}}
{{- .Values.externalStorage.password | b64enc -}}
{{- end -}}
{{- else -}}
{{- if eq (include "clusterpedia.storage.type" .) "postgres" }}
{{- if not (empty .Values.global.postgresql.auth.username) -}}
Expand All @@ -165,7 +185,11 @@ Return the proper Docker Image Registry Secret Names
{{/* use the default port */}}
{{- define "clusterpedia.storage.port" -}}
{{- if eq .Values.storageInstallMode "external" }}
{{- required "Please set correct storage port!" .Values.externalStorage.port -}}
{{- if empty (include "clusterpedia.storage.dsn" .) -}}
{{- required "Please set correct storage port!" .Values.externalStorage.port -}}
{{- else -}}
{{- .Values.externalStorage.port }}
{{- end -}}
{{- else -}}
{{- if eq (include "clusterpedia.storage.type" .) "postgres" -}}
{{- .Values.postgresql.primary.service.ports.postgresql -}}
Expand All @@ -178,7 +202,11 @@ Return the proper Docker Image Registry Secret Names
{{/* use the default port */}}
{{- define "clusterpedia.storage.host" -}}
{{- if eq .Values.storageInstallMode "external" }}
{{- required "Please set correct storage host!" .Values.externalStorage.host -}}
{{- if empty (include "clusterpedia.storage.dsn" .) -}}
{{- required "Please set correct storage host!" .Values.externalStorage.host -}}
{{- else -}}
{{- .Values.externalStorage.host }}
{{- end -}}
{{- else -}}
{{- if eq (include "clusterpedia.storage.type" .) "postgres" -}}
{{- include "clusterpedia.postgresql.fullname" . -}}
Expand All @@ -190,10 +218,10 @@ Return the proper Docker Image Registry Secret Names

{{- define "clusterpedia.storage.database" -}}
{{- if eq .Values.storageInstallMode "external" }}
{{- if empty .Values.externalStorage.database }}
{{ required "Please set correct storage database!" "" }}
{{- if empty (include "clusterpedia.storage.dsn" .) -}}
{{- required "Please set correct storage database!" .Values.externalStorage.database -}}
{{- else -}}
{{- .Values.externalStorage.database }}
{{- .Values.externalStorage.database -}}
{{- end -}}
{{- else -}}
{{- "clusterpedia" -}}
Expand Down
6 changes: 4 additions & 2 deletions charts/clusterpedia/templates/apiserver-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ spec:
# Load YAML data into a Bash variable
source /opt/scripts/parse-yaml.sh;
create_variables /etc/clusterpedia/storage/internalstorage-config.yaml;
until mysql -u${user} -p${DB_PASSWORD} --host=${host} --port=${port} -e 'CREATE DATABASE IF NOT EXISTS ${database}'; do
if [ -z $password ]; then password=${DB_PASSWORD}; fi;
until mysql -u${user} -p${password} --host=${host} --port=${port} -e 'CREATE DATABASE IF NOT EXISTS ${database}'; do
echo waiting for database check && sleep 1;
done;
echo 'DataBase OK ✓'
Expand Down Expand Up @@ -88,7 +89,8 @@ spec:
# Load YAML data into a Bash variable
source /opt/scripts/parse-yaml.sh;
create_variables /etc/clusterpedia/storage/internalstorage-config.yaml;
until mysqladmin status -u${user} -p${DB_PASSWORD} --host=${host} --port=${port}; do
if [ -z $password ]; then password=${DB_PASSWORD}; fi;
until mysqladmin status -u${user} -p${password} --host=${host} --port=${port}; do
echo waiting for database check && sleep 1;
done;
echo 'DataBase OK ✓'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ spec:
# Load YAML data into a Bash variable
source /opt/scripts/parse-yaml.sh;
create_variables /etc/clusterpedia/storage/internalstorage-config.yaml;
until mysql -u${user} -p${DB_PASSWORD} --host=${host} --port=${port} -e 'CREATE DATABASE IF NOT EXISTS {{ include "clusterpedia.storage.database" . }}'; do
if [ -z $password ]; then password=${DB_PASSWORD}; fi;
until mysql -u${user} -p${password} --host=${host} --port=${port} -e 'CREATE DATABASE IF NOT EXISTS {{ include "clusterpedia.storage.database" . }}'; do
echo waiting for database check && sleep 1;
done;
echo 'DataBase OK ✓'
Expand Down Expand Up @@ -88,7 +89,8 @@ spec:
# Load YAML data into a Bash variable
source /opt/scripts/parse-yaml.sh;
create_variables /etc/clusterpedia/storage/internalstorage-config.yaml;
until mysqladmin status -u${user} -p${DB_PASSWORD} --host=${host} --port=${port}; do
if [ -z $password ]; then password=${DB_PASSWORD}; fi;
until mysqladmin status -u${user} -p${password} --host=${host} --port=${port}; do
echo waiting for database check && sleep 1;
done;
echo 'DataBase OK ✓'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ metadata:
data:
internalstorage-config.yaml: |-
type: {{ include "clusterpedia.storage.type" . | quote }}
dsn: {{ include "clusterpedia.storage.dsn" . | quote }}
host: {{ include "clusterpedia.storage.host" . | quote }}
port: {{ include "clusterpedia.storage.port" . }}
user: {{ include "clusterpedia.storage.user" . }}
Expand Down
33 changes: 32 additions & 1 deletion charts/clusterpedia/templates/scripts-configmap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ metadata:
labels: {{- include "common.labels.standard" . | nindent 4 }}
data:
parse-yaml.sh: |
function parse_yaml {
function parse_yaml() {
local prefix=$2
local s='[[:space:]]*' w='[a-zA-Z0-9_]*' fs=$(echo @|tr @ '\034')
sed -ne "s|^\($s\):|\1|" \
Expand All @@ -22,7 +22,38 @@ data:
}
}'
}
function parse_mysql_dsn() {
local dsn=$1
if [ -z $dsn ]
then
return
fi
IFS='?' read -r -a _dsn <<< $dsn; upnad=${_dsn[0]}
IFS='/' read -a upnad <<< $upnad; upna=${upnad[0]}; database=${upnad[1]}
IFS='(' read -a upna <<< $upna ; upn=${upna[0]}; address=${upna[1]%?}
IFS='@' read -r -a upn <<< $upn; up=${upn[0]}; protocal=${upn[1]}
IFS=':' read -r -a up <<< $up; user=${up[0]}; password=${up[1]}
if [ ! -z $address ]
then
IFS=':' read -r -a address <<< $address; host=${address[0]}; port=${address[1]}
fi
echo "database=\"$database\""
echo "host=\"$host\""
echo "port=\"$port\""
echo "user=\"$user\""
echo "password=\"$password\""
}
function create_variables() {
local yaml_file="$1"
eval "$(parse_yaml "$yaml_file")"
if [ $type == "mysql" ]
then
eval $(parse_mysql_dsn "$dsn")
fi
}
4 changes: 4 additions & 0 deletions charts/clusterpedia/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ externalStorage:
## @param externalStorage.type Select which database backend Clusterpedia will use. Can be 'postgres' or 'mysql'
##
type: ""
## @dsn externalStorage.type for storage dsn. If it is set, then the host, port, user, database parameters will be ignored
## It only works for mysql
##
dsn: ""
## @param externalStorage.host for a custom host
##
host: ""
Expand Down

0 comments on commit eb18ea8

Please sign in to comment.