-
-
Notifications
You must be signed in to change notification settings - Fork 961
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
Feature for kubecontext section: Custom colors per context/namespace #576
Feature for kubecontext section: Custom colors per context/namespace #576
Conversation
This is interesting idea, I'd totally use it myself. Here's what I'd like to be added:
I'm wondering how to make the first point generic enough, I don't think hardcoding "popular" groups is good enough. Ideally we allow people to configure as many "rulepattern-color" pairs as they want. |
RegexI love the idea of regex, that will definitely make life easier. $ arr=(*-dev-*)
zsh: no matches found: *-dev-* It can work with actual regex with ctx=(".*-dev-.*" ".*-prod$" exact-context)
...
if [[ $kube_context =~ $crit ]]; then ... Do you think it's good enough to specify that the list of context names is a list of regular expressions rather than strings or expandable patterns? Or is there another way to achieve adding these patterns to a list, to use the syntax you suggested? Edit: $ setopt GLOB_SUBST; [[ "my-dev-cl" == "*-dev-*" ]] && echo y || echo n; unsetopt GLOB_SUBST
n GroupsI agree that it's a great idea, but it sounds like a lot of pain for the user to configure. ctx=(
[red] = (*-prod *-staging)
[yellow] = (*-dev-*)
) but discovered very fast that it's not a supported syntax, and that having a multi-dimensional array is not supported either and is much more pain to implement than its worth :) The next idea is something like: USE_GROUPS=(prod dev blah)
GROUP_prod_COLOR=red
GROUP_prod_PATTERNS=(".+-prod" ".+-staging")
GROUP_dev_COLOR=yellow
GROUP_dev_PATTERNS=(".+-dev-.+") or something in that direction. However, the list of group "names" is really not useful, it's just there to be able to build a list of variables to iterate over (and I still haven't figured how to use regex comparison...) |
Agree on using regex, and using associative arrays is cool too. How about this approach? declare -A SPACESHIP_KUBECONTEXT_COLORGROUPS=(
[-test-]=yellow
[-prod-]=red
[-staging-]=red
)
cluster="my-prod-cluster"
for pattern color in ${(kv)SPACESHIP_KUBECONTEXT_COLORGROUPS}; do
echo "testing $cluster against $pattern"
if [[ "$cluster" =~ "$pattern" ]]; then
echo "matches $pattern!!!"
fi
done UPDATE: apparently |
@maximbaz Your syntax is very usable :) I went with your suggestion and changed the code to support SPACESHIP_KUBECONTEXT_COLORGROUPS and it works quite well! typeset -A SPACESHIP_KUBECONTEXT_COLORGROUPS=(
['dev-system']=green
['production']=red
['staging']=red
['^test-[0-9]+$']=yellow
['\.k8s\.local$']=red
) |
Now it's very cool 👍 Can you explain why you have |
I figured that testing for equality first might have a tiny benefit for performance. If there's exact match, there's really no need to do a regex match. Also, the idea that you can set up first |
I wouldn't worry about this, use |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Works beautifully, nice addition
Thanks for the reviews! |
Friendly ping @salmanulfarzy, this is something very cool and prevents accidentally messing with production environment, definitely worth merging and letting people know that this functionality exists. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great @nomaed 👏 Proposing some minor changes.
Feature: add the ability to specify section color based on context names matching a pattern. Main use case is to mark contexts that should be handled with care, such as "staging" or "production" environments. An example of a use case might be to add the following to .zshrc: ```zsh typeset -A SPACESHIP_KUBECONTEXT_COLORGROUPS=( ['dev-system']=green ['production']=red ['staging']=red ['^test-[0-9]+$']=yellow ['\.k8s\.local$']=red ) ```
Co-Authored-By: nomaed <nomaed@users.noreply.github.com>
I made the changes per review comments:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you @nomaed, Made some minor changes myself.
Co-Authored-By: nomaed <nomaed@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code looks great @nomaed
Super, thank you guys! |
It allows setting SPACESHIP_KUBECONTEXT_COLOR_GROUPS array with colors and patterns that match context names and namespaces, that should change the section to the specified color. This is usually going to be critical environments that we need to make sure we don't modify by mistake, such as "production" or "staging" (as opposed to regular "dev"). Co-Authored-By: Maxim Baz <maximbaz@users.noreply.github.com>
It allows setting SPACESHIP_KUBECONTEXT_COLOR_GROUPS array with colors and patterns that match context names and namespaces, that should change the section to the specified color. This is usually going to be critical environments that we need to make sure we don't modify by mistake, such as "production" or "staging" (as opposed to regular "dev"). Co-Authored-By: Maxim Baz <maximbaz@users.noreply.github.com>
It allows setting SPACESHIP_KUBECONTEXT_COLOR_GROUPS array with colors and patterns that match context names and namespaces, that should change the section to the specified color. This is usually going to be critical environments that we need to make sure we don't modify by mistake, such as "production" or "staging" (as opposed to regular "dev"). Co-Authored-By: Maxim Baz <maximbaz@users.noreply.github.com>
Description
This change adds a new feature for the kubecontext section.
It allows setting
SPACESHIP_KUBECONTEXT_COLORGROUPS
array with colors and patterns that match context names and namespaces, that should change the section to the specified color. This is usually going to be critical environments that we need to make sure we don't modify by mistake, such as "production" or "staging" (as opposed to regular "dev").This is an idea by a colleague after too many people not noticing that they are on the production environment and deploying changes, when all they wanted is to test changes in local development env.
Screenshot