mirror of
https://github.com/ahmetb/kubectx.git
synced 2026-02-25 17:42:14 +00:00
Compare commits
11 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a21638226f | ||
|
|
28e7c12f51 | ||
|
|
1652420a15 | ||
|
|
543e035090 | ||
|
|
a5e810b837 | ||
|
|
62f3f27889 | ||
|
|
4258f03446 | ||
|
|
b9614bd2e0 | ||
|
|
b3732b309e | ||
|
|
a1bce92cc8 | ||
|
|
1356c37cc0 |
@@ -3,7 +3,9 @@ before_install:
|
||||
- sudo apt-get update -qq
|
||||
- sudo apt-get install -qq bats
|
||||
- sudo curl -fsSL -o /usr/bin/kubectl https://storage.googleapis.com/kubernetes-release/release/v1.13.1/bin/linux/amd64/kubectl
|
||||
- sudo chmod +x /usr/bin/kubectl
|
||||
- sudo chmod +x /usr/bin/kubectl
|
||||
script:
|
||||
- bats test/kubectx.bats
|
||||
- bats test/kubens.bats
|
||||
- shellcheck kubectx
|
||||
- shellcheck kubens
|
||||
|
||||
39
README.md
39
README.md
@@ -1,3 +1,11 @@
|
||||
# `kubectx` + `kubens`: Power tools for kubectl
|
||||
|
||||

|
||||

|
||||

|
||||

