Skip to content

Instantly share code, notes, and snippets.

@sbueringer
Last active February 11, 2022 14:34
Show Gist options
  • Save sbueringer/1c64884ef168ad4e6c20bf34a9f6bdbf to your computer and use it in GitHub Desktop.
Save sbueringer/1c64884ef168ad4e6c20bf34a9f6bdbf to your computer and use it in GitHub Desktop.
Shell functions to download/switch kubectl/kind/clusterctl and import kubeconfigs for kind clusters
# clusterctl
clusterctl_download(){
VERSION=$1
BIN_FOLDER=${2:-~/bin}
mkdir -p "$BIN_FOLDER"
cd "$BIN_FOLDER" || exit
if [ ! -f "${BIN_FOLDER}"/clusterctl-"${VERSION}" ];
then
echo "downloading clusterctl-${VERSION}"
curl -L --url https://github.com/kubernetes-sigs/cluster-api/releases/download/"${VERSION}"/clusterctl-linux-amd64 --output "${BIN_FOLDER}"/clusterctl-"${VERSION}"
chmod +x "${BIN_FOLDER}"/clusterctl-"${VERSION}"
else
echo "clusterctl-${VERSION} already exists"
fi
clusterctl_switch "${VERSION}" "${BIN_FOLDER}"
}
_clusterctl_download(){
local cur
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
kubernetes_versions=$(curl -s https://api.github.com/repos/kubernetes-sigs/cluster-api/releases | jq '.[].name' -r)
COMPREPLY=( "$(compgen -W "${kubernetes_versions}" -- "${cur}")" )
return 0
}
complete -F _clusterctl_download clusterctl_download
clusterctl_switch(){
VERSION=$1
BIN_FOLDER=${2:-~/bin}
echo "Switching to clusterctl-${VERSION}"
ln -fs "${BIN_FOLDER}"/clusterctl-"${VERSION}" "${BIN_FOLDER}"/clusterctl
}
_clusterctl_switch(){
local cur
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
kubernetes_versions=$(cd "${BIN_FOLDER}" || exit; find clusterctl-* -maxdepth 1 | cut -d'-' -f2-)
COMPREPLY=( "$(compgen -W "${kubernetes_versions}" -- "${cur}")" )
return 0
}
complete -F _clusterctl_switch clusterctl_switch
# kubectl
kubectl_download(){
VERSION=$1
mkdir -p $BIN_FOLDER
cd $BIN_FOLDER
if [ ! -f $BIN_FOLDER/kubectl-$VERSION ];
then
echo "downloading kubectl-$VERSION"
curl -L --url https://storage.googleapis.com/kubernetes-release/release/${VERSION}/bin/$(go env GOOS)/$(go env GOARCH)/kubectl --output $BIN_FOLDER/kubectl-$VERSION
chmod +x $BIN_FOLDER/kubectl-$VERSION
else
echo "kubectl-$VERSION already exists"
fi
kubectl_switch $VERSION $BIN_FOLDER
}
_kubectl_download(){
local cur
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
kubernetes_versions=$(curl -s https://api.github.com/repos/kubernetes/kubernetes/releases | jq '.[].name' -r)
COMPREPLY=($(compgen -W "${kubernetes_versions}" -- ${cur}))
return 0
}
complete -F _kubectl_download kubectl_download
kubectl_switch(){
VERSION=$1
echo "Switching to kubectl-$VERSION"
ln -fs $BIN_FOLDER/kubectl-$VERSION $BIN_FOLDER/kubectl
}
_kubectl_switch(){
local cur
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
kubernetes_versions=$(cd $BIN_FOLDER; ls kubectl-* | cut -d'-' -f2-)
COMPREPLY=($(compgen -W "${kubernetes_versions}" -- ${cur}))
return 0
}
complete -F _kubectl_switch kubectl_switch
# kind
kind_download(){
VERSION=$1
mkdir -p $BIN_FOLDER
cd $BIN_FOLDER
if [ ! -f $BIN_FOLDER/kind-$VERSION ];
then
echo "downloading kind-$VERSION"
curl -L --url https://github.com/kubernetes-sigs/kind/releases/download/${VERSION}/kind-$(go env GOOS)-$(go env GOARCH) --output $BIN_FOLDER/kind-$VERSION
chmod +x $BIN_FOLDER/kind-$VERSION
else
echo "kind-$VERSION already exists"
fi
kind_switch $VERSION $BIN_FOLDER
}
_kind_download(){
local cur
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
kind_versions=$(curl -s https://api.github.com/repos/kubernetes-sigs/kind/releases | jq '.[].name' -r)
COMPREPLY=($(compgen -W "${kind_versions}" -- ${cur}))
return 0
}
complete -F _kind_download kind_download
kind_switch(){
VERSION=$1
echo "Switching to kind-$VERSION"
ln -fs $BIN_FOLDER/kind-$VERSION $BIN_FOLDER/kind
}
_kind_switch(){
local cur
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
kind_versions=$(cd $BIN_FOLDER; ls kind-* | cut -d'-' -f2-)
COMPREPLY=($(compgen -W "${kind_versions}" -- ${cur}))
return 0
}
complete -F _kind_switch kind_switch
# kind kubeconfig (requires https://github.com/chrischdi/k8s-ctx-import)
kind_kubeconfig() {
cluster=$1
if [[ -z $cluster ]]
then
echo "Getting kubeconfig of first cluster"
cluster=$(kind get clusters | head -n 1)
fi
kind get kubeconfig --name=${cluster} | k8s-ctx-import
}
_kind_kubeconfig(){
local cur
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
clusters=$(kind get clusters)
COMPREPLY=($(compgen -W "${clusters}" -- ${cur}))
return 0
}
complete -F _kind_kubeconfig kind_kubeconfig
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment