Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --force flag to modify annotations and labels #950

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion pkg/commands/edit/add/addmetadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func (k kindOfAdd) String() string {
}

type addMetadataOptions struct {
force bool
metadata map[string]string
mapValidator func(map[string]string) error
kind kindOfAdd
Expand All @@ -66,6 +67,9 @@ func newCmdAddAnnotation(fSys fs.FileSystem, v func(map[string]string) error) *c
return o.runE(args, fSys, o.addAnnotations)
},
}
cmd.Flags().BoolVarP(&o.force, "force", "f", false,
"overwrite commonAnnotation if it already exists",
)
return cmd
}

Expand All @@ -83,6 +87,9 @@ func newCmdAddLabel(fSys fs.FileSystem, v func(map[string]string) error) *cobra.
return o.runE(args, fSys, o.addLabels)
},
}
cmd.Flags().BoolVarP(&o.force, "force", "f", false,
"overwrite commonLabel if it already exists",
)
return cmd
}

Expand Down Expand Up @@ -163,7 +170,7 @@ func (o *addMetadataOptions) addLabels(m *types.Kustomization) error {

func (o *addMetadataOptions) writeToMap(m map[string]string, kind kindOfAdd) error {
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)
}
m[k] = v
Expand Down
68 changes: 68 additions & 0 deletions pkg/commands/edit/add/addmetadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
var o addMetadataOptions
o.metadata = map[string]string{"owls": "cute", "otters": "adorable"}
Expand Down Expand Up @@ -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) {
var o addMetadataOptions
args := "a:b,c:\"d\",e:\"f:g\",g:h:k"
Expand Down