Skip to content
Open
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
41 changes: 29 additions & 12 deletions kubens
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ exit_err() {
exit 1
}

am_i_logged_in() {
if ! $KUBECTL auth whoami >/dev/null 2>&1 ; then
exit_err "You are not logged in to this context. Log in first. Aborting..."
fi
}

current_namespace() {
local cur_ctx

Expand Down Expand Up @@ -100,8 +106,20 @@ save_namespace() {

switch_namespace() {
local ctx="${1}"
$KUBECTL config set-context "${ctx}" --namespace="${2}"
echo "Active namespace is \"${2}\".">&2
local ns="${2}"
local ret=0
local verb="get"
local resource="pods"
local perm="${verb} ${resource}"
if $KUBECTL -n ${ns} auth can-i ${verb} ${resource} >/dev/null 2>&1
then
$KUBECTL config set-context "${ctx}" --namespace="${ns}"
echo "Active namespace is \"${ns}\".">&2
else
echo "Not changing active namespace to \"${ns}\", as permission to \"${perm}\" is missing."
ret=1
fi
return ${ret}
}

choose_namespace_interactive() {
Expand Down Expand Up @@ -130,15 +148,8 @@ set_namespace() {
ctx="$(current_context)" || exit_err "error getting current context"
prev="$(current_namespace)" || exit_error "error getting current namespace"

if grep -q ^"${1}"\$ <(get_namespaces); then
switch_namespace "${ctx}" "${1}"

if [[ "${prev}" != "${1}" ]]; then
save_namespace "${ctx}" "${prev}"
fi
else
echo "error: no namespace exists with name \"${1}\".">&2
exit 1
if ! switch_namespace "${ctx}" "${1}"; then
echo "Failed to swich namespace to ${1}"
fi
}

Expand Down Expand Up @@ -188,12 +199,18 @@ main() {
KUBECTL=kubectl
elif hash kubectl.exe 2>/dev/null; then
KUBECTL=kubectl.exe
elif hash oc 2>/dev/null; then
KUBECTL=oc
elif hash oc.exe 2>/dev/null; then
KUBECTL=oc.exe
else
echo >&2 "kubectl is not installed"
echo >&2 "Neither kubectl nor oc is installed. Aborting..."
exit 1
fi
fi

am_i_logged_in

if [[ "$#" -eq 0 ]]; then
if [[ -t 1 && -z ${KUBECTX_IGNORE_FZF:-} && "$(type fzf &>/dev/null; echo $?)" -eq 0 ]]; then
choose_namespace_interactive
Expand Down