Add the capability to create a test database

In this patchset, if configured to do so, a test database with
a table and a couple of rows in the table is created at bootstrap time
of the mysqlclient utility container.
The test database will be created for each of the Mariadb DBs in the
mariadb backup restore enabled namespaces.

Change-Id: I3afb85debfafde0585abd726fe8b2f999f590294
This commit is contained in:
Huang, Sophie (sh879n) 2020-06-24 20:27:43 +00:00
parent 9c179738b8
commit b597e0addc
7 changed files with 125 additions and 126 deletions

View File

@ -14,4 +14,9 @@ limitations under the License.
*/}} */}}
set -ex set -ex
sudo ./tmp/start.sh sudo ./tmp/start.sh
if [ -f /tmp/create_test_database.sh ]; then
./tmp/create_test_database.sh
fi
tail -f /var/log/syslog tail -f /var/log/syslog

View File

@ -0,0 +1,54 @@
#!/bin/bash
set -e +x
trap cleanup EXIT SIGTERM SIGINT
IFS=', ' read -re -a BACKUP_RESTORE_NAMESPACE_ARRAY <<< "$BACKUP_RESTORE_NAMESPACE_LIST"
ADMIN_USER_CNF=$(mktemp -p /tmp)
function cleanup {
rm -f "${ADMIN_USER_CNF}"
echo 'Cleanup Finished.'
}
for NAMESPACE in "${BACKUP_RESTORE_NAMESPACE_ARRAY[@]}";
do
kubectl -n "$NAMESPACE" get secret mariadb-secrets -o yaml \
| grep admin_user.cnf | awk '{print $2}' | base64 -d > "${ADMIN_USER_CNF}"
USER=$(grep user "$ADMIN_USER_CNF" | awk '{print $3}')
PASSWD=$(grep password "$ADMIN_USER_CNF" | awk '{print $3}')
PORT=$(grep port "$ADMIN_USER_CNF" | awk '{print $3}')
MYSQL="mysql \
-u $USER -p${PASSWD} \
--host=mariadb.$NAMESPACE.svc.cluster.local \
--port=$PORT \
--connect-timeout 10"
# Verify if test database exists already
DB_ARGS="use ${TEST_DB_NAME}"
if $MYSQL --execute="$DB_ARGS" > /dev/null 2>&1; then
echo "Test database already exists in namespace $NAMESPACE."
else
# Create test database
DB_ARGS="CREATE DATABASE ${TEST_DB_NAME};"
$MYSQL --execute="$DB_ARGS"
# Add a table to the test database
DB_ARGS="USE ${TEST_DB_NAME};CREATE TABLE test_table1 \
( id int(11) NOT NULL AUTO_INCREMENT, name varchar(255) NOT NULL, user_id int(11) DEFAULT 0, PRIMARY KEY (id) );"
$MYSQL --execute="$DB_ARGS"
# Add a couple rows to the table of the test database
DB_ARGS="USE ${TEST_DB_NAME};LOCK TABLES test_table1 WRITE \
;INSERT INTO test_table1 (name) value ('name') \
;UPDATE test_table1 SET user_id=id,name=CONCAT(name,user_id) WHERE id = LAST_INSERT_ID() \
;UNLOCK TABLES;"
$MYSQL --execute="$DB_ARGS"
$MYSQL --execute="$DB_ARGS"
echo "Test database created in namespace $NAMESPACE."
fi
done

View File

