Skip to content

Commit 4ee0e26

Browse files
authored
refactor(status): aynsc update status (#151)
1 parent 683bd4f commit 4ee0e26

File tree

14 files changed

+326
-51
lines changed

14 files changed

+326
-51
lines changed

internal/controller/consumer_controller.go

Lines changed: 21 additions & 10 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.
@@ -232,9 +236,7 @@ func (r *ConsumerReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
232236
statusErr = err
233237
}
234238

235-
if err := r.updateStatus(ctx, consumer, statusErr); err != nil {
236-
return ctrl.Result{}, err
237-
}
239+
r.updateStatus(consumer, statusErr)
238240

239241
return ctrl.Result{}, nil
240242
}
@@ -269,20 +271,29 @@ func (r *ConsumerReconciler) processSpec(ctx context.Context, tctx *provider.Tra
269271
return nil
270272
}
271273

272-
func (r *ConsumerReconciler) updateStatus(ctx context.Context, consumer *v1alpha1.Consumer, err error) error {
274+
func (r *ConsumerReconciler) updateStatus(consumer *v1alpha1.Consumer, err error) {
273275
condition := NewCondition(consumer.Generation, true, "Successfully")
274276
if err != nil {
275277
condition = NewCondition(consumer.Generation, false, err.Error())
276278
}
277279
if !VerifyConditions(&consumer.Status.Conditions, condition) {
278-
return nil
280+
return
279281
}
280282
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-
return nil
283+
284+
r.Updater.Update(status.Update{
285+
NamespacedName: NamespacedName(consumer),
286+
Resource: consumer.DeepCopy(),
287+
Mutator: status.MutatorFunc(func(obj client.Object) client.Object {
288+
t, ok := obj.(*v1alpha1.Consumer)
289+
if !ok {
290+
err := fmt.Errorf("unsupported object type %T", obj)
291+
panic(err)
292+
}
293+
t.Status = consumer.Status
294+
return t
295+
}),
296+
})
286297
}
287298

288299
func (r *ConsumerReconciler) getGateway(ctx context.Context, consumer *v1alpha1.Consumer) (*gatewayv1.Gateway, error) {

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 & 11 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,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

Comments
 (0)