From eb990cb34209dc6e31880488750f78a1a8e22c12 Mon Sep 17 00:00:00 2001 From: Titan Lien <6195019+titanlien@users.noreply.github.com> Date: Fri, 9 Jan 2026 16:08:51 +0100 Subject: [PATCH] Add k3d tools as an option to create local cluster --- local-setup/scripts/check-environment.sh | 42 ++++++++++++++++++++++++ local-setup/scripts/start.sh | 8 +++-- 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/local-setup/scripts/check-environment.sh b/local-setup/scripts/check-environment.sh index ff7caa7d..3941e26e 100755 --- a/local-setup/scripts/check-environment.sh +++ b/local-setup/scripts/check-environment.sh @@ -7,6 +7,37 @@ COL='\033[92m' RED='\033[91m' COL_RES='\033[0m' +check_k3d_cluster() { + # Check if k3d is installed + # k3d is an alternative to kind for running local Kubernetes clusters + # Reference: https://github.com/platform-mesh/helm-charts/commit/b082f0cde7b9d7d4242aaeb3e9dba03b0e4fdbad + # Reference: https://github.com/platform-mesh/helm-charts/commit/ae2be316be7d3d49d8bd7bd781cb82bceb5b6c29 + if ! command -v k3d &> /dev/null; then + return 1 # k3d not installed, can't check for k3d clusters + fi + + # Check if any k3d cluster is running + local k3d_clusters=$(k3d cluster list 2>/dev/null | grep -v "NAME" | awk '{print $1}') + + if [ -n "$k3d_clusters" ]; then + local first_cluster=$(echo "$k3d_clusters" | head -n 1) + echo -e "${COL}[$(date '+%H:%M:%S')] k3d cluster '$first_cluster' detected, bypassing kind cluster creation ${COL_RES}" + + # Export kubeconfig for the k3d cluster + k3d kubeconfig merge "$first_cluster" --kubeconfig-switch-context > /dev/null 2>&1 + + if [ $? -eq 0 ]; then + echo -e "${COL}[$(date '+%H:%M:%S')] Using k3d cluster: $first_cluster ${COL_RES}" + return 0 # Return 0 to indicate cluster exists + else + echo -e "${YELLOW}[$(date '+%H:%M:%S')] Warning: Failed to export k3d kubeconfig, will attempt to use kind ${COL_RES}" + return 1 + fi + fi + + return 1 # Return 1 to indicate no k3d cluster exists +} + check_kind_cluster() { # Check if kind cluster is already running if [ $(kind get clusters 2>/dev/null | grep -c platform-mesh) -gt 0 ]; then @@ -18,6 +49,16 @@ check_kind_cluster() { } check_kind_dependency() { + # Check if k3d is available and has clusters - if so, we don't need kind + if command -v k3d &> /dev/null; then + local k3d_clusters=$(k3d cluster list 2>/dev/null | grep -v "NAME" | awk '{print $1}') + if [ -n "$k3d_clusters" ]; then + echo -e "${COL}[$(date '+%H:%M:%S')] ✅ k3d is available with existing clusters, skipping kind dependency check${COL_RES}" + return 0 + fi + fi + + # If k3d is not available or has no clusters, kind is required if ! command -v kind &> /dev/null; then echo -e "${RED}❌ Error: 'kind' (Kubernetes in Docker) is not installed${COL_RES}" echo -e "${COL}📦 Kind is required to create local Kubernetes clusters.${COL_RES}" @@ -199,6 +240,7 @@ run_environment_checks() { } # Export functions so they can be used by the main script +export -f check_k3d_cluster export -f check_kind_cluster export -f check_kind_dependency export -f check_docker_dependency diff --git a/local-setup/scripts/start.sh b/local-setup/scripts/start.sh index d001bc75..6e0bfb0b 100755 --- a/local-setup/scripts/start.sh +++ b/local-setup/scripts/start.sh @@ -57,8 +57,12 @@ if [ "$CACHED" = true ]; then setup_registry_proxies fi -# Check if kind cluster is already running, if not create it -if ! check_kind_cluster; then +# Check if k3d cluster exists first, then check kind cluster, if neither exists create kind +USE_K3D_CLUSTER=false +if check_k3d_cluster; then + echo -e "${COL}[$(date '+%H:%M:%S')] Using existing k3d cluster, bypassing kind cluster creation ${COL_RES}" + USE_K3D_CLUSTER=true +elif ! check_kind_cluster; then if [ -d "$SCRIPT_DIR/certs" ]; then echo -e "${COL}[$(date '+%H:%M:%S')] Clearing existing certs directory ${COL_RES}" rm -rf "$SCRIPT_DIR/certs"