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

Do not raise an error proposing to use '--overwrite' when annotating with the same value

Kubernetes-commit: d96e052d981537ae9d2376ee6ac73f3ae6817f69
This commit is contained in:
Kubernetes Publisher 2022-11-03 14:50:15 -07:00
commit 19f8631311
4 changed files with 35 additions and 19 deletions

12
go.mod
View File

@ -30,10 +30,10 @@ require (
golang.org/x/sys v0.1.0
gopkg.in/yaml.v2 v2.4.0
k8s.io/api v0.0.0-20221103075246-5448eb39ae20
k8s.io/apimachinery v0.0.0-20221028155017-b03a432a2a6d
k8s.io/apimachinery v0.0.0-20221103075033-9e85d3af4ae2
k8s.io/cli-runtime v0.0.0-20221030161243-cf309f8903f6
k8s.io/client-go v0.0.0-20221103075558-4fbef5bda929
k8s.io/component-base v0.0.0-20221102201024-c22075132ffc
k8s.io/client-go v0.0.0-20221103075600-d576a3570dbe
k8s.io/component-base v0.0.0-20221103115736-4e0e60826ac9
k8s.io/component-helpers v0.0.0-20221028160522-56bb98a3ab81
k8s.io/klog/v2 v2.80.1
k8s.io/kube-openapi v0.0.0-20221012153701-172d655c2280
@ -92,11 +92,11 @@ require (
replace (
k8s.io/api => k8s.io/api v0.0.0-20221103075246-5448eb39ae20
k8s.io/apimachinery => k8s.io/apimachinery v0.0.0-20221028155017-b03a432a2a6d
k8s.io/apimachinery => k8s.io/apimachinery v0.0.0-20221103075033-9e85d3af4ae2
k8s.io/cli-runtime => k8s.io/cli-runtime v0.0.0-20221030161243-cf309f8903f6
k8s.io/client-go => k8s.io/client-go v0.0.0-20221103075558-4fbef5bda929
k8s.io/client-go => k8s.io/client-go v0.0.0-20221103075600-d576a3570dbe
k8s.io/code-generator => k8s.io/code-generator v0.0.0-20221028154808-081720d0e256
k8s.io/component-base => k8s.io/component-base v0.0.0-20221102201024-c22075132ffc
k8s.io/component-base => k8s.io/component-base v0.0.0-20221103115736-4e0e60826ac9
k8s.io/component-helpers => k8s.io/component-helpers v0.0.0-20221028160522-56bb98a3ab81
k8s.io/metrics => k8s.io/metrics v0.0.0-20221028162048-b37c1756f70a
)

12
go.sum
View File

@ -542,14 +542,14 @@ honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9
honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
k8s.io/api v0.0.0-20221103075246-5448eb39ae20 h1:L4yzu0YYenWmYNm1WG/jJy/6ioJxAlgDewudcjz0uUE=
k8s.io/api v0.0.0-20221103075246-5448eb39ae20/go.mod h1:G/3aN5AoXC2S2FQkBj1R024v+pPQBOzaaOCf3qJdj+A=
k8s.io/apimachinery v0.0.0-20221028155017-b03a432a2a6d h1:fg/DbLqFKxFESf3AnU5iwCexZWOUnFFLb0JraG20wZo=
k8s.io/apimachinery v0.0.0-20221028155017-b03a432a2a6d/go.mod h1:zSkBXgO5G/dSQOe256tx5Yo2OJytojpY3bsXu/4/ZJE=
k8s.io/apimachinery v0.0.0-20221103075033-9e85d3af4ae2 h1:0nhI6fiyouN4H8MXOcMcCOybGhw4FgxwQbadTKPIRlA=
k8s.io/apimachinery v0.0.0-20221103075033-9e85d3af4ae2/go.mod h1:zSkBXgO5G/dSQOe256tx5Yo2OJytojpY3bsXu/4/ZJE=
k8s.io/cli-runtime v0.0.0-20221030161243-cf309f8903f6 h1:qt3gwW+JHgYf49iawwhBdRn+C0PVn/ttSFUObjuSRsg=
k8s.io/cli-runtime v0.0.0-20221030161243-cf309f8903f6/go.mod h1:VCK6BtIAZKGm+vtIuPX7Izszv8qB+hRMk7aSQl1ymRU=
k8s.io/client-go v0.0.0-20221103075558-4fbef5bda929 h1:LJygi0+VRaOYeVCtigmX/WL48MXQRw60mbD0/vBfP7s=
k8s.io/client-go v0.0.0-20221103075558-4fbef5bda929/go.mod h1:DseZqcKeJ6QOGVPzrKVRaNDHcO+jFueDaza2XFq7nv0=
k8s.io/component-base v0.0.0-20221102201024-c22075132ffc h1:guQG2a4VRQzaH2/LBx/9b+yDzcEHhNgfB495DpJby9c=
k8s.io/component-base v0.0.0-20221102201024-c22075132ffc/go.mod h1:6nB7Hvde4zeOk2qvFvfgSiMNhRpXM0Q2Q04zwBZIfPc=
k8s.io/client-go v0.0.0-20221103075600-d576a3570dbe h1:Ht6eCnwrndb2gMiPG2fW5gppS2GU0AqeadKgpqBnmWI=
k8s.io/client-go v0.0.0-20221103075600-d576a3570dbe/go.mod h1:L+YjBlIC5U+lsvpFH3jgXB9bX/n0xqA1qv0sRoDF5MM=
k8s.io/component-base v0.0.0-20221103115736-4e0e60826ac9 h1:noL8s7Lyec/QtPtr3MXIMLYQUWCaXqftaIZszG3F8OA=
k8s.io/component-base v0.0.0-20221103115736-4e0e60826ac9/go.mod h1:Knaa6iPS89K+o4JtNNb5SW1MrmS3AOhHS73ERE1bASo=
k8s.io/component-helpers v0.0.0-20221028160522-56bb98a3ab81 h1:3CC/oNrmdSptGTCO0NGfo4HsUGjQOfFL8xAret+O6Vc=
k8s.io/component-helpers v0.0.0-20221028160522-56bb98a3ab81/go.mod h1:CWIVMl3HH46Te+rS5pdIrYoJ4Hgpvq3GDjmRojCr7jA=
k8s.io/klog/v2 v2.80.1 h1:atnLQ121W371wYYFawwYx1aEY2eUfs4l3J72wtgAwV4=

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)
func validateNoAnnotationOverwrites(accessor metav1.Object, annotations map[string]string) error {
var buf bytes.Buffer
for key := range annotations {
for key, value := range annotations {
// change-cause annotation can always be overwritten
if key == polymorphichelpers.ChangeCauseAnnotation {
continue
}
if value, found := accessor.GetAnnotations()[key]; found {
if currValue, found := accessor.GetAnnotations()[key]; found && currValue != value {
if buf.Len() > 0 {
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 {

View File

@ -227,7 +227,7 @@ func TestUpdateAnnotations(t *testing.T) {
annotations map[string]string
remove []string
expected runtime.Object
expectErr bool
expectedErr string
}{
{
obj: &v1.Pod{
@ -236,7 +236,20 @@ func TestUpdateAnnotations(t *testing.T) {
},
},
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{
@ -365,13 +378,16 @@ func TestUpdateAnnotations(t *testing.T) {
resourceVersion: test.version,
}
err := options.updateAnnotations(test.obj)
if test.expectErr {
if test.expectedErr != "" {
if err == nil {
t.Errorf("unexpected non-error: %v", test)
}
if err.Error() != test.expectedErr {
t.Errorf("error expected: %v, got: %v", test.expectedErr, err.Error())
}
continue
}
if !test.expectErr && err != nil {
if test.expectedErr == "" && err != nil {
t.Errorf("unexpected error: %v %v", err, test)
}
if !reflect.DeepEqual(test.obj, test.expected) {