mirror of
https://github.com/ahmetb/kubectx.git
synced 2026-03-07 14:32:19 +00:00
Compare commits
15 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dfeb7df363 | ||
|
|
407a84ce9e | ||
|
|
ec994aff89 | ||
|
|
3aeb4e76d2 | ||
|
|
517dae9fc8 | ||
|
|
083e56f221 | ||
|
|
6c94248e98 | ||
|
|
244dd5b8a5 | ||
|
|
121f15d1d3 | ||
|
|
21a1e1e963 | ||
|
|
6811a5f03c | ||
|
|
41296a5fcf | ||
|
|
34a9e100c8 | ||
|
|
365fa23d87 | ||
|
|
ccc077b6c5 |
35
README.md
35
README.md
@@ -101,8 +101,34 @@ them to any POSIX environment that has Bash installed.
|
||||
- or save them to a directory, then create symlinks to `kubectx`/`kubens` from
|
||||
somewhere in your `PATH`, like `/usr/local/bin`
|
||||
- Make `kubectx` and `kubens` executable (`chmod +x ...`)
|
||||
- Figure out how to install bash/zsh/fish [completion scripts](completion/).
|
||||
|
||||
- Install bash/zsh/fish [completion scripts](completion/).
|
||||
- For zsh:
|
||||
The completion scripts have to be in a path that belongs to `$fpath`. Either link or copy them to an existing folder.
|
||||
If using oh-my-zsh you can do as follows:
|
||||
```bash
|
||||
mkdir -p ~/.oh-my-zsh/completions
|
||||
chmod -R 755 ~/.oh-my-zsh/completions
|
||||
ln -s /opt/kubectx/completion/kubectx.zsh ~/.oh-my-zsh/completions/_kubectx.zsh
|
||||
ln -s /opt/kubectx/completion/kubens.zsh ~/.oh-my-zsh/completions/_kubens.zsh
|
||||
```
|
||||
Note that the leading underscore seems to be a convention.
|
||||
If not using oh-my-zsh, you could link to `/usr/share/zsh/functions/Completion` (might require sudo), depending on the `$fpath` of your zsh installation.
|
||||
In case of error, calling `compaudit` might help.
|
||||
- For bash:
|
||||
```bash
|
||||
git clone https://github.com/ahmetb/kubectx.git ~/.kubectx
|
||||
COMPDIR=$(pkg-config --variable=completionsdir bash-completion)
|
||||
ln -sf ~/.kubectx/completion/kubens.bash $COMPDIR/kubens
|
||||
ln -sf ~/.kubectx/completion/kubectx.bash $COMPDIR/kubectx
|
||||
cat << FOE >> ~/.bashrc
|
||||
|
||||
|
||||
#kubectx and kubens
|
||||
export PATH=~/.kubectx:\$PATH
|
||||
FOE
|
||||
```
|
||||
- For fish: Figure out how to install completion scripts and please document here
|
||||
|
||||
Example installation steps:
|
||||
|
||||
``` bash
|
||||
@@ -135,6 +161,9 @@ with fuzzy searching, you just need to [install
|
||||
|
||||

|
||||
|
||||
If you have `fzf` installed, but want to opt out of using this feature, set the environment variable `KUBECTX_IGNORE_FZF=1`.
|
||||
|
||||
|
||||
-----
|
||||
|
||||
### Customizing colors
|
||||
@@ -173,4 +202,4 @@ Disclaimer: This is not an official Google product.
|
||||
#### Stargazers over time
|
||||
|
||||
[](https://starcharts.herokuapp.com/ahmetb/kubectx)
|
||||
|
||||

|
||||
|
||||
@@ -5,8 +5,8 @@ PREV=""
|
||||
if [ -f "$KUBECTX" ]; then
|
||||
# show '-' only if there's a saved previous context
|
||||
local PREV=$(cat "${KUBECTX}")
|
||||
_arguments "1: :((- \
|
||||
$(kubectl config get-contexts --output='name')))"
|
||||
_arguments "1: :(-
|
||||
$(kubectl config get-contexts --output='name'))"
|
||||
else
|
||||
_arguments "1: :($(kubectl config get-contexts --output='name'))"
|
||||
fi
|
||||
|
||||
43
kubectx
43
kubectx
@@ -22,7 +22,7 @@ set -eou pipefail
|
||||
IFS=$'\n\t'
|
||||
|
||||
SELF_CMD="$0"
|
||||
KUBECTX="${HOME}/.kube/kubectx"
|
||||
KUBECTX="${XDG_CACHE_HOME:-$HOME/.kube}/kubectx"
|
||||
|
||||
usage() {
|
||||
cat <<"EOF"
|
||||
@@ -40,18 +40,24 @@ USAGE:
|
||||
EOF
|
||||
}
|
||||
|
||||
exit_err() {
|
||||
echo >&2 "${1}"
|
||||
exit 1
|
||||
}
|
||||
|
||||
current_context() {
|
||||
kubectl config view -o=jsonpath='{.current-context}'
|
||||
$KUBECTL config view -o=jsonpath='{.current-context}'
|
||||
}
|
||||
|
||||
get_contexts() {
|
||||
kubectl config get-contexts -o=name | sort -n
|
||||
$KUBECTL config get-contexts -o=name | sort -n
|
||||
}
|
||||
|
||||
list_contexts() {
|
||||
set -u pipefail
|
||||
local cur
|
||||
cur="$(current_context)"
|
||||
local cur ctx_list
|
||||
cur="$(current_context)" || exit_err "error getting current context"
|
||||
ctx_list=$(get_contexts) || exit_err "error getting context list"
|
||||
|
||||
local yellow darkbg normal
|
||||
yellow=$(tput setaf 3 || true)
|
||||
@@ -62,7 +68,7 @@ list_contexts() {
|
||||
cur_ctx_fg=${KUBECTX_CURRENT_FGCOLOR:-$yellow}
|
||||
cur_ctx_bg=${KUBECTX_CURRENT_BGCOLOR:-$darkbg}
|
||||
|
||||
for c in $(get_contexts); do
|
||||
for c in $ctx_list; do
|
||||
if [[ -t 1 && -z "${NO_COLOR:-}" && "${c}" = "${cur}" ]]; then
|
||||
echo "${cur_ctx_bg}${cur_ctx_fg}${c}${normal}"
|
||||
else
|
||||
@@ -87,7 +93,7 @@ save_context() {
|
||||
}
|
||||
|
||||
switch_context() {
|
||||
kubectl config use-context "${1}"
|
||||
$KUBECTL config use-context "${1}"
|
||||
}
|
||||
|
||||
choose_context_interactive() {
|
||||
@@ -103,7 +109,7 @@ choose_context_interactive() {
|
||||
|
||||
set_context() {
|
||||
local prev
|
||||
prev="$(current_context)"
|
||||
prev="$(current_context)" || exit_err "error getting current context"
|
||||
|
||||
switch_context "${1}"
|
||||
|
||||
@@ -123,7 +129,7 @@ swap_context() {
|
||||
}
|
||||
|
||||
context_exists() {
|
||||
grep -q ^"${1}"\$ <(kubectl config get-contexts -o=name)
|
||||
grep -q ^"${1}"\$ <($KUBECTL config get-contexts -o=name)
|
||||
}
|
||||
|
||||
rename_context() {
|
||||
@@ -136,10 +142,10 @@ rename_context() {
|
||||
|
||||
if context_exists "${new_name}"; then
|
||||
echo "Context \"${new_name}\" exists, deleting..." >&2
|
||||
kubectl config delete-context "${new_name}" 1>/dev/null 2>&1
|
||||
$KUBECTL config delete-context "${new_name}" 1>/dev/null 2>&1
|
||||
fi
|
||||
|
||||
kubectl config rename-context "${old_name}" "${new_name}"
|
||||
$KUBECTL config rename-context "${old_name}" "${new_name}"
|
||||
}
|
||||
|
||||
delete_contexts() {
|
||||
@@ -152,15 +158,24 @@ delete_context() {
|
||||
local ctx
|
||||
ctx="${1}"
|
||||
if [[ "${ctx}" == "." ]]; then
|
||||
ctx="$(current_context)"
|
||||
ctx="$(current_context)" || exit_err "error getting current context"
|
||||
fi
|
||||
echo "Deleting context \"${ctx}\"..." >&2
|
||||
kubectl config delete-context "${ctx}"
|
||||
$KUBECTL config delete-context "${ctx}"
|
||||
}
|
||||
|
||||
main() {
|
||||
if hash kubectl 2>/dev/null; then
|
||||
KUBECTL=kubectl
|
||||
elif hash kubectl.exe 2>/dev/null; then
|
||||
KUBECTL=kubectl.exe
|
||||
else
|
||||
echo >&2 "kubectl is not installed"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [[ "$#" -eq 0 ]]; then
|
||||
if [[ -t 1 && "$(type fzf &>/dev/null; echo $?)" -eq 0 ]]; then
|
||||
if [[ -t 1 && -z "${KUBECTX_IGNORE_FZF:-}" && "$(type fzf &>/dev/null; echo $?)" -eq 0 ]]; then
|
||||
choose_context_interactive
|
||||
else
|
||||
list_contexts
|
||||
|
||||
44
kubens
44
kubens
@@ -1,6 +1,6 @@
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# kubenx(1) is a utility to switch between Kubernetes namespaces.
|
||||
# kubens(1) is a utility to switch between Kubernetes namespaces.
|
||||
|
||||
# Copyright 2017 Google Inc.
|
||||
#
|
||||
@@ -22,7 +22,7 @@ set -eou pipefail
|
||||
IFS=$'\n\t'
|
||||
|
||||
SELF_CMD="$0"
|
||||
KUBENS_DIR="${HOME}/.kube/kubens"
|
||||
KUBENS_DIR="${XDG_CACHE_HOME:-$HOME/.kube}/kubens"
|
||||
|
||||
usage() {
|
||||
cat <<"EOF"
|
||||
@@ -34,10 +34,18 @@ USAGE:
|
||||
EOF
|
||||
}
|
||||
|
||||
exit_err() {
|
||||
echo >&2 "${1}"
|
||||
exit 1
|
||||
}
|
||||
|
||||
current_namespace() {
|
||||
local cur_ctx
|
||||
cur_ctx="$(current_context)"
|
||||
ns="$(kubectl config view -o=jsonpath="{.contexts[?(@.name==\"${cur_ctx}\")].context.namespace}")"
|
||||
|
||||
cur_ctx="$(current_context)" || exit_err "error getting current context"
|
||||
ns="$($KUBECTL config view -o=jsonpath="{.contexts[?(@.name==\"${cur_ctx}\")].context.namespace}")" \
|
||||
|| exit_err "error getting current namespace"
|
||||
|
||||
if [[ -z "${ns}" ]]; then
|
||||
echo "default"
|
||||
else
|
||||
@@ -46,11 +54,11 @@ current_namespace() {
|
||||
}
|
||||
|
||||
current_context() {
|
||||
kubectl config view -o=jsonpath='{.current-context}'
|
||||
$KUBECTL config view -o=jsonpath='{.current-context}'
|
||||
}
|
||||
|
||||
get_namespaces() {
|
||||
kubectl get namespaces -o=jsonpath='{range .items[*].metadata.name}{@}{"\n"}{end}'
|
||||
$KUBECTL get namespaces -o=jsonpath='{range .items[*].metadata.name}{@}{"\n"}{end}'
|
||||
}
|
||||
|
||||
escape_context_name() {
|
||||
@@ -82,7 +90,7 @@ save_namespace() {
|
||||
|
||||
switch_namespace() {
|
||||
local ctx="${1}"
|
||||
kubectl config set-context "${ctx}" --namespace="${2}"
|
||||
$KUBECTL config set-context "${ctx}" --namespace="${2}"
|
||||
echo "Active namespace is \"${2}\".">&2
|
||||
}
|
||||
|
||||
@@ -107,8 +115,8 @@ choose_namespace_interactive() {
|
||||
|
||||
set_namespace() {
|
||||
local ctx prev
|
||||
ctx="$(current_context)"
|
||||
prev="$(current_namespace)"
|
||||
ctx="$(current_context)" || exit_err "error getting current context"
|
||||
prev="$(current_namespace)" || exit_error "error getting current namespace"
|
||||
|
||||
if grep -q ^"${1}"\$ <(get_namespaces); then
|
||||
switch_namespace "${ctx}" "${1}"
|
||||
@@ -133,8 +141,9 @@ list_namespaces() {
|
||||
cur_ctx_bg=${KUBECTX_CURRENT_BGCOLOR:-$darkbg}
|
||||
|
||||
local cur ns_list
|
||||
cur="$(current_namespace)"
|
||||
ns_list=$(get_namespaces)
|
||||
cur="$(current_namespace)" || exit_err "error getting current namespace"
|
||||
ns_list=$(get_namespaces) || exit_err "error getting namespace list"
|
||||
|
||||
for c in $ns_list; do
|
||||
if [[ -t 1 && -z "${NO_COLOR:-}" && "${c}" = "${cur}" ]]; then
|
||||
echo "${cur_ctx_bg}${cur_ctx_fg}${c}${normal}"
|
||||
@@ -146,7 +155,7 @@ list_namespaces() {
|
||||
|
||||
swap_namespace() {
|
||||
local ctx ns
|
||||
ctx="$(current_context)"
|
||||
ctx="$(current_context)" || exit_err "error getting current context"
|
||||
ns="$(read_namespace "${ctx}")"
|
||||
if [[ -z "${ns}" ]]; then
|
||||
echo "error: No previous namespace found for current context." >&2
|
||||
@@ -156,8 +165,17 @@ swap_namespace() {
|
||||
}
|
||||
|
||||
main() {
|
||||
if hash kubectl 2>/dev/null; then
|
||||
KUBECTL=kubectl
|
||||
elif hash kubectl.exe 2>/dev/null; then
|
||||
KUBECTL=kubectl.exe
|
||||
else
|
||||
echo >&2 "kubectl is not installed"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [[ "$#" -eq 0 ]]; then
|
||||
if [[ -t 1 && "$(type fzf &>/dev/null; echo $?)" -eq 0 ]]; then
|
||||
if [[ -t 1 && -z ${KUBECTX_IGNORE_FZF:-} && "$(type fzf &>/dev/null; echo $?)" -eq 0 ]]; then
|
||||
choose_namespace_interactive
|
||||
else
|
||||
list_namespaces
|
||||
|
||||
Reference in New Issue
Block a user