├── README.md ├── img ├── demo.gif ├── how-it-works.graffle └── how-it-works.png └── kubectl-ctx /README.md: -------------------------------------------------------------------------------- 1 | # kubectl ctx 2 | 3 | A kubectl [plugin](https://kubernetes.io/docs/tasks/extend-kubectl/kubectl-plugins/) for interactively changing your *kubeconfig* context. 4 | 5 | ![Demo](img/demo.gif) 6 | 7 | This makes it easier to work with multiple clusters from the same machine. 8 | 9 | Also see [kubectl-ns](https://github.com/weibeld/kubectl-ns) for switching between namespaces. 10 | 11 | ## Installation 12 | 13 | To install the plugin, download the `kubectl-ctx` file and save it in any directory that is in your `PATH`: 14 | 15 | ~~~bash 16 | curl -O https://raw.githubusercontent.com/weibeld/kubectl-ctx/master/kubectl-ctx 17 | ~~~ 18 | 19 | Then, make the `kubectl-ctx` file executable: 20 | 21 | ~~~bash 22 | chmod +x kubectl-ctx 23 | ~~~ 24 | 25 | Now, you can verify that the plugin is correctly installed by running the following command: 26 | 27 | ~~~bash 28 | kubectl plugin list 29 | ~~~~ 30 | 31 | This lists all the plugins that `kubectl` detected, and the `kubectl-ctx` plugin should now be listed there. 32 | 33 | To uninstall the plugin, simply delete the `kubectl-ctx` file. 34 | 35 | ## Usage 36 | 37 | Change the current context: 38 | 39 | ~~~bash 40 | kubectl ctx 41 | ~~~ 42 | 43 | List all contexts: 44 | 45 | ~~~bash 46 | kubectl ctx -l 47 | ~~~ 48 | 49 | ## How It Works 50 | 51 | When you run `kubectl ctx`, the plugin changes the `current-context` element of your [*kubeconfig*](https://kubernetes.io/docs/concepts/configuration/organize-cluster-access-kubeconfig/) configuration: 52 | 53 | ![How it works](img/how-it-works.png) 54 | 55 | Note that this includes a physical change to one of your *kubeconfig* files (the default *kubeconfig* file is `~/.kube/config`, but you can have multiple *kubeconfig* files by by listing them in the [`KUBECONFIG`](https://kubernetes.io/docs/concepts/configuration/organize-cluster-access-kubeconfig/#the-kubeconfig-environment-variable) environment variable). 56 | 57 | When you run `kubectl ctx -l`, the plugin displays the names of all the contexts in your *kubeconfig* configuration. 58 | 59 | ## Dependencies 60 | 61 | The plugin depends on the [fzf](https://github.com/junegunn/fzf) command being available on your system. 62 | 63 | You can install fzf as follows: 64 | 65 | - Homebrew (macOS) and Linuxbrew (Linux): 66 | ~~~bash 67 | brew install fzf 68 | ~~~ 69 | - From source (macOS and Linux): 70 | ~~~bash 71 | git clone https://github.com/junegunn/fzf.git ~/.fzf 72 | ~/.fzf/install 73 | ~~~ 74 | - For further installation options, see [here](https://github.com/junegunn/fzf#installation) 75 | -------------------------------------------------------------------------------- /img/demo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/weibeld/kubectl-ctx/84ce9a632d4cdc33dc31ce7a00bda1365b3707a7/img/demo.gif -------------------------------------------------------------------------------- /img/how-it-works.graffle: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/weibeld/kubectl-ctx/84ce9a632d4cdc33dc31ce7a00bda1365b3707a7/img/how-it-works.graffle -------------------------------------------------------------------------------- /img/how-it-works.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/weibeld/kubectl-ctx/84ce9a632d4cdc33dc31ce7a00bda1365b3707a7/img/how-it-works.png -------------------------------------------------------------------------------- /kubectl-ctx: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | usage() { 4 | cat </dev/null) 45 | kubectl config get-contexts -o name | sed "s/^/ /;\|^ $c$|s/ /*/" 46 | } 47 | 48 | set_context() { 49 | which fzf >/dev/null || { fzf_missing; exit 1; } 50 | local c=$(list_contexts | fzf -e | sed 's/^..//') 51 | if [[ -z "$c" ]]; then 52 | echo "Aborted. Nothing has been changed." 53 | else 54 | kubectl config use-context "$c" 55 | fi 56 | } 57 | 58 | if [[ -z "$1" ]]; then 59 | set_context 60 | elif [[ "$1" = -l ]]; then 61 | list_contexts 62 | else 63 | usage 64 | fi 65 | --------------------------------------------------------------------------------