Skip to content

Commit

Permalink
Fix removing duplicate label when setting common label
Browse files Browse the repository at this point in the history
  • Loading branch information
Mrflatt committed Sep 9, 2024
1 parent 3290329 commit 4fb55f1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
11 changes: 3 additions & 8 deletions kustomize/commands/edit/set/setlabel.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,8 @@ func (o *setLabelOptions) validateAndParse(args []string) error {
}

func (o *setLabelOptions) setLabels(m *types.Kustomization) error {
o.removeDuplicateLabels(m)
if o.labelsWithoutSelector {
o.removeDuplicateLabels(m)

var labelPairs *types.Label
for _, label := range m.Labels {
if !label.IncludeSelectors && label.IncludeTemplates == o.includeTemplates {
Expand Down Expand Up @@ -131,12 +130,8 @@ func (o *setLabelOptions) removeDuplicateLabels(m *types.Kustomization) {
// delete duplicate label from deprecated common labels
delete(m.CommonLabels, k)
for idx, label := range m.Labels {
// delete label if it's already present in labels with mismatched includeTemplates value
if label.IncludeTemplates != o.includeTemplates {
m.Labels = deleteLabel(k, label, m.Labels, idx)
}
if label.IncludeSelectors {
// delete label if it's already present in labels and includes selectors
// delete label if it's already present in labels
if _, found := label.Pairs[k]; found {
m.Labels = deleteLabel(k, label, m.Labels, idx)
}
}
Expand Down
17 changes: 17 additions & 0 deletions kustomize/commands/edit/set/setlabel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,3 +210,20 @@ func TestSetLabelWithoutSelectorWithCommonLabel(t *testing.T) {
require.Equal(t, m.CommonLabels, map[string]string{"app": "helloworld", "key1": "foo"})
require.Equal(t, m.Labels[0], types.Label{Pairs: map[string]string{"key2": "bar"}})
}

func TestSetLabelCommonLabelWithWithoutSelector(t *testing.T) {
var o setLabelOptions
o.metadata = map[string]string{"key1": "foo", "key2": "bar"}

m := makeKustomization(t)
o.labelsWithoutSelector = true
require.NoError(t, o.setLabels(m))
require.Equal(t, m.Labels[0], types.Label{Pairs: map[string]string{"key1": "foo", "key2": "bar"}})
require.Equal(t, m.CommonLabels, map[string]string{"app": "helloworld"})

o.metadata = map[string]string{"key2": "bar2"}
o.labelsWithoutSelector = false
require.NoError(t, o.setLabels(m))
require.Equal(t, m.Labels[0], types.Label{Pairs: map[string]string{"key1": "foo"}})
require.Equal(t, m.CommonLabels, map[string]string{"app": "helloworld", "key2": "bar2"})
}

0 comments on commit 4fb55f1

Please sign in to comment.