1313package controller
1414
1515import (
16+ "cmp"
1617 "context"
1718 "slices"
1819
@@ -59,26 +60,25 @@ func (r *HTTPRouteReconciler) processHTTPRoutePolicies(tctx *provider.TranslateC
5960 var (
6061 policy = list .Items [i ]
6162 namespacedName = types.NamespacedName {Namespace : policy .GetNamespace (), Name : policy .GetName ()}
62-
63- status = false
64- reason = string (v1alpha2 .PolicyReasonConflicted )
65- message = "HTTPRoutePolicy conflict with others target to the HTTPRoute"
63+ condition metav1.Condition
6664 )
67- if _ , conflict := conflicts [namespacedName ]; ! conflict {
68- status = true
69- reason = string (v1alpha2 .PolicyReasonAccepted )
70- message = ""
71-
65+ if _ , conflict := conflicts [namespacedName ]; conflict {
66+ condition . Status = metav1 . ConditionFalse
67+ condition . Reason = string (v1alpha2 .PolicyReasonConflicted )
68+ condition . Message = "HTTPRoutePolicy conflict with others target to the HTTPRoute "
69+ } else {
7270 tctx .HTTPRoutePolicies = append (tctx .HTTPRoutePolicies , policy )
7371 }
74- modifyHTTPRoutePolicyStatus (httpRoute .Spec .ParentRefs , & policy , status , reason , message )
75- tctx .StatusUpdaters = append (tctx .StatusUpdaters , & policy )
72+
73+ if updated := setAncestorsForHTTPRoutePolicyStatus (httpRoute .Spec .ParentRefs , & policy , condition ); updated {
74+ tctx .StatusUpdaters = append (tctx .StatusUpdaters , & policy )
75+ }
7676 }
7777
7878 return nil
7979}
8080
81- func (r * HTTPRouteReconciler ) updateHTTPRoutePolicyStatusOnDeleting (nn types.NamespacedName ) error {
81+ func (r * HTTPRouteReconciler ) updateHTTPRoutePolicyStatusOnDeleting (ctx context. Context , nn types.NamespacedName ) error {
8282 var (
8383 list v1alpha1.HTTPRoutePolicyList
8484 key = indexer .GenIndexKeyWithGK (gatewayv1 .GroupName , "HTTPRoute" , nn .Namespace , nn .Name )
@@ -104,7 +104,7 @@ func (r *HTTPRouteReconciler) updateHTTPRoutePolicyStatusOnDeleting(nn types.Nam
104104 parentRefs = append (parentRefs , httpRoute .Spec .ParentRefs ... )
105105 }
106106 // delete AncestorRef which is not exist in the all parentRefs for each policy
107- updateDeleteAncestors (r .Client , r .Log , policy , parentRefs )
107+ updateDeleteAncestors (ctx , r .Client , r .Log , policy , parentRefs )
108108 }
109109
110110 return nil
@@ -124,33 +124,32 @@ func (r *IngressReconciler) processHTTPRoutePolicies(tctx *provider.TranslateCon
124124 }
125125
126126 var (
127- status = false
128- reason = string (v1alpha2 .PolicyReasonConflicted )
129- message = "HTTPRoutePolicy conflict with others target to the Ingress"
127+ condition metav1.Condition
130128 )
131- if conflict := checkPoliciesConflict (list .Items ); ! conflict {
132- status = true
133- reason = string (v1alpha2 .PolicyReasonAccepted )
134- message = ""
135-
129+ if conflict := checkPoliciesConflict (list .Items ); conflict {
130+ condition . Status = metav1 . ConditionFalse
131+ condition . Reason = string (v1alpha2 .PolicyReasonConflicted )
132+ condition . Message = "HTTPRoutePolicy conflict with others target to the Ingress "
133+ } else {
136134 tctx .HTTPRoutePolicies = list .Items
137135 }
138136
139137 for i := range list .Items {
140138 policy := list .Items [i ]
141- modifyHTTPRoutePolicyStatus (tctx .RouteParentRefs , & policy , status , reason , message )
142- tctx .StatusUpdaters = append (tctx .StatusUpdaters , & policy )
139+ if updated := setAncestorsForHTTPRoutePolicyStatus (tctx .RouteParentRefs , & policy , condition ); updated {
140+ tctx .StatusUpdaters = append (tctx .StatusUpdaters , & policy )
141+ }
143142 }
144143
145144 return nil
146145}
147146
148- func (r * IngressReconciler ) updateHTTPRoutePolicyStatusOnDeleting (nn types.NamespacedName ) error {
147+ func (r * IngressReconciler ) updateHTTPRoutePolicyStatusOnDeleting (ctx context. Context , nn types.NamespacedName ) error {
149148 var (
150149 list v1alpha1.HTTPRoutePolicyList
151150 key = indexer .GenIndexKeyWithGK (networkingv1 .GroupName , "Ingress" , nn .Namespace , nn .Name )
152151 )
153- if err := r .List (context . Background () , & list , client.MatchingFields {indexer .PolicyTargetRefs : key }); err != nil {
152+ if err := r .List (ctx , & list , client.MatchingFields {indexer .PolicyTargetRefs : key }); err != nil {
154153 return err
155154 }
156155 var (
@@ -164,7 +163,7 @@ func (r *IngressReconciler) updateHTTPRoutePolicyStatusOnDeleting(nn types.Names
164163 parentRef , ok := ingress2ParentRef [namespacedName ]
165164 if ! ok {
166165 var ingress networkingv1.Ingress
167- if err := r .Get (context . Background () , namespacedName , & ingress ); err != nil {
166+ if err := r .Get (ctx , namespacedName , & ingress ); err != nil {
168167 continue
169168 }
170169 ingressClass , err := r .getIngressClass (& ingress )
@@ -173,33 +172,29 @@ func (r *IngressReconciler) updateHTTPRoutePolicyStatusOnDeleting(nn types.Names
173172 }
174173 parentRef = gatewayv1.ParentReference {
175174 Group : ptr .To (gatewayv1 .Group (ingressClass .GroupVersionKind ().Group )),
176- Kind : ptr .To (gatewayv1 .Kind ("IngressClass" )),
175+ Kind : ptr .To (gatewayv1 .Kind (KindIngressClass )),
177176 Name : gatewayv1 .ObjectName (ingressClass .Name ),
178177 }
179178 ingress2ParentRef [namespacedName ] = parentRef
180179 }
181180 parentRefs = append (parentRefs , parentRef )
182181 }
183182 // delete AncestorRef which is not exist in the all parentRefs
184- updateDeleteAncestors (r .Client , r .Log , policy , parentRefs )
183+ updateDeleteAncestors (ctx , r .Client , r .Log , policy , parentRefs )
185184 }
186185
187186 return nil
188187}
189188
190- func modifyHTTPRoutePolicyStatus (parentRefs []gatewayv1.ParentReference , policy * v1alpha1.HTTPRoutePolicy , status bool , reason , message string ) {
191- condition := metav1.Condition {
192- Type : string (v1alpha2 .PolicyConditionAccepted ),
193- Status : metav1 .ConditionTrue ,
189+ func setAncestorsForHTTPRoutePolicyStatus (parentRefs []gatewayv1.ParentReference , policy * v1alpha1.HTTPRoutePolicy , condition metav1. Condition ) bool {
190+ return SetAncestors ( & policy . Status , parentRefs , metav1.Condition {
191+ Type : cmp . Or ( condition . Type , string (v1alpha2 .PolicyConditionAccepted ) ),
192+ Status : cmp . Or ( condition . Status , metav1 .ConditionTrue ) ,
194193 ObservedGeneration : policy .GetGeneration (),
195194 LastTransitionTime : metav1 .Now (),
196- Reason : reason ,
197- Message : message ,
198- }
199- if ! status {
200- condition .Status = metav1 .ConditionFalse
201- }
202- _ = SetAncestors (& policy .Status , parentRefs , condition )
195+ Reason : cmp .Or (condition .Reason , string (v1alpha2 .PolicyReasonAccepted )),
196+ Message : condition .Message ,
197+ })
203198}
204199
205200// checkPoliciesConflict determines if there is a conflict among the given HTTPRoutePolicy objects based on their priority values.
@@ -234,15 +229,15 @@ func findPoliciesWhichTargetRefTheRule(ruleName *gatewayv1.SectionName, kind str
234229}
235230
236231// updateDeleteAncestors removes ancestor references from HTTPRoutePolicy statuses that are no longer present in the provided parentRefs.
237- func updateDeleteAncestors (client client.Client , logger logr.Logger , policy v1alpha1.HTTPRoutePolicy , parentRefs []gatewayv1.ParentReference ) {
232+ func updateDeleteAncestors (ctx context. Context , client client.Client , logger logr.Logger , policy v1alpha1.HTTPRoutePolicy , parentRefs []gatewayv1.ParentReference ) {
238233 length := len (policy .Status .Ancestors )
239- policy .Status .Ancestors = slices .DeleteFunc (policy .Status .Ancestors , func (status v1alpha2.PolicyAncestorStatus ) bool {
234+ policy .Status .Ancestors = slices .DeleteFunc (policy .Status .Ancestors , func (ancestor v1alpha2.PolicyAncestorStatus ) bool {
240235 return ! slices .ContainsFunc (parentRefs , func (ref gatewayv1.ParentReference ) bool {
241- return parentRefValueEqual (status .AncestorRef , ref )
236+ return parentRefValueEqual (ancestor .AncestorRef , ref )
242237 })
243238 })
244239 if length != len (policy .Status .Ancestors ) {
245- if err := client .Status ().Update (context . Background () , & policy ); err != nil {
240+ if err := client .Status ().Update (ctx , & policy ); err != nil {
246241 logger .Error (err , "failed to update HTTPRoutePolicy status" )
247242 }
248243 }
0 commit comments