# Packages
# README
K8sCtx - Kubernetes Context (Switcher)
Change your kube context via ktx
command.
Features
- support for multiple kubeconfigs (globally - not per terminal session; see Extras)
- add aliases for kubeconfig files
- add aliases for the kube contexts
- fuzzy search a context via Terminal User Interface (short TUI)
- jump back and forth between two contexts via
ktx -
- manage your contexts in an extra config file with the help of Jsonnet
- or manage your contexts in different extra
contexts_<kubeconfig alias>.yaml
files; one per kubeconfig
Usage
- Opens the TUI in Filter mode with all contexts of all kubeconfig files:
ktx
- Opens the TUI in Filter mode with all contexts of a kubeconfig file with the alias "
d
":
ktx d
- Opens the TUI in Select mode with contexts filtered by "lab" of the kubeconfig with the alias "d":
ktx d lab
- Switches to the previous context (no TUI involved):
ktx -
- Switches directly to the context with the name/alias "cluster-lab-oci-eu-frankfurt-1-dev" (no TUI involved):
ktx -c cluster-lab-oci-eu-frankfurt-1-dev
- Returns the current context (no TUI involved):
ktx -c
Installation
[!NOTE] Before you run the next command, make sure you have go installed.
Run the following command to install the binary on your local machine:
go install -v github.com/peterbueschel/k8sctx@latest
Configuration
On a first run, the tool will create a folder called ktx
and the required files inside the user config folder (under Linux it is usually ~/.config/
; under Windows it is APPDATA
).
[!NOTE] You can define a different location with the help of the
KTX_CONFIG_DIR
environment variable.
The required files are:
filename | content | description |
---|---|---|
config.jsonnet | Settings for ktx itself and every context in all kubeconfigs. | The main config file written in jsonnet for ktx , which is also used to update the different contexts_<alias>.yaml files.🔗 see config_jsonnet for more details. |
contexts_<alias>.yaml | Settings for the contexts of a single kubeconfig. | For every kubeconfig (given by its <alias> ), such a .yaml will be generated. Inside, you can set in turn an alias for each context next to other fields, like environment or region . These fields will be shown in the TUI.Here you could theoretically also specify the default namespace, but it is more recommended to use the config.jsonnet .⚠️ If you delete such .yaml file, the tool will automatically recreate it with the help of the config.jsonnet . |
.libsonnet | Jsonnet code used by the config.jsonnet in order to import all contexts_<alias>.yaml files. | This is a helper file for the config.jsonnet . It makes the content of all contexts_<alias>.yaml files available under the alias of the related kubeconfig.This file doesn't need to be touched. |
.state | Stores the last & current context, together with the related kubeconfig | This file is required for the ktx - command in order to jump back and forth between two contexts. |
TUI
The TUI was made with the help of the charmbracelet/bubbletea framework.
It has two modes:
- the Filter mode (using a fuzzy search)
- and the Select mode
which are used based on the way you run the ktx
tool (see Usage)
You can switch between the modes by pressing the esc
key and the /
key, where:
- pressing
esc
in the Filter mode will enter the Select mode - and pressing the
/
key in the Select mode will return to the Filter mode
Depending on your settings, you can filter by either the name of the context or its alias.
Leave the TUI without changing the context via q
in Select mode or directly via ctrl + c
.
Extras
Setup Multiple Kubeconfigs
According to the official Kubernetes documentation:
The KUBECONFIG environment variable is a list of paths to configuration files. The list is colon-delimited for Linux and Mac, and semicolon-delimited for Windows.
# Example: Multiple Kube config files - one per environment
export KUBECONFIG="$HOME/.kube/config.lab:$HOME/.kube/config.ed:$HOME/.kube/config.stage:$HOME/.kube/config.live"
Using the config.jsonnet
file
Please have a look into extra documentation file: docs/config_jsonnet.md