Skip to content

Commit 7aa33ff

Browse files
committed
add edit add labels command add option for labels without selector
1 parent 8dab949 commit 7aa33ff

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

kustomize/commands/edit/add/addmetadata.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,11 @@ func (k kindOfAdd) String() string {
3434
}
3535

3636
type addMetadataOptions struct {
37-
force bool
38-
metadata map[string]string
39-
mapValidator func(map[string]string) error
40-
kind kindOfAdd
37+
force bool
38+
metadata map[string]string
39+
mapValidator func(map[string]string) error
40+
kind kindOfAdd
41+
labelsWithoutSelector bool
4142
}
4243

4344
// newCmdAddAnnotation adds one or more commonAnnotations to the kustomization file.
@@ -79,6 +80,9 @@ func newCmdAddLabel(fSys filesys.FileSystem, v func(map[string]string) error) *c
7980
cmd.Flags().BoolVarP(&o.force, "force", "f", false,
8081
"overwrite commonLabel if it already exists",
8182
)
83+
cmd.Flags().BoolVar(&o.labelsWithoutSelector, "without-selector", false,
84+
"using add labels without selector option",
85+
)
8286
return cmd
8387
}
8488

@@ -127,6 +131,10 @@ func (o *addMetadataOptions) addAnnotations(m *types.Kustomization) error {
127131
}
128132

129133
func (o *addMetadataOptions) addLabels(m *types.Kustomization) error {
134+
if o.labelsWithoutSelector {
135+
m.Labels = append(m.Labels, types.Label{Pairs: make(map[string]string), IncludeSelectors: false})
136+
return o.writeToMap(m.Labels[len(m.Labels)-1].Pairs, label)
137+
}
130138
if m.CommonLabels == nil {
131139
m.CommonLabels = make(map[string]string)
132140
}

kustomize/commands/edit/add/addmetadata_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,3 +273,27 @@ func TestAddLabelForce(t *testing.T) {
273273
assert.NoError(t, cmd.RunE(cmd, args))
274274
v.VerifyCall()
275275
}
276+
277+
func TestAddLabelWithoutSelector(t *testing.T) {
278+
var o addMetadataOptions
279+
o.labelsWithoutSelector = true
280+
m := makeKustomization(t)
281+
o.metadata = map[string]string{"new": "label"}
282+
assert.NoError(t, o.addLabels(m))
283+
assert.Equal(t, m.Labels[0], types.Label{Pairs: map[string]string{"new": "label"}})
284+
}
285+
286+
func TestAddLabelWithoutSelectorAddLabel(t *testing.T) {
287+
var o addMetadataOptions
288+
o.metadata = map[string]string{"owls": "cute", "otters": "adorable"}
289+
o.labelsWithoutSelector = true
290+
291+
m := makeKustomization(t)
292+
assert.NoError(t, o.addLabels(m))
293+
// adding new labels should work
294+
o.metadata = map[string]string{"new": "label"}
295+
assert.NoError(t, o.addLabels(m))
296+
297+
assert.Equal(t, m.Labels[0], types.Label{Pairs: map[string]string{"owls": "cute", "otters": "adorable"}})
298+
assert.Equal(t, m.Labels[1], types.Label{Pairs: map[string]string{"new": "label"}})
299+
}

0 commit comments

Comments
 (0)