From cd6937a1202ca2513a990985792b4ed88d481e2e Mon Sep 17 00:00:00 2001 From: kevin1689 Date: Mon, 10 Jul 2023 18:50:31 +0800 Subject: [PATCH] fix: use metav1.Object instead of runtime.Object for more ergonomic annotation handling Signed-off-by: kevin1689 --- pkg/controllers/util/annotation/annotation.go | 43 ++++++------------- .../util/annotation/annotation_test.go | 12 +++--- 2 files changed, 19 insertions(+), 36 deletions(-) diff --git a/pkg/controllers/util/annotation/annotation.go b/pkg/controllers/util/annotation/annotation.go index c2bcb2dd6..108b7d607 100644 --- a/pkg/controllers/util/annotation/annotation.go +++ b/pkg/controllers/util/annotation/annotation.go @@ -20,26 +20,21 @@ import ( "fmt" "reflect" - "k8s.io/apimachinery/pkg/api/meta" - "k8s.io/apimachinery/pkg/runtime" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) const ( - SyncSuccessTimestamp = "syncSuccessTimestamp" - LastGeneration = "lastGeneration" + SyncSuccessTimestamp = "syncSuccessTimestamp" + LastGeneration = "lastGeneration" LastSyncSuccessGeneration = "lastSyncSuccessGeneration" ) // HasAnnotationKey returns true if the given object has the given annotation key in its ObjectMeta. -func HasAnnotationKey(obj runtime.Object, key string) (bool, error) { +func HasAnnotationKey(obj metav1.Object, key string) (bool, error) { if IsNilPointer(obj) { return false, fmt.Errorf("object(%T) is nil pointer", obj) } - accessor, err := meta.Accessor(obj) - if err != nil { - return false, err - } - annotations := accessor.GetAnnotations() + annotations := obj.GetAnnotations() if annotations == nil { return false, nil } @@ -48,15 +43,11 @@ func HasAnnotationKey(obj runtime.Object, key string) (bool, error) { } // HasAnnotationKeyValue returns true if the given object has the given annotation key and value in its ObjectMeta. -func HasAnnotationKeyValue(obj runtime.Object, key, value string) (bool, error) { +func HasAnnotationKeyValue(obj metav1.Object, key, value string) (bool, error) { if IsNilPointer(obj) { return false, fmt.Errorf("object(%T) is nil pointer", obj) } - accessor, err := meta.Accessor(obj) - if err != nil { - return false, err - } - annotations := accessor.GetAnnotations() + annotations := obj.GetAnnotations() if annotations == nil { return false, nil } @@ -67,7 +58,7 @@ func HasAnnotationKeyValue(obj runtime.Object, key, value string) (bool, error) // AddAnnotation adds the given annotation key and value to the given objects ObjectMeta, // and overwrites the annotation value if it already exists. // Returns true if the object was updated. -func AddAnnotation(obj runtime.Object, key, value string) (bool, error) { +func AddAnnotation(obj metav1.Object, key, value string) (bool, error) { if IsNilPointer(obj) { return false, fmt.Errorf("object(%T) is nil pointer", obj) } @@ -76,10 +67,6 @@ func AddAnnotation(obj runtime.Object, key, value string) (bool, error) { return false, fmt.Errorf("key is a empty string.") } - accessor, err := meta.Accessor(obj) - if err != nil { - return false, err - } has, err := HasAnnotationKeyValue(obj, key, value) if has && err == nil { return false, nil @@ -87,25 +74,21 @@ func AddAnnotation(obj runtime.Object, key, value string) (bool, error) { if err != nil { return false, err } - annotations := accessor.GetAnnotations() + annotations := obj.GetAnnotations() if annotations == nil { annotations = make(map[string]string) } annotations[key] = value - accessor.SetAnnotations(annotations) + obj.SetAnnotations(annotations) return true, nil } // RemoveAnnotation removes the given annotation key from the given objects ObjectMeta. // Returns true if the object was updated. -func RemoveAnnotation(obj runtime.Object, key string) (bool, error) { +func RemoveAnnotation(obj metav1.Object, key string) (bool, error) { if IsNilPointer(obj) { return false, fmt.Errorf("object(%T) is nil pointer", obj) } - accessor, err := meta.Accessor(obj) - if err != nil { - return false, err - } has, err := HasAnnotationKey(obj, key) if !has && err == nil { return false, nil @@ -114,13 +97,13 @@ func RemoveAnnotation(obj runtime.Object, key string) (bool, error) { return false, err } - annotations := accessor.GetAnnotations() + annotations := obj.GetAnnotations() if annotations == nil { return false, nil } delete(annotations, key) - accessor.SetAnnotations(annotations) + obj.SetAnnotations(annotations) return true, nil } diff --git a/pkg/controllers/util/annotation/annotation_test.go b/pkg/controllers/util/annotation/annotation_test.go index 4dd87d0f2..7e8c59d4e 100644 --- a/pkg/controllers/util/annotation/annotation_test.go +++ b/pkg/controllers/util/annotation/annotation_test.go @@ -23,10 +23,10 @@ import ( "github.com/stretchr/testify/assert" corev1 "k8s.io/api/core/v1" meta "k8s.io/apimachinery/pkg/api/meta" - "k8s.io/apimachinery/pkg/runtime" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) -func newObj(annotation map[string]string) runtime.Object { +func newObj(annotation map[string]string) metav1.Object { pod := corev1.Pod{} pod.ObjectMeta.Annotations = annotation return &pod @@ -34,7 +34,7 @@ func newObj(annotation map[string]string) runtime.Object { func TestHasAnnotationKey(t *testing.T) { testCases := []struct { - obj runtime.Object + obj metav1.Object annotation string result bool }{ @@ -82,7 +82,7 @@ func TestHasAnnotationKey(t *testing.T) { func TestHasAnnotationKeyValue(t *testing.T) { testCases := []struct { - obj runtime.Object + obj metav1.Object key string value string result bool @@ -149,7 +149,7 @@ func TestHasAnnotationKeyValue(t *testing.T) { func TestAddAnnotation(t *testing.T) { testCases := []struct { - obj runtime.Object + obj metav1.Object key string value string isUpdated bool @@ -231,7 +231,7 @@ func TestAddAnnotation(t *testing.T) { func TestRemoveAnnotation(t *testing.T) { testCases := []struct { - obj runtime.Object + obj metav1.Object key string isUpdated bool newAnnotations map[string]string