Skip to content
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

Closed
wants to merge 1 commit into from
Closed

Add filtering contexts #432

wants to merge 1 commit into from

Conversation

malarzm
Copy link

@malarzm malarzm commented Nov 25, 2024

In our work we're using separate contexts for each shop we're running. We have a custom tool that automates running same commands across multiple apps, it boils down to being a fancy for loop over kubectx's contexts :) Lately we've found ourselves in a situation we need to be able to run things across all shops but only within one cluster. To keep shop vs cluster configuration in one place we thought it'd be nice if kubectx could filter contexts for us, thus this PR.

Disclaimer: this is very first time I've written in Go so I could have misplaced/misnamed/missed some obvious stuff. I'll be updating this PR with all suggestions you may have

@@ -40,6 +40,16 @@ func parseArgs(argv []string) Op {
return ListOp{}
}

if argv[0] == "--list" || argv[0] == "-l" {
Copy link
Author

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

@@ -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 {
Copy link
Author

@malarzm malarzm Nov 25, 2024

Choose a reason for hiding this comment

The 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

@@ -54,8 +54,19 @@ func (k *Kubeconfig) ContextNames() []string {
}

var ctxNames []string
ctxLoop:
Copy link
Author

@malarzm malarzm Nov 25, 2024

Choose a reason for hiding this comment

The 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 goto to me, maybe having the filtering as a separate function instead of a nested loop. I'm open to suggestions

@malarzm malarzm marked this pull request as ready for review November 25, 2024 13:51
@dosubot dosubot bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Nov 25, 2024
@malarzm
Copy link
Author

malarzm commented Nov 29, 2024

@ahmetb is this functionality something you'd even consider merging? We'll work on something on our end if it's not a good fit

@ahmetb
Copy link
Owner

ahmetb commented Nov 29, 2024

Have you tried the fzf integration in kubectx? I think it does a lot of what you're trying to achieve here without us having to maintain extra code.

@malarzm
Copy link
Author

malarzm commented Nov 29, 2024

If I understand correctly, fzf allows using interactive mode. In our case there will be no interactivity, as it's a script that needs to get filtered contexts. To illustrate, we'd like to have kubemob cluster=c1 disable-cron xxx (kubemob being a fancy for loop I've mentioned in the original description) and it should

  1. Somehow get all contexts that operate within cluster: c1
  2. Do its thing (disable cron) for all contexts

I'm not sure how to utilize fzf in this scenario

@ahmetb
Copy link
Owner

ahmetb commented Nov 29, 2024

Sorry but kubectx isn't meant to be scripted around. This is explained in the README. :) We dont provide a guaranteed output format. Use kubectl directly for that.

@malarzm
Copy link
Author

malarzm commented Nov 30, 2024

All right, thank you for your time!

@malarzm malarzm closed this Nov 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size:M This PR changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants