Skip to content

Commit

Permalink
Implement label and annotation equality checking (#1467) (#1470)
Browse files Browse the repository at this point in the history
* Implement label and annotation equality checking

* Update pkg/equality/equality_test.go



---------

Co-authored-by: Timothée Bavelier <[email protected]>
  • Loading branch information
khewonc and tbavelier authored Oct 17, 2024
1 parent 59191c6 commit 516d191
Show file tree
Hide file tree
Showing 2 changed files with 164 additions and 4 deletions.
6 changes: 2 additions & 4 deletions pkg/equality/equality.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,12 +250,10 @@ func IsEqualOperatorObjectMeta(a, b metav1.Object) bool {

// IsEqualOperatorAnnotations use to check if Operator annotations are equal between 2 Objects
func IsEqualOperatorAnnotations(a, b metav1.Object) bool {
// TODO compare Operator annotations only
return true
return apiequality.Semantic.DeepEqual(a.GetAnnotations(), b.GetAnnotations())
}

// IsEqualOperatorLabels use to check if Operator labels are equal between 2 Objects
func IsEqualOperatorLabels(a, b metav1.Object) bool {
// TODO compare Operator labels only
return true
return apiequality.Semantic.DeepEqual(a.GetLabels(), b.GetLabels())
}
162 changes: 162 additions & 0 deletions pkg/equality/equality_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.

package equality

import (
"testing"

"github.com/stretchr/testify/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestIsEqualOperatorAnnotations(t *testing.T) {
tests := []struct {
name string
objA metav1.Object
objB metav1.Object
want bool
}{
{
name: "obj annotations equal",
objA: &metav1.ObjectMeta{
Name: "foo",
Annotations: map[string]string{
"foo": "bar",
"one": "two",
},
},
objB: &metav1.ObjectMeta{
Name: "foo2",
Annotations: map[string]string{
"foo": "bar",
"one": "two",
},
},
want: true,
},
{
name: "objs not equal",
objA: &metav1.ObjectMeta{
Annotations: map[string]string{
"foo": "bar",
"one": "two",
},
},
objB: &metav1.ObjectMeta{
Annotations: map[string]string{
"foo": "bar",
"one2": "two",
},
},
want: false,
},
{
name: "objs not equal, but annotations equal",
objA: &metav1.ObjectMeta{
Name: "foo",
Labels: map[string]string{
"foo": "bar",
"one": "two",
},
Annotations: map[string]string{
"foo": "bar",
"one": "two",
},
},
objB: &metav1.ObjectMeta{
Name: "foo2",
Labels: map[string]string{
"foo2": "bar",
"one2": "two",
},
Annotations: map[string]string{
"foo": "bar",
"one": "two",
},
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.want, IsEqualOperatorAnnotations(tt.objA, tt.objB))
})
}
}
func TestIsEqualOperatorLabels(t *testing.T) {
tests := []struct {
name string
objA metav1.Object
objB metav1.Object
want bool
}{
{
name: "obj labels equal",
objA: &metav1.ObjectMeta{
Name: "foo",
Labels: map[string]string{
"foo": "bar",
"one": "two",
},
},
objB: &metav1.ObjectMeta{
Name: "foo2",
Labels: map[string]string{
"foo": "bar",
"one": "two",
},
},
want: true,
},
{
name: "objs not equal",
objA: &metav1.ObjectMeta{
Labels: map[string]string{
"foo": "bar",
"one": "two",
},
},
objB: &metav1.ObjectMeta{
Labels: map[string]string{
"foo": "bar",
"one2": "two",
},
},
want: false,
},
{
name: "objs not equal, but labels equal",
objA: &metav1.ObjectMeta{
Name: "foo",
Annotations: map[string]string{
"foo": "bar",
"one": "two",
},
Labels: map[string]string{
"foo": "bar",
"one": "two",
},
},
objB: &metav1.ObjectMeta{
Name: "foo2",
Annotations: map[string]string{
"foo2": "bar",
"one2": "two",
},
Labels: map[string]string{
"foo": "bar",
"one": "two",
},
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.want, IsEqualOperatorLabels(tt.objA, tt.objB))
})
}
}

0 comments on commit 516d191

Please sign in to comment.