Skip to content

Commit 2dc0ee0

Browse files
committed
refactor(status): aynsc update status
1 parent bf07565 commit 2dc0ee0

File tree

14 files changed

+307
-43
lines changed

14 files changed

+307
-43
lines changed

internal/controller/consumer_controller.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ package controller
1414

1515
import (
1616
"context"
17+
"fmt"
1718

1819
"github.com/go-logr/logr"
1920
corev1 "k8s.io/api/core/v1"
@@ -32,6 +33,7 @@ import (
3233

3334
"github.com/apache/apisix-ingress-controller/api/v1alpha1"
3435
"github.com/apache/apisix-ingress-controller/internal/controller/indexer"
36+
"github.com/apache/apisix-ingress-controller/internal/controller/status"
3537
"github.com/apache/apisix-ingress-controller/internal/provider"
3638
)
3739

@@ -42,6 +44,8 @@ type ConsumerReconciler struct { //nolint:revive
4244
Log logr.Logger
4345

4446
Provider provider.Provider
47+
48+
Updater status.Updater
4549
}
4650

4751
// SetupWithManager sets up the controller with the Manager.
@@ -278,10 +282,20 @@ func (r *ConsumerReconciler) updateStatus(ctx context.Context, consumer *v1alpha
278282
return nil
279283
}
280284
meta.SetStatusCondition(&consumer.Status.Conditions, condition)
281-
if err := r.Status().Update(ctx, consumer); err != nil {
282-
r.Log.Error(err, "failed to update consumer status", "consumer", consumer)
283-
return err
284-
}
285+
286+
r.Updater.Update(status.Update{
287+
NamespacedName: NamespacedName(consumer),
288+
Resource: consumer.DeepCopy(),
289+
Mutator: status.MutatorFunc(func(obj client.Object) client.Object {
290+
t, ok := obj.(*v1alpha1.Consumer)
291+
if !ok {
292+
err := fmt.Errorf("unsupported object type %T", obj)
293+
panic(err)
294+
}
295+
t.Status = consumer.Status
296+
return t
297+
}),
298+
})
285299
return nil
286300
}
287301

internal/controller/gateway_controller.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535

3636
"github.com/apache/apisix-ingress-controller/api/v1alpha1"
3737
"github.com/apache/apisix-ingress-controller/internal/controller/indexer"
38+
"github.com/apache/apisix-ingress-controller/internal/controller/status"
3839
"github.com/apache/apisix-ingress-controller/internal/provider"
3940
)
4041

@@ -45,6 +46,8 @@ type GatewayReconciler struct { //nolint:revive
4546
Log logr.Logger
4647

4748
Provider provider.Provider
49+
50+
Updater status.Updater
4851
}
4952

