@@ -15,9 +15,9 @@ package controller
1515import (
1616 "cmp"
1717 "context"
18+ "fmt"
1819 "slices"
1920
20- "github.com/go-logr/logr"
2121 networkingv1 "k8s.io/api/networking/v1"
2222 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2323 "k8s.io/apimachinery/pkg/types"
@@ -28,6 +28,7 @@ import (
2828
2929 "github.com/apache/apisix-ingress-controller/api/v1alpha1"
3030 "github.com/apache/apisix-ingress-controller/internal/controller/indexer"
31+ "github.com/apache/apisix-ingress-controller/internal/controller/status"
3132 "github.com/apache/apisix-ingress-controller/internal/provider"
3233)
3334
@@ -71,7 +72,19 @@ func (r *HTTPRouteReconciler) processHTTPRoutePolicies(tctx *provider.TranslateC
7172 }
7273
7374 if updated := setAncestorsForHTTPRoutePolicyStatus (httpRoute .Spec .ParentRefs , & policy , condition ); updated {
74- tctx .StatusUpdaters = append (tctx .StatusUpdaters , & policy )
75+ tctx .StatusUpdaters = append (tctx .StatusUpdaters , status.Update {
76+ NamespacedName : NamespacedName (& policy ),
77+ Resource : policy .DeepCopy (),
78+ Mutator : status .MutatorFunc (func (obj client.Object ) client.Object {
79+ t , ok := obj .(* v1alpha1.HTTPRoutePolicy )
80+ if ! ok {
81+ err := fmt .Errorf ("unsupported object type %T" , obj )
82+ panic (err )
83+ }
84+ t .Status = policy .Status
85+ return t
86+ }),
87+ })
7588 }
7689 }
7790
@@ -83,7 +96,7 @@ func (r *HTTPRouteReconciler) updateHTTPRoutePolicyStatusOnDeleting(ctx context.
8396 list v1alpha1.HTTPRoutePolicyList
8497 key = indexer .GenIndexKeyWithGK (gatewayv1 .GroupName , "HTTPRoute" , nn .Namespace , nn .Name )
8598 )
86- if err := r .List (context . Background () , & list , client.MatchingFields {indexer .PolicyTargetRefs : key }); err != nil {
99+ if err := r .List (ctx , & list , client.MatchingFields {indexer .PolicyTargetRefs : key }); err != nil {
87100 return err
88101 }
89102 var (
@@ -96,15 +109,15 @@ func (r *HTTPRouteReconciler) updateHTTPRoutePolicyStatusOnDeleting(ctx context.
96109 var namespacedName = types.NamespacedName {Namespace : policy .GetNamespace (), Name : string (ref .Name )}
97110 httpRoute , ok := httpRoutes [namespacedName ]
98111 if ! ok {
99- if err := r .Get (context . Background () , namespacedName , & httpRoute ); err != nil {
112+ if err := r .Get (ctx , namespacedName , & httpRoute ); err != nil {
100113 continue
101114 }
102115 httpRoutes [namespacedName ] = httpRoute
103116 }
104117 parentRefs = append (parentRefs , httpRoute .Spec .ParentRefs ... )
105118 }
106119 // delete AncestorRef which is not exist in the all parentRefs for each policy
107- updateDeleteAncestors (ctx , r . Client , r . Log , policy , parentRefs )
120+ updateDeleteAncestors (r . Updater , policy , parentRefs )
108121 }
109122
110123 return nil
@@ -137,7 +150,19 @@ func (r *IngressReconciler) processHTTPRoutePolicies(tctx *provider.TranslateCon
137150 for i := range list .Items {
138151 policy := list .Items [i ]
139152 if updated := setAncestorsForHTTPRoutePolicyStatus (tctx .RouteParentRefs , & policy , condition ); updated {
140- tctx .StatusUpdaters = append (tctx .StatusUpdaters , & policy )
153+ tctx .StatusUpdaters = append (tctx .StatusUpdaters , status.Update {
154+ NamespacedName : NamespacedName (& policy ),
155+ Resource : policy .DeepCopy (),
156+ Mutator : status .MutatorFunc (func (obj client.Object ) client.Object {
157+ t , ok := obj .(* v1alpha1.HTTPRoutePolicy )
158+ if ! ok {
159+ err := fmt .Errorf ("unsupported object type %T" , obj )
160+ panic (err )
161+ }
162+ t .Status = policy .Status
163+ return t
164+ }),
165+ })
141166 }
142167 }
143168
@@ -180,7 +205,7 @@ func (r *IngressReconciler) updateHTTPRoutePolicyStatusOnDeleting(ctx context.Co
180205 parentRefs = append (parentRefs , parentRef )
181206 }
182207 // delete AncestorRef which is not exist in the all parentRefs
183- updateDeleteAncestors (ctx , r . Client , r . Log , policy , parentRefs )
208+ updateDeleteAncestors (r . Updater , policy , parentRefs )
184209 }
185210
186211 return nil
@@ -229,16 +254,26 @@ func findPoliciesWhichTargetRefTheRule(ruleName *gatewayv1.SectionName, kind str
229254}
230255
231256// updateDeleteAncestors removes ancestor references from HTTPRoutePolicy statuses that are no longer present in the provided parentRefs.
232- func updateDeleteAncestors (ctx context. Context , client client. Client , logger logr. Logger , policy v1alpha1.HTTPRoutePolicy , parentRefs []gatewayv1.ParentReference ) {
257+ func updateDeleteAncestors (updater status. Updater , policy v1alpha1.HTTPRoutePolicy , parentRefs []gatewayv1.ParentReference ) {
233258 length := len (policy .Status .Ancestors )
234259 policy .Status .Ancestors = slices .DeleteFunc (policy .Status .Ancestors , func (ancestor v1alpha2.PolicyAncestorStatus ) bool {
235260 return ! slices .ContainsFunc (parentRefs , func (ref gatewayv1.ParentReference ) bool {
236261 return parentRefValueEqual (ancestor .AncestorRef , ref )
237262 })
238263 })
239264 if length != len (policy .Status .Ancestors ) {
240- if err := client .Status ().Update (ctx , & policy ); err != nil {
241- logger .Error (err , "failed to update HTTPRoutePolicy status" )
242- }
265+ updater .Update (status.Update {
266+ NamespacedName : NamespacedName (& policy ),
267+ Resource : policy .DeepCopy (),
268+ Mutator : status .MutatorFunc (func (obj client.Object ) client.Object {
269+ t , ok := obj .(* v1alpha1.HTTPRoutePolicy )
270+ if ! ok {
271+ err := fmt .Errorf ("unsupported object type %T" , obj )
272+ panic (err )
273+ }
274+ t .Status = policy .Status
275+ return t
276+ }),
277+ })
243278 }
244279}
0 commit comments