Safe to interact with Kubernetes CLI

meddlesome

meddlesome

Posted on April 20, 2022

Safe to interact with Kubernetes CLI

When you interacting with Kubernetes CLI like kubectl or any related commands. It normally interact with current context of kubeconfig file with Project, Cluster, Namespace.

It will mess you up when interacting with multiple projects, clusters, namespaces. Since you cannot sure what is context that you will pull a trigger. And quite error-prone when interact with that.


Current Context Prompt

Ensure on your current context of kubectl by showing current context prompt on your screen when typing related Kubernetes commands.

Context Prompt in your zsh shell

Prerequisites

When you using zsh with romkatv/powerlevel10k theme. It feature "Show in Command" which support kubecontext which can show current context at right hand side of each prompt and always show when you run related Kubernetes commands.


Here is a sample guide to customize on romkatv/powerlevel10k configuration file ~/.p10k.zsh which you can open on any text editor, then edit and reload your shell session.

Show on Command

This will specific a list of command that will trigger to show current context while you typing-in, feel free to adding more if you have more related Kubernetes tools.



typeset -g POWERLEVEL9K_KUBECONTEXT_SHOW_ON_COMMAND='kubectl|helm|kubens|kubectx|oc|istioctl|kogito|k9s|helmfile|fluxctl|stern|ytt'


Enter fullscreen mode Exit fullscreen mode

Customize context detail

You can specific on what Kubernetes context information to show on the prompt when you typing-in any related Kubernetes commands

Many options are available and already commented on ~./p10k.zsh file, but here is my recommended pattern that quite clear when you working with multiple contexts.

CLOUD_PROVIDER/PROJECT_NAME/CLUSTER_NAME/NAMESPACE
Same as above sample images



typeset -g POWERLEVEL9K_KUBECONTEXT_DEFAULT_CONTENT_EXPANSION='${${P9K_KUBECONTEXT_CLOUD_NAME:+/$P9K_KUBECONTEXT_CLOUD_NAME/$P9K_KUBECONTEXT_CLOUD_ACCOUNT/$P9K_KUBECONTEXT_CLOUD_CLUSTER}:-$P9K_KUBECONTEXT_NAME}${${:-/$P9K_KUBECONTEXT_NAMESPACE}:#/default'


Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
meddlesome
meddlesome

Posted on April 20, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Safe to interact with Kubernetes CLI
kubernetes Safe to interact with Kubernetes CLI

April 20, 2022