@@ -34,8 +34,8 @@ func (r *HTTPRouteReconciler) processHTTPRoutePolicies(tctx *provider.TranslateC
3434
3535 var conflicts = make (map [types.NamespacedName ]v1alpha1.HTTPRoutePolicy )
3636 for _ , rule := range httpRoute .Spec .Rules {
37- var policies = findPolicyWhichTargetRefTheRule (rule .Name , "HTTPRoute" , list )
38- if conflict := isPoliciesConflict (policies ); conflict {
37+ var policies = findPoliciesWhichTargetRefTheRule (rule .Name , "HTTPRoute" , list )
38+ if conflict := checkPoliciesConflict (policies ); conflict {
3939 for _ , policy := range policies {
4040 namespacedName := types.NamespacedName {Namespace : policy .GetNamespace (), Name : policy .GetName ()}
4141 conflicts [namespacedName ] = policy
@@ -66,7 +66,7 @@ func (r *HTTPRouteReconciler) processHTTPRoutePolicies(tctx *provider.TranslateC
6666 return nil
6767}
6868
69- func (r * HTTPRouteReconciler ) updateHTTPRouteStatusOnDeleting (nn types.NamespacedName ) error {
69+ func (r * HTTPRouteReconciler ) updateHTTPRoutePolicyStatusOnDeleting (nn types.NamespacedName ) error {
7070 var (
7171 list v1alpha1.HTTPRoutePolicyList
7272 key = indexer .GenIndexKeyWithGK (gatewayv1 .GroupName , "HTTPRoute" , nn .Namespace , nn .Name )
@@ -75,26 +75,25 @@ func (r *HTTPRouteReconciler) updateHTTPRouteStatusOnDeleting(nn types.Namespace
7575 return err
7676 }
7777 var (
78- objs = make (map [types.NamespacedName ]struct {})
79- parentRefs []gatewayv1.ParentReference
78+ httpRoutes = make (map [types.NamespacedName ]gatewayv1.HTTPRoute )
8079 )
81- // collect all parentRefs
8280 for _ , policy := range list .Items {
81+ // collect all parentRefs for the HTTPRoutePolicy
82+ var parentRefs []gatewayv1.ParentReference
8383 for _ , ref := range policy .Spec .TargetRefs {
84- var obj = types.NamespacedName {Namespace : policy .GetNamespace (), Name : string (ref .Name )}
85- if _ , ok := objs [obj ]; ! ok {
86- objs [obj ] = struct {}{}
87-
88- var httpRoute gatewayv1.HTTPRoute
89- if err := r .Get (context .Background (), obj , & httpRoute ); err != nil {
84+ var namespacedName = types.NamespacedName {Namespace : policy .GetNamespace (), Name : string (ref .Name )}
85+ httpRoute , ok := httpRoutes [namespacedName ]
86+ if ! ok {
87+ if err := r .Get (context .Background (), namespacedName , & httpRoute ); err != nil {
9088 continue
9189 }
92- parentRefs = append ( parentRefs , httpRoute . Spec . ParentRefs ... )
90+ httpRoutes [ namespacedName ] = httpRoute
9391 }
92+ parentRefs = append (parentRefs , httpRoute .Spec .ParentRefs ... )
9493 }
94+ // delete AncestorRef which is not exist in the all parentRefs for each policy
95+ updateDeleteAncestors (r .Client , r .Log , policy , parentRefs )
9596 }
96- // delete AncestorRef which is not exist in the all parentRefs for each policy
97- updateDeleteAncestors (r .Client , r .Log , list .Items , parentRefs )
9897
9998 return nil
10099}
@@ -117,7 +116,7 @@ func (r *IngressReconciler) processHTTPRoutePolicies(tctx *provider.TranslateCon
117116 reason = string (v1alpha2 .PolicyReasonConflicted )
118117 message = "HTTPRoutePolicy conflict with others target to the Ingress"
119118 )
120- if conflict := isPoliciesConflict (list .Items ); ! conflict {
119+ if conflict := checkPoliciesConflict (list .Items ); ! conflict {
121120 status = true
122121 reason = string (v1alpha2 .PolicyReasonAccepted )
123122 message = ""
@@ -143,34 +142,35 @@ func (r *IngressReconciler) updateHTTPRoutePolicyStatusOnDeleting(nn types.Names
143142 return err
144143 }
145144 var (
146- objs = make (map [types.NamespacedName ]struct {})
147- parentRefs []gatewayv1.ParentReference
145+ ingress2ParentRef = make (map [types.NamespacedName ]gatewayv1.ParentReference )
148146 )
149- // collect all parentRefs
150147 for _ , policy := range list .Items {
148+ // collect all parentRefs for the HTTPRoutePolicy
149+ var parentRefs []gatewayv1.ParentReference
151150 for _ , ref := range policy .Spec .TargetRefs {
152- var obj = types.NamespacedName {Namespace : policy .GetNamespace (), Name : string (ref .Name )}
153- if _ , ok := objs [obj ]; ! ok {
154- objs [obj ] = struct {}{}
155-
151+ var namespacedName = types.NamespacedName {Namespace : policy .GetNamespace (), Name : string (ref .Name )}
152+ parentRef , ok := ingress2ParentRef [namespacedName ]
153+ if ! ok {
156154 var ingress networkingv1.Ingress
157- if err := r .Get (context .Background (), obj , & ingress ); err != nil {
155+ if err := r .Get (context .Background (), namespacedName , & ingress ); err != nil {
158156 continue
159157 }
160158 ingressClass , err := r .getIngressClass (& ingress )
161159 if err != nil {
162160 continue
163161 }
164- parentRefs = append ( parentRefs , gatewayv1.ParentReference {
162+ parentRef = gatewayv1.ParentReference {
165163 Group : ptr .To (gatewayv1 .Group (ingressClass .GroupVersionKind ().Group )),
166164 Kind : ptr .To (gatewayv1 .Kind ("IngressClass" )),
167165 Name : gatewayv1 .ObjectName (ingressClass .Name ),
168- })
166+ }
167+ ingress2ParentRef [namespacedName ] = parentRef
169168 }
169+ parentRefs = append (parentRefs , parentRef )
170170 }
171+ // delete AncestorRef which is not exist in the all parentRefs
172+ updateDeleteAncestors (r .Client , r .Log , policy , parentRefs )
171173 }
172- // delete AncestorRef which is not exist in the all parentRefs
173- updateDeleteAncestors (r .Client , r .Log , list .Items , parentRefs )
174174
175175 return nil
176176}
@@ -190,7 +190,11 @@ func modifyHTTPRoutePolicyStatus(parentRefs []gatewayv1.ParentReference, policy
190190 _ = SetAncestors (& policy .Status , parentRefs , condition )
191191}
192192
193- func isPoliciesConflict (policies []v1alpha1.HTTPRoutePolicy ) bool {
193+ // checkPoliciesConflict determines if there is a conflict among the given HTTPRoutePolicy objects based on their priority values.
194+ // It returns true if any policy has a different priority than the first policy in the list, otherwise false.
195+ // An empty or single-element policies slice is considered non-conflicting.
196+ // The function assumes all policies have a valid Spec.Priority field for comparison.
197+ func checkPoliciesConflict (policies []v1alpha1.HTTPRoutePolicy ) bool {
194198 if len (policies ) == 0 {
195199 return false
196200 }
@@ -203,7 +207,9 @@ func isPoliciesConflict(policies []v1alpha1.HTTPRoutePolicy) bool {
203207 return false
204208}
205209
206- func findPolicyWhichTargetRefTheRule (ruleName * gatewayv1.SectionName , kind string , list v1alpha1.HTTPRoutePolicyList ) (policies []v1alpha1.HTTPRoutePolicy ) {
210+ // findPoliciesWhichTargetRefTheRule filters HTTPRoutePolicy objects whose TargetRefs match the given ruleName and kind.
211+ // A match occurs if the TargetRef's Kind equals the provided kind and its SectionName is nil, empty, or equal to ruleName.
212+ func findPoliciesWhichTargetRefTheRule (ruleName * gatewayv1.SectionName , kind string , list v1alpha1.HTTPRoutePolicyList ) (policies []v1alpha1.HTTPRoutePolicy ) {
207213 for _ , policy := range list .Items {
208214 for _ , ref := range policy .Spec .TargetRefs {
209215 if string (ref .Kind ) == kind && (ref .SectionName == nil || * ref .SectionName == "" || ptr .Equal (ref .SectionName , ruleName )) {
@@ -215,19 +221,17 @@ func findPolicyWhichTargetRefTheRule(ruleName *gatewayv1.SectionName, kind strin
215221 return
216222}
217223
218- func updateDeleteAncestors (client client.Client , logger logr.Logger , policies []v1alpha1.HTTPRoutePolicy , parentRefs []gatewayv1.ParentReference ) {
219- for i := range policies {
220- policy := policies [i ]
221- length := len (policy .Status .Ancestors )
222- policy .Status .Ancestors = slices .DeleteFunc (policy .Status .Ancestors , func (status v1alpha2.PolicyAncestorStatus ) bool {
223- return ! slices .ContainsFunc (parentRefs , func (ref gatewayv1.ParentReference ) bool {
224- return parentRefValueEqual (status .AncestorRef , ref )
225- })
224+ // updateDeleteAncestors removes ancestor references from HTTPRoutePolicy statuses that are no longer present in the provided parentRefs.
225+ func updateDeleteAncestors (client client.Client , logger logr.Logger , policy v1alpha1.HTTPRoutePolicy , parentRefs []gatewayv1.ParentReference ) {
226+ length := len (policy .Status .Ancestors )
227+ policy .Status .Ancestors = slices .DeleteFunc (policy .Status .Ancestors , func (status v1alpha2.PolicyAncestorStatus ) bool {
228+ return ! slices .ContainsFunc (parentRefs , func (ref gatewayv1.ParentReference ) bool {
229+ return parentRefValueEqual (status .AncestorRef , ref )
226230 })
227- if length != len ( policy . Status . Ancestors ) {
228- if err := client .Status (). Update ( context . Background (), & policy ); err != nil {
229- logger . Error ( err , "failed to update HTTPRoutePolicy status" )
230- }
231+ })
232+ if length != len ( policy .Status . Ancestors ) {
233+ if err := client . Status (). Update ( context . Background (), & policy ); err != nil {
234+ logger . Error ( err , "failed to update HTTPRoutePolicy status" )
231235 }
232236 }
233237}
0 commit comments