Skip to content

Commit fa95ac7

Browse files
committed
Fix removing duplicate label when setting common label
1 parent c5f499b commit fa95ac7

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

kustomize/commands/edit/set/setlabel.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,8 @@ func (o *setLabelOptions) validateAndParse(args []string) error {
8585
}
8686

8787
func (o *setLabelOptions) setLabels(m *types.Kustomization) error {
88+
o.removeDuplicateLabels(m)
8889
if o.labelsWithoutSelector {
89-
o.removeDuplicateLabels(m)
90-
9190
var labelPairs *types.Label
9291
for _, label := range m.Labels {
9392
if !label.IncludeSelectors && label.IncludeTemplates == o.includeTemplates {
@@ -131,12 +130,8 @@ func (o *setLabelOptions) removeDuplicateLabels(m *types.Kustomization) {
131130
// delete duplicate label from deprecated common labels
132131
delete(m.CommonLabels, k)
133132
for idx, label := range m.Labels {
134-
// delete label if it's already present in labels with mismatched includeTemplates value
135-
if label.IncludeTemplates != o.includeTemplates {
136-
m.Labels = deleteLabel(k, label, m.Labels, idx)
137-
}
138-
if label.IncludeSelectors {
139-
// delete label if it's already present in labels and includes selectors
133+
// delete label if it's already present in labels
134+
if _, found := label.Pairs[k]; found {
140135
m.Labels = deleteLabel(k, label, m.Labels, idx)
141136
}
142137
}

kustomize/commands/edit/set/setlabel_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,3 +210,20 @@ func TestSetLabelWithoutSelectorWithCommonLabel(t *testing.T) {
210210
require.Equal(t, m.CommonLabels, map[string]string{"app": "helloworld", "key1": "foo"})
211211
require.Equal(t, m.Labels[0], types.Label{Pairs: map[string]string{"key2": "bar"}})
212212
}
213+
214+
func TestSetLabelCommonLabelWithWithoutSelector(t *testing.T) {
215+
var o setLabelOptions
216+
o.metadata = map[string]string{"key1": "foo", "key2": "bar"}
217+
218+
m := makeKustomization(t)
219+
o.labelsWithoutSelector = true
220+
require.NoError(t, o.setLabels(m))
221+
require.Equal(t, m.Labels[0], types.Label{Pairs: map[string]string{"key1": "foo", "key2": "bar"}})
222+
require.Equal(t, m.CommonLabels, map[string]string{"app": "helloworld"})
223+
224+
o.metadata = map[string]string{"key2": "bar2"}
225+
o.labelsWithoutSelector = false
226+
require.NoError(t, o.setLabels(m))
227+
require.Equal(t, m.Labels[0], types.Label{Pairs: map[string]string{"key1": "foo"}})
228+
require.Equal(t, m.CommonLabels, map[string]string{"app": "helloworld", "key2": "bar2"})
229+
}

0 commit comments

Comments
 (0)