Skip to content

Commit

Permalink
Fix the APIRule OwnerReferences equality check (#17932)
Browse files Browse the repository at this point in the history
* Fix the APIRule OwnerReferences equality check

* Update image
  • Loading branch information
marcobebway authored Aug 7, 2023
1 parent 2feb177 commit 2d1d652
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 9 deletions.
30 changes: 25 additions & 5 deletions components/eventing-controller/pkg/object/equality.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ package object
import (
"reflect"

eventingv1alpha2 "github.com/kyma-project/kyma/components/eventing-controller/api/v1alpha2"

appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/conversion"

apigatewayv1beta1 "github.com/kyma-incubator/api-gateway/api/v1beta1"

eventingv1alpha1 "github.com/kyma-project/kyma/components/eventing-controller/api/v1alpha1"
eventingv1alpha2 "github.com/kyma-project/kyma/components/eventing-controller/api/v1alpha2"
)

// Semantic can do semantic deep equality checks for API objects. Fields which
Expand All @@ -35,8 +34,7 @@ func apiRuleEqual(a1, a2 *apigatewayv1beta1.APIRule) bool {
if !reflect.DeepEqual(a1.Labels, a2.Labels) {
return false
}

if !reflect.DeepEqual(a1.OwnerReferences, a2.OwnerReferences) {
if !ownerReferencesDeepEqual(a1.OwnerReferences, a2.OwnerReferences) {
return false
}
if !reflect.DeepEqual(a1.Spec.Service.Name, a2.Spec.Service.Name) {
Expand All @@ -58,6 +56,28 @@ func apiRuleEqual(a1, a2 *apigatewayv1beta1.APIRule) bool {
return true
}

func ownerReferencesDeepEqual(ors1, ors2 []v1.OwnerReference) bool {
if len(ors1) != len(ors2) {
return false
}

var found bool
for _, or1 := range ors1 {
found = false
for _, or2 := range ors2 {
if reflect.DeepEqual(or1, or2) {
found = true
break
}
}
if !found {
return false
}
}

return true
}

// eventingBackendEqual asserts the equality of two EventingBackend objects.
func eventingBackendEqual(b1, b2 *eventingv1alpha1.EventingBackend) bool {
if b1 == nil || b2 == nil {
Expand Down
135 changes: 132 additions & 3 deletions components/eventing-controller/pkg/object/equality_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ import (
"net/http"
"testing"

eventingv1alpha2 "github.com/kyma-project/kyma/components/eventing-controller/api/v1alpha2"
"github.com/stretchr/testify/require"

apigatewayv1beta1 "github.com/kyma-incubator/api-gateway/api/v1beta1"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/utils/pointer"

apigatewayv1beta1 "github.com/kyma-incubator/api-gateway/api/v1beta1"
eventingv1alpha1 "github.com/kyma-project/kyma/components/eventing-controller/api/v1alpha1"
eventingv1alpha2 "github.com/kyma-project/kyma/components/eventing-controller/api/v1alpha2"
"github.com/kyma-project/kyma/components/eventing-controller/pkg/deployment"
"github.com/kyma-project/kyma/components/eventing-controller/pkg/env"
"github.com/kyma-project/kyma/components/eventing-controller/utils"
Expand Down Expand Up @@ -603,3 +604,131 @@ func TestPublisherProxyDeploymentEqual(t *testing.T) {
})
}
}

func Test_ownerReferencesDeepEqual(t *testing.T) {
ownerReference := func(version, kind, name, uid string, controller, block *bool) metav1.OwnerReference {
return metav1.OwnerReference{
APIVersion: version,
Kind: kind,
Name: name,
UID: types.UID(uid),
Controller: controller,
BlockOwnerDeletion: block,
}
}

tests := []struct {
name string
givenOwnerReferences1 []metav1.OwnerReference
givenOwnerReferences2 []metav1.OwnerReference
wantEqual bool
}{
{
name: "both OwnerReferences are nil",
givenOwnerReferences1: nil,
givenOwnerReferences2: nil,
wantEqual: true,
},
{
name: "both OwnerReferences are empty",
givenOwnerReferences1: []metav1.OwnerReference{},
givenOwnerReferences2: []metav1.OwnerReference{},
wantEqual: true,
},
{
name: "same OwnerReferences and same order",
givenOwnerReferences1: []metav1.OwnerReference{
ownerReference("v-0", "k-0", "n-0", "u-0", pointer.Bool(false), pointer.Bool(false)),
ownerReference("v-1", "k-1", "n-1", "u-1", pointer.Bool(false), pointer.Bool(false)),
ownerReference("v-2", "k-2", "n-2", "u-2", pointer.Bool(false), pointer.Bool(false)),
},
givenOwnerReferences2: []metav1.OwnerReference{
ownerReference("v-0", "k-0", "n-0", "u-0", pointer.Bool(false), pointer.Bool(false)),
ownerReference("v-1", "k-1", "n-1", "u-1", pointer.Bool(false), pointer.Bool(false)),
ownerReference("v-2", "k-2", "n-2", "u-2", pointer.Bool(false), pointer.Bool(false)),
},
wantEqual: true,
},
{
name: "same OwnerReferences but different order",
givenOwnerReferences1: []metav1.OwnerReference{
ownerReference("v-0", "k-0", "n-0", "u-0", pointer.Bool(false), pointer.Bool(false)),
ownerReference("v-1", "k-1", "n-1", "u-1", pointer.Bool(false), pointer.Bool(false)),
ownerReference("v-2", "k-2", "n-2", "u-2", pointer.Bool(false), pointer.Bool(false)),
},
givenOwnerReferences2: []metav1.OwnerReference{
ownerReference("v-2", "k-2", "n-2", "u-2", pointer.Bool(false), pointer.Bool(false)),
ownerReference("v-0", "k-0", "n-0", "u-0", pointer.Bool(false), pointer.Bool(false)),
ownerReference("v-1", "k-1", "n-1", "u-1", pointer.Bool(false), pointer.Bool(false)),
},
wantEqual: true,
},
{
name: "different OwnerReference APIVersion",
givenOwnerReferences1: []metav1.OwnerReference{
ownerReference("v-0", "k-0", "n-0", "u-0", pointer.Bool(false), pointer.Bool(false)),
},
givenOwnerReferences2: []metav1.OwnerReference{
ownerReference("v-1", "k-0", "n-0", "u-0", pointer.Bool(false), pointer.Bool(false)),
},
wantEqual: false,
},
{
name: "different OwnerReference Kind",
givenOwnerReferences1: []metav1.OwnerReference{
ownerReference("v-0", "k-0", "n-0", "u-0", pointer.Bool(false), pointer.Bool(false)),
},
givenOwnerReferences2: []metav1.OwnerReference{
ownerReference("v-0", "k-1", "n-0", "u-0", pointer.Bool(false), pointer.Bool(false)),
},
wantEqual: false,
},
{
name: "different OwnerReference Name",
givenOwnerReferences1: []metav1.OwnerReference{
ownerReference("v-0", "k-0", "n-0", "u-0", pointer.Bool(false), pointer.Bool(false)),
},
givenOwnerReferences2: []metav1.OwnerReference{
ownerReference("v-0", "k-0", "n-1", "u-0", pointer.Bool(false), pointer.Bool(false)),
},
wantEqual: false,
},
{
name: "different OwnerReference UID",
givenOwnerReferences1: []metav1.OwnerReference{
ownerReference("v-0", "k-0", "n-0", "u-0", pointer.Bool(false), pointer.Bool(false)),
},
givenOwnerReferences2: []metav1.OwnerReference{
ownerReference("v-0", "k-0", "n-0", "u-1", pointer.Bool(false), pointer.Bool(false)),
},
wantEqual: false,
},
{
name: "different OwnerReference Controller",
givenOwnerReferences1: []metav1.OwnerReference{
ownerReference("v-0", "k-0", "n-0", "u-0", pointer.Bool(false), pointer.Bool(false)),
},
givenOwnerReferences2: []metav1.OwnerReference{
ownerReference("v-0", "k-0", "n-0", "u-0", pointer.Bool(true), pointer.Bool(false)),
},
wantEqual: false,
},
{
name: "different OwnerReference BlockOwnerDeletion",
givenOwnerReferences1: []metav1.OwnerReference{
ownerReference("v-0", "k-0", "n-0", "u-0", pointer.Bool(false), pointer.Bool(false)),
},
givenOwnerReferences2: []metav1.OwnerReference{
ownerReference("v-0", "k-0", "n-0", "u-0", pointer.Bool(false), pointer.Bool(true)),
},
wantEqual: false,
},
}

for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
require.Equal(t, tc.wantEqual, ownerReferencesDeepEqual(tc.givenOwnerReferences1, tc.givenOwnerReferences2))
})
}
}
2 changes: 1 addition & 1 deletion resources/eventing/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ global:
images:
eventing_controller:
name: eventing-controller
version: PR-17888
version: PR-17932
directory: dev
pullPolicy: "IfNotPresent"
publisher_proxy:
Expand Down

0 comments on commit 2d1d652

Please sign in to comment.