mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-05-17 18:25:26 +00:00
Merge pull request #4486 from koba1t/feature/add_edit_set_labels_subcommand_add_option_for_labels_on_without_selector
add: "edit add labels" command adds an option for labels without selector
This commit is contained in:
@@ -34,10 +34,11 @@ func (k kindOfAdd) String() string {
|
||||
}
|
||||
|
||||
type addMetadataOptions struct {
|
||||
force bool
|
||||
metadata map[string]string
|
||||
mapValidator func(map[string]string) error
|
||||
kind kindOfAdd
|
||||
force bool
|
||||
metadata map[string]string
|
||||
mapValidator func(map[string]string) error
|
||||
kind kindOfAdd
|
||||
labelsWithoutSelector bool
|
||||
}
|
||||
|
||||
// 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
|
||||
cmd.Flags().BoolVarP(&o.force, "force", "f", false,
|
||||
"overwrite commonLabel if it already exists",
|
||||
)
|
||||
cmd.Flags().BoolVar(&o.labelsWithoutSelector, "without-selector", false,
|
||||
"using add labels without selector option",
|
||||
)
|
||||
return cmd
|
||||
}
|
||||
|
||||
@@ -127,6 +131,10 @@ 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})
|
||||
return o.writeToMap(m.Labels[len(m.Labels)-1].Pairs, label)
|
||||
}
|
||||
if m.CommonLabels == nil {
|
||||
m.CommonLabels = make(map[string]string)
|
||||
}
|
||||
|
||||
@@ -274,3 +274,27 @@ func TestAddLabelForce(t *testing.T) {
|
||||
assert.NoError(t, cmd.RunE(cmd, args))
|
||||
v.VerifyCall()
|
||||
}
|
||||
|
||||
func TestAddLabelWithoutSelector(t *testing.T) {
|
||||
var o addMetadataOptions
|
||||
o.labelsWithoutSelector = true
|
||||
m := makeKustomization(t)
|
||||
o.metadata = map[string]string{"new": "label"}
|
||||
assert.NoError(t, o.addLabels(m))
|
||||
assert.Equal(t, m.Labels[0], types.Label{Pairs: map[string]string{"new": "label"}})
|
||||
}
|
||||
|
||||
func TestAddLabelWithoutSelectorAddLabel(t *testing.T) {
|
||||
var o addMetadataOptions
|
||||
o.metadata = map[string]string{"owls": "cute", "otters": "adorable"}
|
||||
o.labelsWithoutSelector = true
|
||||
|
||||
m := makeKustomization(t)
|
||||
assert.NoError(t, o.addLabels(m))
|
||||
// adding new labels should work
|
||||
o.metadata = map[string]string{"new": "label"}
|
||||
assert.NoError(t, o.addLabels(m))
|
||||
|
||||
assert.Equal(t, m.Labels[0], types.Label{Pairs: map[string]string{"owls": "cute", "otters": "adorable"}})
|
||||
assert.Equal(t, m.Labels[1], types.Label{Pairs: map[string]string{"new": "label"}})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user