mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-11 17:12:51 +00:00
add edit add labels command add option for labels without selector
This commit is contained in:
@@ -38,6 +38,7 @@ type addMetadataOptions struct {
|
|||||||
metadata map[string]string
|
metadata map[string]string
|
||||||
mapValidator func(map[string]string) error
|
mapValidator func(map[string]string) error
|
||||||
kind kindOfAdd
|
kind kindOfAdd
|
||||||
|
labelsWithoutSelector bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// newCmdAddAnnotation adds one or more commonAnnotations to the kustomization file.
|
// 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,
|
cmd.Flags().BoolVarP(&o.force, "force", "f", false,
|
||||||
"overwrite commonLabel if it already exists",
|
"overwrite commonLabel if it already exists",
|
||||||
)
|
)
|
||||||
|
cmd.Flags().BoolVar(&o.labelsWithoutSelector, "without-selector", false,
|
||||||
|
"using add labels without selector option",
|
||||||
|
)
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -127,6 +131,10 @@ func (o *addMetadataOptions) addAnnotations(m *types.Kustomization) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (o *addMetadataOptions) addLabels(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 {
|
if m.CommonLabels == nil {
|
||||||
m.CommonLabels = make(map[string]string)
|
m.CommonLabels = make(map[string]string)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -273,3 +273,27 @@ func TestAddLabelForce(t *testing.T) {
|
|||||||
assert.NoError(t, cmd.RunE(cmd, args))
|
assert.NoError(t, cmd.RunE(cmd, args))
|
||||||
v.VerifyCall()
|
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