diff --git a/monitoring/setup.sh b/monitoring/setup.sh index 2fd65b3..ae4f334 100755 --- a/monitoring/setup.sh +++ b/monitoring/setup.sh @@ -28,15 +28,7 @@ source $(git rev-parse --show-toplevel)/scripts/common.sh # --- Main Logic --- # Determine regions from arguments, or auto-detect if none are provided -REGIONS=() -if [ $# -gt 0 ]; then - REGIONS=("$@") - echo "🎯 Targeting specified regions for monitoring setup: ${REGIONS[*]}" -else - echo "🔎 Auto-detecting all active playground regions for monitoring setup..." - # The '|| true' prevents the script from exiting if grep finds no matches. - REGIONS=($(kind get clusters | grep "^${K8S_BASE_NAME}-" | sed "s/^${K8S_BASE_NAME}-//" || true)) -fi +detect_running_regions "$@" # Add a target port for the port-forward, the port will be incremeted by 1 for each region port=3000 diff --git a/scripts/common.sh b/scripts/common.sh index 01f0bdb..b0ce94f 100644 --- a/scripts/common.sh +++ b/scripts/common.sh @@ -59,3 +59,6 @@ fi # Determine project root and kubeconfig path GIT_REPO_ROOT=$(git rev-parse --show-toplevel) KUBE_CONFIG_PATH="${GIT_REPO_ROOT}/k8s/kube-config.yaml" + +# source funcs_regions.sh +source $(git rev-parse --show-toplevel)/scripts/funcs_regions.sh diff --git a/scripts/funcs_regions.sh b/scripts/funcs_regions.sh new file mode 100755 index 0000000..a4c05d6 --- /dev/null +++ b/scripts/funcs_regions.sh @@ -0,0 +1,55 @@ +#!/usr/bin/env bash +# +# This script contains the functions used to set the ${REGIONS} variable. +# set_regions() --> if called without an argument, EU and US are set +# otherwise the arguments. +# +# detect_running_regions() --> if called without an argument, the running +# CNPG-Playground Kind clusters regions are set +# otherwise the arguments. +# +# +# Copyright The CloudNativePG Contributors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +set -euo pipefail + +# --- Set regions --- +set_regions() { + if [ $# -eq 0 ]; then + REGIONS=("eu" "us") + echo "❌ No region provided, using the default regions "eu" "us"..." + else + REGIONS=("$@") + echo "🔎 Using the provided regions: ${REGIONS[*]}" + fi +} + +# --- detect regions --- +detect_running_regions() { + if [ $# -gt 0 ]; then + REGIONS=("$@") + echo "🎯 Targeting specified regions: ${REGIONS[*]}" + else + echo "🔎 Auto-detecting all active playground regions..." + # The '|| true' prevents the script from exiting if grep finds no matches. + REGIONS=($(kind get clusters | grep "^${K8S_BASE_NAME}-" | sed "s/^${K8S_BASE_NAME}-//" || true)) + if [ ${#REGIONS[@]} -gt 0 ]; then + echo "✅ Found regions: ${REGIONS[*]}" + else + echo "✅ No region detected" + fi + fi +} diff --git a/scripts/info.sh b/scripts/info.sh index 2df068c..9145683 100755 --- a/scripts/info.sh +++ b/scripts/info.sh @@ -30,14 +30,7 @@ fi export KUBECONFIG="${KUBE_CONFIG_PATH}" # --- Auto-detect Regions --- -echo "🔎 Detecting active playground clusters..." -REGIONS=($(kind get clusters | grep "^${K8S_BASE_NAME}-" | sed "s/^${K8S_BASE_NAME}-//" || true)) - -if [ ${#REGIONS[@]} -eq 0 ]; then - echo "🤷 No active playground clusters found with the prefix '${K8S_BASE_NAME}-'." - exit 0 -fi -echo "✅ Found regions: ${REGIONS[*]}" +detect_running_regions # --- Access Instructions --- echo diff --git a/scripts/setup.sh b/scripts/setup.sh index db2bf91..8b84876 100755 --- a/scripts/setup.sh +++ b/scripts/setup.sh @@ -53,10 +53,7 @@ echo # --- Script Setup --- # Determine regions from arguments, or use defaults -REGIONS=("$@") -if [ ${#REGIONS[@]} -eq 0 ]; then - REGIONS=("eu" "us") -fi +set_regions "$@" # Setup a single, shared Kubeconfig for all clusters export KUBECONFIG="${KUBE_CONFIG_PATH}" diff --git a/scripts/teardown.sh b/scripts/teardown.sh index 8d80148..59b653f 100755 --- a/scripts/teardown.sh +++ b/scripts/teardown.sh @@ -23,22 +23,7 @@ source "$(dirname "$0")/common.sh" # --- Main Logic --- # Determine regions from arguments, or auto-detect if none are provided -REGIONS=() -if [ $# -gt 0 ]; then - REGIONS=("$@") - echo "🎯 Targeting specified regions for teardown: ${REGIONS[*]}" -else - echo "🔎 Auto-detecting all active playground regions for teardown..." - # The '|| true' prevents the script from exiting if grep finds no matches. - REGIONS=($(kind get clusters | grep "^${K8S_BASE_NAME}-" | sed "s/^${K8S_BASE_NAME}-//" || true)) -fi - -if [ ${#REGIONS[@]} -eq 0 ]; then - echo "🤷 No regions found to tear down. Exiting." - exit 0 -fi - -echo "🔥 Tearing down regions: ${REGIONS[*]}" +detect_running_regions "$@" for region in "${REGIONS[@]}"; do K8S_CLUSTER_NAME="${K8S_BASE_NAME}-${region}"