@@ -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,20 @@ 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 ,
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+ tCopy := t .DeepCopy ()
85+ tCopy .Status = policy .Status
86+ return tCopy
87+ }),
88+ })
7589 }
7690 }
7791
@@ -104,7 +118,7 @@ func (r *HTTPRouteReconciler) updateHTTPRoutePolicyStatusOnDeleting(ctx context.
104118 parentRefs = append (parentRefs , httpRoute .Spec .ParentRefs ... )
105119 }
106120 // delete AncestorRef which is not exist in the all parentRefs for each policy
107- updateDeleteAncestors (ctx , r . Client , r . Log , policy , parentRefs )
121+ updateDeleteAncestors (r . Updater , policy , parentRefs )
108122 }
109123
110124 return nil
@@ -137,7 +151,20 @@ func (r *IngressReconciler) processHTTPRoutePolicies(tctx *provider.TranslateCon
137151 for i := range list .Items {
138152 policy := list .Items [i ]
139153 if updated := setAncestorsForHTTPRoutePolicyStatus (tctx .RouteParentRefs , & policy , condition ); updated {
140- tctx .StatusUpdaters = append (tctx .StatusUpdaters , & policy )
154+ tctx .StatusUpdaters = append (tctx .StatusUpdaters , status.Update {
155+ NamespacedName : NamespacedName (& policy ),
156+ Resource : & policy ,
157+ Mutator : status .MutatorFunc (func (obj client.Object ) client.Object {
158+ t , ok := obj .(* v1alpha1.HTTPRoutePolicy )
159+ if ! ok {
160+ err := fmt .Errorf ("unsupported object type %T" , obj )
161+ panic (err )
162+ }
163+ tCopy := t .DeepCopy ()
164+ tCopy .Status = policy .Status
165+ return tCopy
166+ }),
167+ })
141168 }
142169 }
143170
@@ -180,7 +207,7 @@ func (r *IngressReconciler) updateHTTPRoutePolicyStatusOnDeleting(ctx context.Co
180207 parentRefs = append (parentRefs , parentRef )
181208 }
182209 // delete AncestorRef which is not exist in the all parentRefs
183- updateDeleteAncestors (ctx , r . Client , r . Log , policy , parentRefs )
210+ updateDeleteAncestors (r . Updater , policy , parentRefs )
184211 }
185212
186213 return nil
@@ -229,16 +256,26 @@ func findPoliciesWhichTargetRefTheRule(ruleName *gatewayv1.SectionName, kind str
229256}
230257
231258// 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 ) {
259+ func updateDeleteAncestors (updater status. Updater , policy v1alpha1.HTTPRoutePolicy , parentRefs []gatewayv1.ParentReference ) {
233260 length := len (policy .Status .Ancestors )
234261 policy .Status .Ancestors = slices .DeleteFunc (policy .Status .Ancestors , func (ancestor v1alpha2.PolicyAncestorStatus ) bool {
235262 return ! slices .ContainsFunc (parentRefs , func (ref gatewayv1.ParentReference ) bool {
236263 return parentRefValueEqual (ancestor .AncestorRef , ref )
237264 })
238265 })
239266 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- }
267+ updater .Update (status.Update {
268+ NamespacedName : NamespacedName (& policy ),
269+ Resource : policy .DeepCopy (),
270+ Mutator : status .MutatorFunc (func (obj client.Object ) client.Object {
271+ t , ok := obj .(* v1alpha1.HTTPRoutePolicy )
272+ if ! ok {
273+ err := fmt .Errorf ("unsupported object type %T" , obj )
274+ panic (err )
275+ }
276+ t .Status = policy .Status
277+ return t
278+ }),
279+ })
243280 }
244281}
0 commit comments