mirror of
https://github.com/ahmetb/kubectx.git
synced 2026-02-22 16:13:30 +00:00
Compare commits
59 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7bc9a1277c | ||
|
|
bc46739ab5 | ||
|
|
5a1366b7c9 | ||
|
|
5144c0f958 | ||
|
|
11336883cb | ||
|
|
b1324570ad | ||
|
|
5f4160766d | ||
|
|
c606382a62 | ||
|
|
428fb0045c | ||
|
|
a97ab8367d | ||
|
|
9beb1a1587 | ||
|
|
c23c2a9e29 | ||
|
|
dad48e5397 | ||
|
|
e9fbafc923 | ||
|
|
b7af607a91 | ||
|
|
bcb89389c6 | ||
|
|
4a1d73d5fe | ||
|
|
f986c148b2 | ||
|
|
2799a9e331 | ||
|
|
d61654cb39 | ||
|
|
d1b69a995a | ||
|
|
4520d3f54b | ||
|
|
d906013970 | ||
|
|
c4867bc1f2 | ||
|
|
7b6528a4ae | ||
|
|
07583efe3d | ||
|
|
cce04a3279 | ||
|
|
d6f706a28e | ||
|
|
0141d66224 | ||
|
|
fbce3de6b9 | ||
|
|
6610d70ca8 | ||
|
|
80336137bd | ||
|
|
1c9b5c54e8 | ||
|
|
b188f4da88 | ||
|
|
57893cb668 | ||
|
|
8da629d98e | ||
|
|
b2992aa0df | ||
|
|
2f33693466 | ||
|
|
c813642dc3 | ||
|
|
12575f8ce4 | ||
|
|
46d4236f1a | ||
|
|
7ae68ada83 | ||
|
|
146b67d2b0 | ||
|
|
e3ceb14db5 | ||
|
|
6e250aecb6 | ||
|
|
d613c37c16 | ||
|
|
61f1aa8fd7 | ||
|
|
8390860474 | ||
|
|
394288edc0 | ||
|
|
dc6d2e57b3 | ||
|
|
aa215b9eeb | ||
|
|
e82879a185 | ||
|
|
8c17834c36 | ||
|
|
9ed6690978 | ||
|
|
ed2afa7572 | ||
|
|
5d79a0663d | ||
|
|
e7131add7a | ||
|
|
fd91e53d1f | ||
|
|
b06fcaf054 |
@@ -1,26 +0,0 @@
|
||||
class Kubectx < Formula
|
||||
desc "Tool that can switch between kubectl contexts easily and create aliases"
|
||||
homepage "https://github.com/ahmetb/kubectx"
|
||||
url "https://github.com/ahmetb/kubectx/archive/v0.3.0.tar.gz"
|
||||
sha256 "720b6d9a960a7583983ac84c90d2ff89f4494584cf82a3abb5d4e66e72444621"
|
||||
head "https://github.com/ahmetb/kubectx.git", :branch => "short-names"
|
||||
bottle :unneeded
|
||||
|
||||
option "with-short-names", "link as \"kctx\" and \"kns\" instead"
|
||||
|
||||
def install
|
||||
bin.install "kubectx" => build.with?("short-names") ? "kctx" : "kubectx"
|
||||
bin.install "kubens" => build.with?("short-names") ? "kns" : "kubens"
|
||||
include.install "utils.bash"
|
||||
|
||||
bash_completion.install "completion/kubectx.bash" => "kubectx"
|
||||
bash_completion.install "completion/kubens.bash" => "kubens"
|
||||
zsh_completion.install "completion/kubectx.zsh" => "_kubectx"
|
||||
zsh_completion.install "completion/kubens.zsh" => "_kubens"
|
||||
end
|
||||
|
||||
test do
|
||||
system "which", build.with?("short-names") ? "kctx" : "kubectx"
|
||||
system "which", build.with?("short-names") ? "kns" : "kubens"
|
||||
end
|
||||
end
|
||||
118
README.md
118
README.md
@@ -1,6 +1,11 @@
|
||||
This repository provides both `kubectx` and `kubens` tools. Purpose of this
|
||||
project is to provide an utility and facilitate discussion about how `kubectl`
|
||||
can manage contexts better.
|
||||
This repository provides both `kubectx` and `kubens` tools.
|
||||
|
||||
|
||||
**`kubectx`** helps you switch between clusters back and forth:
|
||||

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

