Skip to content

add unset flag #187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Feb 4, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ USAGE:
kubectx -d <NAME> : delete context <NAME> ('.' 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
Expand Down
8 changes: 8 additions & 0 deletions kubectx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ USAGE:
$SELF -d <NAME> [<NAME...>] : delete context <NAME> ('.' 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
Expand Down Expand Up @@ -185,6 +186,11 @@ delete_context() {
$KUBECTL config delete-context "${ctx}"
}

unset_context() {
echo "Unsetting default context." >&2
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

default -> current

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this message is not really necessary, as kubectl will always print Property "current-context" unset. even though the file is empty.

$ KUBECONFIG=$(mktemp) kubectl config unset current-context
Property "current-context" unset.

So the "progress" message is not necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just going for consistency. Eg.

kubectx -d gke_dl-rapid_us-central1-a_rapid-dev
Deleting context "gke_dl-rapid_us-central1-a_rapid-dev"...
deleted context gke_dl-rapid_us-central1-a_rapid-dev from /Users/rsa/.kube/config

$KUBECTL config unset current-context
}

main() {
if hash kubectl 2>/dev/null; then
KUBECTL=kubectl
Expand Down Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions test/kubectx.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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" ]]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this message is coming from kubectl. we shouldn't test it as we have no control over it.

maybe let's just do this:

[ "$status" -ne 0 ] and test that there must be a failure.

tbh you can just omit this test in this patch. it's not about -u.


}

@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" ]]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto let's not check the error message equality here, it's coming from kubectl, and may change.

}
24 changes: 24 additions & 0 deletions test/testdata/config3
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# config with two contexts and a selected default
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need a new file?
You can use another 2-context file, and set a context in the test (if that’s the only difference).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah good question, I didn't want to make existing / future tests more complicated by overloading fixtures but happy to merge into config2 if you are.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I meant was that you can use an existing testdata file, and first set a context in test code, and then unset it. This way you wouldn't be modifying the existing testdata file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah that makes more sense!


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: {}