Skip to content

Commit

Permalink
bin: use global variable for cluster
Browse files Browse the repository at this point in the history
  • Loading branch information
anders-elastisys committed Oct 18, 2023
1 parent 3047d2f commit 4e1eb05
Show file tree
Hide file tree
Showing 13 changed files with 43 additions and 69 deletions.
27 changes: 12 additions & 15 deletions bin/upgrade.bash
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ ROOT="$(readlink -f "${here}/../")"

CK8S_STACK="$(basename "$0")"
export CK8S_STACK
export CK8S_CLUSTER="${1}"

# shellcheck source=scripts/migration/lib.sh
CK8S_ROOT_SCRIPT="true" source "${ROOT}/scripts/migration/lib.sh"
Expand Down Expand Up @@ -45,7 +46,6 @@ snippets_check() {
}

prepare() {
local cluster="${1}"
local snippets
snippets="$(snippets_list prepare)"

Expand All @@ -57,30 +57,28 @@ prepare() {
fi

log_info "prepare snippet \"${snippet##"${ROOT}/migration/"}\":"
if "${snippet}" "${cluster}"; then
if "${snippet}"; then
log_info "prepare snippet success\n---"
else
log_fatal "prepare snippet failure"
fi
done

config_validate secrets
if [[ "${cluster:-}" =~ ^(sc|both)$ ]]; then
if [[ "${CK8S_CLUSTER:-}" =~ ^(sc|both)$ ]]; then
config_validate sc
fi
if [[ "${cluster:-}" =~ ^(wc|both)$ ]]; then
if [[ "${CK8S_CLUSTER:-}" =~ ^(wc|both)$ ]]; then
config_validate wc
fi
}

apply() {
local cluster="${1}"

config_validate secrets
if [[ "${cluster:-}" =~ ^(sc|both)$ ]]; then
if [[ "${CK8S_CLUSTER:-}" =~ ^(sc|both)$ ]]; then
config_validate sc
fi
if [[ "${cluster:-}" =~ ^(wc|both)$ ]]; then
if [[ "${CK8S_CLUSTER:-}" =~ ^(wc|both)$ ]]; then
config_validate wc
fi

Expand All @@ -100,7 +98,7 @@ apply() {
fi

log_info "apply snippet \"${snippet##"${ROOT}/migration/"}\":"
if "${snippet}" execute "${cluster}"; then
if "${snippet}" execute; then
log_info "apply snippet success\n---"
else
local return="${?}"
Expand All @@ -110,7 +108,7 @@ apply() {
log_warn "apply snippet execute rollback"
fi

if "${snippet}" rollback "${cluster}"; then
if "${snippet}" rollback; then
log_warn "apply snippet rollback success"
exit $(( return + 2 )) # 3 on rollback failure/success 4 on rollback success/success
else
Expand All @@ -136,7 +134,6 @@ usage() {
}

main() {
local cluster="${1}"
local version="${2}"
local action="${3}"

Expand All @@ -151,22 +148,22 @@ main() {
exit 1
fi

export CK8S_CLUSTER="${cluster}"

export CK8S_TARGET_VERSION="${version}"
export CK8S_STACK="${version}/${action}"

check_config

if [[ "${cluster:-}" =~ ^(sc|both)$ ]]; then
if [[ "${CK8S_CLUSTER:-}" =~ ^(sc|both)$ ]]; then
config_load "sc"
check_version "sc" "${action}"
fi
if [[ "${cluster:-}" =~ ^(wc|both)$ ]]; then
if [[ "${CK8S_CLUSTER:-}" =~ ^(wc|both)$ ]]; then
config_load "wc"
check_version "wc" "${action}"
fi

"${action}" "${cluster}"
"${action}"

log_info "${action} complete"
}
Expand Down
10 changes: 4 additions & 6 deletions migration/template/apply/00-template.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,27 +44,25 @@ source "${ROOT}/scripts/migration/lib.sh"
# - helmfile_upgrade <sc|wc> <selectors...>

run() {
local cluster="${2:-both}"

case "${1:-}" in
execute)
# Note: 00-template.sh will be skipped by the upgrade command
log_info "no operation: this is a template"

if [[ "${cluster:-}" =~ ^(sc|both)$ ]]; then
if [[ "${CK8S_CLUSTER:-}" =~ ^(sc|both)$ ]]; then
log_info "operation on service cluster"
fi
if [[ "${cluster:-}" =~ ^(wc|both)$ ]]; then
if [[ "${CK8S_CLUSTER:-}" =~ ^(wc|both)$ ]]; then
log_info "operation on workload cluster"
fi
;;
rollback)
log_warn "rollback not implemented"

# if [[ "${cluster:-}" =~ ^(sc|both)$ ]]; then
# if [[ "${CK8S_CLUSTER:-}" =~ ^(sc|both)$ ]]; then
# log_info "rollback operation on service cluster"
# fi
# if [[ "${cluster:-}" =~ ^(wc|both)$ ]]; then
# if [[ "${CK8S_CLUSTER:-}" =~ ^(wc|both)$ ]]; then
# log_info "rollback operation on workload cluster"
# fi
;;
Expand Down
6 changes: 2 additions & 4 deletions migration/template/apply/20-bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ ROOT="$(readlink -f "$(dirname "${0}")/../../../")"
source "${ROOT}/scripts/migration/lib.sh"

run() {
local cluster="${2:-both}"

case "${1:-}" in
execute)
if [[ "${cluster:-}" =~ ^(sc|both)$ ]]; then
if [[ "${CK8S_CLUSTER:-}" =~ ^(sc|both)$ ]]; then
"${ROOT}/bin/ck8s" bootstrap sc
fi
if [[ "${cluster:-}" =~ ^(wc|both)$ ]]; then
if [[ "${CK8S_CLUSTER:-}" =~ ^(wc|both)$ ]]; then
"${ROOT}/bin/ck8s" bootstrap wc
fi
;;
Expand Down
10 changes: 4 additions & 6 deletions migration/template/apply/80-apply.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,18 @@ skipped_wc=(
)

run() {
local cluster="${2-both}"

case "${1:-}" in
execute)
local -a filters
local selector

if [[ "${cluster:-}" =~ ^(sc|both)$ ]]; then
if [[ "${CK8S_CLUSTER:-}" =~ ^(sc|both)$ ]]; then
filters=("${skipped[@]}" "${skipped_sc[@]}")
selector="${filters[*]:-"app!=null"}"
helmfile_upgrade sc "${selector// /,}"
fi

if [[ "${cluster:-}" =~ ^(wc|both)$ ]]; then
if [[ "${CK8S_CLUSTER:-}" =~ ^(wc|both)$ ]]; then
filters=("${skipped[@]}" "${skipped_wc[@]}")
selector="${filters[*]:-"app!=null"}"
helmfile_upgrade wc "${selector// /,}"
Expand All @@ -41,10 +39,10 @@ run() {
rollback)
log_warn "rollback not implemented"

# if [[ "${cluster:-}" =~ ^(sc|both)$ ]]; then
# if [[ "${CK8S_CLUSTER:-}" =~ ^(sc|both)$ ]]; then
# log_info "rollback operation on service cluster"
# fi
# if [[ "${cluster:-}" =~ ^(wc|both)$ ]]; then
# if [[ "${CK8S_CLUSTER:-}" =~ ^(wc|both)$ ]]; then
# log_info "rollback operation on workload cluster"
# fi
;;
Expand Down
6 changes: 2 additions & 4 deletions migration/template/prepare/00-template.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,9 @@ source "${ROOT}/scripts/migration/lib.sh"
# Note: 00-template.sh will be skipped by the upgrade command
log_info "no operation: this is a template"

cluster="${1:-both}"

if [[ "${cluster:-}" =~ ^(sc|both)$ ]]; then
if [[ "${CK8S_CLUSTER:-}" =~ ^(sc|both)$ ]]; then
log_info "operation on service cluster"
fi
if [[ "${cluster:-}" =~ ^(wc|both)$ ]]; then
if [[ "${CK8S_CLUSTER:-}" =~ ^(wc|both)$ ]]; then
log_info "operation on workload cluster"
fi
10 changes: 4 additions & 6 deletions migration/v0.34/apply/00-template.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,27 +44,25 @@ source "${ROOT}/scripts/migration/lib.sh"
# - helmfile_upgrade <sc|wc> <selectors...>

run() {
local cluster="${2:-both}"

case "${1:-}" in
execute)
# Note: 00-template.sh will be skipped by the upgrade command
log_info "no operation: this is a template"

if [[ "${cluster:-}" =~ ^(sc|both)$ ]]; then
if [[ "${CK8S_CLUSTER:-}" =~ ^(sc|both)$ ]]; then
log_info "operation on service cluster"
fi
if [[ "${cluster:-}" =~ ^(wc|both)$ ]]; then
if [[ "${CK8S_CLUSTER:-}" =~ ^(wc|both)$ ]]; then
log_info "operation on workload cluster"
fi
;;
rollback)
log_warn "rollback not implemented"

# if [[ "${cluster:-}" =~ ^(sc|both)$ ]]; then
# if [[ "${CK8S_CLUSTER:-}" =~ ^(sc|both)$ ]]; then
# log_info "rollback operation on service cluster"
# fi
# if [[ "${cluster:-}" =~ ^(wc|both)$ ]]; then
# if [[ "${CK8S_CLUSTER:-}" =~ ^(wc|both)$ ]]; then
# log_info "rollback operation on workload cluster"
# fi
;;
Expand Down
10 changes: 4 additions & 6 deletions migration/v0.34/apply/11-prometheus-operator-crds.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ ROOT="$(readlink -f "$(dirname "${0}")/../../../")"
source "${ROOT}/scripts/migration/lib.sh"

run() {
local cluster="${2:-both}"

case "${1:-}" in
execute)
clusters=("sc" "wc")
if [[ ! "${cluster}" = both ]]; then
clusters=("${cluster}")
if [[ ! "${CK8S_CLUSTER}" = both ]]; then
clusters=("${CK8S_CLUSTER}")
fi

for CLUSTER in "${clusters[@]}"; do
Expand All @@ -35,8 +33,8 @@ run() {
;;
rollback)
clusters=("sc" "wc")
if [[ ! "${2}" = both ]]; then
clusters=("${2}")
if [[ ! "${CK8S_CLUSTER}" = both ]]; then
clusters=("${CK8S_CLUSTER}")
fi

for CLUSTER in "${clusters[@]}"; do
Expand Down
6 changes: 2 additions & 4 deletions migration/v0.34/apply/20-bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@ ROOT="$(readlink -f "$(dirname "${0}")/../../../")"
source "${ROOT}/scripts/migration/lib.sh"

run() {
local cluster="${2:-both}"

case "${1:-}" in
execute)

if [[ "${cluster:-}" =~ ^(sc|both)$ ]]; then
if [[ "${CK8S_CLUSTER:-}" =~ ^(sc|both)$ ]]; then
"${ROOT}/bin/ck8s" bootstrap sc
fi
if [[ "${cluster:-}" =~ ^(wc|both)$ ]]; then
if [[ "${CK8S_CLUSTER:-}" =~ ^(wc|both)$ ]]; then
"${ROOT}/bin/ck8s" bootstrap wc
fi
;;
Expand Down
4 changes: 1 addition & 3 deletions migration/v0.34/apply/30-delete-obsolete-ns.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ ROOT="$(readlink -f "$(dirname "${0}")/../../../")"
source "${ROOT}/scripts/migration/lib.sh"

run() {
local cluster="${2:-both}"

case "${1:-}" in
execute)

if [[ "${cluster:-}" =~ ^(sc|both)$ ]]; then
if [[ "${CK8S_CLUSTER:-}" =~ ^(sc|both)$ ]]; then
for NS in elastic-system influxdb-prometheus; do
if kubectl_do sc get namespace $NS >/dev/null 2>/dev/null; then
log_info " - deleting unused namespace $NS in sc"
Expand Down
6 changes: 2 additions & 4 deletions migration/v0.34/apply/80-apply.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,18 @@ skipped_wc=(
)

run() {
local cluster="${2:-both}"

case "${1:-}" in
execute)
local -a filters
local selector

if [[ "${cluster:-}" =~ ^(sc|both)$ ]]; then
if [[ "${CK8S_CLUSTER:-}" =~ ^(sc|both)$ ]]; then
filters=("${skipped[@]}" "${skipped_sc[@]}")
selector="${filters[*]:-"app!=null"}"
helmfile_upgrade sc "${selector// /,}"
fi

if [[ "${cluster:-}" =~ ^(wc|both)$ ]]; then
if [[ "${CK8S_CLUSTER:-}" =~ ^(wc|both)$ ]]; then
filters=("${skipped[@]}" "${skipped_wc[@]}")
selector="${filters[*]:-"app!=null"}"
helmfile_upgrade wc "${selector// /,}"
Expand Down
6 changes: 2 additions & 4 deletions migration/v0.34/prepare/00-template.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,9 @@ source "${ROOT}/scripts/migration/lib.sh"
# Note: 00-template.sh will be skipped by the upgrade command
log_info "no operation: this is a template"

cluster="${1:-both}"

if [[ "${cluster:-}" =~ ^(sc|both)$ ]]; then
if [[ "${CK8S_CLUSTER:-}" =~ ^(sc|both)$ ]]; then
log_info "operation on service cluster"
fi
if [[ "${cluster:-}" =~ ^(wc|both)$ ]]; then
if [[ "${CK8S_CLUSTER:-}" =~ ^(wc|both)$ ]]; then
log_info "operation on workload cluster"
fi
4 changes: 1 addition & 3 deletions migration/v0.34/prepare/30-delete-obsolete-ns.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ ROOT="$(readlink -f "${HERE}/../../../")"
# shellcheck source=scripts/migration/lib.sh
source "${ROOT}/scripts/migration/lib.sh"

cluster="${1:-both}"

if [[ "${cluster:-}" =~ ^(sc|both)$ ]]; then
if [[ "${CK8S_CLUSTER:-}" =~ ^(sc|both)$ ]]; then
for NS in elastic-system influxdb-prometheus; do
log_info "checking for resources in namespace $NS"
NS_RESOURCES="$(kubectl_do sc get all -n $NS 2>&1)"
Expand Down
7 changes: 3 additions & 4 deletions scripts/migration/lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -198,17 +198,16 @@ check_config() {
log_info "using config path: \"${CK8S_CONFIG_PATH}\""

local pass="true"
local cluster="${CK8S_CLUSTER}"

if [[ "${cluster:-}" =~ ^(sc|both)$ ]]; then
if [[ "${CK8S_CLUSTER:-}" =~ ^(sc|both)$ ]]; then
for FILE in "${SC_CONFIG_FILES[@]}" "$SC_STATE_FILE"; do
if [ ! -f "${CK8S_CONFIG_PATH}/${FILE}" ]; then
log_error "error: \"${FILE}\" is not a file"
pass="false"
fi
done
fi
if [[ "${cluster:-}" =~ ^(wc|both)$ ]]; then
if [[ "${CK8S_CLUSTER:-}" =~ ^(wc|both)$ ]]; then
for FILE in "${WC_CONFIG_FILES[@]}" "$WC_STATE_FILE"; do
if [ ! -f "${CK8S_CONFIG_PATH}/${FILE}" ]; then
log_error "error: \"${FILE}\" is not a file"
Expand All @@ -227,7 +226,7 @@ check_config() {
fi

for prefix in wc sc; do
if [[ "${cluster:-}" =~ ^($prefix|both)$ ]]; then
if [[ "${CK8S_CLUSTER:-}" =~ ^($prefix|both)$ ]]; then
if check_sops "${CK8S_CONFIG_PATH}/.state/kube_config_${prefix}.yaml"; then
CONFIG["${prefix}-kubeconfig"]="encrypted"
else
Expand Down

0 comments on commit 4e1eb05

Please sign in to comment.