Home

Awesome

English | 简体中文

zsh-kubeconfig-mgr

License

This project is a plugin for Zsh and oh-my-zsh, Designed to make managing and using multiple kubeconfig files easier.

In some cases, it may be necessary to operate multiple different Kubernetes clusters, each with different credentials. And it might not be convenient to combine them into one kubeconfig file for various reasons. Therefore, when operating different clusters, you must frequently set the environment variable $KUBECONFIG to different values, or use the parameter --kubeconfig /path/to/config ... to specify different kubeconfig files when executing kubectl, these operations are very tedious and error-prone.

This project will provide an idea to solve these problems.

Installation

1 Install Plugin

Oh-my-zsh:

If you have installed oh-my-zsh, then:

  1. Clone this repository in oh-my-zsh's plugins directory:
    git clone https://github.com/yhlooo/zsh-kubeconfig-mgr.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-kubeconfig-mgr
    
  2. Activate the plugin in ~/.zshrc :
    plugins=( [plugins...] zsh-kubeconfig-mgr)
    
  3. Restart zsh (such as by opening a new instance of your terminal emulator).

Zsh:

If you don't have oh-my-zsh installed, you can simply clone this repository and source the script:

git clone https://github.com/keybrl/zsh-kubeconfig-mgr.git
source ./zsh-kubeconfig-mgr/zsh-kubeconfig-mgr.zsh
echo "source ${(q-)PWD}/zsh-kubeconfig-mgr/zsh-kubeconfig-mgr.zsh" >> ${ZDOTDIR:-$HOME}/.zshrc

2 Set command line prompt

The plugin provides a command line prompt which can be added to the variable PROMPT or RPROMPT as you need

Simply add something like the following to the end of ~/.zshrc :

PROMPT='$(kubeconfig_prompt_info) '$PROMPT

Usage

Command line prompt

The function kubeconfig_prompt_info will print the kubeconfig specified by the current environment variable $KUBECONFIG . Its value has the following situations:

The printed color indicates whether the corresponding kubeconfig exists or not:

Example:

Add the following to the end of ~/.zshrc :

PROMPT='$(kubeconfig_prompt_info) '$PROMPT

Effect:

prompt_info

List kubeconfig

kcmgr ls , kcmgr list or lkc (list kubeconfig)

list all filenames in the directory ~/.kube/configs/

kcmgr_ls

Show the content of kubeconfig

kcmgr show [config] or rkc [config] (read kubeconfig)

kcmgr_show

Set kubeconfig

kcmgr set [config] or skc [config] (set kubeconfig)

kcmgr_set

Delete kubeconfig

kcmgr delete [config] , kcmgr del [config] or dkc [config] (delete kubeconfig)

kcmgr_del

Edit / Add kubeconfig

kcmgr edit [config] or ekc [config] (edit kubeconfig)

Prefer vim , or vi if vim not found. If kubeconfig does not exist it will be created.

kcmgr_edit