Skip to content

Commit

Permalink
Introduce -c/--current options for kubectx/kubens (#171)
Browse files Browse the repository at this point in the history
Per #127 the user community wants to have this feature, primarily as
-c and --current flags.

Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
  • Loading branch information
ahmetb committed Aug 30, 2019
1 parent 1652420 commit 28e7c12
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -24,6 +24,7 @@ USAGE:
kubectx : list the contexts kubectx : list the contexts
kubectx <NAME> : switch to context <NAME> kubectx <NAME> : switch to context <NAME>
kubectx - : switch to the previous context 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>=<NAME> : rename context <NAME> to <NEW_NAME>
kubectx <NEW_NAME>=. : rename current-context to <NEW_NAME> kubectx <NEW_NAME>=. : rename current-context to <NEW_NAME>
kubectx -d <NAME> : delete context <NAME> ('.' for current-context) kubectx -d <NAME> : delete context <NAME> ('.' for current-context)
Expand Down Expand Up @@ -62,6 +63,7 @@ USAGE:
kubens : list the namespaces kubens : list the namespaces
kubens <NAME> : change the active namespace kubens <NAME> : change the active namespace
kubens - : switch to the previous namespace kubens - : switch to the previous namespace
kubens -c, --current : show the current namespace
``` ```




Expand Down
6 changes: 6 additions & 0 deletions kubectx
Expand Up @@ -30,6 +30,7 @@ USAGE:
kubectx : list the contexts kubectx : list the contexts
kubectx <NAME> : switch to context <NAME> kubectx <NAME> : switch to context <NAME>
kubectx - : switch to the previous context 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>=<NAME> : rename context <NAME> to <NEW_NAME>
kubectx <NEW_NAME>=. : rename current-context to <NEW_NAME> kubectx <NEW_NAME>=. : rename current-context to <NEW_NAME>
kubectx -d <NAME> [<NAME...>] : delete context <NAME> ('.' for current-context) kubectx -d <NAME> [<NAME...>] : delete context <NAME> ('.' for current-context)
Expand Down Expand Up @@ -207,6 +208,11 @@ main() {
elif [[ "$#" -eq 1 ]]; then elif [[ "$#" -eq 1 ]]; then
if [[ "${1}" == "-" ]]; then if [[ "${1}" == "-" ]]; then
swap_context 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 elif [[ "${1}" == '-h' || "${1}" == '--help' ]]; then
usage usage
elif [[ "${1}" =~ ^-(.*) ]]; then elif [[ "${1}" =~ ^-(.*) ]]; then
Expand Down
3 changes: 3 additions & 0 deletions kubens
Expand Up @@ -30,6 +30,7 @@ USAGE:
kubens : list the namespaces in the current context kubens : list the namespaces in the current context
kubens <NAME> : change the active namespace of current context kubens <NAME> : change the active namespace of current context
kubens - : switch to the previous namespace in this context kubens - : switch to the previous namespace in this context
kubens -c, --current : show the current namespace
kubens -h,--help : show this message kubens -h,--help : show this message
EOF EOF
} }
Expand Down Expand Up @@ -197,6 +198,8 @@ main() {
usage usage
elif [[ "${1}" == "-" ]]; then elif [[ "${1}" == "-" ]]; then
swap_namespace swap_namespace
elif [[ "${1}" == '-c' || "${1}" == '--current' ]]; then
current_namespace
elif [[ "${1}" =~ ^-(.*) ]]; then elif [[ "${1}" =~ ^-(.*) ]]; then
echo "error: unrecognized flag \"${1}\"" >&2 echo "error: unrecognized flag \"${1}\"" >&2
usage usage
Expand Down
27 changes: 27 additions & 0 deletions test/kubectx.bats
Expand Up @@ -113,6 +113,33 @@ load common
[ "$status" -eq 1 ] [ "$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" { @test "rename context" {
use_config config2 use_config config2


Expand Down
40 changes: 40 additions & 0 deletions test/kubens.bats
Expand Up @@ -102,3 +102,43 @@ load common
[[ "$status" -eq 1 ]] [[ "$status" -eq 1 ]]
[[ "$output" = *"current-context is not set"* ]] [[ "$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 ]]
}

0 comments on commit 28e7c12

Please sign in to comment.