Skip to content

steffakasid/kubectl-co

Repository files navigation

Kubectl-co

License Apache%202.0 blue link:https://github.com/steffakasid/kubectl-co/actions/workflows/codeql-analysis.yml link:https://github.com/steffakasid/kubectl-co/actions/workflows/release.yml link:https://github.com/steffakasid/kubectl-co/actions/workflows/go-test.yml

This tool aims to make it easy to manage multiple config files. I know there are alternatives around e.g. kubectl krew info config-registry but for me I didn’t like the deep structure of the commands. I want something as short as possible. Before I created this tool I used some shell functions to basically do the same:

setKubeCtx() {
    export KUBECONFIG=$1
    unlink ~/.kube/config
    ln -s $1 ~/.kube/config
    kubectx $2
}

k8s() {
  if [ -z ${1} ]; then
    echo $KUBECONFIG
    ll ~/.kube/config
  else
    if [ $1 = "prod" ]; then
      setKubeCtx ~/.kube/eks-prod.yml eks-prod
    elif [ $1 = "dev" ]; then
      setKubeCtx ~/.kube/eks-dev.yml eks-dev
    elif [ $1 = "poc" ]; then
      setKubeCtx ~/.kube/eks-poc.yml eks-poc
    elif [ $1 = "nextcloud" ]; then
      setKubeCtx ~/.kube/config-nextcloud.yml microk8s
    elif [ $1 = "berlin" ]; then
      setKubeCtx ~/.kube/config-berlin.yml default/berlin/steffenrumpf
      oc project &> /dev/null || oc login | tail -n 2 | head -n 1 | sed -e 's/You must obtain an API token by visiting //g' | xargs open
    elif [ $1 = "madrid" ]; then
      setKubeCtx ~/.kube/config-madrid.yml default/madrid/steffenrumpf
      oc project &> /dev/null || oc login | tail -n 2 | head -n 1 | sed -e 's/You must obtain an API token by visiting //g' | xargs open
    else
      setKubeCtx ~/.kube/eks-test.yml eks-test
    fi
  fi
}

Unfortunately this was a bit limited so I decided to write a little go tool and integrate it as plugin into kubectl.

Note
If you set the KUBECONFIG environment var this will always take precedence before the config file.

Usage

  kubectl co [flags]
  kubectl-co [flags]

Preqrequisites

kubectl should be installed (even if the application would also run for it own as kubectl-co)

Examples

  kubectl co --add new-config ~/.kube/config    - adds your current kubeconfig to be used by co with the name 'new-config'
  kubectl co --add completly-new                - adds a plain new config file which must be inialised afterwards
  kubectl co --list                             - list all available configs
  kubectl co --delete new-config                - delete config with name 'new-config'
  kubectl co new-config                         - switch to 'new-config' this will overwrite ~/.kube/config with a symbolic link
  kubectl co                                    - switch to previous config and set current config to previous

Flags:

-a, --add

Add a new given config providing the path and the name. Usage: kubectl co --add [configname] [configpath]

--debug

Turn on debug output

-d, --delete

Delete the config with the given name. Usage: kubectl co --delete [configname]

-l, --list

List all available config files

Environment variables

# Prefix all config keys with KUBECTL_CO_
export KUBECTL_CO_DEBUG=true
export KUBECTL_CO_DELETE=true

Shell completion

Manually enable shell completion
# bash
source <(kubectl-co completion bash)

# zsh (use bash completion)
source <(kubectl-co completion zsh)
Or add to your shell rc file:
# ~/.bashrc
echo 'source <(kubectl-co completion bash)' >> ~/.bashrc

# ~/.zshrc
echo 'source <(kubectl-co completion zsh)' >> ~/.zshrc

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages