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

feat: add support to set labels without selector #5741

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Mrflatt
Copy link

@Mrflatt Mrflatt commented Aug 3, 2024

Tries to solve issue: #3866

Based on existing feature merged in #4486, uses same command flags with edit set label
Set label removes existing duplicate labels from commonLabels, and labels where includeTemplates value does not match.

Duplicate of #4478, added modifications to command flags and test

Copy link

linux-foundation-easycla bot commented Aug 3, 2024

CLA Signed

The committers listed above are authorized under a signed CLA.

@k8s-ci-robot
Copy link
Contributor

Welcome @Mrflatt!

It looks like this is your first PR to kubernetes-sigs/kustomize 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes-sigs/kustomize has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot k8s-ci-robot added the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Aug 3, 2024
@k8s-ci-robot
Copy link
Contributor

Hi @Mrflatt. Thanks for your PR.

I'm waiting for a kubernetes-sigs member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@k8s-ci-robot k8s-ci-robot added cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Aug 3, 2024
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: Mrflatt
Once this PR has been reviewed and has the lgtm label, please assign stormqueen1990 for approval. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. and removed cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels Aug 3, 2024
@k8s-ci-robot
Copy link
Contributor

This PR has multiple commits, and the default merge method is: merge.
You can request commits to be squashed using the label: tide/merge-method-squash

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

Comment on lines 128 to 154
func (o *setLabelOptions) removeDuplicateLabels(m *types.Kustomization) {
for k := range o.metadata {
delete(m.CommonLabels, k)
for idx, label := range m.Labels {
if label.IncludeTemplates != o.includeTemplates {
m.Labels = deleteLabel(k, label, m.Labels, idx)
}
if label.IncludeSelectors {
m.Labels = deleteLabel(k, label, m.Labels, idx)
}
}
}
}

func deleteLabel(key string, label types.Label, labels []types.Label, idx int) []types.Label {
delete(label.Pairs, key)
if len(label.Pairs) == 0 {
labels = append(labels[:idx], labels[idx+1:]...)
}
return labels
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add some comments explaining the mechanism of action of these functions to deduplicate existing labels?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any specific reason to use assert instead of require for the Equal assertions?

Copy link
Author

@Mrflatt Mrflatt Aug 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, all of these should be require, will change all of them.

@stormqueen1990
Copy link
Member

/label tide/merge-method-squash

@k8s-ci-robot k8s-ci-robot added the tide/merge-method-squash Denotes a PR that should be squashed by tide when it merges. label Aug 28, 2024
@stormqueen1990
Copy link
Member

/test all

Copy link
Member

@stormqueen1990 stormqueen1990 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi there, @Mrflatt! 👋🏻

Thank you for your contribution!

This looks mostly LGTM. I just have one outstanding question: I noticed you wrote code to deduplicate labels when --without-selector is specified, but this current logic still duplicates labels when you execute the following sequence of commands:

  • Run kustomize edit set label test:value1:
    apiVersion: kustomize.config.k8s.io/v1beta1
    kind: Kustomization
    commonLabels:
      test: value1
  • After that, run kustomize edit set label test:value2 --without-selector. This will remove the previously added label and replace it with an entry to the labels key, as expected:
    apiVersion: kustomize.config.k8s.io/v1beta1
    kind: Kustomization
    labels:
    - pairs:
        test: value2
  • However, if I again run kustomize edit set label test:value3, this will cause a duplicate in commonLabels:
    apiVersion: kustomize.config.k8s.io/v1beta1
    kind: Kustomization
    labels:
    - pairs:
        test: value2
    commonLabels:
      test: value3

Would you be interested in addressing this use case as part of this PR as well?

@Mrflatt
Copy link
Author

Mrflatt commented Sep 4, 2024

Hi there, @Mrflatt! 👋🏻

Thank you for your contribution!

This looks mostly LGTM. I just have one outstanding question: I noticed you wrote code to deduplicate labels when --without-selector is specified, but this current logic still duplicates labels when you execute the following sequence of commands:

  • Run kustomize edit set label test:value1:

    apiVersion: kustomize.config.k8s.io/v1beta1
    
    kind: Kustomization
    
    commonLabels:
    
      test: value1
    
  • After that, run kustomize edit set label test:value2 --without-selector. This will remove the previously added label and replace it with an entry to the labels key, as expected:

    apiVersion: kustomize.config.k8s.io/v1beta1
    
    kind: Kustomization
    
    labels:
    
    - pairs:
    
        test: value2
    
  • However, if I again run kustomize edit set label test:value3, this will cause a duplicate in commonLabels:

    apiVersion: kustomize.config.k8s.io/v1beta1
    
    kind: Kustomization
    
    labels:
    
    - pairs:
    
        test: value2
    
    commonLabels:
    
      test: value3
    

Would you be interested in addressing this use case as part of this PR as well?

@stormqueen1990 Yeah, will fix that also. Would solution be to remove labels and add commonLabel? Or modify labels and not add deprecated commonLabel, which would a bit change to functionality

@stormqueen1990
Copy link
Member

@stormqueen1990 Yeah, will fix that also. Would solution be to remove labels and add commonLabel? Or modify labels and not add deprecated commonLabel, which would a bit change to functionality

Hi again, @Mrflatt!

In my opinion the request of the user should be honoured. So if the command issued had --without-selector specified, the labels field should be retained; otherwise, the commonLabels field should be retained.

The main goal for my request was ensuring kustomize edit fix will still work after using this command. It will fail if there are duplicates between these two fields (which makes sense).

@Mrflatt
Copy link
Author

Mrflatt commented Sep 9, 2024

@stormqueen1990 Yeah, will fix that also. Would solution be to remove labels and add commonLabel? Or modify labels and not add deprecated commonLabel, which would a bit change to functionality

Hi again, @Mrflatt!

In my opinion the request of the user should be honoured. So if the command issued had --without-selector specified, the labels field should be retained; otherwise, the commonLabels field should be retained.

The main goal for my request was ensuring kustomize edit fix will still work after using this command. It will fail if there are duplicates between these two fields (which makes sense).

Yeap, it should be fixed. Also simplified duplicate label removing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. tide/merge-method-squash Denotes a PR that should be squashed by tide when it merges.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants