mirror of
https://github.com/ahmetb/kubectx.git
synced 2026-02-28 02:52:17 +00:00
Compare commits
28 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
46d593305a | ||
|
|
595c27ada7 | ||
|
|
8df92316d6 | ||
|
|
7b23263fc2 | ||
|
|
e368d13eea | ||
|
|
7bc9a1277c | ||
|
|
bc46739ab5 | ||
|
|
5a1366b7c9 | ||
|
|
5144c0f958 | ||
|
|
11336883cb | ||
|
|
b1324570ad | ||
|
|
5f4160766d | ||
|
|
c606382a62 | ||
|
|
428fb0045c | ||
|
|
a97ab8367d | ||
|
|
9beb1a1587 | ||
|
|
c23c2a9e29 | ||
|
|
dad48e5397 | ||
|
|
e9fbafc923 | ||
|
|
b7af607a91 | ||
|
|
bcb89389c6 | ||
|
|
4a1d73d5fe | ||
|
|
f986c148b2 | ||
|
|
2799a9e331 | ||
|
|
d61654cb39 | ||
|
|
d1b69a995a | ||
|
|
4520d3f54b | ||
|
|
d906013970 |
51
README.md
51
README.md
@@ -1,10 +1,10 @@
|
||||
This repository provides both `kubectx` and `kubens` tools.
|
||||
|
||||
|
||||
**`kubectx`** help you switch between clusters back and forth:
|
||||
**`kubectx`** helps you switch between clusters back and forth:
|
||||

|
||||
|
||||
**`kubens`** help you switch between Kubernetes namespaces smoothly:
|
||||
**`kubens`** helps you switch between Kubernetes namespaces smoothly:
|
||||

|
||||
|
||||
# kubectx(1)
|
||||
@@ -18,7 +18,9 @@ USAGE:
|
||||
kubectx - : switch to the previous context
|
||||
kubectx <NEW_NAME>=<NAME> : rename context <NAME> to <NEW_NAME>
|
||||
kubectx <NEW_NAME>=. : rename current-context to <NEW_NAME>
|
||||
kubectx -h,--help : show this message
|
||||
kubectx -d <NAME> : delete context <NAME> ('.' for current-context)
|
||||
(this command won't delete the user/cluster entry
|
||||
that is used by the context)
|
||||
```
|
||||
|
||||
### Usage
|
||||
@@ -52,7 +54,6 @@ USAGE:
|
||||
kubens : list the namespaces
|
||||
kubens <NAME> : change the active namespace
|
||||
kubens - : switch to the previous namespace
|
||||
kubens -h,--help : show this message
|
||||
```
|
||||
|
||||
|
||||
@@ -74,7 +75,7 @@ Active namespace is "default".
|
||||
|
||||
## Installation
|
||||
|
||||
**macOS:**
|
||||
### macOS
|
||||
|
||||
:confetti_ball: Use the [Homebrew](https://brew.sh/) package manager:
|
||||
|
||||
@@ -89,7 +90,7 @@ This command will set up bash/zsh/fish completion scripts automatically.
|
||||
- If you like to add context/namespace info to your shell prompt (`$PS1`),
|
||||
I recommend trying out [kube-ps1](https://github.com/jonmosco/kube-ps1).
|
||||
|
||||
**Linux:**
|
||||
### Linux
|
||||
|
||||
Since `kubectx`/`kubens` are written in Bash, you should be able to instal
|
||||
them to any POSIX environment that has Bash installed.
|
||||
@@ -110,6 +111,44 @@ sudo ln -s /opt/kubectx/kubectx /usr/local/bin/kubectx
|
||||
sudo ln -s /opt/kubectx/kubens /usr/local/bin/kubens
|
||||
```
|
||||
|
||||
#### Arch Linux
|
||||
|
||||
An unofficial [AUR package](https://aur.archlinux.org/packages/kubectx) `kubectx`
|
||||
is available. Install instructions can be found on the [Arch
|
||||
wiki](https://wiki.archlinux.org/index.php/Arch_User_Repository#Installing_packages).
|
||||
|
||||
#### Debian/Ubuntu
|
||||
|
||||
Available as a Debian package for [Debian Buster (testing)](https://packages.debian.org/buster/kubectx), [Sid (unstable)](https://packages.debian.org/sid/kubectx) (_note: if you are unfamiliar with Debian release process and how to enable testing/unstable repos, check the [Debian Wiki](https://wiki.debian.org/DebianReleases)_):
|
||||
|
||||
``` bash
|
||||
sudo apt install kubectx
|
||||
```
|
||||
|
||||
-----
|
||||
|
||||
### Interactive mode
|
||||
|
||||
If you want `kubectx` and `kubens` commands to present you an interactive menu
|
||||
with fuzzy searching, you just need to [install
|
||||
`fzf`](https://github.com/junegunn/fzf) in your PATH.
|
||||
|
||||

|
||||
|
||||
-----
|
||||
|
||||
### Customizing colors
|
||||
|
||||
If you like to customize the colors indicating the current namespace or context, set the environment variables `KUBECTX_CURRENT_FGCOLOR` and `KUBECTX_CURRENT_BGCOLOR` (refer color codes [here](https://linux.101hacks.com/ps1-examples/prompt-color-using-tput/)):
|
||||
|
||||
```
|
||||
export KUBECTX_CURRENT_FGCOLOR=$(tput setaf 6) # blue text
|
||||
export KUBECTX_CURRENT_BGCOLOR=$(tput setaf 7) # white background
|
||||
```
|
||||
|
||||
Colors in the output can be disabled by setting the
|
||||
[`NO_COLOR`](http://no-color.org/) environment variable.
|
||||
|
||||
-----
|
||||
|
||||
#### Users
|
||||
|
||||
BIN
img/kubectx-interactive.gif
Normal file
BIN
img/kubectx-interactive.gif
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 81 KiB |
86
kubectx
86
kubectx
@@ -1,4 +1,4 @@
|
||||
#!/bin/bash
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# kubectx(1) is a utility to manage and switch between kubectl contexts.
|
||||
|
||||
@@ -26,18 +26,17 @@ KUBECTX="${HOME}/.kube/kubectx"
|
||||
usage() {
|
||||
cat <<"EOF"
|
||||
USAGE:
|
||||
kubectx : list the contexts
|
||||
kubectx <NAME> : switch to context <NAME>
|
||||
kubectx - : switch to the previous context
|
||||
kubectx <NEW_NAME>=<NAME> : rename context <NAME> to <NEW_NAME>
|
||||
kubectx <NEW_NAME>=. : rename current-context to <NEW_NAME>
|
||||
kubectx -d <NAME> : delete context <NAME> ('.' for current-context)
|
||||
(this command won't delete the user/cluster entry
|
||||
that is used by the context)
|
||||
kubectx : list the contexts
|
||||
kubectx <NAME> : switch to context <NAME>
|
||||
kubectx - : switch to the previous context
|
||||
kubectx <NEW_NAME>=<NAME> : rename context <NAME> to <NEW_NAME>
|
||||
kubectx <NEW_NAME>=. : rename current-context to <NEW_NAME>
|
||||
kubectx -d <NAME> [<NAME...>] : delete context <NAME> ('.' for current-context)
|
||||
(this command won't delete the user/cluster entry
|
||||
that is used by the context)
|
||||
|
||||
kubectx -h,--help : show this message
|
||||
EOF
|
||||
exit 1
|
||||
}
|
||||
|
||||
current_context() {
|
||||
@@ -54,13 +53,17 @@ list_contexts() {
|
||||
cur="$(current_context)"
|
||||
|
||||
local yellow darkbg normal
|
||||
yellow=$(tput setaf 3)
|
||||
darkbg=$(tput setab 0)
|
||||
normal=$(tput sgr0)
|
||||
yellow=$(tput setaf 3 || true)
|
||||
darkbg=$(tput setab 0 || true)
|
||||
normal=$(tput sgr0 || true)
|
||||
|
||||
local cur_ctx_fg cur_ctx_bg
|
||||
cur_ctx_fg=${KUBECTX_CURRENT_FGCOLOR:-$yellow}
|
||||
cur_ctx_bg=${KUBECTX_CURRENT_BGCOLOR:-$darkbg}
|
||||
|
||||
for c in $(get_contexts); do
|
||||
if [[ "${c}" = "${cur}" ]]; then
|
||||
echo "${darkbg}${yellow}${c}${normal}"
|
||||
if [[ -t 1 && -z "${NO_COLOR:-}" && "${c}" = "${cur}" ]]; then
|
||||
echo "${cur_ctx_bg}${cur_ctx_fg}${c}${normal}"
|
||||
else
|
||||
echo "${c}"
|
||||
fi
|
||||
@@ -86,6 +89,17 @@ switch_context() {
|
||||
kubectl config use-context "${1}"
|
||||
}
|
||||
|
||||
choose_context_interactive() {
|
||||
local choice
|
||||
choice="$(FZF_DEFAULT_COMMAND='kubectx' fzf --ansi || true)"
|
||||
if [[ -z "${choice}" ]]; then
|
||||
echo 2>&1 "error: you did not choose any of the options"
|
||||
exit 1
|
||||
else
|
||||
set_context "${choice}"
|
||||
fi
|
||||
}
|
||||
|
||||
set_context() {
|
||||
local prev
|
||||
prev="$(current_context)"
|
||||
@@ -107,18 +121,6 @@ swap_context() {
|
||||
set_context "${ctx}"
|
||||
}
|
||||
|
||||
user_of_context() {
|
||||
# TODO(ahmetb) no longer used, consider deleting
|
||||
kubectl config view \
|
||||
-o=jsonpath="{.contexts[?(@.name==\"${1}\")].context.user}"
|
||||
}
|
||||
|
||||
cluster_of_context() {
|
||||
# TODO(ahmetb) no longer used, consider deleting
|
||||
kubectl config view \
|
||||
-o=jsonpath="{.contexts[?(@.name==\"${1}\")].context.cluster}"
|
||||
}
|
||||
|
||||
context_exists() {
|
||||
grep -q ^"${1}"\$ <(kubectl config get-contexts -o=name)
|
||||
}
|
||||
@@ -131,15 +133,6 @@ rename_context() {
|
||||
old_name="$(current_context)"
|
||||
fi
|
||||
|
||||
# TODO(ahmetb) old_user and old_cluster are no longer used, clean up
|
||||
local old_user old_cluster
|
||||
old_user="$(user_of_context "${old_name}")"
|
||||
old_cluster="$(cluster_of_context "${old_name}")"
|
||||
if [[ -z "$old_user" || -z "$old_cluster" ]]; then
|
||||
echo "error: Cannot retrieve context ${old_name}." >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if context_exists "${new_name}"; then
|
||||
echo "Context \"${new_name}\" exists, deleting..." >&2
|
||||
kubectl config delete-context "${new_name}" 1>/dev/null 2>&1
|
||||
@@ -148,6 +141,12 @@ rename_context() {
|
||||
kubectl config rename-context "${old_name}" "${new_name}"
|
||||
}
|
||||
|
||||
delete_contexts() {
|
||||
for i in "${@}"; do
|
||||
delete_context "${i}"
|
||||
done
|
||||
}
|
||||
|
||||
delete_context() {
|
||||
local ctx
|
||||
ctx="${1}"
|
||||
@@ -160,19 +159,22 @@ delete_context() {
|
||||
|
||||
main() {
|
||||
if [[ "$#" -eq 0 ]]; then
|
||||
list_contexts
|
||||
if [[ -t 1 && "$(type fzf &>/dev/null; echo $?)" -eq 0 ]]; then
|
||||
choose_context_interactive
|
||||
else
|
||||
list_contexts
|
||||
fi
|
||||
elif [[ "${1}" == "-d" ]]; then
|
||||
if [[ "$#" -lt 2 ]]; then
|
||||
echo "error: missing context NAME" >&2
|
||||
usage
|
||||
elif [[ "$#" -gt 2 ]]; then
|
||||
echo "error: too many arguments" >&2
|
||||
usage
|
||||
exit 1
|
||||
fi
|
||||
delete_context "${2}"
|
||||
delete_contexts "${@:2}"
|
||||
elif [[ "$#" -gt 1 ]]; then
|
||||
echo "error: too many arguments" >&2
|
||||
usage
|
||||
exit 1
|
||||
elif [[ "$#" -eq 1 ]]; then
|
||||
if [[ "${1}" == "-" ]]; then
|
||||
swap_context
|
||||
@@ -181,6 +183,7 @@ main() {
|
||||
elif [[ "${1}" =~ ^-(.*) ]]; then
|
||||
echo "error: unrecognized flag \"${1}\"" >&2
|
||||
usage
|
||||
exit 1
|
||||
elif [[ "${1}" =~ (.+)=(.+) ]]; then
|
||||
rename_context "${BASH_REMATCH[2]}" "${BASH_REMATCH[1]}"
|
||||
else
|
||||
@@ -188,6 +191,7 @@ main() {
|
||||
fi
|
||||
else
|
||||
usage
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
44
kubens
44
kubens
@@ -1,4 +1,4 @@
|
||||
#!/bin/bash
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# kubenx(1) is a utility to switch between Kubernetes namespaces.
|
||||
|
||||
@@ -31,7 +31,6 @@ USAGE:
|
||||
kubens - : switch to the previous namespace in this context
|
||||
kubens -h,--help : show this message
|
||||
EOF
|
||||
exit 1
|
||||
}
|
||||
|
||||
current_namespace() {
|
||||
@@ -86,6 +85,25 @@ switch_namespace() {
|
||||
echo "Active namespace is \"${2}\".">&2
|
||||
}
|
||||
|
||||
choose_namespace_interactive() {
|
||||
# directly calling kubens via fzf might fail with a cryptic error like
|
||||
# "$FZF_DEFAULT_COMMAND failed", so try to see if we can list namespaces
|
||||
# locally first
|
||||
if [[ -z "$(list_namespaces)" ]]; then
|
||||
echo >&2 "error: could not list namespaces (is the cluster accessible?)"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
local choice
|
||||
choice="$(FZF_DEFAULT_COMMAND='kubens' fzf --ansi || true)"
|
||||
if [[ -z "${choice}" ]]; then
|
||||
echo 2>&1 "error: you did not choose any of the options"
|
||||
exit 1
|
||||
else
|
||||
set_namespace "${choice}"
|
||||
fi
|
||||
}
|
||||
|
||||
set_namespace() {
|
||||
local ctx prev
|
||||
ctx="$(current_context)"
|
||||
@@ -105,16 +123,20 @@ set_namespace() {
|
||||
|
||||
list_namespaces() {
|
||||
local yellow darkbg normal
|
||||
yellow=$(tput setaf 3)
|
||||
darkbg=$(tput setab 0)
|
||||
normal=$(tput sgr0)
|
||||
yellow=$(tput setaf 3 || true)
|
||||
darkbg=$(tput setab 0 || true)
|
||||
normal=$(tput sgr0 || true)
|
||||
|
||||
local cur_ctx_fg cur_ctx_bg
|
||||
cur_ctx_fg=${KUBECTX_CURRENT_FGCOLOR:-$yellow}
|
||||
cur_ctx_bg=${KUBECTX_CURRENT_BGCOLOR:-$darkbg}
|
||||
|
||||
local cur ns_list
|
||||
cur="$(current_namespace)"
|
||||
ns_list=$(get_namespaces)
|
||||
for c in $ns_list; do
|
||||
if [[ "${c}" = "${cur}" ]]; then
|
||||
echo "${darkbg}${yellow}${c}${normal}"
|
||||
if [[ -t 1 && -z "${NO_COLOR:-}" && "${c}" = "${cur}" ]]; then
|
||||
echo "${cur_ctx_bg}${cur_ctx_fg}${c}${normal}"
|
||||
else
|
||||
echo "${c}"
|
||||
fi
|
||||
@@ -134,7 +156,11 @@ swap_namespace() {
|
||||
|
||||
main() {
|
||||
if [[ "$#" -eq 0 ]]; then
|
||||
list_namespaces
|
||||
if [[ -t 1 && "$(type fzf &>/dev/null; echo $?)" -eq 0 ]]; then
|
||||
choose_namespace_interactive
|
||||
else
|
||||
list_namespaces
|
||||
fi
|
||||
elif [[ "$#" -eq 1 ]]; then
|
||||
if [[ "${1}" == '-h' || "${1}" == '--help' ]]; then
|
||||
usage
|
||||
@@ -143,6 +169,7 @@ main() {
|
||||
elif [[ "${1}" =~ ^-(.*) ]]; then
|
||||
echo "error: unrecognized flag \"${1}\"" >&2
|
||||
usage
|
||||
exit 1
|
||||
elif [[ "${1}" =~ (.+)=(.+) ]]; then
|
||||
alias_context "${BASH_REMATCH[2]}" "${BASH_REMATCH[1]}"
|
||||
else
|
||||
@@ -151,6 +178,7 @@ main() {
|
||||
else
|
||||
echo "error: too many flags" >&2
|
||||
usage
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user