Skip to content
This repository has been archived by the owner on Aug 30, 2024. It is now read-only.

Commit

Permalink
cleanup and use one database for health checks and init of job
Browse files Browse the repository at this point in the history
  • Loading branch information
sstubbs committed Mar 5, 2020
1 parent fdfcbc0 commit 17e58dd
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 16 deletions.
2 changes: 1 addition & 1 deletion scripts/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
17 changes: 9 additions & 8 deletions scripts/job_on_load
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -45,22 +45,23 @@ 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"

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)
Expand All @@ -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 $?
Expand Down
8 changes: 4 additions & 4 deletions src/health_checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand All @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion src/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
}

Expand Down
3 changes: 3 additions & 0 deletions templates/3_config_map/envs.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}"

Expand Down
5 changes: 3 additions & 2 deletions yaml_structs/postgres-xl-cluster.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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

Expand Down

0 comments on commit 17e58dd

Please sign in to comment.