@ -432,11 +432,12 @@ function do_show_schema() {
fi fi
} }
# Params: <namespace> <database> # Params: <namespace> <tablename>
# NOTE: "test_" is automatically prepended before the provided database # Column names and types will be hardcoded for now
# name, in order to prevent accidental modification/deletion of # NOTE: In order for this function to work, create_test_database in
# an application database. # values.yaml file needs to be set to true to create a test database
function do_create_database() { # at bootrap time.
function do_create_table() {
CREATE_ARGS=("$@") CREATE_ARGS=("$@")
@ -449,53 +450,29 @@ function do_create_database() {
ensure_ondemand_pod_exists ensure_ondemand_pod_exists
CREATE_ARGS[3]=$ONDEMAND_POD CREATE_ARGS[3]=$ONDEMAND_POD
create_database "${CREATE_ARGS[@]}"
if [[ $? -ne 0 ]]; then
return 1
fi
}
# Params: <namespace> <database> <tablename>
# Column names and types will be hardcoded for now
# NOTE: "test_" is automatically prepended before the provided database
# name, in order to prevent accidental modification of
# an application database.
function do_create_table() {
CREATE_ARGS=("$@")
check_args CREATE_ARGS 3 3
if [[ $? -ne 0 ]]; then
return 1
fi
# Be sure that an ondemand pod is ready (start if not started)
ensure_ondemand_pod_exists
CREATE_ARGS[4]=$ONDEMAND_POD
create_table "${CREATE_ARGS[@]}" create_table "${CREATE_ARGS[@]}"
if [[ $? -ne 0 ]]; then if [[ $? -ne 0 ]]; then
return 1 return 1
fi fi
} }
# Params: <namespace> <database> <table> # Params: <namespace> <table>
# The row values are hardcoded for now. # The row values are hardcoded for now.
# NOTE: "test_" is automatically prepended before the provided database # NOTE: In order for this function to work, create_test_database in
# name, in order to prevent accidental modification of # values.yaml file needs to be set to true to create a test database
# an application database. # at bootrap time.
function do_create_row() { function do_create_row() {
CREATE_ARGS=("$@") CREATE_ARGS=("$@")
check_args CREATE_ARGS 3 3 check_args CREATE_ARGS 2 2
if [[ $? -ne 0 ]]; then if [[ $? -ne 0 ]]; then
return 1 return 1
fi fi
# Be sure that an ondemand pod is ready (start if not started) # Be sure that an ondemand pod is ready (start if not started)
ensure_ondemand_pod_exists ensure_ondemand_pod_exists
CREATE_ARGS[4]=$ONDEMAND_POD CREATE_ARGS[3]=$ONDEMAND_POD
create_row "${CREATE_ARGS[@]}" create_row "${CREATE_ARGS[@]}"
if [[ $? -ne 0 ]]; then if [[ $? -ne 0 ]]; then
@ -503,23 +480,23 @@ function do_create_row() {
fi fi
} }
# Params: <namespace> <database> <table> <colname> <value> # Params: <namespace> <table> <colname> <value>
# Where: <colname> = <value> is the condition used to find the row to be deleted. # Where: <colname> = <value> is the condition used to find the row to be deleted.
# NOTE: "test_" is automatically prepended before the provided database # NOTE: In order for this function to work, create_test_database in
# name, in order to prevent accidental modification/deletion of # values.yaml file needs to be set to true to create a test database
# an application database. # at bootrap time.
function do_delete_row() { function do_delete_row() {
DELETE_ARGS=("$@") DELETE_ARGS=("$@")
check_args DELETE_ARGS 5 5 check_args DELETE_ARGS 4 4
if [[ $? -ne 0 ]]; then if [[ $? -ne 0 ]]; then
return 1 return 1
fi fi
# Be sure that an ondemand pod is ready (start if not started) # Be sure that an ondemand pod is ready (start if not started)
ensure_ondemand_pod_exists ensure_ondemand_pod_exists
DELETE_ARGS[6]=$ONDEMAND_POD DELETE_ARGS[5]=$ONDEMAND_POD
delete_row "${DELETE_ARGS[@]}" delete_row "${DELETE_ARGS[@]}"
if [[ $? -ne 0 ]]; then if [[ $? -ne 0 ]]; then
@ -527,37 +504,14 @@ function do_delete_row() {
fi fi
} }
# Params: <namespace> <database> <tablename> # Params: <namespace> <tablename>
# NOTE: "test_" is automatically prepended before the provided database # NOTE: In order for this function to work, create_test_database in
# name, in order to prevent accidental modification/deletion of # values.yaml file needs to be set to true to create a test database
# an application database. # at bootrap time.
function do_delete_table() { function do_delete_table() {
DELETE_ARGS=("$@") DELETE_ARGS=("$@")
check_args DELETE_ARGS 3 3
if [[ $? -ne 0 ]]; then
return 1
fi
# Be sure that an ondemand pod is ready (start if not started)
ensure_ondemand_pod_exists
DELETE_ARGS[4]=$ONDEMAND_POD
delete_table "${DELETE_ARGS[@]}"
if [[ $? -ne 0 ]]; then
return 1
fi
}
# Params: <namespace> <database>
# NOTE: "test_" is automatically prepended before the provided database
# name, in order to prevent accidental modification/deletion of
# an application database.
function do_delete_database() {
DELETE_ARGS=("$@")
check_args DELETE_ARGS 2 2 check_args DELETE_ARGS 2 2
if [[ $? -ne 0 ]]; then if [[ $? -ne 0 ]]; then
return 1 return 1
@ -567,7 +521,7 @@ function do_delete_database() {
ensure_ondemand_pod_exists ensure_ondemand_pod_exists
DELETE_ARGS[3]=$ONDEMAND_POD DELETE_ARGS[3]=$ONDEMAND_POD
delete_database "${DELETE_ARGS[@]}" delete_table "${DELETE_ARGS[@]}"
if [[ $? -ne 0 ]]; then if [[ $? -ne 0 ]]; then
return 1 return 1
fi fi
@ -778,12 +732,10 @@ function execute_selection() {
"show_tables"|"st") do_show_tables "${ARGS[@]}";; "show_tables"|"st") do_show_tables "${ARGS[@]}";;
"show_rows"|"sr") do_show_rows "${ARGS[@]}";; "show_rows"|"sr") do_show_rows "${ARGS[@]}";;
"show_schema"|"ss") do_show_schema "${ARGS[@]}";; "show_schema"|"ss") do_show_schema "${ARGS[@]}";;
"create_test_database"|"ctd") do_create_database "${ARGS[@]}";;
"create_test_table"|"ctt") do_create_table "${ARGS[@]}";; "create_test_table"|"ctt") do_create_table "${ARGS[@]}";;
"create_test_row"|"ctr") do_create_row "${ARGS[@]}";; "create_test_row"|"ctr") do_create_row "${ARGS[@]}";;
"delete_test_row"|"dtr") do_delete_row "${ARGS[@]}";; "delete_test_row"|"dtr") do_delete_row "${ARGS[@]}";;
"delete_test_table"|"dtt") do_delete_table "${ARGS[@]}";; "delete_test_table"|"dtt") do_delete_table "${ARGS[@]}";;
"delete_test_database"|"dtd") do_delete_database "${ARGS[@]}";;
"restore"|"r") do_restore "${ARGS[@]}";; "restore"|"r") do_restore "${ARGS[@]}";;
"sql_prompt"|"sql") do_sql_prompt "${ARGS[@]}";; "sql_prompt"|"sql") do_sql_prompt "${ARGS[@]}";;
"command_history"|"ch") do_command_history;; "command_history"|"ch") do_command_history;;

