Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement label and annotation equality checking #1467

Merged
merged 3 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions pkg/equality/equality.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,12 +238,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 annotations equal",
khewonc marked this conversation as resolved.
Show resolved Hide resolved
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))
})
}
}
Loading