|
||||
|
||||
|
||||
This repository provides both `kubectx` and `kubens` tools.
|
||||
|
||||
|
||||
@@ -9,13 +17,14 @@ This repository provides both `kubectx` and `kubens` tools.
|
||||
|
||||
# kubectx(1)
|
||||
|
||||
kubectx is an utility to manage and switch between kubectl(1) contexts.
|
||||
kubectx is a utility to manage and switch between kubectl(1) contexts.
|
||||
|
||||
```
|
||||
USAGE:
|
||||
kubectx : list the contexts
|
||||
kubectx <NAME> : switch to context <NAME>
|
||||
kubectx - : switch to the previous context
|
||||
kubectx -c, --current : show the current context name
|
||||
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)
|
||||
@@ -47,13 +56,14 @@ long context names. You don't have to remember full context names anymore.
|
||||
|
||||
# kubens(1)
|
||||
|
||||
kubens is an utility to switch between Kubernetes namespaces.
|
||||
kubens is a utility to switch between Kubernetes namespaces.
|
||||
|
||||
```
|
||||
USAGE:
|
||||
kubens : list the namespaces
|
||||
kubens <NAME> : change the active namespace
|
||||
kubens - : switch to the previous namespace
|
||||
kubens -c, --current : show the current namespace
|
||||
```
|
||||
|
||||
|
||||
@@ -77,7 +87,9 @@ Active namespace is "default".
|
||||
|
||||
### macOS
|
||||
|
||||
:confetti_ball: Use the [Homebrew](https://brew.sh/) package manager:
|
||||
#### Homebrew
|
||||
|
||||
:confetti_ball: If you use [Homebrew](https://brew.sh/) you can install like this:
|
||||
|
||||
brew install kubectx
|
||||
|
||||
@@ -86,9 +98,15 @@ 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).
|
||||
|
||||
#### MacPorts
|
||||
|
||||
If you use [MacPorts](https://www.macports.org) you can install like this:
|
||||
|
||||
sudo port install kubectx
|
||||
|
||||
### Linux
|
||||
|
||||
Since `kubectx`/`kubens` are written in Bash, you should be able to instal
|
||||
Since `kubectx`/`kubens` are written in Bash, you should be able to install
|
||||
them to any POSIX environment that has Bash installed.
|
||||
|
||||
- Download the `kubectx`, and `kubens` scripts.
|
||||
@@ -140,9 +158,11 @@ 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).
|
||||
Available as official Arch Linux package. Install it via:
|
||||
|
||||
```bash
|
||||
sudo pacman -S kubectx
|
||||
```
|
||||
|
||||
#### Debian/Ubuntu
|
||||
|
||||
@@ -173,7 +193,7 @@ If you like to customize the colors indicating the current namespace or context,
|
||||
|
||||
```
|
||||
export KUBECTX_CURRENT_FGCOLOR=$(tput setaf 6) # blue text
|
||||
export KUBECTX_CURRENT_BGCOLOR=$(tput setaf 7) # white background
|
||||
export KUBECTX_CURRENT_BGCOLOR=$(tput setab 7) # white background
|
||||
```
|
||||
|
||||
Colors in the output can be disabled by setting the
|
||||
@@ -191,7 +211,8 @@ Colors in the output can be disabled by setting the
|
||||
| _“Also using it on a daily basis. This and my zsh config that shows me the current k8s context 😉”_ – [@puja108](https://twitter.com/puja108/status/928742521139810305) |
|
||||
| _“Lately I've found myself using the kubens command more than kubectx. Both very useful though :-)”_ – [@stuartleeks](https://twitter.com/stuartleeks/status/928562850464907264) |
|
||||
| _“yeah kubens rocks!”_ – [@embano1](https://twitter.com/embano1/status/928698440732815360) |
|
||||
| _“Special thanks to Ahmet Alp Balkan for creating kubectx, kubens, and kubectl aliases, as these tools made my life better.”_ – [@strebeld](https://medium.com/@strebeld/5-ways-to-enhance-kubectl-ux-97c8893227a)
|
||||
| _“Special thanks to Ahmet Alp Balkan for creating kubectx, kubens, and kubectl aliases, as these tools made my life better.”_ – [@strebeld](https://medium.com/@strebeld/5-ways-to-enhance-kubectl-ux-97c8893227a) |
|
||||
| _“❤️ this shell script @ahmetb wrote to help make switching between kubectl config contexts a breeze.”_ – [@briandanowski](https://twitter.com/briandanowski/status/1085409568165896193) |
|
||||
|
||||
> If you liked `kubectx`, you may like my [`kubectl-aliases`](https://github.com/ahmetb/kubectl-aliases) project, too.
|
||||
|
||||
|
||||
13
kubectx
13
kubectx
@@ -30,13 +30,14 @@ USAGE:
|
||||
kubectx : list the contexts
|
||||
kubectx <NAME> : switch to context <NAME>
|
||||
kubectx - : switch to the previous context
|
||||
kubectx -c, --current : show the current context name
|
||||
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
|
||||
kubectx -h,--help : show this message
|
||||
EOF
|
||||
}
|
||||
|
||||
@@ -148,6 +149,11 @@ rename_context() {
|
||||
old_name="$(current_context)"
|
||||
fi
|
||||
|
||||
if ! context_exists "${old_name}"; then
|
||||
echo "error: Context \"${old_name}\" not found, can't rename it." >&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
|
||||
@@ -202,6 +208,11 @@ main() {
|
||||
elif [[ "$#" -eq 1 ]]; then
|
||||
if [[ "${1}" == "-" ]]; then
|
||||
swap_context
|
||||
elif [[ "${1}" == '-c' || "${1}" == '--current' ]]; then
|
||||
# we don't call current_context here for two reasons:
|
||||
# - it does not fail when current-context property is not set
|
||||
# - it does not return a trailing newline
|
||||
kubectl config current-context
|
||||
elif [[ "${1}" == '-h' || "${1}" == '--help' ]]; then
|
||||
usage
|
||||
elif [[ "${1}" =~ ^-(.*) ]]; then
|
||||
|
||||
7
kubens
7
kubens
@@ -30,6 +30,7 @@ USAGE:
|
||||
kubens : list the namespaces in the current context
|
||||
kubens <NAME> : change the active namespace of current context
|
||||
kubens - : switch to the previous namespace in this context
|
||||
kubens -c, --current : show the current namespace
|
||||
kubens -h,--help : show this message
|
||||
EOF
|
||||
}
|
||||
@@ -66,7 +67,9 @@ escape_context_name() {
|
||||
}
|
||||
|
||||
namespace_file() {
|
||||
local ctx="$(escape_context_name "${1}")"
|
||||
local ctx
|
||||
|
||||
ctx="$(escape_context_name "${1}")"
|
||||
echo "${KUBENS_DIR}/${ctx}"
|
||||
}
|
||||
|
||||
@@ -195,6 +198,8 @@ main() {
|
||||
usage
|
||||
elif [[ "${1}" == "-" ]]; then
|
||||
swap_namespace
|
||||
elif [[ "${1}" == '-c' || "${1}" == '--current' ]]; then
|
||||
current_namespace
|
||||
elif [[ "${1}" =~ ^-(.*) ]]; then
|
||||
echo "error: unrecognized flag \"${1}\"" >&2
|
||||
usage
|
||||
|
||||
@@ -113,6 +113,33 @@ load common
|
||||
[ "$status" -eq 1 ]
|
||||
}
|
||||
|
||||
@test "-c/--current fails when no context set" {
|
||||
use_config config1
|
||||
|
||||
run "${COMMAND}" -c
|
||||
echo "$output"
|
||||
[ $status -eq 1 ]
|
||||
run "${COMMAND}" --current
|
||||
echo "$output"
|
||||
[ $status -eq 1 ]
|
||||
}
|
||||
|
||||
@test "-c/--current prints the current context" {
|
||||
use_config config1
|
||||
|
||||
run "${COMMAND}" user1@cluster1
|
||||
[ $status -eq 0 ]
|
||||
|
||||
run "${COMMAND}" -c
|
||||
echo "$output"
|
||||
[ $status -eq 0 ]
|
||||
[[ "$output" = "user1@cluster1" ]]
|
||||
run "${COMMAND}" --current
|
||||
echo "$output"
|
||||
[ $status -eq 0 ]
|
||||
[[ "$output" = "user1@cluster1" ]]
|
||||
}
|
||||
|
||||
@test "rename context" {
|
||||
use_config config2
|
||||
|
||||
|
||||
@@ -102,3 +102,43 @@ load common
|
||||
[[ "$status" -eq 1 ]]
|
||||
[[ "$output" = *"current-context is not set"* ]]
|
||||
}
|
||||
|
||||
@test "-c/--current works when no namespace is set on context" {
|
||||
use_config config1
|
||||
switch_context user1@cluster1
|
||||
|
||||
run ${COMMAND} "-c"
|
||||
echo "$output"
|
||||
[[ "$status" -eq 0 ]]
|
||||
[[ "$output" = "default" ]]
|
||||
run ${COMMAND} "--current"
|
||||
echo "$output"
|
||||
[[ "$status" -eq 0 ]]
|
||||
[[ "$output" = "default" ]]
|
||||
}
|
||||
|
||||
@test "-c/--current prints the namespace after it is set" {
|
||||
use_config config1
|
||||
switch_context user1@cluster1
|
||||
${COMMAND} ns1
|
||||
|
||||
run ${COMMAND} "-c"
|
||||
echo "$output"
|
||||
[[ "$status" -eq 0 ]]
|
||||
[[ "$output" = "ns1" ]]
|
||||
run ${COMMAND} "--current"
|
||||
echo "$output"
|
||||
[[ "$status" -eq 0 ]]
|
||||
[[ "$output" = "ns1" ]]
|
||||
}
|
||||
|
||||
@test "-c/--current fails when current context is not set" {
|
||||
use_config config1
|
||||
run ${COMMAND} -c
|
||||
echo "$output"
|
||||
[[ "$status" -eq 1 ]]
|
||||
|
||||
run ${COMMAND} --current
|
||||
echo "$output"
|
||||
[[ "$status" -eq 1 ]]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user