Skip to content

Commit

Permalink
Merge pull request #5682 from kozjan/add-label-include-templates
Browse files Browse the repository at this point in the history
fix: include label in templates when adding by cli
  • Loading branch information
k8s-ci-robot committed Jul 10, 2024
2 parents c1de030 + cba3688 commit 735ad0b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
13 changes: 12 additions & 1 deletion kustomize/commands/edit/add/addmetadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type addMetadataOptions struct {
mapValidator func(map[string]string) error
kind kindOfAdd
labelsWithoutSelector bool
includeTemplates bool
}

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

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

func (o *addMetadataOptions) addLabels(m *types.Kustomization) error {
if o.labelsWithoutSelector {
m.Labels = append(m.Labels, types.Label{Pairs: make(map[string]string), IncludeSelectors: false})
m.Labels = append(m.Labels, types.Label{
Pairs: make(map[string]string),
IncludeSelectors: false,
IncludeTemplates: o.includeTemplates,
})
return o.writeToMap(m.Labels[len(m.Labels)-1].Pairs, label)
}
if m.CommonLabels == nil {
Expand Down
23 changes: 23 additions & 0 deletions kustomize/commands/edit/add/addmetadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,29 @@ func TestAddLabelWithoutSelector(t *testing.T) {
assert.Equal(t, m.Labels[0], types.Label{Pairs: map[string]string{"new": "label"}})
}

func TestAddLabelWithoutSelectorIncludeTemplates(t *testing.T) {
var o addMetadataOptions
o.labelsWithoutSelector = true
m := makeKustomization(t)
o.metadata = map[string]string{"new": "label"}
o.includeTemplates = true
require.NoError(t, o.addLabels(m))
assert.Equal(t, m.Labels[0], types.Label{Pairs: map[string]string{"new": "label"}, IncludeTemplates: true})
}

func TestAddLabelIncludeTemplatesWithoutRequiredFlag(t *testing.T) {
fSys := filesys.MakeFsInMemory()
v := valtest_test.MakeHappyMapValidator(t)
cmd := newCmdAddLabel(fSys, v.Validator)
args := []string{"new:label"}
_ = cmd.Flag("include-templates").Value.Set("true")
_ = cmd.Flag("without-selector").Value.Set("false")
err := cmd.RunE(cmd, args)
v.VerifyNoCall()
require.Error(t, err)
require.Containsf(t, err.Error(), "--without-selector flag must be specified for --include-templates to work", "incorrect error: %s", err.Error())
}

func TestAddLabelWithoutSelectorAddLabel(t *testing.T) {
var o addMetadataOptions
o.metadata = map[string]string{"owls": "cute", "otters": "adorable"}
Expand Down

0 comments on commit 735ad0b

Please sign in to comment.