Skip to content

Commit 735ad0b

Browse files
authored
Merge pull request #5682 from kozjan/add-label-include-templates
fix: include label in templates when adding by cli
2 parents c1de030 + cba3688 commit 735ad0b

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

kustomize/commands/edit/add/addmetadata.go

Lines changed: 12 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

@@ -112,6 +116,9 @@ func (o *addMetadataOptions) validateAndParse(args []string) error {
112116
if len(args) < 1 {
113117
return fmt.Errorf("must specify %s", o.kind)
114118
}
119+
if !o.labelsWithoutSelector && o.includeTemplates {
120+
return fmt.Errorf("--without-selector flag must be specified for --include-templates to work")
121+
}
115122
m, err := util.ConvertSliceToMap(args, o.kind.String())
116123
if err != nil {
117124
return err
@@ -132,7 +139,11 @@ func (o *addMetadataOptions) addAnnotations(m *types.Kustomization) error {
132139

133140
func (o *addMetadataOptions) addLabels(m *types.Kustomization) error {
134141
if o.labelsWithoutSelector {
135-
m.Labels = append(m.Labels, types.Label{Pairs: make(map[string]string), IncludeSelectors: false})
142+
m.Labels = append(m.Labels, types.Label{
143+
Pairs: make(map[string]string),
144+
IncludeSelectors: false,
145+
IncludeTemplates: o.includeTemplates,
146+
})
136147
return o.writeToMap(m.Labels[len(m.Labels)-1].Pairs, label)
137148
}
138149
if m.CommonLabels == nil {

kustomize/commands/edit/add/addmetadata_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,29 @@ 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+
297+
func TestAddLabelIncludeTemplatesWithoutRequiredFlag(t *testing.T) {
298+
fSys := filesys.MakeFsInMemory()
299+
v := valtest_test.MakeHappyMapValidator(t)
300+
cmd := newCmdAddLabel(fSys, v.Validator)
301+
args := []string{"new:label"}
302+
_ = cmd.Flag("include-templates").Value.Set("true")
303+
_ = cmd.Flag("without-selector").Value.Set("false")
304+
err := cmd.RunE(cmd, args)
305+
v.VerifyNoCall()
306+
require.Error(t, err)
307+
require.Containsf(t, err.Error(), "--without-selector flag must be specified for --include-templates to work", "incorrect error: %s", err.Error())
308+
}
309+
287310
func TestAddLabelWithoutSelectorAddLabel(t *testing.T) {
288311
var o addMetadataOptions
289312
o.metadata = map[string]string{"owls": "cute", "otters": "adorable"}

0 commit comments

Comments
 (0)