From 17e58dd0db1283f75094252bc38963aacafae39c Mon Sep 17 00:00:00 2001 From: Stephen Stubbs Date: Thu, 5 Mar 2020 13:51:19 +0000 Subject: [PATCH] cleanup and use one database for health checks and init of job --- scripts/common.sh | 2 +- scripts/job_on_load | 17 +++++++++-------- src/health_checks.rs | 8 ++++---- src/structs.rs | 2 +- templates/3_config_map/envs.tpl | 3 +++ yaml_structs/postgres-xl-cluster.yaml | 5 +++-- 6 files changed, 21 insertions(+), 16 deletions(-) diff --git a/scripts/common.sh b/scripts/common.sh index 466dcff..6cf5917 100644 --- a/scripts/common.sh +++ b/scripts/common.sh @@ -71,8 +71,8 @@ function log:archive() { function print_bash_error_stack() { for ((i = 1; i < ${#FUNCNAME[@]} - 1; i++)); do - FPATH="$(realpath "${BASH_SOURCE[$i + 1]}")" local FPATH + FPATH="$(realpath "${BASH_SOURCE[$i + 1]}")" log:error "$i: ${FPATH}:${BASH_LINENO[$i]} @ ${FUNCNAME[$i]}" done } diff --git a/scripts/job_on_load b/scripts/job_on_load index 66f48f6..d40831b 100644 --- a/scripts/job_on_load +++ b/scripts/job_on_load @@ -18,7 +18,7 @@ log:info "Waiting for database to be ready..." assert $? "Aborted, timed out while waiting for database to be ready @" "${EXTERNAL_SERVICE}:${PG_PORT}" || exit $? function is_initialized() { - local DOES_STATE_TABLE_EXIST="" + local DOES_STATE_TABLE_EXIST DOES_STATE_TABLE_EXIST=$(psql -t -c " SELECT EXISTS ( SELECT 1 @@ -45,12 +45,12 @@ CREATE TABLE postgres_xl_helm.state AS (SELECT 'initialized' as key, 'ok' as val function run_scripts_that_match() { local PATTERN="$1" - SRC_PATH="$(realpath "$2")" local SRC_PATH + SRC_PATH="$(realpath "$2")" - log:info "Searching for and executing scripts that match PATTERN '$PATTERN' in folder '${SRC_PATH}'" - local LOAD_SCRIPTS="" - LOAD_SCRIPTS="$(find "${SRC_PATH}" -maxdepth 1 -name "$PATTERN" | sort)" + log:info "Searching for and executing scripts that match PATTERN '${PATTERN}' in folder '${SRC_PATH}'" + local LOAD_SCRIPTS + LOAD_SCRIPTS="$(find "${SRC_PATH}" -maxdepth 1 -name "${PATTERN}" | sort)" LOAD_SCRIPTS=(${LOAD_SCRIPTS}) local IS_FOUND="0" @@ -58,9 +58,10 @@ function run_scripts_that_match() { for FPATH in "${LOAD_SCRIPTS[@]}"; do IS_FOUND="1" - FNAME=$(basename "${FPATH}") local FNAME - local FILEEXT=${FNAME##*.} + FNAME=$(basename "${FPATH}") + local FILEEXT + FILEEXT=${FNAME##*.} case ${FILEEXT} in sh) @@ -70,7 +71,7 @@ function run_scripts_that_match() { ;; sql) log:info "Calling psql to execute script in ${FPATH}:" - local LOAD_SCRIPTS_SQL="" + local LOAD_SCRIPTS_SQL LOAD_SCRIPTS_SQL=$(cat "${FPATH}") psql -c "$LOAD_SCRIPTS_SQL" assert $? "Failed when executing init script file ${FPATH}. Exiting... " || return $? diff --git a/src/health_checks.rs b/src/health_checks.rs index a2fcf79..6fe7ccd 100644 --- a/src/health_checks.rs +++ b/src/health_checks.rs @@ -46,7 +46,7 @@ pub async fn watch() -> anyhow::Result<()> { // Check if health check is enabled if context.cluster.values.health_check.enabled - && context.cluster.values.health_check.database != "" + && context.cluster.values.config.database != "" { // If secret is being used get the password for tge database_url let mut password = "".to_owned(); @@ -79,7 +79,7 @@ pub async fn watch() -> anyhow::Result<()> { let health_check_database_url = format!( "{}/{}", - database_url, context.cluster.values.health_check.database + database_url, context.cluster.values.config.database ); let patch_params = PatchParams::default(); @@ -91,13 +91,13 @@ pub async fn watch() -> anyhow::Result<()> { let database_connection_unwrapped = database_connection.unwrap(); let create_health_check_database = sql_query(format!( "CREATE DATABASE {}", - context.cluster.values.health_check.database + context.cluster.values.config.database )) .execute(&database_connection_unwrapped); if create_health_check_database.is_ok() { info!( "database {} created", - context.cluster.values.health_check.database + context.cluster.values.config.database ) } else { error!("{}", create_health_check_database.err().unwrap()) diff --git a/src/structs.rs b/src/structs.rs index c000101..360d1cc 100644 --- a/src/structs.rs +++ b/src/structs.rs @@ -118,6 +118,7 @@ pub struct Values { #[derive(Debug, Gtmpl, Clone, PartialEq, Serialize, Deserialize)] pub struct Config { append: ConfigAppend, + pub database: String, log_level: String, managers_port: u16, pub postgres_port: u16, @@ -181,7 +182,6 @@ pub struct ExtraLabels { #[derive(Debug, Gtmpl, Clone, PartialEq, Serialize, Deserialize)] pub struct HealthCheck { pub enabled: bool, - pub database: String, pub user: String, } diff --git a/templates/3_config_map/envs.tpl b/templates/3_config_map/envs.tpl index 289d982..734a94a 100644 --- a/templates/3_config_map/envs.tpl +++ b/templates/3_config_map/envs.tpl @@ -18,6 +18,9 @@ data: # db user. PGUSER: "{{ .cluster.values.config.postgres_user }}" + # The operator database + OPERATOR_DATABASE: "{{ .cluster.values.config.postgres_user }}" + # The mount path to the secret for passwords PASSWORD_SECRET_MOUNT_PATH: "{{ .cluster.values.security.password.mount_path }}" diff --git a/yaml_structs/postgres-xl-cluster.yaml b/yaml_structs/postgres-xl-cluster.yaml index 469babc..4c055e2 100644 --- a/yaml_structs/postgres-xl-cluster.yaml +++ b/yaml_structs/postgres-xl-cluster.yaml @@ -29,6 +29,9 @@ config: postgres_port: 5432 # the root user. postgres_user: postgres + # This is the the operator database that will be automatically created if needed. + # It is used for state of extra init scripts and health checks if enabled. + database: "pgxlo" # Enables a Pgbouncer container in the coordinator pods connected by unix socket. connection_pool: @@ -52,8 +55,6 @@ homedir: /var/lib/postgresql health_check: enabled: false - # This is the the health check database that will be automatically created. - database: "health_check" # This is the the health check user that will be automatically created. user: health_check