From 9999e85a0dac4839ded5077593f62dcc6aab101d Mon Sep 17 00:00:00 2001 From: Moritz Clasmeier Date: Mon, 8 Jul 2024 13:35:50 +0200 Subject: [PATCH] Rework handling of extra kubeconfigs, rework cluster artifact deletion. Incorporate PR feedback --- scripts/dev/infra-sync.sh | 66 ++++++++++++++++++++++++++++----------- 1 file changed, 48 insertions(+), 18 deletions(-) diff --git a/scripts/dev/infra-sync.sh b/scripts/dev/infra-sync.sh index 1b4f518..4dd3624 100755 --- a/scripts/dev/infra-sync.sh +++ b/scripts/dev/infra-sync.sh @@ -3,10 +3,25 @@ set -euo pipefail -infra_dir="$HOME/.kube/infra" -clusters_dir="$infra_dir/clusters" - os=$(uname -o) +infra_dir="${HOME}/.kube/infra" +clusters_dir="${infra_dir}/clusters" +infra_kubeconfig="${INFRA_KUBECONFIG:-${infra_dir}/kubeconfig}" +extra_kubeconfigs="${EXTRA_KUBECONFIGS:-}" + +delete_cluster_artifacts() { + echo "Deleting old cluster artifacts..." + # Just a couple of safety checks to make sure that we are not attempting to delete + # something like `/`. + mkdir -p "${clusters_dir}" + if [[ ( "${#clusters_dir}" -le 1 ) || ( ! -d "${clusters_dir}" ) ]]; then + echo >&2 "The clusters directory '${clusters_dir}' does not seem properly initialized." + exit 1 + fi + # A call of the form `rm -rf some_directory/*` would fail if `some_directory/*` doesn't + # evaluate to anything. + find "${clusters_dir}" -mindepth 1 -maxdepth 1 -type d -exec rm -rf {} + +} export INFRA_TOKEN="${INFRA_TOKEN:-}" if [[ "${INFRA_TOKEN}" == "" ]]; then @@ -30,18 +45,12 @@ if [[ "${INFRA_TOKEN}" == "" ]]; then esac fi -mkdir -p "${clusters_dir}" - echo "Syncing your infra clusters..." - -for cluster_dir in "${clusters_dir}"/*; do - cluster_name=$(basename "${cluster_dir}") - rm -rf "${clusters_dir:?}/$cluster_name" -done +delete_cluster_artifacts # Fetching artifacts for infra clusters. cluster_names="" -if ! cluster_names=$(infractl list --json | jq -r "(.Clusters // [])[].ID"); then +if ! cluster_names=$(infractl list --json | jq -r '(.Clusters // [])[] | select(.Status==2).ID'); then echo >&2 "Failed to retrieve clusters names from infra." exit 1 fi @@ -52,15 +61,36 @@ for cluster_name in ${cluster_names}; do cluster_dir="${clusters_dir}/${cluster_name}" echo "Downloading artifacts for cluster ${cluster_name} into ${clusters_dir}" infractl artifacts "${cluster_name}" --download-dir="${cluster_dir}" >/dev/null - if [ -e "${cluster_dir}/kubeconfig" ]; then - chmod 600 "${cluster_dir}/kubeconfig" - else - echo " Skipping (cluster not ready yet)" - fi + kubeconfig="${cluster_dir}/kubeconfig" + chmod 600 "${kubeconfig}" + echo "Stored kubeconfig at ${kubeconfig}" done if [[ $idx == 0 ]]; then - echo "No clusters found." + echo "No ready clusters found." fi -kubecfg-merge +kubecfg_merge() { + # Backup previous kubeconfig file. + infra_kubeconfig_backup="${infra_kubeconfig}.bkp" + if [[ -e "${infra_kubeconfig}" ]]; then + echo "Backing up previous infra kubeconfig ${infra_kubeconfig} as ${infra_kubeconfig_backup}" + mv "${infra_kubeconfig}" "${infra_kubeconfig_backup}" + fi + + # Merge all the kubeconfigs. + export KUBECONFIG="/dev/null" + for cluster_dir in "${clusters_dir}"/*; do + if [ -e "${cluster_dir}/kubeconfig" ]; then + KUBECONFIG="${KUBECONFIG}:${cluster_dir}/kubeconfig" + fi + done + if [[ -n "${extra_kubeconfigs}" ]]; then + KUBECONFIG="${KUBECONFIG}:${extra_kubeconfigs}" + fi + + echo "Merging kube configurations into ${infra_kubeconfig}" + kubectl config view --flatten > "${infra_kubeconfig}" +} + +kubecfg_merge