Skip to content

Instantly share code, notes, and snippets.

@tienthanh2509
Created April 8, 2022 07:16
Show Gist options
  • Save tienthanh2509/5cdadb48c67e8bdfd0d4a1835d60d8a5 to your computer and use it in GitHub Desktop.
Save tienthanh2509/5cdadb48c67e8bdfd0d4a1835d60d8a5 to your computer and use it in GitHub Desktop.
Kubectl config switcher

You are tried many kubeconfig files kc command will help it easy to switch multiple kubeconfig file for you

How does it work? The script will scan your config file in ~/.kube/*.yaml and provide a bash shell autocompletion

Provide kc command switch kube config in ~/.kube/*.yaml
Usage: kc <file> | kc <press tab tab>

Installation

  1. Download kubeconfig.completion.bash
  2. Place the completion script in /etc/bash_completion.d/
  3. ReOpen your terminal or source this file

Sample output

thanh@thanh-pc:~$ kc /home/thanh/.kube/
/home/thanh/.kube/agim8.yaml                        /home/thanh/.kube/rke2-olive-ci.yaml
/home/thanh/.kube/olive-prod.yaml                   /home/thanh/.kube/rke2-ptthanh-i5-128-62-oidc.yaml
/home/thanh/.kube/ov.yaml                           /home/thanh/.kube/rke2-ptthanh-i5-128-62.yaml
/home/thanh/.kube/rke2-localhost.yaml               /home/thanh/.kube/rke2-tdmu.yaml
/home/thanh/.kube/rke2-olive-ci-oidc.yaml           /home/thanh/.kube/rke2-tma-45.yaml
#/usr/bin/env bash
# Provide kc command switch kube config in ~/.kube/*.yaml
# Usage: kc <file> | kc <press tab tab>
kube_config_autocomplete() {
local KUBE_CONFIG_ROOT=$HOME/.kube
FILES=$(ls $KUBE_CONFIG_ROOT/*.yaml)
complete -W "${FILES}" kc
}
kc() {
export KUBECONFIG=$1
echo "Switch KubeConfig to file ${KUBECONFIG}"
}
kube_config_autocomplete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment