28 Commits

Author SHA1 Message Date
Ahmet Alp Balkan
46d593305a Release v0.6.0
- FEATURE: interactive search mode when kubectx and kubens are ran without any
  arguments and fzf(1) is detected in PATH. (#71, #74)
- FIX: kubectx -d now doesn't ignore arguments after the first argument. (#75)
- FIX: empty output bug when TERM=vt100 even though NO_COLOR is set. (#57, #73)
- FIX: --help exits with code 0 now. (#69, #72)
2018-08-23 10:19:12 -07:00
Ahmet Alp Balkan
595c27ada7 fix: ignored args while deleting multiple clusters (#76)
Fixes #75.
2018-08-23 10:17:42 -07:00
Ahmet Alp Balkan
8df92316d6 add support for interactive selection with fzf (#74)
Present a fuzzy search choice in "kubectx" and "kubens" commands without
arguments.

![demo2](https://user-images.githubusercontent.com/159209/44478683-40f16d00-a5f3-11e8-99e2-f32f2a3539c1.gif)

Fixes #71.
2018-08-22 10:08:13 -07:00
Ahmet Alp Balkan
7b23263fc2 ignore errors from tput (to fix TERM=vt100) (#73)
Currently TERM=vt100 is causing kubectx failure since tput is returning
exitcode=1. vt100 does not have colors. Ignoring tput exit code.

Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
2018-08-22 09:50:34 -07:00
Vít Listík
e368d13eea help exit status 0 (#72) 2018-08-19 16:40:47 -07:00
Ahmet Alp Balkan
7bc9a1277c Release v0.5.1 2018-08-06 12:21:18 -07:00
Ahmet Alp Balkan
bc46739ab5 add note about NO_COLOR 2018-08-06 12:20:51 -07:00
Ahmet Alp Balkan
5a1366b7c9 Merge pull request #64 from idvoretskyi/patch-1
Debian package installation information added
2018-07-19 20:04:13 -07:00
Ihor Dvoretskyi
5144c0f958 Debian releases information added
Signed-off-by: Ihor Dvoretskyi <ihor@linux.com>
2018-07-19 23:03:25 +00:00
Ihor Dvoretskyi
11336883cb Debian releases information added
Signed-off-by: Ihor Dvoretskyi <ihor@linux.com>
2018-07-19 23:00:17 +00:00
Ihor Dvoretskyi
b1324570ad Debian releases information added
Signed-off-by: Ihor Dvoretskyi <ihor@linux.com>
2018-07-19 22:58:08 +00:00
Ihor Dvoretskyi
5f4160766d Debian package installation information added
As the package is currently available for Debian Buster and Sid, the brief installation information added.
2018-07-18 22:53:09 +03:00
Ahmet Alp Balkan
c606382a62 Respect $NO_COLOR
Fixes #54.

Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
2018-06-10 14:16:05 -07:00
Ahmet Alp Balkan
428fb0045c Merge pull request #53 from prabhu43/master
Configure highlight color for current context & namespace
2018-06-07 12:27:45 -07:00
Prabhu Jayakumar
a97ab8367d Update Readme 2018-06-08 00:35:45 +05:30
Prabhu Jayakumar
9beb1a1587 Configure highlight color for current context & namespace using environment variable 2018-06-05 23:29:08 +05:30
Prabhu Jayakumar
c23c2a9e29 Revert "Use * to indicate current context & current namespace"
This reverts commit dad48e5397.
2018-05-30 00:32:23 +05:30
Prabhu Jayakumar
dad48e5397 Use * to indicate current context & current namespace 2018-05-28 23:03:03 +05:30
Ahmet Alp Balkan
e9fbafc923 Merge pull request #46 from uesteibar/delete-multiple-contexts
Allow deleting multiple contexts at once
2018-05-25 11:06:09 -07:00
Ahmet Alp Balkan
b7af607a91 Merge pull request #48 from xnaveira/portable
Make the scripts portable
2018-05-23 09:26:23 -07:00
Xavier Naveira
bcb89389c6 Make the scripts portable 2018-05-23 17:09:54 +02:00
uesteibar
4a1d73d5fe Allow deleting multiple contexts at once
fixes #39

Example: `kubectx -d ctx1 ctx2`
2018-05-18 23:20:49 +02:00
Ahmet Alp Balkan
f986c148b2 Merge pull request #44 from fiws/patch-1
add Arch AUR note to the readme
2018-04-28 16:35:13 -07:00
Ahmet Alp Balkan
2799a9e331 Fix indentation to 80 chars, make title Arch Linux 2018-04-28 16:34:57 -07:00
Filip Weiss
d61654cb39 add Arch AUR note to the readme 2018-04-28 00:34:00 +02:00
Ahmet Alp Balkan
d1b69a995a Merge pull request #41 from ahmetb/tty_check
Do not print colors if stdout isn't tty
2018-04-17 13:17:10 -07:00
Ahmet Alp Balkan
4520d3f54b Do not print colors if stdout isn't tty
This is previously offered in #18, #22, and #27 and I previously rejected
these PRs. My rationale was to prevent scripting around kubectx as it is
meant to be used as an interactive tool.

But clearly that will be a problem, when you're doing operations like:

    kubectx -d $(kubectx)

which I proposed in #39. Plus this change is harmless. I think this
implementation does a better check than the previous ones.

Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
2018-04-10 21:53:14 -07:00
Ahmet Alp Balkan
d906013970 Fix readme.md
Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
2018-04-10 09:45:57 -07:00
4 changed files with 126 additions and 55 deletions

View File

@@ -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:
![kubectx demo GIF](img/kubectx-demo.gif)
**`kubens`** help you switch between Kubernetes namespaces smoothly:
**`kubens`** helps you switch between Kubernetes namespaces smoothly:
![kubens demo GIF](img/kubens-demo.gif)
# 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.
![kubectx interactive search with fzf](img/kubectx-interactive.gif)
-----
### 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

Binary file not shown.

After

Width:  |  Height:  |  Size: 81 KiB

86
kubectx
View File

@@ -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
View File

@@ -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
}