Last active
December 31, 2023 11:40
-
-
Save diegocattelan-kitme/7de608184418d743f033e753b4474ce7 to your computer and use it in GitHub Desktop.
kubectl bash wrapper, i put it in .bashrc.d and invoke kubectl, requires jq, yq, dialog
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| function k8s_download_kubectl { | |
| mkdir -p ${HOME}/.kube/bin | |
| export KUBECTL_RELEASE=${1} | |
| if [ ! -f "${HOME}/.kube/bin/kubectl_${KUBECTL_RELEASE}" ]; then | |
| echo "Downloading kubectl ${KUBECTL_RELEASE}" | |
| curl -L "https://dl.k8s.io/release/${KUBECTL_RELEASE}/bin/linux/amd64/kubectl" \ | |
| -o "${HOME}/.kube/bin/kubectl_${KUBECTL_RELEASE}" | |
| curl -L "https://dl.k8s.io/release/${KUBECTL_RELEASE}/bin/linux/amd64/kubectl.sha256" \ | |
| -o "${HOME}/.kube/bin/kubectl_sha256.${KUBECTL_RELEASE}" | |
| CHKSUM=$(cat "${HOME}/.kube/bin/kubectl_sha256.${KUBECTL_RELEASE}") | |
| echo "$CHKSUM ${HOME}/.kube/bin/kubectl_${KUBECTL_RELEASE}" | sha256sum --check | |
| chmod +x "${HOME}/.kube/bin/kubectl_${KUBECTL_RELEASE}" | |
| fi | |
| } | |
| function k8s_download_last_kubectl { | |
| k8s_download_kubectl $(curl -L -s https://dl.k8s.io/release/stable.txt) | |
| } | |
| #consente la selezione del contesto di kubernetes da utilizzare | |
| function k8s_select { | |
| k8s_download_last_kubectl | |
| filesList=() | |
| for file in $HOME/.kube/*.config | |
| do | |
| base_name=$(basename ${file}) | |
| CC=$(yq .current-context ${file}) | |
| filesList+=("${base_name}" "$CC") | |
| done | |
| choice=$(dialog --clear --title "Scegli il file da " \ | |
| --menu "Please select" 15 80 3 "${filesList[@]}" \ | |
| 2>&1 >/dev/tty) | |
| clear | |
| echo "Selected kubernetes config: $choice" | |
| if [ -f $HOME/.kube/$choice ]; then | |
| K8S_P1=$(echo "${base_name}" | cut -d "_" -f 1 -s) | |
| K8S_P2=$(echo "${base_name}" | cut -d "_" -f 2 -s) | |
| if [ -f "$HOME/.kube/${K8S_P1}_${K8S_P2}" ]; then | |
| k8s_download_kubectl ${K8S_P1} | |
| fi | |
| export KUBECONFIG=$HOME/.kube/$choice | |
| export KUBECTL="${HOME}/.kube/bin/kubectl_${KUBECTL_RELEASE}" | |
| $KUBECTL version | |
| export K8S_CLIENT_VERSION=$($KUBECTL version -o json | jq -r .clientVersion.gitVersion) | |
| export K8S_SERVER_VERSION=$($KUBECTL version -o json | jq -r .serverVersion.gitVersion) | |
| check_k8s_versions | |
| fi | |
| } | |
| function check_k8s_versions { | |
| if test "$K8S_CLIENT_VERSION" = "$K8S_SERVER_VERSION" | |
| then | |
| echo "OK : kubectl version and kubernetes version matches (${K8S_SERVER_VERSION})" | |
| else | |
| echo "WARNING : kubectl version ($K8S_CLIENT_VERSION) and kubernetes ($K8S_SERVER_VERSION) version mismatch" | |
| echo "HINT: prepend config file name with version and _ so i can use the correct kubectl client" | |
| echo " $(basename ${KUBECONFIG}) should be renamed ${K8S_SERVER_VERSION}_$(basename ${KUBECONFIG})" | |
| fi | |
| } | |
| function kubectl { | |
| if [ -z ${KUBECTL+x} ]; then | |
| k8s_select | |
| else | |
| if [ $# -eq 0 ]; then | |
| check_k8s_versions | |
| fi | |
| $KUBECTL "$@" | |
| fi | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment