├── README.md └── kubeconfig-loader /README.md: -------------------------------------------------------------------------------- 1 | ## Simple kubeconfig switcher 2 | 3 | **Why do I did this project?** 4 | 5 | I know there is the *cool* way of dealing with multiple k8s clusters, by merging the different kube-configs in `~/.kube/config` and use `kubectl config use-context` to switch them. Currently I'm setting up a lot of clusters, with their own kubeconfigs, and I'm getting tired of all that merging. 6 | 7 | Actually, life can be simpler just by rotating that configs around. So I spent that 10 minutes of bashing that project to make my (and maybe your) life easier. 8 | 9 | ## How to install? 10 | 11 | * clone that project 12 | * put it into a folder behind your $PATH, like `/usr/bin`, `/usr/local/bin` or what ever you want 13 | * you are done...it's simple bash, and does not depend on any lib 14 | 15 | ## How to use? 16 | 17 | whenever a wild kubeconfig appears, add it with 18 | 19 | ``` 20 | $ kubeconfig-loader add my-new-cluster /path/to/new/kubeconfig/file 21 | ``` 22 | 23 | proof you have `my-new-cluster` available with 24 | 25 | ``` 26 | $ kubeconfig-loader ls 27 | ``` 28 | 29 | switch to that cluster by 30 | 31 | ``` 32 | $ kubeconfig-loader use my-new-cluster 33 | ``` 34 | 35 | and check it works with 36 | 37 | ``` 38 | $ kubectl get node 39 | ``` 40 | 41 | or any other kubectl related command 42 | 43 | 44 | ***happy kubing*** 45 | 46 | 47 | -------------------------------------------------------------------------------- /kubeconfig-loader: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | KUBECONFIGS=~/.kube-configs/ 4 | KUBECONFIG=~/.kube/config 5 | 6 | if [ ! -d $KUBECONFIGS ]; then 7 | mkdir $KUBECONFIGS; 8 | fi 9 | 10 | if [ $# -lt 1 ]; then 11 | echo "Usage: kubeconfig-loader [ls | use | add | rm] []" 12 | exit 1 13 | fi 14 | 15 | CMD=$1 16 | CONFIG_NAME=$2 17 | 18 | case $CMD in 19 | "ls") 20 | ls -1 $KUBECONFIGS 21 | ;; 22 | "use") 23 | if [ ! $# -eq 2 ]; then 24 | echo "Usage: kubeconfig-loader use " 25 | exit 1 26 | fi 27 | if [ ! -f $KUBECONFIGS$CONFIG_NAME ]; then 28 | echo "no config found with name '$CONFIG_NAME'" 29 | exit 1 30 | fi 31 | cp $KUBECONFIGS$CONFIG_NAME $KUBECONFIG 32 | 33 | ;; 34 | "add") 35 | if [ $# != 3 ]; then 36 | echo "usage: kubeconfig-loader add " 37 | exit 1 38 | fi 39 | CONFIG_FILE=$3 40 | cat $CONFIG_FILE > "$KUBECONFIGS$CONFIG_NAME" 41 | echo "added $CONFIG_NAME" 42 | ;; 43 | "rm") 44 | if [ ! $# -eq 2 ]; then 45 | echo "Usage: kubeconfig-loader rm " 46 | exit 1 47 | fi 48 | if [ ! -f $KUBECONFIGS$CONFIG_NAME ]; then 49 | echo "no config found with name '$CONFIG_NAME'" 50 | exit 1 51 | fi 52 | rm $KUBECONFIGS$CONFIG_NAME 53 | ;; 54 | *) 55 | echo "unknown command $CMD" 56 | exit 1 57 | ;; 58 | esac 59 | 60 | 61 | 62 | --------------------------------------------------------------------------------