From 7509ce3f0670153655197089247456e19b21c87d Mon Sep 17 00:00:00 2001 From: kirinnee Date: Wed, 20 Dec 2023 11:32:17 +0800 Subject: [PATCH] feat: seamless configuration for pulling kubeconfig --- Taskfile.yml | 1 + scripts/local/update-kubectl.sh | 32 ++++++++++++++++++++++++++++++++ tasks/Taskfile.kubectl.yaml | 17 +++++++++++++++++ 3 files changed, 50 insertions(+) create mode 100755 scripts/local/update-kubectl.sh create mode 100644 tasks/Taskfile.kubectl.yaml diff --git a/Taskfile.yml b/Taskfile.yml index abb7ffa..1d9b01d 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -5,6 +5,7 @@ env: includes: util: tasks/Taskfile.util.yaml + kubectl: tasks/Taskfile.kubectl.yaml suicune:opal-ruby: taskfile: tasks/Taskfile.cluster.yaml vars: diff --git a/scripts/local/update-kubectl.sh b/scripts/local/update-kubectl.sh new file mode 100755 index 0000000..ec5b2a2 --- /dev/null +++ b/scripts/local/update-kubectl.sh @@ -0,0 +1,32 @@ +#!/usr/bin/env bash + +set -eou pipefail + +L=$(echo "$LANDSCAPE" | tr '[:upper:]' '[:lower:]') +C=$(echo "$CLUSTER" | tr '[:upper:]' '[:lower:]') + +name="${L}-${C}" + +# wait for virtual cluster to be ready +echo "⌛️ Waiting for cluster '$name' to be ready..." +kubectl --context "${name}" -n pichu wait --for=jsonpath=.status.readyReplicas=1 --timeout=300s deployment "${L}-pichu-iodine" +kubectl --context "${name}" -n pikachu wait --for=jsonpath=.status.readyReplicas=1 --timeout=300s deployment "${L}-pikachu-iodine" +kubectl --context "${name}" -n raichu wait --for=jsonpath=.status.readyReplicas=1 --timeout=300s deployment "${L}-raichu-iodine" + +echo "✅ Virtual clusters are ready!" + +pichu_kube=$(kubectl get secrets --context "${name}" -n pichu "vc-${L}-pichu-iodine" -o jsonpath='{.data.config}' | base64 -d) +pikachu_kube=$(kubectl get secrets --context "${name}" -n pikachu "vc-${L}-pikachu-iodine" -o jsonpath='{.data.config}' | base64 -d) +raichu_kube=$(kubectl get secrets --context "${name}" -n raichu "vc-${L}-raichu-iodine" -o jsonpath='{.data.config}' | base64 -d) + +echo "📝 Writing to '$HOME/.kube/k3dconfigs/pichu-${C}'" +echo "${pichu_kube}" >"$HOME/.kube/k3dconfigs/pichu-${C}" +echo "📝 Writing to '$HOME/.kube/k3dconfigs/pikachu-${C}'" +echo "${pikachu_kube}" >"$HOME/.kube/k3dconfigs/pikachu-${C}" +echo "📝 Writing to '$HOME/.kube/k3dconfigs/raichu-${C}'" +echo "${raichu_kube}" >"$HOME/.kube/k3dconfigs/raichu-${C}" + +echo "🛠️ Attempting to merge kubeconfig cluster '$name'..." +KUBECONFIG=$(cd ~/.kube/configs && find "$(pwd)"/* | awk 'ORS=":"')$(cd ~/.kube/k3dconfigs && find "$(pwd)"/* | awk 'ORS=":"') kubectl config view --flatten >~/.kube/config +chmod 600 ~/.kube/config +echo "✅ Config is merged!" diff --git a/tasks/Taskfile.kubectl.yaml b/tasks/Taskfile.kubectl.yaml new file mode 100644 index 0000000..4fa60f5 --- /dev/null +++ b/tasks/Taskfile.kubectl.yaml @@ -0,0 +1,17 @@ +version: "3" + +tasks: + entei:opal: + desc: Obtain Entei Opal Virtual Cluster KubeConfigs + env: + CLUSTER: opal + LANDSCAPE: entei + cmds: + - ./scripts/local/update-kubectl.sh + entei:ruby: + desc: Obtain Entei Ruby Virtual Cluster KubeConfigs + env: + CLUSTER: ruby + LANDSCAPE: entei + cmds: + - ./scripts/local/update-kubectl.sh