Skip to content

Instantly share code, notes, and snippets.

@leonmax
Last active November 14, 2022 03:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leonmax/c0d71980cf6d782e67afa71c3ae1559a to your computer and use it in GitHub Desktop.
Save leonmax/c0d71980cf6d782e67afa71c3ae1559a to your computer and use it in GitHub Desktop.
import each aliyun ACK cluster into it's own kubeconfig
#!/usr/bin/env bash
CMD=`basename "$0"`
if [ -z $1 ] ; then
echo "Usage:"
echo " $CMD cluster_name [save_to_path]"
echo "To list cluster names:"
echo " aliyun cs DescribeClusters | jq -r '.[].name'"
echo "Dependencies:"
echo " - aliyun-cli"
echo " - jq"
echo " - kubernetes-cli"
echo " - python-yq"
exit 1;
fi
CLUSTER_NAME=$1
mkdir -p ~/.kube/conf.d/
SAVE_TO_PATH=${2:-~/.kube/conf.d/$CLUSTER_NAME}
CLUSTER_ID=`aliyun cs DescribeClusters --name $CLUSTER_NAME | jq -r .[].cluster_id`
aliyun cs DescribeClusterUserKubeconfig --ClusterId $CLUSTER_ID | jq -r .config | yq -y \
--arg cluster "$CLUSTER_NAME" '
.clusters[0].name = $cluster |
.contexts[0].context.cluster = $cluster |
.contexts[0].name = $cluster |
."current-context" = $cluster
' > $SAVE_TO_PATH
echo $SAVE_TO_PATH

Put the following line to your .bashrc/.zshrc

export KUBECONFIG=`find ~/.kube -type f -path "*conf.d/*" -or -path '*config' | paste -sd: -`

and then put the import-ali-kubeconfig to somewhere in your PATH, and make it chmod +x import-ali-kubeconfig

find your cluster name:

aliyun cs DescribeClusters | jq -r '.[].name'"

then import your cluster

import-ali-kubeconfig [CLUSTER-NAME]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment