Skip to content

Commit

Permalink
add kubeconfig preview
Browse files Browse the repository at this point in the history
  • Loading branch information
hedgieinsocks committed Jul 14, 2024
1 parent db210e8 commit 6ac2f39
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 13 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
# kubectl-cnf

`kubectl-cnf` is a `kubectl` plugin that allows one to switch between multiple k8s configs within the terminal tab scope.
`kubectl-cnf` is a simple `kubectl` plugin that allows one to switch between multiple k8s configs within the terminal tab scope.

If you are working with many clusters that come with their own kubeconfigs, you can use this tool to allow yourself multitask by keeping a dedicated terminal tab for each cluster.

## Dependencies

* `fzf` - https://github.com/junegunn/fzf
* `bat` - https://github.com/sharkdp/bat

## Installation

1. Install `fzf`
2. Place `kubectl-cnf` into the directory that is a part of your `PATH` (e.g. `~/.krew/bin`)
3. Create the `~/.kube/configs` directory and place your kubeconfigs there (or create symlinks)
1. Run `kubectl krew install cnf` or just place `kubectl-cnf` into the directory within your `PATH` (e.g. `~/.local/bin`)
2. Create the `~/.kube/configs` directory and place your kubeconfigs there (or create symlinks)

## Customization

Expand All @@ -23,7 +23,8 @@ You can export the following variables to tweak the plugin's behaviour.
| `KCNF_DIR` | `~/.kube/configs` | directory with your kubeconfigs |
| `KCNF_VERBOSE` | `true` | print a notificaiton when entering or exiting subshell |
| `KCNF_SUBSHELL` | `true` | export `KUBECONFIG` and launch subshell |
| `KCNF_HEIGHT` | `40%` | fzf height |

## P.S.

You can opt in for a shell funciton from `kcnf.sh` to avoid the plugin overhead.
You can opt in for the shell funciton from `kcnf.sh` to avoid the plugin overhead.
21 changes: 16 additions & 5 deletions kcnf.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,25 @@
kcnf() {
local config

hash fzf || return 1
hash fzf bat || return 1
mkdir -p "${KCNF_DIR:-$HOME/.kube/configs}"

config=$(find "${KCNF_DIR:-$HOME/.kube/configs}" \( -type f -o -type l \) -exec awk '/^current-context:/ {print FILENAME, $2}' {} + \
| sort -k2 \
| fzf --cycle --layout=reverse --query "${*:-}" --with-nth=2)
| fzf \
--cycle \
--height="${KCNF_HEIGHT:-40%}" \
--layout=reverse \
--with-nth=2 \
--query="${*:-}" \
--bind="tab:toggle-preview" \
--preview="bat --style=plain --color=always --language=yaml {1}" \
--preview-window="hidden,wrap,75%")

[[ -n "${config}" ]] || return 0
if [[ -n "${config}" ]]; then
export KUBECONFIG="${config%% *}"
export KUBECONTEXT="${config##* }"
fi

export KUBECONFIG="${config%% *}"
export KUBECONTEXT="${config##* }"
return 0
}
17 changes: 14 additions & 3 deletions kubectl-cnf
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
#!/usr/bin/env bash

[[ -n "${DEBUG}" ]] && set -x

KCNF_DIR="${KCNF_DIR:-$HOME/.kube/configs}"
KCNF_VERBOSE="${KCNF_VERBOSE:-true}"
KCNF_SUBSHELL="${KCNF_SUBSHELL:-true}"
KCNF_HEIGHT="${KCNF_HEIGHT:-40%}"

main() {
local config

hash fzf || return 1
hash fzf bat || return 1
mkdir -p "${KCNF_DIR}"

config=$(find "${KCNF_DIR}" \( -type f -o -type l \) -exec awk '/^current-context:/ {print FILENAME, $2}' {} + \
| sort -k2 \
| fzf --cycle --layout=reverse --query "${*:-}" --with-nth=2)

| fzf \
--cycle \
--height="${KCNF_HEIGHT}" \
--layout=reverse \
--with-nth=2 \
--query="${*:-}" \
--bind="tab:toggle-preview" \
--preview="bat --style=plain --color=always --language=yaml {1}" \
--preview-window="hidden,wrap,75%")
[[ -n "${config}" ]] || return 0

export KUBECONFIG="${config%% *}"
Expand Down

0 comments on commit 6ac2f39

Please sign in to comment.