From a76c55d0f5433379c70fdddfaa5e4daa36ba514e Mon Sep 17 00:00:00 2001 From: Rob Salmond Date: Thu, 12 Dec 2019 07:59:29 -0700 Subject: [PATCH 1/7] add unset flag --- kubectx | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/kubectx b/kubectx index ae5ac6f5..41f72f2b 100755 --- a/kubectx +++ b/kubectx @@ -43,6 +43,7 @@ USAGE: $SELF -d [] : delete context ('.' for current-context) (this command won't delete the user/cluster entry that is used by the context) + $SELF -u, --unset : unset the current context $SELF -h,--help : show this message EOF @@ -185,6 +186,11 @@ delete_context() { $KUBECTL config delete-context "${ctx}" } +unset_context() { + echo "Unsetting default context." >&2 + $KUBECTL config unset current-context +} + main() { if hash kubectl 2>/dev/null; then KUBECTL=kubectl @@ -220,6 +226,8 @@ main() { # - it does not fail when current-context property is not set # - it does not return a trailing newline kubectl config current-context + elif [[ "${1}" == '-u' || "${1}" == '--unset' ]]; then + unset_context elif [[ "${1}" == '-h' || "${1}" == '--help' ]]; then usage elif [[ "${1}" =~ ^-(.*) ]]; then From 6d4fee750e535041e7e84968d3d4cb0d3984ab1c Mon Sep 17 00:00:00 2001 From: rob salmond Date: Tue, 31 Dec 2019 10:55:47 -0700 Subject: [PATCH 2/7] test unsetting selected context --- test/kubectx.bats | 23 +++++++++++++++++++++++ test/testdata/config3 | 24 ++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 test/testdata/config3 diff --git a/test/kubectx.bats b/test/kubectx.bats index cfb24c91..ea63fd4a 100644 --- a/test/kubectx.bats +++ b/test/kubectx.bats @@ -239,3 +239,26 @@ load common [ "$status" -eq 0 ] [[ "$output" = "user2@cluster1" ]] } + +@test "no selected context" { + use_config config1 + + run ${COMMAND} -c + echo "$output" + [ "$status" -eq 1 ] + [[ "$output" = "error: current-context is not set" ]] + +} + +@test "unset selected context" { + use_config config3 + + run ${COMMAND} -u + echo "$output" + [ "$status" -eq 0 ] + + run ${COMMAND} -c + echo "$output" + [ "$status" -eq 1 ] + [[ "$output" = "error: current-context is not set" ]] +} diff --git a/test/testdata/config3 b/test/testdata/config3 new file mode 100644 index 00000000..e1a312b8 --- /dev/null +++ b/test/testdata/config3 @@ -0,0 +1,24 @@ +# config with two contexts + +apiVersion: v1 +clusters: +- cluster: + server: "" + name: cluster1 +contexts: +- context: + cluster: cluster1 + user: user1 + name: user1@cluster1 +- context: + cluster: cluster1 + user: user2 + name: user2@cluster1 +current-context: "user1@cluster1" +kind: Config +preferences: {} +users: +- name: user1 + user: {} +- name: user2 + user: {} From 6d534bb5e819e12933487babe07b335a298d35b7 Mon Sep 17 00:00:00 2001 From: rob salmond Date: Tue, 31 Dec 2019 10:57:50 -0700 Subject: [PATCH 3/7] update readme with new unset flag --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 7dbc4552..7367f390 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,7 @@ USAGE: kubectx -d : delete context ('.' for current-context) (this command won't delete the user/cluster entry that is used by the context) + kubectx -u, --unset : unset the current context ``` ### Usage From 2fff847e487c2c372170d50e730f333795caac36 Mon Sep 17 00:00:00 2001 From: rob salmond Date: Tue, 31 Dec 2019 10:59:13 -0700 Subject: [PATCH 4/7] testdata notes --- test/testdata/config3 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/testdata/config3 b/test/testdata/config3 index e1a312b8..dfcf0bdf 100644 --- a/test/testdata/config3 +++ b/test/testdata/config3 @@ -1,4 +1,4 @@ -# config with two contexts +# config with two contexts and a selected default apiVersion: v1 clusters: From 83d24030df9e4dafbc9bfd5078aa8050796cb0c1 Mon Sep 17 00:00:00 2001 From: rob salmond Date: Thu, 2 Jan 2020 15:26:58 -0700 Subject: [PATCH 5/7] set a current context --- test/testdata/config2 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/testdata/config2 b/test/testdata/config2 index 05db93cc..e1a312b8 100644 --- a/test/testdata/config2 +++ b/test/testdata/config2 @@ -14,7 +14,7 @@ contexts: cluster: cluster1 user: user2 name: user2@cluster1 -current-context: "" +current-context: "user1@cluster1" kind: Config preferences: {} users: From 0106afb124227c446fc5b554d797438f3bde0feb Mon Sep 17 00:00:00 2001 From: rob salmond Date: Thu, 2 Jan 2020 15:41:03 -0700 Subject: [PATCH 6/7] cleanup --- kubectx | 2 +- test/kubectx.bats | 17 ++--------------- test/testdata/config3 | 24 ------------------------ 3 files changed, 3 insertions(+), 40 deletions(-) delete mode 100644 test/testdata/config3 diff --git a/kubectx b/kubectx index 41f72f2b..d2079d5f 100755 --- a/kubectx +++ b/kubectx @@ -187,7 +187,7 @@ delete_context() { } unset_context() { - echo "Unsetting default context." >&2 + echo "Unsetting current context." >&2 $KUBECTL config unset current-context } diff --git a/test/kubectx.bats b/test/kubectx.bats index ea63fd4a..a11eab20 100644 --- a/test/kubectx.bats +++ b/test/kubectx.bats @@ -240,25 +240,12 @@ load common [[ "$output" = "user2@cluster1" ]] } -@test "no selected context" { - use_config config1 - - run ${COMMAND} -c - echo "$output" - [ "$status" -eq 1 ] - [[ "$output" = "error: current-context is not set" ]] - -} - @test "unset selected context" { - use_config config3 + use_config config2 run ${COMMAND} -u - echo "$output" [ "$status" -eq 0 ] run ${COMMAND} -c - echo "$output" - [ "$status" -eq 1 ] - [[ "$output" = "error: current-context is not set" ]] + [ "$status" -ne 0 ] } diff --git a/test/testdata/config3 b/test/testdata/config3 deleted file mode 100644 index dfcf0bdf..00000000 --- a/test/testdata/config3 +++ /dev/null @@ -1,24 +0,0 @@ -# config with two contexts and a selected default - -apiVersion: v1 -clusters: -- cluster: - server: "" - name: cluster1 -contexts: -- context: - cluster: cluster1 - user: user1 - name: user1@cluster1 -- context: - cluster: cluster1 - user: user2 - name: user2@cluster1 -current-context: "user1@cluster1" -kind: Config -preferences: {} -users: -- name: user1 - user: {} -- name: user2 - user: {} From e3a338ea69cfca7c6e653f205f412baa336b5bd9 Mon Sep 17 00:00:00 2001 From: rob salmond Date: Thu, 2 Jan 2020 16:09:24 -0700 Subject: [PATCH 7/7] omit fixture changes --- test/kubectx.bats | 3 +++ test/testdata/config2 | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/test/kubectx.bats b/test/kubectx.bats index a11eab20..d5e0e64b 100644 --- a/test/kubectx.bats +++ b/test/kubectx.bats @@ -243,6 +243,9 @@ load common @test "unset selected context" { use_config config2 + run ${COMMAND} user1@cluster1 + [ "$status" -eq 0 ] + run ${COMMAND} -u [ "$status" -eq 0 ] diff --git a/test/testdata/config2 b/test/testdata/config2 index e1a312b8..05db93cc 100644 --- a/test/testdata/config2 +++ b/test/testdata/config2 @@ -14,7 +14,7 @@ contexts: cluster: cluster1 user: user2 name: user2@cluster1 -current-context: "user1@cluster1" +current-context: "" kind: Config preferences: {} users: