Merge pull request #109505 from zigarn/no-error-reannotate

Do not raise an error proposing to use '--overwrite' when annotating with the same value
This commit is contained in:
Kubernetes Prow Robot 2022-11-03 14:50:15 -07:00 committed by GitHub
commit d96e052d98
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 7 deletions

View File

@ -404,16 +404,16 @@ func validateAnnotations(removeAnnotations []string, newAnnotations map[string]s
// validateNoAnnotationOverwrites validates that when overwrite is false, to-be-updated annotations don't exist in the object annotation map (yet) // validateNoAnnotationOverwrites validates that when overwrite is false, to-be-updated annotations don't exist in the object annotation map (yet)
func validateNoAnnotationOverwrites(accessor metav1.Object, annotations map[string]string) error { func validateNoAnnotationOverwrites(accessor metav1.Object, annotations map[string]string) error {
var buf bytes.Buffer var buf bytes.Buffer
for key := range annotations { for key, value := range annotations {
// change-cause annotation can always be overwritten // change-cause annotation can always be overwritten
if key == polymorphichelpers.ChangeCauseAnnotation { if key == polymorphichelpers.ChangeCauseAnnotation {
continue continue
} }
if value, found := accessor.GetAnnotations()[key]; found { if currValue, found := accessor.GetAnnotations()[key]; found && currValue != value {
if buf.Len() > 0 { if buf.Len() > 0 {
buf.WriteString("; ") buf.WriteString("; ")
} }
buf.WriteString(fmt.Sprintf("'%s' already has a value (%s)", key, value)) buf.WriteString(fmt.Sprintf("'%s' already has a value (%s)", key, currValue))
} }
} }
if buf.Len() > 0 { if buf.Len() > 0 {

View File

@ -227,7 +227,7 @@ func TestUpdateAnnotations(t *testing.T) {
annotations map[string]string annotations map[string]string
remove []string remove []string
expected runtime.Object expected runtime.Object
expectErr bool expectedErr string
}{ }{
{ {
obj: &v1.Pod{ obj: &v1.Pod{
@ -236,7 +236,20 @@ func TestUpdateAnnotations(t *testing.T) {
}, },
}, },
annotations: map[string]string{"a": "b"}, annotations: map[string]string{"a": "b"},
expectErr: true, expected: &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{"a": "b"},
},
},
},
{
obj: &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{"a": "b"},
},
},
annotations: map[string]string{"a": "c"},
expectedErr: "--overwrite is false but found the following declared annotation(s): 'a' already has a value (b)",
}, },
{ {
obj: &v1.Pod{ obj: &v1.Pod{
@ -365,13 +378,16 @@ func TestUpdateAnnotations(t *testing.T) {
resourceVersion: test.version, resourceVersion: test.version,
} }
err := options.updateAnnotations(test.obj) err := options.updateAnnotations(test.obj)
if test.expectErr { if test.expectedErr != "" {
if err == nil { if err == nil {
t.Errorf("unexpected non-error: %v", test) t.Errorf("unexpected non-error: %v", test)
} }
if err.Error() != test.expectedErr {
t.Errorf("error expected: %v, got: %v", test.expectedErr, err.Error())
}
continue continue
} }
if !test.expectErr && err != nil { if test.expectedErr == "" && err != nil {
t.Errorf("unexpected error: %v %v", err, test) t.Errorf("unexpected error: %v %v", err, test)
} }
if !reflect.DeepEqual(test.obj, test.expected) { if !reflect.DeepEqual(test.obj, test.expected) {