-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Add filtering contexts #432
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,7 +44,7 @@ func (k *Kubeconfig) contextNode(name string) (*yaml.Node, error) { | |
return nil, errors.Errorf("context with name \"%s\" not found", name) | ||
} | ||
|
||
func (k *Kubeconfig) ContextNames() []string { | ||
func (k *Kubeconfig) ContextNames(filters map[string]string) []string { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm open to suggestions how you want to handle optional parameters, be it a struct, separate function, or anything else |
||
contexts := valueOf(k.rootNode, "contexts") | ||
if contexts == nil { | ||
return nil | ||
|
@@ -54,8 +54,19 @@ func (k *Kubeconfig) ContextNames() []string { | |
} | ||
|
||
var ctxNames []string | ||
ctxLoop: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe this could be refactored to avoid syntax that feels like a |
||
for _, ctx := range contexts.Content { | ||
nameVal := valueOf(ctx, "name") | ||
for k, v := range filters { | ||
ctxVal := valueOf(ctx, "context") | ||
if ctxVal == nil { | ||
continue ctxLoop | ||
} | ||
vVal := valueOf(ctxVal, k) | ||
if vVal == nil || vVal.Value != v { | ||
continue ctxLoop | ||
} | ||
} | ||
if nameVal != nil { | ||
ctxNames = append(ctxNames, nameVal.Value) | ||
} | ||
|
@@ -64,7 +75,7 @@ func (k *Kubeconfig) ContextNames() []string { | |
} | ||
|
||
func (k *Kubeconfig) ContextExists(name string) bool { | ||
ctxNames := k.ContextNames() | ||
ctxNames := k.ContextNames(nil) | ||
for _, v := range ctxNames { | ||
if v == name { | ||
return true | ||
|
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.
The call will be
kubectx --list cluster=c1
so it does not collide with any existing functionality