Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 27 additions & 26 deletions kubectx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ SELF_CMD="$0"
KUBECTX="${XDG_CACHE_HOME:-$HOME/.kube}/kubectx"

usage() {
if [ -n "$1" ] ; then
err "$1"
fi

local SELF
SELF="kubectx"
if [[ "$(basename "$0")" == kubectl-* ]]; then # invoked as plugin
Expand All @@ -47,13 +51,24 @@ USAGE:

$SELF -h,--help : show this message
EOF
if [ -n "$1" ] ; then
exit 1
fi
}

exit_err() {
echo >&2 "${1}"
err "${1}"
exit 1
}

err() {
warn "error: ${1}"
}

warn() {
echo >&2 "${1}"
}

current_context() {
$KUBECTL config view -o=jsonpath='{.current-context}'
}
Expand Down Expand Up @@ -117,11 +132,9 @@ choose_context_interactive() {
FZF_DEFAULT_COMMAND="${SELF_CMD}" \
fzf --ansi --no-preview || true)"
if [[ -z "${choice}" ]]; then
echo 2>&1 "error: you did not choose any of the options"
exit 1
else
set_context "${choice}"
exit_err "you did not choose any of the options"
fi
set_context "${choice}"
}

set_context() {
Expand All @@ -139,8 +152,7 @@ swap_context() {
local ctx
ctx="$(read_context)"
if [[ -z "${ctx}" ]]; then
echo "error: No previous context found." >&2
exit 1
exit_err "No previous context found."
fi
set_context "${ctx}"
}
Expand All @@ -158,12 +170,11 @@ rename_context() {
fi

if ! context_exists "${old_name}"; then
echo "error: Context \"${old_name}\" not found, can't rename it." >&2
exit 1
exit_err "Context \"${old_name}\" not found, can't rename it."
fi

if context_exists "${new_name}"; then
echo "Context \"${new_name}\" exists, deleting..." >&2
warn "Context \"${new_name}\" exists, deleting..."
$KUBECTL config delete-context "${new_name}" 1>/dev/null 2>&1
fi

Expand All @@ -182,12 +193,12 @@ delete_context() {
if [[ "${ctx}" == "." ]]; then
ctx="$(current_context)" || exit_err "error getting current context"
fi
echo "Deleting context \"${ctx}\"..." >&2
warn "Deleting context \"${ctx}\"..."
$KUBECTL config delete-context "${ctx}"
}

unset_context() {
echo "Unsetting current context." >&2
warn "Unsetting current context."
$KUBECTL config unset current-context
}

Expand All @@ -198,8 +209,7 @@ main() {
elif hash kubectl.exe 2>/dev/null; then
KUBECTL=kubectl.exe
else
echo >&2 "kubectl is not installed"
exit 1
exit_err "kubectl is not installed"
fi
fi

Expand All @@ -211,15 +221,9 @@ main() {
fi
elif [[ "${1}" == "-d" ]]; then
if [[ "$#" -lt 2 ]]; then
echo "error: missing context NAME" >&2
usage
exit 1
usage "missing context NAME"
fi
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
Expand All @@ -233,17 +237,14 @@ main() {
elif [[ "${1}" == '-h' || "${1}" == '--help' ]]; then
usage
elif [[ "${1}" =~ ^-(.*) ]]; then
echo "error: unrecognized flag \"${1}\"" >&2
usage
exit 1
usage "unrecognized flag \"${1}\""
elif [[ "${1}" =~ (.+)=(.+) ]]; then
rename_context "${BASH_REMATCH[2]}" "${BASH_REMATCH[1]}"
else
set_context "${1}"
fi
else
usage
exit 1
usage "too many arguments"
fi
}

Expand Down
42 changes: 24 additions & 18 deletions kubens
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ SELF_CMD="$0"
KUBENS_DIR="${XDG_CACHE_HOME:-$HOME/.kube}/kubens"

usage() {
if [ -n "$1" ] ; then
err "$1"
fi

local SELF
SELF="kubens"
if [[ "$(basename "$0")" == kubectl-* ]]; then # invoked as plugin
Expand All @@ -40,13 +44,24 @@ USAGE:
$SELF -c, --current : show the current namespace
$SELF -h,--help : show this message
EOF
if [ -n "$1" ] ; then
exit 1
fi
}

exit_err() {
echo >&2 "${1}"
err "${1}"
exit 1
}

err() {
warn "error: ${1}"
}

warn() {
echo >&2 "${1}"
}

current_namespace() {
local cur_ctx

Expand Down Expand Up @@ -101,25 +116,23 @@ save_namespace() {
switch_namespace() {
local ctx="${1}"
$KUBECTL config set-context "${ctx}" --namespace="${2}"
echo "Active namespace is \"${2}\".">&2
warn "Active namespace is \"${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
exit_err "could not list namespaces (is the cluster accessible?)"
fi

local choice
choice="$(_KUBECTX_FORCE_COLOR=1 \
FZF_DEFAULT_COMMAND="${SELF_CMD}" \
fzf --ansi --no-preview || true)"
if [[ -z "${choice}" ]]; then
echo 2>&1 "error: you did not choose any of the options"
exit 1
exit_err "you did not choose any of the options"
else
set_namespace "${choice}"
fi
Expand All @@ -137,8 +150,7 @@ set_namespace() {
save_namespace "${ctx}" "${prev}"
fi
else
echo "error: no namespace exists with name \"${1}\".">&2
exit 1
exit_err "no namespace exists with name \"${1}\"."
fi
}

Expand Down Expand Up @@ -176,8 +188,7 @@ swap_namespace() {
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
exit 1
exit_err "No previous namespace found for current context."
fi
set_namespace "${ns}"
}
Expand All @@ -189,8 +200,7 @@ main() {
elif hash kubectl.exe 2>/dev/null; then
KUBECTL=kubectl.exe
else
echo >&2 "kubectl is not installed"
exit 1
exit_err "kubectl is not installed"
fi
fi

Expand All @@ -208,18 +218,14 @@ main() {
elif [[ "${1}" == '-c' || "${1}" == '--current' ]]; then
current_namespace
elif [[ "${1}" =~ ^-(.*) ]]; then
echo "error: unrecognized flag \"${1}\"" >&2
usage
exit 1
usage "unrecognized flag \"${1}\""
elif [[ "${1}" =~ (.+)=(.+) ]]; then
alias_context "${BASH_REMATCH[2]}" "${BASH_REMATCH[1]}"
else
set_namespace "${1}"
fi
else
echo "error: too many flags" >&2
usage
exit 1
usage "too many flags"
fi
}

Expand Down