mirror of
https://github.com/ahmetb/kubectx.git
synced 2025-08-06 09:54:10 +00:00
parent
517dae9fc8
commit
3aeb4e76d2
16
kubectx
16
kubectx
@ -40,6 +40,11 @@ USAGE:
|
|||||||
EOF
|
EOF
|
||||||
}
|
}
|
||||||
|
|
||||||
|
exit_err() {
|
||||||
|
echo >&2 "${1}"
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
|
||||||
current_context() {
|
current_context() {
|
||||||
kubectl config view -o=jsonpath='{.current-context}'
|
kubectl config view -o=jsonpath='{.current-context}'
|
||||||
}
|
}
|
||||||
@ -50,8 +55,9 @@ get_contexts() {
|
|||||||
|
|
||||||
list_contexts() {
|
list_contexts() {
|
||||||
set -u pipefail
|
set -u pipefail
|
||||||
local cur
|
local cur ctx_list
|
||||||
cur="$(current_context)"
|
cur="$(current_context)" || exit_err "error getting current context"
|
||||||
|
ctx_list=$(get_contexts) || exit_err "error getting context list"
|
||||||
|
|
||||||
local yellow darkbg normal
|
local yellow darkbg normal
|
||||||
yellow=$(tput setaf 3 || true)
|
yellow=$(tput setaf 3 || true)
|
||||||
@ -62,7 +68,7 @@ list_contexts() {
|
|||||||
cur_ctx_fg=${KUBECTX_CURRENT_FGCOLOR:-$yellow}
|
cur_ctx_fg=${KUBECTX_CURRENT_FGCOLOR:-$yellow}
|
||||||
cur_ctx_bg=${KUBECTX_CURRENT_BGCOLOR:-$darkbg}
|
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
|
if [[ -t 1 && -z "${NO_COLOR:-}" && "${c}" = "${cur}" ]]; then
|
||||||
echo "${cur_ctx_bg}${cur_ctx_fg}${c}${normal}"
|
echo "${cur_ctx_bg}${cur_ctx_fg}${c}${normal}"
|
||||||
else
|
else
|
||||||
@ -103,7 +109,7 @@ choose_context_interactive() {
|
|||||||
|
|
||||||
set_context() {
|
set_context() {
|
||||||
local prev
|
local prev
|
||||||
prev="$(current_context)"
|
prev="$(current_context)" || exit_err "error getting current context"
|
||||||
|
|
||||||
switch_context "${1}"
|
switch_context "${1}"
|
||||||
|
|
||||||
@ -152,7 +158,7 @@ delete_context() {
|
|||||||
local ctx
|
local ctx
|
||||||
ctx="${1}"
|
ctx="${1}"
|
||||||
if [[ "${ctx}" == "." ]]; then
|
if [[ "${ctx}" == "." ]]; then
|
||||||
ctx="$(current_context)"
|
ctx="$(current_context)" || exit_err "error getting current context"
|
||||||
fi
|
fi
|
||||||
echo "Deleting context \"${ctx}\"..." >&2
|
echo "Deleting context \"${ctx}\"..." >&2
|
||||||
kubectl config delete-context "${ctx}"
|
kubectl config delete-context "${ctx}"
|
||||||
|
21
kubens
21
kubens
@ -34,10 +34,16 @@ USAGE:
|
|||||||
EOF
|
EOF
|
||||||
}
|
}
|
||||||
|
|
||||||
|
exit_err() {
|
||||||
|
echo >&2 "${1}"
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
|
||||||
current_namespace() {
|
current_namespace() {
|
||||||
local cur_ctx
|
local cur_ctx
|
||||||
cur_ctx="$(current_context)"
|
cur_ctx="$(current_context)" || exit_err "error getting current context"
|
||||||
ns="$(kubectl config view -o=jsonpath="{.contexts[?(@.name==\"${cur_ctx}\")].context.namespace}")"
|
ns="$(kubectl config view -o=jsonpath="{.contexts[?(@.name==\"${cur_ctx}\")].context.namespace}")" \
|
||||||
|
|| exit_err "error getting current namespace"
|
||||||
if [[ -z "${ns}" ]]; then
|
if [[ -z "${ns}" ]]; then
|
||||||
echo "default"
|
echo "default"
|
||||||
else
|
else
|
||||||
@ -107,8 +113,8 @@ choose_namespace_interactive() {
|
|||||||
|
|
||||||
set_namespace() {
|
set_namespace() {
|
||||||
local ctx prev
|
local ctx prev
|
||||||
ctx="$(current_context)"
|
ctx="$(current_context)" || exit_err "error getting current context"
|
||||||
prev="$(current_namespace)"
|
prev="$(current_namespace)" || exit_error "error getting current namespace"
|
||||||
|
|
||||||
if grep -q ^"${1}"\$ <(get_namespaces); then
|
if grep -q ^"${1}"\$ <(get_namespaces); then
|
||||||
switch_namespace "${ctx}" "${1}"
|
switch_namespace "${ctx}" "${1}"
|
||||||
@ -133,8 +139,9 @@ list_namespaces() {
|
|||||||
cur_ctx_bg=${KUBECTX_CURRENT_BGCOLOR:-$darkbg}
|
cur_ctx_bg=${KUBECTX_CURRENT_BGCOLOR:-$darkbg}
|
||||||
|
|
||||||
local cur ns_list
|
local cur ns_list
|
||||||
cur="$(current_namespace)"
|
cur="$(current_namespace)" || exit_err "error getting current namespace"
|
||||||
ns_list=$(get_namespaces)
|
ns_list=$(get_namespaces) || exit_err "error getting namespace list"
|
||||||
|
|
||||||
for c in $ns_list; do
|
for c in $ns_list; do
|
||||||
if [[ -t 1 && -z "${NO_COLOR:-}" && "${c}" = "${cur}" ]]; then
|
if [[ -t 1 && -z "${NO_COLOR:-}" && "${c}" = "${cur}" ]]; then
|
||||||
echo "${cur_ctx_bg}${cur_ctx_fg}${c}${normal}"
|
echo "${cur_ctx_bg}${cur_ctx_fg}${c}${normal}"
|
||||||
@ -146,7 +153,7 @@ list_namespaces() {
|
|||||||
|
|
||||||
swap_namespace() {
|
swap_namespace() {
|
||||||
local ctx ns
|
local ctx ns
|
||||||
ctx="$(current_context)"
|
ctx="$(current_context)" || exit_err "error getting current context"
|
||||||
ns="$(read_namespace "${ctx}")"
|
ns="$(read_namespace "${ctx}")"
|
||||||
if [[ -z "${ns}" ]]; then
|
if [[ -z "${ns}" ]]; then
|
||||||
echo "error: No previous namespace found for current context." >&2
|
echo "error: No previous namespace found for current context." >&2
|
||||||
|
Loading…
Reference in New Issue
Block a user