feat(mock_val): common_core_lib — mode-aware default config path helper

Add common_core_default_config_path() which resolves the correct knoe.cfg
path (k3d/k3s/gke) from KNOE_MODE/DEPLOYMENT_MODE/CLUSTER_ENV env vars,
falling back to the first cfg file found under conf/. Replaces the
hardcoded knoe.cfg reference in the config-loading comment.

Co-authored-by: Junie <junie@jetbrains.com>
This commit is contained in:
chrisfu 2026-05-23 21:32:23 -07:00
parent 73dce037f3
commit d806905b2e

View File

@ -1,13 +1,44 @@
#!/usr/bin/env bash
# Shared helpers for common core init scripts (ArgoCD, OpenBao, OpenTofu, Garage).
# Responsibilities:
# - handle [-c|--config] early so knoe_cfg.sh loads the right knoe.cfg
# - handle [-c|--config] early so knoe_cfg.sh loads the right config file
# - parse standard actions/options
# - resolve and apply namespaces consistently
# Actions supported by all common core scripts
COMMON_CORE_ACTIONS="start|stop|status|restart|initialize|update|reload"
common_core_default_config_path() {
local script_dir="${1:-}"
local mode_hint="${KNOE_MODE:-${DEPLOYMENT_MODE:-${CLUSTER_ENV:-}}}"
local mode="${mode_hint,,}"
if declare -F knoe_normalize_mode >/dev/null 2>&1; then
mode="$(knoe_normalize_mode "$mode_hint")"
fi
case "$mode" in
k3d)
[[ -f "$script_dir/../conf/k3d.cfg" ]] && { printf '%s' "$script_dir/../conf/k3d.cfg"; return 0; }
;;
k3s)
[[ -f "$script_dir/../conf/k3s.cfg" ]] && { printf '%s' "$script_dir/../conf/k3s.cfg"; return 0; }
;;
k8s)
[[ -f "$script_dir/../conf/gke.cfg" ]] && { printf '%s' "$script_dir/../conf/gke.cfg"; return 0; }
;;
esac
local cfg
for cfg in k3d.cfg k3s.cfg gke.cfg knoe.cfg; do
if [[ -f "$script_dir/../conf/$cfg" ]]; then
printf '%s' "$script_dir/../conf/$cfg"
return 0
fi
done
return 1
}
# Parse -c/--config before loading knoe_cfg.sh so KNOE_CONF is set in time.
# Sets:
# COMMON_CORE_CONFIG_PATH - path to knoe.cfg (if provided)
@ -42,8 +73,17 @@ common_core_preparse_config() {
fi
local cfg_dir
cfg_dir=$(cd "$(dirname "$COMMON_CORE_CONFIG_PATH")" && pwd)
KNOE_CONF="$cfg_dir"
export KNOE_CONF
# Preserve an already-set KNOE_CONF when it is a more-specific env subdir of
# cfg_dir (e.g. conf/service/ vs the injected-default conf/). This allows the
# parent shell / Python milestone to pass the correct env-specific conf dir and
# have it survive even when init scripts inject a generic fallback -c path.
local _current_conf="${KNOE_CONF:-}"
if [[ -n "$_current_conf" && "$_current_conf" == "${cfg_dir}/"* && ( -f "$_current_conf/k3d.cfg" || -f "$_current_conf/k3s.cfg" || -f "$_current_conf/gke.cfg" || -f "$_current_conf/knoe.cfg" ) ]]; then
: # keep inherited KNOE_CONF — it is already more specific than cfg_dir
else
KNOE_CONF="$cfg_dir"
export KNOE_CONF
fi
fi
}
@ -108,7 +148,7 @@ common_core_parse_args() {
common_core_usage() {
local prog="${1:-$0}"
echo "Usage: ${prog##*/} [${COMMON_CORE_ACTIONS//|/|}] [-n namespace] [--mode k3d|k3s|k8s] [-c conf/knoe.cfg]" >&2
echo "Usage: ${prog##*/} [${COMMON_CORE_ACTIONS//|/|}] [-n namespace] [--mode k3d|k3s|k8s] [-c conf/{k3d|k3s|gke}.cfg]" >&2
}
# Resolve namespace with precedence: CLI override -> SERVICE_NAMESPACE -> NAMESPACE -> provided default
@ -121,7 +161,7 @@ common_core_resolve_namespace() {
# In k3s mode, common core services must not default into the `default` namespace.
# If the caller didn't supply an explicit namespace, normalize `default` -> SERVICE_NAMESPACE
# from knoe.cfg (Explicit `-n default` is treated as a legacy/mistake and will be corrected).
# from config (Explicit `-n default` is treated as a legacy/mistake and will be corrected).
local mode="${KNOE_MODE:-}"
if declare -F knoe_normalize_mode >/dev/null 2>&1; then
mode="$(knoe_normalize_mode "$mode")"