Ramilito / kubesess

Kubectl plugin managing sessions
MIT License
221 stars 5 forks source link

Add option to retrieve current context and namespace #24

Closed MisterTimn closed 2 years ago

MisterTimn commented 2 years ago

Describe the solution you'd like A flag to output the current context and namespaces OR environment variables that get populated on context change. This can then be used to build a custom prompt.

Describe alternatives you've considered Right now I use kubectx -c and kubens -c to get context and namespace to build my prompt. I use this in favor of other solutions because it is significantly faster.

Additional context Since kubesess is apparently more efficient than those tools to switch context/namespace, I was wondering if retrieval of current values would perhaps be as fast or even faster.

If this is added, then I wouldn't have a need for kubectx any longer

Ramilito commented 2 years ago

That sounds like a good idea! I've been using p10k but have been thinking of doing it myself.

The command would look like this: kubesess -c context and kubesess -c namespace

I'll add the same functionality to the default commands as well, might be useful, that would look like: kubesess -c default-context and kubesess -c default-namespace

Did some preliminary testing, and it looks promising!

Getting current context

image image

Getting current namespace

image image

It's not a lot of time gain but I like my console to be snappy.

Ramilito commented 2 years ago

There is a release now, both in binary and homebrew with this feature!

Ramilito commented 2 years ago

Would love it if you would share your prompt script?

MisterTimn commented 2 years ago
prompt_context() {
    KUBE_CTX=$(kubesess -c context)
    KUBE_NS=$(kubesess -c namespace)
    KUBE_PS1="$KUBE_CTX/$KUBE_NS"
    if [[ $KUBE_CTX == *"staging"* ]]; then
        prompt_segment yellow black "$KUBE_PS1"
    elif [[ $KUBE_CTX == *"prod"* ]]; then
        prompt_segment red black "$KUBE_PS1"
    else
        prompt_segment green black "$KUBE_PS1"
    fi
}

This is using the agnoster theme with zsh/oh-my-zsh. Mainly wanted it to be very clear wether im deploying to production, staging or dev clusters. image

Also I've been hacking together a way to merge all my configs, so far coming up with this (all my base configs have .yaml extension)

KUBECONFIG=$(for YAML in $(find ${HOME}/.kube -name '*.yaml') ; do echo -n ":${YAML}"; done)
kubectl config view --raw > tmp && mv tmp $HOME/.kube/config
chmod o-r $HOME/.kube/config
chmod g-r $HOME/.kube/config

Afterwards I source kubesess and set the config back.

Ramilito commented 2 years ago

Hmm I've been meaning to support multiple configs actually, created a feature request about it now, do you have any suggestions on how to make it possible? Do you mind if I copy this part to #26

image
Ramilito commented 2 years ago

And thanks for the prompt script, will try it out!

MisterTimn commented 2 years ago

Do you mind if I copy this part to #26

Go ahead! fine for me. I'll chime in on the issue

Ramilito commented 2 years ago

@MisterTimn Played around with your script a bit and thought that I'd share it back! Got rid of kube-ps1 just to simplify my zshrc file tbh and because it was pretty simple to do what it does.

image