Skip to content

Commit

Permalink
Rework handling of extra kubeconfigs, rework cluster artifact deletio…
Browse files Browse the repository at this point in the history
…n. Incorporate PR feedback
  • Loading branch information
Moritz Clasmeier committed Jul 8, 2024
1 parent 5b78dea commit 9999e85
Showing 1 changed file with 48 additions and 18 deletions.
66 changes: 48 additions & 18 deletions scripts/dev/infra-sync.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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

0 comments on commit 9999e85

Please sign in to comment.