View File

@ -63,96 +63,67 @@ function sql_prompt() {
kubectl exec -it -n "${SHOW_ARGS[1]}" "${SHOW_ARGS[2]}" -- ${MYSQL_CMD} kubectl exec -it -n "${SHOW_ARGS[1]}" "${SHOW_ARGS[2]}" -- ${MYSQL_CMD}
} }
# Params: <namespace> <database> <pod_name> # Params: <namespace> <table> <pod_name>
# NOTE: "test_" is automatically prepended before the provided database # Create a table in an existing test database.
# name, in order to prevent accidental modification/deletion of # NOTE: In order for this function to work, create_test_database in
# an application database. # values.yaml file needs to be set to true to create a test database
function create_database() { # at bootstrap time.
CREATE_ARGS=("$@")
MYSQL_CMD=$(database_cmd)
DB_ARGS="CREATE DATABASE test_${CREATE_ARGS[2]};"
# Execute the command in the on-demand pod
kubectl exec -it -n "${CREATE_ARGS[1]}" "${CREATE_ARGS[3]}" -- ${MYSQL_CMD} --execute="$DB_ARGS"
}
# Params: <namespace> <database> <table> <pod_name>
# NOTE: "test_" is automatically prepended before the provided database
# name, in order to prevent accidental modification/deletion of
# an application database.
function create_table() { function create_table() {
CREATE_ARGS=("$@") CREATE_ARGS=("$@")
MYSQL_CMD=$(database_cmd) MYSQL_CMD=$(database_cmd)
DB_ARGS="USE test_${CREATE_ARGS[2]};CREATE TABLE ${CREATE_ARGS[3]} \ DB_ARGS="USE $TEST_DB_NAME;CREATE TABLE ${CREATE_ARGS[2]} \
( id int(11) NOT NULL AUTO_INCREMENT, name varchar(255) NOT NULL, user_id int(11) DEFAULT 0, PRIMARY KEY (id) );" ( id int(11) NOT NULL AUTO_INCREMENT, name varchar(255) NOT NULL, user_id int(11) DEFAULT 0, PRIMARY KEY (id) );"
# Execute the command in the on-demand pod # Execute the command in the on-demand pod
kubectl exec -it -n "${CREATE_ARGS[1]}" "${CREATE_ARGS[4]}" -- ${MYSQL_CMD} --execute="$DB_ARGS" kubectl exec -it -n "${CREATE_ARGS[1]}" "${CREATE_ARGS[3]}" -- ${MYSQL_CMD} --execute="$DB_ARGS"
} }
# Params: <namespace> <database> <table> <pod_name> # Params: <namespace> <table> <pod_name>
# NOTE: "test_" is automatically prepended before the provided database # NOTE: In order for this function to work, create_test_database in
# name, in order to prevent accidental modification/deletion of # values.yaml file needs to be set to true to create a test database
# an application database. # at bootstrap time.
function create_row() { function create_row() {
CREATE_ARGS=("$@") CREATE_ARGS=("$@")
MYSQL_CMD=$(database_cmd) MYSQL_CMD=$(database_cmd)
DB_ARGS="USE test_${CREATE_ARGS[2]};LOCK TABLES ${CREATE_ARGS[3]} WRITE \ DB_ARGS="USE $TEST_DB_NAME;LOCK TABLES ${CREATE_ARGS[2]} WRITE \
;INSERT INTO ${CREATE_ARGS[3]} (name) value ('name') \ ;INSERT INTO ${CREATE_ARGS[2]} (name) value ('name') \
;UPDATE ${CREATE_ARGS[3]} SET user_id=id,name=CONCAT(name,user_id) WHERE id = LAST_INSERT_ID() \ ;UPDATE ${CREATE_ARGS[2]} SET user_id=id,name=CONCAT(name,user_id) WHERE id = LAST_INSERT_ID() \
;UNLOCK TABLES;" ;UNLOCK TABLES;"
# Execute the command in the on-demand pod # Execute the command in the on-demand pod
kubectl exec -it -n "${CREATE_ARGS[1]}" "${CREATE_ARGS[4]}" -- ${MYSQL_CMD} --execute="$DB_ARGS" kubectl exec -it -n "${CREATE_ARGS[1]}" "${CREATE_ARGS[3]}" -- ${MYSQL_CMD} --execute="$DB_ARGS"
} }
# Params: <namespace> <database> <table> <colname> <value> <pod_name> # Params: <namespace> <table> <colname> <value> <pod_name>
# Where: <colname> = <value> is the condition used to find the row to be deleted. # Where: <colname> = <value> is the condition used to find the row to be deleted.
# NOTE: "test_" is automatically prepended before the provided database # NOTE: In order for this function to work, create_test_database in
# name, in order to prevent accidental modification/deletion of # values.yaml file needs to be set to true to create a test database
# an application database. # at bootstrap time.
function delete_row() { function delete_row() {
DELETE_ARGS=("$@") DELETE_ARGS=("$@")
MYSQL_CMD=$(database_cmd) MYSQL_CMD=$(database_cmd)
DB_ARGS="USE test_${DELETE_ARGS[2]};DELETE FROM ${DELETE_ARGS[3]} WHERE ${DELETE_ARGS[4]} = '${DELETE_ARGS[5]}';" DB_ARGS="USE $TEST_DB_NAME;DELETE FROM ${DELETE_ARGS[2]} WHERE ${DELETE_ARGS[3]} = '${DELETE_ARGS[4]}';"
# Execute the command in the on-demand pod # Execute the command in the on-demand pod
kubectl exec -it -n "${DELETE_ARGS[1]}" "${DELETE_ARGS[6]}" -- ${MYSQL_CMD} --execute="$DB_ARGS" kubectl exec -it -n "${DELETE_ARGS[1]}" "${DELETE_ARGS[5]}" -- ${MYSQL_CMD} --execute="$DB_ARGS"
} }
# Params: <namespace> <database> <tablename> <pod_name> # Params: <namespace> <tablename> <pod_name>
# NOTE: "test_" is automatically prepended before the provided database # NOTE: In order for this function to work, create_test_database in
# name, in order to prevent accidental modification/deletion of # values.yaml file needs to be set to true to create a test database
# an application database. # at bootstrap time.
function delete_table() { function delete_table() {
DELETE_ARGS=("$@") DELETE_ARGS=("$@")
MYSQL_CMD=$(database_cmd) MYSQL_CMD=$(database_cmd)
DB_ARGS="USE test_${DELETE_ARGS[2]};DROP TABLE IF EXISTS ${DELETE_ARGS[3]};" DB_ARGS="USE $TEST_DB_NAME;DROP TABLE IF EXISTS ${DELETE_ARGS[2]};"
# Execute the command in the on-demand pod
kubectl exec -it -n "${DELETE_ARGS[1]}" "${DELETE_ARGS[4]}" -- ${MYSQL_CMD} --execute="$DB_ARGS"
}
# Params: <namespace> <database> <pod_name>
# NOTE: "test_" is automatically prepended before the provided database
# name, in order to prevent accidental modification/deletion of
# an application database.
function delete_database() {
DELETE_ARGS=("$@")
MYSQL_CMD=$(database_cmd)
DB_ARGS="DROP DATABASE IF EXISTS test_${DELETE_ARGS[2]};"
# Execute the command in the on-demand pod # Execute the command in the on-demand pod
kubectl exec -it -n "${DELETE_ARGS[1]}" "${DELETE_ARGS[3]}" -- ${MYSQL_CMD} --execute="$DB_ARGS" kubectl exec -it -n "${DELETE_ARGS[1]}" "${DELETE_ARGS[3]}" -- ${MYSQL_CMD} --execute="$DB_ARGS"

View File

@ -49,6 +49,11 @@ data:
mysqlutils.sh: | mysqlutils.sh: |
{{ tuple "bin/utility/_mysqlutils.sh.tpl" . | include "helm-toolkit.utils.template" | indent 4 }} {{ tuple "bin/utility/_mysqlutils.sh.tpl" . | include "helm-toolkit.utils.template" | indent 4 }}
{{- if .Values.manifests.create_test_database }}
create_test_database.sh: |
{{ tuple "bin/utility/_create_test_database.sh.tpl" . | include "helm-toolkit.utils.template" | indent 4 }}
{{- end }}
bootstrap.sh: | bootstrap.sh: |
{{ tuple "bin/_bootstrap.sh.tpl" . | include "helm-toolkit.utils.template" | indent 4 }} {{ tuple "bin/_bootstrap.sh.tpl" . | include "helm-toolkit.utils.template" | indent 4 }}

View File

@ -123,6 +123,10 @@ spec:
env: env:
- name: BACKUP_RESTORE_NAMESPACE_LIST - name: BACKUP_RESTORE_NAMESPACE_LIST
value: {{ .Values.conf.mariadb_backup_restore.enabled_namespaces | quote }} value: {{ .Values.conf.mariadb_backup_restore.enabled_namespaces | quote }}
{{- if .Values.manifests.create_test_database }}
- name: TEST_DB_NAME
value: {{ .Values.conf.mariadb_backup_restore.test_database_name | quote }}
{{- end }}
- name: BACKUP_RESTORE_SCOPE - name: BACKUP_RESTORE_SCOPE
value: "mariadb" value: "mariadb"
volumeMounts: volumeMounts:
@ -154,6 +158,12 @@ spec:
mountPath: /tmp/mariadb-ondemand-job.sh mountPath: /tmp/mariadb-ondemand-job.sh
subPath: mariadb-ondemand-job.sh subPath: mariadb-ondemand-job.sh
readOnly: true readOnly: true
{{- if .Values.manifests.create_test_database }}
- name: mysqlclient-utility-bin-utilscli
mountPath: /tmp/create_test_database.sh
subPath: create_test_database.sh
readOnly: true
{{- end }}
- name: mysqlclient-utility-sudoers - name: mysqlclient-utility-sudoers
mountPath: /etc/sudoers.d/utilscli-sudo mountPath: /etc/sudoers.d/utilscli-sudo
subPath: utilscli-sudo subPath: utilscli-sudo

View File

@ -101,6 +101,7 @@ pod:
conf: conf:
mariadb_backup_restore: mariadb_backup_restore:
enabled_namespaces: "" enabled_namespaces: ""
test_database_name: "test_database"
secrets: secrets:
rgw_secret: mariadb-backup-user rgw_secret: mariadb-backup-user
conf_secret: mariadb-backup-restore conf_secret: mariadb-backup-restore
@ -188,3 +189,4 @@ manifests:
configmap_etc_client: true configmap_etc_client: true
configmap_etc_sudoers: true configmap_etc_sudoers: true
deployment_utility: true deployment_utility: true
create_test_database: false