Skip to content

Commit a83f102

Browse files
committed
fix: include label in templates when adding by cli
1 parent a68f407 commit a83f102

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

kustomize/commands/edit/add/addmetadata.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ type addMetadataOptions struct {
3939
mapValidator func(map[string]string) error
4040
kind kindOfAdd
4141
labelsWithoutSelector bool
42+
includeTemplates bool
4243
}
4344

4445
// newCmdAddAnnotation adds one or more commonAnnotations to the kustomization file.
@@ -83,6 +84,9 @@ func newCmdAddLabel(fSys filesys.FileSystem, v func(map[string]string) error) *c
8384
cmd.Flags().BoolVar(&o.labelsWithoutSelector, "without-selector", false,
8485
"using add labels without selector option",
8586
)
87+
cmd.Flags().BoolVar(&o.includeTemplates, "include-templates", false,
88+
"include labels in templates (requires --without-selector)",
89+
)
8690
return cmd
8791
}
8892

@@ -132,7 +136,11 @@ func (o *addMetadataOptions) addAnnotations(m *types.Kustomization) error {
132136

133137
func (o *addMetadataOptions) addLabels(m *types.Kustomization) error {
134138
if o.labelsWithoutSelector {
135-
m.Labels = append(m.Labels, types.Label{Pairs: make(map[string]string), IncludeSelectors: false})
139+
m.Labels = append(m.Labels, types.Label{
140+
Pairs: make(map[string]string),
141+
IncludeSelectors: false,
142+
IncludeTemplates: o.includeTemplates,
143+
})
136144
return o.writeToMap(m.Labels[len(m.Labels)-1].Pairs, label)
137145
}
138146
if m.CommonLabels == nil {

kustomize/commands/edit/add/addmetadata_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,16 @@ func TestAddLabelWithoutSelector(t *testing.T) {
284284
assert.Equal(t, m.Labels[0], types.Label{Pairs: map[string]string{"new": "label"}})
285285
}
286286

287+
func TestAddLabelWithoutSelectorIncludeTemplates(t *testing.T) {
288+
var o addMetadataOptions
289+
o.labelsWithoutSelector = true
290+
m := makeKustomization(t)
291+
o.metadata = map[string]string{"new": "label"}
292+
o.includeTemplates = true
293+
require.NoError(t, o.addLabels(m))
294+
assert.Equal(t, m.Labels[0], types.Label{Pairs: map[string]string{"new": "label"}, IncludeTemplates: true})
295+
}
296+
287297
func TestAddLabelWithoutSelectorAddLabel(t *testing.T) {
288298
var o addMetadataOptions
289299
o.metadata = map[string]string{"owls": "cute", "otters": "adorable"}

0 commit comments

Comments
 (0)