|
||||
|
||||
# kubectx(1)
|
||||
|
||||
@@ -9,15 +14,15 @@ kubectx is an utility to manage and switch between kubectl(1) contexts.
|
||||
```
|
||||
USAGE:
|
||||
kubectx : list the contexts
|
||||
kubectx <NAME> : switch to context
|
||||
kubectx <NAME> : switch to context <NAME>
|
||||
kubectx - : switch to the previous context
|
||||
kubectx <NEW_NAME>=<NAME> : create alias for context
|
||||
kubectx -h,--help : show this message
|
||||
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)
|
||||
```
|
||||
|
||||
Purpose of this project is to provide an utility and facilitate discussion
|
||||
about how `kubectl` can manage contexts better.
|
||||
|
||||
### Usage
|
||||
|
||||
```sh
|
||||
@@ -35,7 +40,7 @@ Context "dublin" set.
|
||||
Aliased "gke_ahmetb_europe-west1-b_dublin" as "dublin".
|
||||
```
|
||||
|
||||
`kubectx` supports <kbd>Tab</kbd> completion on bash/zsh shells to help with
|
||||
`kubectx` supports <kbd>Tab</kbd> completion on bash/zsh/fish shells to help with
|
||||
long context names. You don't have to remember full context names anymore.
|
||||
|
||||
-----
|
||||
@@ -49,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
|
||||
```
|
||||
|
||||
|
||||
@@ -65,28 +69,98 @@ Context "test" set.
|
||||
Active namespace is "default".
|
||||
```
|
||||
|
||||
`kubens` also supports <kbd>Tab</kbd> completion on bash/zsh shells.
|
||||
`kubens` also supports <kbd>Tab</kbd> completion on bash/zsh/fish shells.
|
||||
|
||||
-----
|
||||
|
||||
## Installation
|
||||
|
||||
**For macOS:**
|
||||
### macOS
|
||||
|
||||
> Use [Homebrew](https://brew.sh/) package manager:
|
||||
>
|
||||
> brew tap ahmetb/kubectx https://github.com/ahmetb/kubectx.git
|
||||
> brew install kubectx
|
||||
> this will also set up bash/zsh completion scripts automatically.
|
||||
:confetti_ball: Use the [Homebrew](https://brew.sh/) package manager:
|
||||
|
||||
Running `brew install` with `--with-short-names` will install tools with names
|
||||
brew install kubectx
|
||||
|
||||
This command will set up bash/zsh/fish completion scripts automatically.
|
||||
|
||||
|
||||
- Running `brew install` with `--with-short-names` will install tools with names
|
||||
`kctx` and `kns` to prevent prefix collision with `kubectl` name.
|
||||
|
||||
**Other platforms:**
|
||||
- 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).
|
||||
|
||||
> Download the `kubectx` script, make it executable and add it to your PATH. You
|
||||
> can also install bash/zsh [completion scripts](completion/) manually.
|
||||
### Linux
|
||||
|
||||
Since `kubectx`/`kubens` are written in Bash, you should be able to instal
|
||||
them to any POSIX environment that has Bash installed.
|
||||
|
||||
- Download the `kubectx`, and `kubens` scripts.
|
||||
- Either:
|
||||
- save them all to somewhere in your `PATH`,
|
||||
- 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/).
|
||||
|
||||
Example installation steps:
|
||||
|
||||
``` bash
|
||||
sudo git clone https://github.com/ahmetb/kubectx /opt/kubectx
|
||||
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
|
||||
```
|
||||
|
||||
-----
|
||||
|
||||
### Customizing current context 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
|
||||
|
||||
| What are others saying about kubectx? |
|
||||
| ---- |
|
||||
| _“Thank you for kubectx & kubens - I use them all the time & have them in my k8s toolset to maintain happiness :) ”_ – [@pbouwer](https://twitter.com/pbouwer/status/925896377929949184) |
|
||||
| _“I can't imagine working without kubectx and especially kubens anymore. It's pure gold.”_ – [@timoreimann](https://twitter.com/timoreimann/status/925801946757419008) |
|
||||
| _“I'm liking kubectx from @ahmetb, makes it super-easy to switch #Kubernetes contexts [...]”_ — [@lizrice](https://twitter.com/lizrice/status/928556415517589505) |
|
||||
| _“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)
|
||||
|
||||
> If you liked `kubectx`, you may like my [`kubectl-aliases`](https://github.com/ahmetb/kubectl-aliases) project, too.
|
||||
|
||||
-----
|
||||
|
||||
Disclaimer: This is not an official Google product.
|
||||
|
||||
|
||||
#### Stargazers over time
|
||||
|
||||
[](https://starcharts.herokuapp.com/ahmetb/kubectx)
|
||||
|
||||
|
||||
3
completion/kubectx.fish
Normal file
3
completion/kubectx.fish
Normal file
@@ -0,0 +1,3 @@
|
||||
# kubectx
|
||||
complete -f -c kubectx -a "- (kubectl config get-contexts --output='name')"
|
||||
|
||||
@@ -5,7 +5,7 @@ PREV=""
|
||||
if [ -f "$KUBECTX" ]; then
|
||||
# show '-' only if there's a saved previous context
|
||||
local PREV=$(cat "${KUBECTX}")
|
||||
_arguments "1: :((-\:Back\ to\ ${PREV} \
|
||||
_arguments "1: :((- \
|
||||
$(kubectl config get-contexts --output='name')))"
|
||||
else
|
||||
_arguments "1: :($(kubectl config get-contexts --output='name'))"
|
||||
|
||||
3
completion/kubens.fish
Normal file
3
completion/kubens.fish
Normal file
@@ -0,0 +1,3 @@
|
||||
# kubens
|
||||
complete -f -c kubens -a "(kubectl get ns -o=custom-columns=NAME:.metadata.name --no-headers)"
|
||||
|
||||
BIN
img/kubectx-demo.gif
Normal file
BIN
img/kubectx-demo.gif
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 163 KiB |
BIN
img/kubens-demo.gif
Normal file
BIN
img/kubens-demo.gif
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 226 KiB |
113
kubectx
113
kubectx
@@ -1,4 +1,4 @@
|
||||
#!/bin/bash
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# kubectx(1) is a utility to manage and switch between kubectl contexts.
|
||||
|
||||
@@ -21,39 +21,50 @@
|
||||
set -eou pipefail
|
||||
IFS=$'\n\t'
|
||||
|
||||
SCRIPT_DIR="$(dirname "$( readlink -f "${0}" 2>/dev/null || \
|
||||
python -c "import os,sys; print(os.path.realpath(sys.argv[1]))" "${0}" )")"
|
||||
|
||||
if [[ -f "${SCRIPT_DIR}/utils.bash" ]]; then
|
||||
source "${SCRIPT_DIR}/utils.bash"
|
||||
else
|
||||
source "${SCRIPT_DIR}/../include/utils.bash"
|
||||
fi
|
||||
|
||||
KUBECTX="${HOME}/.kube/kubectx"
|
||||
|
||||
usage() {
|
||||
cat <<"EOF"
|
||||
USAGE:
|
||||
kubectx : list the contexts
|
||||
kubectx <NAME> : switch to context
|
||||
kubectx - : switch to the previous context
|
||||
kubectx <NEW_NAME>=<NAME> : create alias for 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() {
|
||||
kubectl config view -o=jsonpath='{.current-context}'
|
||||
}
|
||||
|
||||
get_contexts() {
|
||||
kubectl config get-contexts -o=name | sort -n
|
||||
}
|
||||
|
||||
list_contexts() {
|
||||
set -u pipefail
|
||||
local cur="$(current_context)"
|
||||
local yellow=$(tput setaf 3)
|
||||
local darkbg=$(tput setab 0)
|
||||
local normal=$(tput sgr0)
|
||||
local cur
|
||||
cur="$(current_context)"
|
||||
|
||||
local yellow darkbg normal
|
||||
yellow=$(tput setaf 3)
|
||||
darkbg=$(tput setab 0)
|
||||
normal=$(tput sgr0)
|
||||
|
||||
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
|
||||
@@ -67,7 +78,8 @@ read_context() {
|
||||
}
|
||||
|
||||
save_context() {
|
||||
local saved="$(read_context)"
|
||||
local saved
|
||||
saved="$(read_context)"
|
||||
|
||||
if [[ "${saved}" != "${1}" ]]; then
|
||||
printf %s "${1}" > "${KUBECTX}"
|
||||
@@ -79,7 +91,8 @@ switch_context() {
|
||||
}
|
||||
|
||||
set_context() {
|
||||
local prev="$(current_context)"
|
||||
local prev
|
||||
prev="$(current_context)"
|
||||
|
||||
switch_context "${1}"
|
||||
|
||||
@@ -89,7 +102,8 @@ set_context() {
|
||||
}
|
||||
|
||||
swap_context() {
|
||||
local ctx="$(read_context)"
|
||||
local ctx
|
||||
ctx="$(read_context)"
|
||||
if [[ -z "${ctx}" ]]; then
|
||||
echo "error: No previous context found." >&2
|
||||
exit 1
|
||||
@@ -98,39 +112,74 @@ swap_context() {
|
||||
}
|
||||
|
||||
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}"
|
||||
}
|
||||
|
||||
alias_context() {
|
||||
context_exists() {
|
||||
grep -q ^"${1}"\$ <(kubectl config get-contexts -o=name)
|
||||
}
|
||||
|
||||
rename_context() {
|
||||
local old_name="${1}"
|
||||
local new_name="${2}"
|
||||
|
||||
local old_user="$(user_of_context "${old_name}")"
|
||||
local old_cluster="$(cluster_of_context "${old_name}")"
|
||||
if [[ "${old_name}" == "." ]]; then
|
||||
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
|
||||
|
||||
kubectl config set-context "${new_name}" \
|
||||
--cluster="${old_cluster}" \
|
||||
--user="${old_user}" \
|
||||
if context_exists "${new_name}"; then
|
||||
echo "Context \"${new_name}\" exists, deleting..." >&2
|
||||
kubectl config delete-context "${new_name}" 1>/dev/null 2>&1
|
||||
fi
|
||||
|
||||
echo "Aliased \"${old_name}\" as \"${new_name}\"." >&2
|
||||
kubectl config rename-context "${old_name}" "${new_name}"
|
||||
}
|
||||
|
||||
delete_contexts() {
|
||||
IFS=' ' read -ra CTXS <<< "${1}"
|
||||
for i in "${CTXS[@]}"; do
|
||||
delete_context "${i}"
|
||||
done
|
||||
}
|
||||
|
||||
delete_context() {
|
||||
local ctx
|
||||
ctx="${1}"
|
||||
if [[ "${ctx}" == "." ]]; then
|
||||
ctx="$(current_context)"
|
||||
fi
|
||||
echo "Deleting context \"${ctx}\"..." >&2
|
||||
kubectl config delete-context "${ctx}"
|
||||
}
|
||||
|
||||
main() {
|
||||
if [[ "$#" -eq 0 ]]; then
|
||||
list_contexts
|
||||
elif [[ "${1}" == "-d" ]]; then
|
||||
if [[ "$#" -lt 2 ]]; then
|
||||
echo "error: missing context NAME" >&2
|
||||
usage
|
||||
fi
|
||||
delete_contexts "${@:2}"
|
||||
elif [[ "$#" -gt 1 ]]; then
|
||||
echo "error: too many flags" >&2
|
||||
echo "error: too many arguments" >&2
|
||||
usage
|
||||
elif [[ "$#" -eq 1 ]]; then
|
||||
if [[ "${1}" == "-" ]]; then
|
||||
@@ -141,7 +190,7 @@ main() {
|
||||
echo "error: unrecognized flag \"${1}\"" >&2
|
||||
usage
|
||||
elif [[ "${1}" =~ (.+)=(.+) ]]; then
|
||||
alias_context "${BASH_REMATCH[2]}" "${BASH_REMATCH[1]}"
|
||||
rename_context "${BASH_REMATCH[2]}" "${BASH_REMATCH[1]}"
|
||||
else
|
||||
set_context "${1}"
|
||||
fi
|
||||
|
||||
67
kubens
67
kubens
@@ -1,4 +1,4 @@
|
||||
#!/bin/bash
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# kubenx(1) is a utility to switch between Kubernetes namespaces.
|
||||
|
||||
@@ -21,15 +21,6 @@
|
||||
set -eou pipefail
|
||||
IFS=$'\n\t'
|
||||
|
||||
SCRIPT_DIR="$(dirname "$( readlink -f "${0}" 2>/dev/null || \
|
||||
python -c "import os,sys; print(os.path.realpath(sys.argv[1]))" "${0}" )")"
|
||||
|
||||
if [[ -f "${SCRIPT_DIR}/utils.bash" ]]; then
|
||||
source "${SCRIPT_DIR}/utils.bash"
|
||||
else
|
||||
source "${SCRIPT_DIR}/../include/utils.bash"
|
||||
fi
|
||||
|
||||
KUBENS_DIR="${HOME}/.kube/kubens"
|
||||
|
||||
usage() {
|
||||
@@ -44,7 +35,8 @@ EOF
|
||||
}
|
||||
|
||||
current_namespace() {
|
||||
local cur_ctx=$(current_context)
|
||||
local cur_ctx
|
||||
cur_ctx="$(current_context)"
|
||||
ns="$(kubectl config view -o=jsonpath="{.contexts[?(@.name==\"${cur_ctx}\")].context.namespace}")"
|
||||
if [[ -z "${ns}" ]]; then
|
||||
echo "default"
|
||||
@@ -53,20 +45,35 @@ current_namespace() {
|
||||
fi
|
||||
}
|
||||
|
||||
current_context() {
|
||||
kubectl config view -o=jsonpath='{.current-context}'
|
||||
}
|
||||
|
||||
get_namespaces() {
|
||||
kubectl get namespaces -o=jsonpath='{range .items[*].metadata.name}{@}{"\n"}{end}'
|
||||
}
|
||||
|
||||
escape_context_name() {
|
||||
echo "${1//\//-}"
|
||||
}
|
||||
|
||||
namespace_file() {
|
||||
local ctx="${1}"
|
||||
local ctx="$(escape_context_name "${1}")"
|
||||
echo "${KUBENS_DIR}/${ctx}"
|
||||
}
|
||||
|
||||
read_namespace() {
|
||||
local f="$(namespace_file "${1}")"
|
||||
local f
|
||||
f="$(namespace_file "${1}")"
|
||||
[[ -f "${f}" ]] && cat "${f}"
|
||||
return 0
|
||||
}
|
||||
|
||||
save_namespace() {
|
||||
mkdir -p "${KUBENS_DIR}"
|
||||
local f="$(namespace_file "${1}")"
|
||||
local saved="$(read_namespace "${1}")"
|
||||
local f saved
|
||||
f="$(namespace_file "${1}")"
|
||||
saved="$(read_namespace "${1}")"
|
||||
|
||||
if [[ "${saved}" != "${2}" ]]; then
|
||||
printf %s "${2}" > "${f}"
|
||||
@@ -80,8 +87,9 @@ switch_namespace() {
|
||||
}
|
||||
|
||||
set_namespace() {
|
||||
local ctx="$(current_context)"
|
||||
local prev="$(current_namespace)"
|
||||
local ctx prev
|
||||
ctx="$(current_context)"
|
||||
prev="$(current_namespace)"
|
||||
|
||||
if grep -q ^"${1}"\$ <(get_namespaces); then
|
||||
switch_namespace "${ctx}" "${1}"
|
||||
@@ -96,15 +104,21 @@ set_namespace() {
|
||||
}
|
||||
|
||||
list_namespaces() {
|
||||
local cur="$(current_namespace)"
|
||||
local yellow darkbg normal
|
||||
yellow=$(tput setaf 3)
|
||||
darkbg=$(tput setab 0)
|
||||
normal=$(tput sgr0)
|
||||
|
||||
local yellow=$(tput setaf 3)
|
||||
local darkbg=$(tput setab 0)
|
||||
local normal=$(tput sgr0)
|
||||
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_namespaces); do
|
||||
if [[ "${c}" = "${cur}" ]]; then
|
||||
echo "${darkbg}${yellow}${c}${normal}"
|
||||
local cur ns_list
|
||||
cur="$(current_namespace)"
|
||||
ns_list=$(get_namespaces)
|
||||
for c in $ns_list; do
|
||||
if [[ -t 1 && -z "${NO_COLOR:-}" && "${c}" = "${cur}" ]]; then
|
||||
echo "${cur_ctx_bg}${cur_ctx_fg}${c}${normal}"
|
||||
else
|
||||
echo "${c}"
|
||||
fi
|
||||
@@ -112,8 +126,9 @@ list_namespaces() {
|
||||
}
|
||||
|
||||
swap_namespace() {
|
||||
local ctx="$(current_context)"
|
||||
local ns="$(read_namespace "${ctx}")"
|
||||
local ctx ns
|
||||
ctx="$(current_context)"
|
||||
ns="$(read_namespace "${ctx}")"
|
||||
if [[ -z "${ns}" ]]; then
|
||||
echo "error: No previous namespace found for current context." >&2
|
||||
exit 1
|
||||
|
||||
11
utils.bash
11
utils.bash
@@ -1,11 +0,0 @@
|
||||
current_context() {
|
||||
kubectl config view -o=jsonpath='{.current-context}'
|
||||
}
|
||||
|
||||
get_contexts() {
|
||||
kubectl config get-contexts -o=name | sort -n
|
||||
}
|
||||
|
||||
get_namespaces() {
|
||||
kubectl get namespaces -o=jsonpath='{range .items[*].metadata.name}{@}{"\n"}{end}'
|
||||
}
|
||||
Reference in New Issue
Block a user