5053
// SetupWithManager sets up the controller with the Manager.
@@ -117,11 +120,11 @@ func (r *GatewayReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
117120
conditionProgrammedStatus, conditionProgrammedMsg := true, "Programmed"
118121

119122
r.Log.Info("gateway has been accepted", "gateway", gateway.GetName())
120-
type status struct {
123+
type conditionStatus struct {
121124
status bool
122125
msg string
123126
}
124-
acceptStatus := status{
127+
acceptStatus := conditionStatus{
125128
status: true,
126129
msg: acceptedMessage("gateway"),
127130
}
@@ -131,7 +134,7 @@ func (r *GatewayReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
131134

132135
r.processListenerConfig(tctx, gateway)
133136
if err := r.processInfrastructure(tctx, gateway); err != nil {
134-
acceptStatus = status{
137+
acceptStatus = conditionStatus{
135138
status: false,
136139
msg: err.Error(),
137140
}
@@ -147,7 +150,7 @@ func (r *GatewayReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
147150

148151
gatewayProxy, ok := tctx.GatewayProxies[rk]
149152
if !ok {
150-
acceptStatus = status{
153+
acceptStatus = conditionStatus{
151154
status: false,
152155
msg: "gateway proxy not found",
153156
}
@@ -167,7 +170,7 @@ func (r *GatewayReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
167170
}
168171

169172
if err := r.Provider.Update(ctx, tctx, gateway); err != nil {
170-
acceptStatus = status{
173+
acceptStatus = conditionStatus{
171174
status: false,
172175
msg: err.Error(),
173176
}
@@ -189,7 +192,21 @@ func (r *GatewayReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
189192
gateway.Status.Listeners = listenerStatuses
190193
}
191194

192-
return ctrl.Result{}, r.Status().Update(ctx, gateway)
195+
r.Updater.Update(status.Update{
196+
NamespacedName: NamespacedName(gateway),
197+
Resource: gateway.DeepCopy(),
198+
Mutator: status.MutatorFunc(func(obj client.Object) client.Object {
199+
t, ok := obj.(*gatewayv1.Gateway)
200+
if !ok {
201+
err := fmt.Errorf("unsupported object type %T", obj)
202+
panic(err)
203+
}
204+
t.Status = gateway.Status
205+
return t
206+
}),
207+
})
208+
209+
return ctrl.Result{}, nil
193210
}
194211

195212
return ctrl.Result{}, nil

internal/controller/gatewayclass_congroller.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030

3131
"github.com/apache/apisix-ingress-controller/internal/controller/config"
3232
"github.com/apache/apisix-ingress-controller/internal/controller/indexer"
33+
"github.com/apache/apisix-ingress-controller/internal/controller/status"
3334
)
3435

3536
const (
@@ -46,6 +47,8 @@ type GatewayClassReconciler struct { //nolint:revive
4647

4748
record.EventRecorder
4849
Log logr.Logger
50+
51+
Updater status.Updater
4952
}
5053

5154
// SetupWithManager sets up the controller with the Manager.
@@ -111,9 +114,19 @@ func (r *GatewayClassReconciler) Reconcile(ctx context.Context, req ctrl.Request
111114
if !IsConditionPresentAndEqual(gc.Status.Conditions, condition) {
112115
r.Log.Info("gatewayclass has been accepted", "gatewayclass", gc.Name)
113116
setGatewayClassCondition(gc, condition)
114-
if err := r.Status().Update(ctx, gc); err != nil {
115-
return ctrl.Result{}, err
116-
}
117+
r.Updater.Update(status.Update{
118+
NamespacedName: NamespacedName(gc),
119+
Resource: gc.DeepCopy(),
120+
Mutator: status.MutatorFunc(func(obj client.Object) client.Object {
121+
t, ok := obj.(*gatewayv1.GatewayClass)
122+
if !ok {
123+
err := fmt.Errorf("unsupported object type %T", obj)
124+
panic(err)
125+
}
126+
t.Status = gc.Status
127+
return t
128+
}),
129+
})
117130
}
118131
return ctrl.Result{}, nil
119132
}

internal/controller/httproute_controller.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import (
4242

4343
"github.com/apache/apisix-ingress-controller/api/v1alpha1"
4444
"github.com/apache/apisix-ingress-controller/internal/controller/indexer"
45+
"github.com/apache/apisix-ingress-controller/internal/controller/status"
4546
"github.com/apache/apisix-ingress-controller/internal/provider"
4647
)
4748

@@ -55,6 +56,8 @@ type HTTPRouteReconciler struct { //nolint:revive
5556
Provider provider.Provider
5657

5758
genericEvent chan event.GenericEvent
59+
60+
Updater status.Updater
5861
}
5962

6063
// SetupWithManager sets up the controller with the Manager.
@@ -143,13 +146,13 @@ func (r *HTTPRouteReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
143146
return ctrl.Result{}, err
144147
}
145148

146-
type status struct {
149+
type ResourceStatus struct {
147150
status bool
148151
msg string
149152
}
150153

151154
// Only keep acceptStatus since we're using error objects directly now
152-
acceptStatus := status{
155+
acceptStatus := ResourceStatus{
153156
status: true,
154157
msg: "Route is accepted",
155158
}
@@ -233,10 +236,20 @@ func (r *HTTPRouteReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
233236

234237
hr.Status.Parents = append(hr.Status.Parents, parentStatus)
235238
}
236-
if err := r.Status().Update(ctx, hr); err != nil {
237-
return ctrl.Result{}, err
238-
}
239-
UpdateStatus(r.Client, r.Log, tctx)
239+
r.Updater.Update(status.Update{
240+
NamespacedName: NamespacedName(hr),
241+
Resource: hr.DeepCopy(),
242+
Mutator: status.MutatorFunc(func(obj client.Object) client.Object {
243+
h, ok := obj.(*gatewayv1.HTTPRoute)
244+
if !ok {
245+
err := fmt.Errorf("unsupported object type %T", obj)
246+
panic(err)
247+
}
248+
h.Status = hr.Status
249+
return h
250+
}),
251+
})
252+
UpdateStatus(r.Updater, r.Log, tctx)
240253
return ctrl.Result{}, nil
241254
}
242255

internal/controller/httproutepolicy.go

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ package controller
1515
import (
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
}

internal/controller/ingress_controller.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import (
4141
"github.com/apache/apisix-ingress-controller/api/v1alpha1"
4242
"github.com/apache/apisix-ingress-controller/internal/controller/config"
4343
"github.com/apache/apisix-ingress-controller/internal/controller/indexer"
44+
"github.com/apache/apisix-ingress-controller/internal/controller/status"
4445
"github.com/apache/apisix-ingress-controller/internal/provider"
4546
)
4647

@@ -52,6 +53,8 @@ type IngressReconciler struct { //nolint:revive
5253

5354
Provider provider.Provider
5455
genericEvent chan event.GenericEvent
56+
57+
Updater status.Updater
5558
}
5659

5760
// SetupWithManager sets up the controller with the Manager.
@@ -185,7 +188,7 @@ func (r *IngressReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
185188
}
186189

187190
// update the status of related resources
188-
UpdateStatus(r.Client, r.Log, tctx)
191+
UpdateStatus(r.Updater, r.Log, tctx)
189192

190193
// update the ingress status
191194
if err := r.updateStatus(ctx, tctx, ingress, ingressClass); err != nil {
@@ -661,7 +664,20 @@ func (r *IngressReconciler) updateStatus(ctx context.Context, tctx *provider.Tra
661664
// update the load balancer status
662665
if len(loadBalancerStatus.Ingress) > 0 && !reflect.DeepEqual(ingress.Status.LoadBalancer, loadBalancerStatus) {
663666
ingress.Status.LoadBalancer = loadBalancerStatus
664-
return r.Status().Update(ctx, ingress)
667+
r.Updater.Update(status.Update{
668+
NamespacedName: NamespacedName(ingress),
669+
Resource: ingress.DeepCopy(),
670+
Mutator: status.MutatorFunc(func(obj client.Object) client.Object {
671+
t, ok := obj.(*networkingv1.Ingress)
672+
if !ok {
673+
err := fmt.Errorf("unsupported object type %T", obj)
674+
panic(err)
675+
}
676+
t.Status = ingress.Status
677+
return t
678+
}),
679+
})
680+
return nil
665681
}
666682

667683
return nil

0 commit comments

Comments
 (0)