mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-11 17:12:51 +00:00
Merge pull request #950 from 2opremio/564-add-force-flag-to-annotations-and-labels
Add --force flag to modify annotations and labels
This commit is contained in:
@@ -47,6 +47,7 @@ func (k kindOfAdd) String() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type addMetadataOptions struct {
|
type addMetadataOptions struct {
|
||||||
|
force bool
|
||||||
metadata map[string]string
|
metadata map[string]string
|
||||||
mapValidator func(map[string]string) error
|
mapValidator func(map[string]string) error
|
||||||
kind kindOfAdd
|
kind kindOfAdd
|
||||||
@@ -66,6 +67,9 @@ func newCmdAddAnnotation(fSys fs.FileSystem, v func(map[string]string) error) *c
|
|||||||
return o.runE(args, fSys, o.addAnnotations)
|
return o.runE(args, fSys, o.addAnnotations)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
cmd.Flags().BoolVarP(&o.force, "force", "f", false,
|
||||||
|
"overwrite commonAnnotation if it already exists",
|
||||||
|
)
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -83,6 +87,9 @@ func newCmdAddLabel(fSys fs.FileSystem, v func(map[string]string) error) *cobra.
|
|||||||
return o.runE(args, fSys, o.addLabels)
|
return o.runE(args, fSys, o.addLabels)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
cmd.Flags().BoolVarP(&o.force, "force", "f", false,
|
||||||
|
"overwrite commonLabel if it already exists",
|
||||||
|
)
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -163,7 +170,7 @@ func (o *addMetadataOptions) addLabels(m *types.Kustomization) error {
|
|||||||
|
|
||||||
func (o *addMetadataOptions) writeToMap(m map[string]string, kind kindOfAdd) error {
|
func (o *addMetadataOptions) writeToMap(m map[string]string, kind kindOfAdd) error {
|
||||||
for k, v := range o.metadata {
|
for k, v := range o.metadata {
|
||||||
if _, ok := m[k]; ok {
|
if _, ok := m[k]; ok && !o.force {
|
||||||
return fmt.Errorf("%s %s already in kustomization file", kind, k)
|
return fmt.Errorf("%s %s already in kustomization file", kind, k)
|
||||||
}
|
}
|
||||||
m[k] = v
|
m[k] = v
|
||||||
|
|||||||
@@ -187,6 +187,40 @@ func TestAddAnnotationMultipleArgs(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAddAnnotationForce(t *testing.T) {
|
||||||
|
fakeFS := fs.MakeFakeFS()
|
||||||
|
fakeFS.WriteTestKustomization()
|
||||||
|
v := validators.MakeHappyMapValidator(t)
|
||||||
|
cmd := newCmdAddAnnotation(fakeFS, v.Validator)
|
||||||
|
args := []string{"key:foo"}
|
||||||
|
err := cmd.RunE(cmd, args)
|
||||||
|
v.VerifyCall()
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("unexpected error: %v", err.Error())
|
||||||
|
}
|
||||||
|
// trying to add the same annotation again should not work
|
||||||
|
args = []string{"key:bar"}
|
||||||
|
v = validators.MakeHappyMapValidator(t)
|
||||||
|
cmd = newCmdAddAnnotation(fakeFS, v.Validator)
|
||||||
|
err = cmd.RunE(cmd, args)
|
||||||
|
v.VerifyCall()
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("expected an error")
|
||||||
|
}
|
||||||
|
if err.Error() != "annotation key already in kustomization file" {
|
||||||
|
t.Errorf("expected an error")
|
||||||
|
}
|
||||||
|
// but trying to add it with --force should
|
||||||
|
v = validators.MakeHappyMapValidator(t)
|
||||||
|
cmd = newCmdAddAnnotation(fakeFS, v.Validator)
|
||||||
|
cmd.Flag("force").Value.Set("true")
|
||||||
|
err = cmd.RunE(cmd, args)
|
||||||
|
v.VerifyCall()
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("unexpected error: %v", err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestRunAddLabel(t *testing.T) {
|
func TestRunAddLabel(t *testing.T) {
|
||||||
var o addMetadataOptions
|
var o addMetadataOptions
|
||||||
o.metadata = map[string]string{"owls": "cute", "otters": "adorable"}
|
o.metadata = map[string]string{"owls": "cute", "otters": "adorable"}
|
||||||
@@ -295,6 +329,40 @@ func TestAddLabelMultipleArgs(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAddLabelForce(t *testing.T) {
|
||||||
|
fakeFS := fs.MakeFakeFS()
|
||||||
|
fakeFS.WriteTestKustomization()
|
||||||
|
v := validators.MakeHappyMapValidator(t)
|
||||||
|
cmd := newCmdAddLabel(fakeFS, v.Validator)
|
||||||
|
args := []string{"key:foo"}
|
||||||
|
err := cmd.RunE(cmd, args)
|
||||||
|
v.VerifyCall()
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("unexpected error: %v", err.Error())
|
||||||
|
}
|
||||||
|
// trying to add the same label again should not work
|
||||||
|
args = []string{"key:bar"}
|
||||||
|
v = validators.MakeHappyMapValidator(t)
|
||||||
|
cmd = newCmdAddLabel(fakeFS, v.Validator)
|
||||||
|
err = cmd.RunE(cmd, args)
|
||||||
|
v.VerifyCall()
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("expected an error")
|
||||||
|
}
|
||||||
|
if err.Error() != "label key already in kustomization file" {
|
||||||
|
t.Errorf("expected an error")
|
||||||
|
}
|
||||||
|
// but trying to add it with --force should
|
||||||
|
v = validators.MakeHappyMapValidator(t)
|
||||||
|
cmd = newCmdAddLabel(fakeFS, v.Validator)
|
||||||
|
cmd.Flag("force").Value.Set("true")
|
||||||
|
err = cmd.RunE(cmd, args)
|
||||||
|
v.VerifyCall()
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("unexpected error: %v", err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestConvertToMap(t *testing.T) {
|
func TestConvertToMap(t *testing.T) {
|
||||||
var o addMetadataOptions
|
var o addMetadataOptions
|
||||||
args := "a:b,c:\"d\",e:\"f:g\",g:h:k"
|
args := "a:b,c:\"d\",e:\"f:g\",g:h:k"
|
||||||
|
|||||||
Reference in New Issue
Block a user