Skip to content

Commit

Permalink
Merge pull request #144 from kevin1689-cloud/fix/ergonomic-annotation…
Browse files Browse the repository at this point in the history
…-handling
  • Loading branch information
gary-lgy authored Jul 13, 2023
2 parents 185b98e + f973cbc commit b40dfff
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 38 deletions.
43 changes: 13 additions & 30 deletions pkg/controllers/util/annotation/annotation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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)
}
Expand All @@ -76,36 +67,28 @@ 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
}
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
Expand All @@ -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
}

Expand Down
12 changes: 6 additions & 6 deletions pkg/controllers/util/annotation/annotation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@ 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
}

func TestHasAnnotationKey(t *testing.T) {
testCases := []struct {
obj runtime.Object
obj metav1.Object
annotation string
result bool
}{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions pkg/controllers/util/sourcefeedback/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ package sourcefeedback
import (
"encoding/json"

"k8s.io/apimachinery/pkg/runtime"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/klog/v2"

"github.com/kubewharf/kubeadmiral/pkg/controllers/util/annotation"
)

func setAnnotation(object runtime.Object, key string, value interface{}, hasChanged *bool) {
func setAnnotation(object metav1.Object, key string, value interface{}, hasChanged *bool) {
jsonBuf, err := json.Marshal(value)
if err != nil {
klog.Errorf("Cannot marshal JSON: %v", err)
Expand Down

0 comments on commit b40dfff

Please sign in to comment.