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

Ability to set Namespace when switching contexts #1

Closed
jwhitcraft opened this Issue May 1, 2017 · 9 comments

Comments

Projects
None yet
2 participants
@jwhitcraft

jwhitcraft commented May 1, 2017

When switching a context, it would be nice to be able to set the active (default) namespace for the context as well.

kubectx my-cluster test-namespace

If test-namespace is left off, it would just be ignored.

@ahmetb

This comment has been minimized.

Owner

ahmetb commented May 1, 2017

@jwhitcraft Interesting idea. I don’t have a lot of daily experience with namespaces. It looks like if you want to associate a namespace with a context entry, you need to create a new context entry (or modify the existing context entry).

Please correct me if I'm wrong, but in your example, my-cluster would always be associated with the test-namespace. I can think of refactoring the kubectx alias functionality to accept a namespace argument like this:

kubectx prod=my-cluster
kubectx test=my-cluster:test-namespace

because there's no way to do kubectl config use-namespace today. What do you think of this?

@jwhitcraft

This comment has been minimized.

jwhitcraft commented May 2, 2017

@ahmetb, that works for me. Currently I do this in my bash script which allows me to keep the same context but change the active namespace at will.

function k8sns() {
    ctx=`kubectl config current-context`
    ns=$1

    // verify that the namespace exists
    ns=`kubectl get namespace $1 --no-headers --output=go-template={{.metadata.name}} 2>/dev/null`

    if [ -z "${ns}" ]; then
        echo "Namespace (${1}) not found, using default"
        ns="default"
    fi

    kubectl config set-context ${ctx} --namespace="${ns}"
}
@ahmetb

This comment has been minimized.

Owner

ahmetb commented May 2, 2017

@jwhitcraft I see, that's essentially modifying your context. But it should be fine. I have an alternative proposal:

We can ship an additional kubens script, just like kubectx it can list/auto-complete the namespaces active in the current context and modify the current context to use the namespace, like your script does. It would also provide switching back and forth between namespaces using kubens –.

Which solution do you like better, a separate tool, or aliasing feature charged with namespaces?

@jwhitcraft

This comment has been minimized.

jwhitcraft commented May 2, 2017

@ahmetb a seperate tool would be best the more and more i think about it.

@ahmetb ahmetb referenced this issue May 9, 2017

Closed

Rename kubectx #3

@ahmetb

This comment has been minimized.

Owner

ahmetb commented May 11, 2017

@jwhitcraft I just hacked a kubens tool. Can you try check out this branch, add it to your PATH and try kubens? https://github.com/ahmetb/kubectx/tree/wip-kubens I appreciate feedback.

@jwhitcraft

This comment has been minimized.

jwhitcraft commented May 15, 2017

@ahmetb I just got back to work and i'll get this setup today.

Thanks!

@ahmetb

This comment has been minimized.

Owner

ahmetb commented May 16, 2017

@jwhitcraft have you got any feedback?

@jwhitcraft

This comment has been minimized.

jwhitcraft commented May 16, 2017

@ahmetb, so far working well. nice job 👍

ahmetb added a commit that referenced this issue May 17, 2017

Add kubens tool for namespace switching
Fixes #1. Now kubectx also ships with kubens.

Extracted utility functions to a utils.bash file, loaded
from ../include/utils.bash.

Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>

@ahmetb ahmetb closed this in #6 May 17, 2017

@ahmetb

This comment has been minimized.

Owner

ahmetb commented May 17, 2017

This can now be installed brew update && brew install kubectx --HEAD

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment