fix: include label in templates when adding by cli

This commit is contained in:
jan.kozlowski
2024-04-26 23:21:51 +02:00
parent a68f40738a
commit a83f102cc9
2 changed files with 19 additions and 1 deletions

View File

@@ -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.
@@ -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
}
@@ -132,7 +136,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 {

View File

@@ -284,6 +284,16 @@ 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 TestAddLabelWithoutSelectorAddLabel(t *testing.T) {
var o addMetadataOptions
o.metadata = map[string]string{"owls": "cute", "otters": "adorable"}