diff --git a/internal/gatewayapi/contexts.go b/internal/gatewayapi/contexts.go index 8841e48a73..81e5967a0b 100644 --- a/internal/gatewayapi/contexts.go +++ b/internal/gatewayapi/contexts.go @@ -6,8 +6,6 @@ package gatewayapi import ( - "reflect" - corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" @@ -456,7 +454,7 @@ func GetRouteParentContext(route RouteContext, forParentRef gwapiv1.ParentRefere var parentRef *gwapiv1.ParentReference specParentRefs := route.GetParentReferences() for _, p := range specParentRefs { - if isParentRefEqual(p, forParentRef, route.GetNamespace()) { + if IsParentRefEqual(p, forParentRef, route.GetNamespace()) { parentRef = &p break } @@ -470,7 +468,7 @@ func GetRouteParentContext(route RouteContext, forParentRef gwapiv1.ParentRefere routeStatus := route.GetRouteStatus() for i, parent := range routeStatus.Parents { - if isParentRefEqual(parent.ParentRef, *parentRef, route.GetNamespace()) { + if IsParentRefEqual(parent.ParentRef, *parentRef, route.GetNamespace()) { routeParentStatusIdx = i break } @@ -510,32 +508,74 @@ func GetRouteParentContext(route RouteContext, forParentRef gwapiv1.ParentRefere return ctx } -func isParentRefEqual(ref1, ref2 gwapiv1.ParentReference, routeNS string) bool { +// IsParentRefEqual compares two ParentReference objects for equality without using reflection. +func IsParentRefEqual(ref1, ref2 gwapiv1.ParentReference, routeNS string) bool { + // Compare Group with default handling defaultGroup := (*gwapiv1.Group)(&gwapiv1.GroupVersion.Group) - if ref1.Group == nil { - ref1.Group = defaultGroup + group1 := ref1.Group + if group1 == nil { + group1 = defaultGroup + } + group2 := ref2.Group + if group2 == nil { + group2 = defaultGroup } - if ref2.Group == nil { - ref2.Group = defaultGroup + if *group1 != *group2 { + return false } + // Compare Kind with default handling defaultKind := gwapiv1.Kind(resource.KindGateway) - if ref1.Kind == nil { - ref1.Kind = &defaultKind + kind1 := ref1.Kind + if kind1 == nil { + kind1 = &defaultKind } - if ref2.Kind == nil { - ref2.Kind = &defaultKind + kind2 := ref2.Kind + if kind2 == nil { + kind2 = &defaultKind + } + if *kind1 != *kind2 { + return false } - // If the parent's namespace is not set, default to the namespace of the Route. + // Compare Name (required field) + if ref1.Name != ref2.Name { + return false + } + + // Compare Namespace with default handling defaultNS := gwapiv1.Namespace(routeNS) - if ref1.Namespace == nil { - ref1.Namespace = &defaultNS + namespace1 := ref1.Namespace + if namespace1 == nil { + namespace1 = &defaultNS + } + namespace2 := ref2.Namespace + if namespace2 == nil { + namespace2 = &defaultNS + } + if *namespace1 != *namespace2 { + return false } - if ref2.Namespace == nil { - ref2.Namespace = &defaultNS + + // Compare SectionName (optional field) + if ref1.SectionName == nil && ref2.SectionName == nil { + return true + } + if ref1.SectionName == nil || ref2.SectionName == nil { + return false + } + if *ref1.SectionName != *ref2.SectionName { + return false + } + + // Compare Port (optional field) + if ref1.Port == nil && ref2.Port == nil { + return true + } + if ref1.Port == nil || ref2.Port == nil { + return false } - return reflect.DeepEqual(ref1, ref2) + return *ref1.Port == *ref2.Port } // RouteParentContext wraps a ParentReference and provides helper methods for