Skip to content

Conversation

isarns
Copy link
Contributor

@isarns isarns commented Oct 5, 2025

Description

Fixes a bug in the kustomize edit add label --without-selector command where adding multiple labels would fail with a "label already in kustomization file" error, even when the labels didn't actually exist.
Fixes: #5978

Problem

When running:

kustomize edit add label --without-selector test1:a test2:b

The command would fail with:

Error: label test2 already in kustomization file. Use --force to override.

Root Cause

The issue was in the writeToLabels function in addmetadata.go. The function was calling o.writeToMap(lbl.Pairs, kind) which attempts to add all metadata pairs to the lbl.Pairs map. However, since lbl.Pairs already contained pairs from previous iterations of the loop, it would find duplicates and throw the "already exists" error.

Solution

Changed line 186 from:

if err := o.writeToMap(lbl.Pairs, kind); err != nil {

To:

if err := o.writeToMapEntry(lbl.Pairs, k, v, kind); err != nil {

This ensures only the current key-value pair is added to the map, avoiding the duplicate key issue.

Testing

  • Verified the fix works with multiple labels using --without-selector
  • Existing tests continue to pass
  • No regression in other label functionality

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

@k8s-ci-robot k8s-ci-robot added 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. labels Oct 5, 2025
@k8s-ci-robot
Copy link
Contributor

Hi @isarns. 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 the size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. label Oct 5, 2025
@koba1t
Copy link
Member

koba1t commented Oct 9, 2025

Hi @isarns
Thanks for your contribution!

So, Could you add testacases to check this change will be fix that problem?

@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.

@k8s-ci-robot k8s-ci-robot added size/S Denotes a PR that changes 10-29 lines, ignoring generated files. and removed size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Oct 12, 2025
@koba1t
Copy link
Member

koba1t commented Oct 12, 2025

@isarns
Could you check lint failure?

@koba1t
Copy link
Member

koba1t commented Oct 12, 2025

/ok-to-test

@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Oct 12, 2025
@koba1t
Copy link
Member

koba1t commented Oct 13, 2025

@isarns
Thanks for your contribution!

/lgtm
/approve

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Oct 13, 2025
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: isarns, koba1t

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

The pull request process is described 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 the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Oct 13, 2025
@k8s-ci-robot k8s-ci-robot merged commit de01137 into kubernetes-sigs:master Oct 13, 2025
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. size/S Denotes a PR that changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Using edit add label with the --without-selector flag without pre-existing labels causes an error

3 participants