Skip to content

Commit 3226d01

Browse files
authored
chore: remove useless watch (#173)
Signed-off-by: ashing <[email protected]>
1 parent fc15891 commit 3226d01

File tree

2 files changed

+4
-255
lines changed

2 files changed

+4
-255
lines changed

internal/controller/apisixpluginconfig_controller.go

Lines changed: 2 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,11 @@ import (
1717
"fmt"
1818

1919
"github.com/go-logr/logr"
20-
networkingv1 "k8s.io/api/networking/v1"
21-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2220
"k8s.io/apimachinery/pkg/runtime"
2321
ctrl "sigs.k8s.io/controller-runtime"
24-
"sigs.k8s.io/controller-runtime/pkg/builder"
2522
"sigs.k8s.io/controller-runtime/pkg/client"
26-
"sigs.k8s.io/controller-runtime/pkg/handler"
2723
"sigs.k8s.io/controller-runtime/pkg/predicate"
28-
"sigs.k8s.io/controller-runtime/pkg/reconcile"
2924

30-
"github.com/apache/apisix-ingress-controller/api/v1alpha1"
3125
apiv2 "github.com/apache/apisix-ingress-controller/api/v2"
3226
"github.com/apache/apisix-ingress-controller/internal/controller/status"
3327
"github.com/apache/apisix-ingress-controller/internal/utils"
@@ -46,15 +40,6 @@ func (r *ApisixPluginConfigReconciler) SetupWithManager(mgr ctrl.Manager) error
4640
return ctrl.NewControllerManagedBy(mgr).
4741
For(&apiv2.ApisixPluginConfig{}).
4842
WithEventFilter(predicate.GenerationChangedPredicate{}).
49-
Watches(&networkingv1.IngressClass{},
50-
handler.EnqueueRequestsFromMapFunc(r.listApisixPluginConfigForIngressClass),
51-
builder.WithPredicates(
52-
predicate.NewPredicateFuncs(r.matchesIngressController),
53-
),
54-
).
55-
Watches(&v1alpha1.GatewayProxy{},
56-
handler.EnqueueRequestsFromMapFunc(r.listApisixPluginConfigForGatewayProxy),
57-
).
5843
Named("apisixpluginconfig").
5944
Complete(r)
6045
}
@@ -65,123 +50,11 @@ func (r *ApisixPluginConfigReconciler) Reconcile(ctx context.Context, req ctrl.R
6550
return ctrl.Result{}, client.IgnoreNotFound(err)
6651
}
6752

68-
var (
69-
ic *networkingv1.IngressClass
70-
err error
71-
)
72-
defer func() {
73-
r.updateStatus(&pc, err)
74-
}()
75-
76-
if ic, err = r.getIngressClass(&pc); err != nil {
77-
return ctrl.Result{}, err
78-
}
79-
if err = r.processIngressClassParameters(ctx, &pc, ic); err != nil {
80-
return ctrl.Result{}, err
81-
}
53+
// Only update status
54+
r.updateStatus(&pc, nil)
8255
return ctrl.Result{}, nil
8356
}
8457

85-
func (r *ApisixPluginConfigReconciler) listApisixPluginConfigForIngressClass(ctx context.Context, object client.Object) (requests []reconcile.Request) {
86-
ic, ok := object.(*networkingv1.IngressClass)
87-
if !ok {
88-
return nil
89-
}
90-
91-
isDefaultIngressClass := IsDefaultIngressClass(ic)
92-
var pcList apiv2.ApisixPluginConfigList
93-
if err := r.List(ctx, &pcList); err != nil {
94-
return nil
95-
}
96-
for _, pc := range pcList.Items {
97-
if pc.Spec.IngressClassName == ic.Name || (isDefaultIngressClass && pc.Spec.IngressClassName == "") {
98-
requests = append(requests, reconcile.Request{NamespacedName: utils.NamespacedName(&pc)})
99-
}
100-
}
101-
return requests
102-
}
103-
104-
func (r *ApisixPluginConfigReconciler) listApisixPluginConfigForGatewayProxy(ctx context.Context, object client.Object) (requests []reconcile.Request) {
105-
gp, ok := object.(*v1alpha1.GatewayProxy)
106-
if !ok {
107-
return nil
108-
}
109-
110-
var icList networkingv1.IngressClassList
111-
if err := r.List(ctx, &icList); err != nil {
112-
r.Log.Error(err, "failed to list ingress classes for gateway proxy", "gatewayproxy", gp.GetName())
113-
return nil
114-
}
115-
116-
for _, ic := range icList.Items {
117-
requests = append(requests, r.listApisixPluginConfigForIngressClass(ctx, &ic)...)
118-
}
119-
120-
return requests
121-
}
122-
123-
func (r *ApisixPluginConfigReconciler) matchesIngressController(obj client.Object) bool {
124-
ingressClass, ok := obj.(*networkingv1.IngressClass)
125-
if !ok {
126-
return false
127-
}
128-
return matchesController(ingressClass.Spec.Controller)
129-
}
130-
131-
func (r *ApisixPluginConfigReconciler) getIngressClass(pc *apiv2.ApisixPluginConfig) (*networkingv1.IngressClass, error) {
132-
if pc.Spec.IngressClassName == "" {
133-
return r.getDefaultIngressClass()
134-
}
135-
136-
var ic networkingv1.IngressClass
137-
if err := r.Get(context.Background(), client.ObjectKey{Name: pc.Spec.IngressClassName}, &ic); err != nil {
138-
return nil, err
139-
}
140-
return &ic, nil
141-
}
142-
143-
func (r *ApisixPluginConfigReconciler) getDefaultIngressClass() (*networkingv1.IngressClass, error) {
144-
var icList networkingv1.IngressClassList
145-
if err := r.List(context.Background(), &icList); err != nil {
146-
r.Log.Error(err, "failed to list ingress classes")
147-
return nil, err
148-
}
149-
for _, ic := range icList.Items {
150-
if IsDefaultIngressClass(&ic) && matchesController(ic.Spec.Controller) {
151-
return &ic, nil
152-
}
153-
}
154-
return nil, ReasonError{
155-
Reason: string(metav1.StatusReasonNotFound),
156-
Message: "default ingress class not found or does not match the controller",
157-
}
158-
}
159-
160-
// processIngressClassParameters processes the IngressClass parameters that reference GatewayProxy
161-
func (r *ApisixPluginConfigReconciler) processIngressClassParameters(ctx context.Context, pc *apiv2.ApisixPluginConfig, ingressClass *networkingv1.IngressClass) error {
162-
if ingressClass == nil || ingressClass.Spec.Parameters == nil {
163-
return nil
164-
}
165-
166-
var (
167-
parameters = ingressClass.Spec.Parameters
168-
)
169-
if parameters.APIGroup == nil || *parameters.APIGroup != v1alpha1.GroupVersion.Group || parameters.Kind != KindGatewayProxy {
170-
return nil
171-
}
172-
173-
// check if the parameters reference GatewayProxy
174-
var (
175-
gatewayProxy v1alpha1.GatewayProxy
176-
ns = parameters.Namespace
177-
)
178-
if ns == nil {
179-
ns = &pc.Namespace
180-
}
181-
182-
return r.Get(ctx, client.ObjectKey{Namespace: *ns, Name: parameters.Name}, &gatewayProxy)
183-
}
184-
18558
func (r *ApisixPluginConfigReconciler) updateStatus(pc *apiv2.ApisixPluginConfig, err error) {
18659
SetApisixCRDConditionAccepted(&pc.Status, pc.GetGeneration(), err)
18760
r.Updater.Update(status.Update{

internal/controller/apisixupstream_controller.go

Lines changed: 2 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,14 @@
1313
package controller
1414

1515
import (
16-
"cmp"
1716
"context"
1817

1918
"github.com/go-logr/logr"
20-
networkingv1 "k8s.io/api/networking/v1"
21-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2219
"k8s.io/apimachinery/pkg/runtime"
2320
ctrl "sigs.k8s.io/controller-runtime"
24-
"sigs.k8s.io/controller-runtime/pkg/builder"
2521
"sigs.k8s.io/controller-runtime/pkg/client"
26-
"sigs.k8s.io/controller-runtime/pkg/handler"
2722
"sigs.k8s.io/controller-runtime/pkg/predicate"
28-
"sigs.k8s.io/controller-runtime/pkg/reconcile"
2923

30-
"github.com/apache/apisix-ingress-controller/api/v1alpha1"
3124
apiv2 "github.com/apache/apisix-ingress-controller/api/v2"
3225
"github.com/apache/apisix-ingress-controller/internal/controller/status"
3326
"github.com/apache/apisix-ingress-controller/internal/utils"
@@ -46,15 +39,6 @@ func (r *ApisixUpstreamReconciler) SetupWithManager(mgr ctrl.Manager) error {
4639
return ctrl.NewControllerManagedBy(mgr).
4740
For(&apiv2.ApisixUpstream{}).
4841
WithEventFilter(predicate.GenerationChangedPredicate{}).
49-
Watches(&networkingv1.IngressClass{},
50-
handler.EnqueueRequestsFromMapFunc(r.listApisixUpstreamForIngressClass),
51-
builder.WithPredicates(
52-
predicate.NewPredicateFuncs(r.matchesIngressController),
53-
),
54-
).
55-
Watches(&v1alpha1.GatewayProxy{},
56-
handler.EnqueueRequestsFromMapFunc(r.listApisixUpstreamForGatewayProxy),
57-
).
5842
Named("apisixupstream").
5943
Complete(r)
6044
}
@@ -65,119 +49,11 @@ func (r *ApisixUpstreamReconciler) Reconcile(ctx context.Context, req ctrl.Reque
6549
return ctrl.Result{}, client.IgnoreNotFound(err)
6650
}
6751

68-
var (
69-
ic *networkingv1.IngressClass
70-
err error
71-
)
72-
defer func() {
73-
r.updateStatus(&au, err)
74-
}()
75-
76-
if ic, err = r.getIngressClass(&au); err != nil {
77-
return ctrl.Result{}, err
78-
}
79-
if err = r.processIngressClassParameters(ctx, &au, ic); err != nil {
80-
return ctrl.Result{}, err
81-
}
52+
// Only update status
53+
r.updateStatus(&au, nil)
8254
return ctrl.Result{}, nil
8355
}
8456

85-
func (r *ApisixUpstreamReconciler) listApisixUpstreamForIngressClass(ctx context.Context, object client.Object) (requests []reconcile.Request) {
86-
ic, ok := object.(*networkingv1.IngressClass)
87-
if !ok {
88-
return nil
89-
}
90-
91-
isDefaultIngressClass := IsDefaultIngressClass(ic)
92-
var auList apiv2.ApisixUpstreamList
93-
if err := r.List(ctx, &auList); err != nil {
94-
return nil
95-
}
96-
for _, pc := range auList.Items {
97-
if pc.Spec.IngressClassName == ic.Name || (isDefaultIngressClass && pc.Spec.IngressClassName == "") {
98-
requests = append(requests, reconcile.Request{NamespacedName: utils.NamespacedName(&pc)})
99-
}
100-
}
101-
return requests
102-
}
103-
104-
func (r *ApisixUpstreamReconciler) listApisixUpstreamForGatewayProxy(ctx context.Context, object client.Object) (requests []reconcile.Request) {
105-
gp, ok := object.(*v1alpha1.GatewayProxy)
106-
if !ok {
107-
return nil
108-
}
109-
110-
var icList networkingv1.IngressClassList
111-
if err := r.List(ctx, &icList); err != nil {
112-
r.Log.Error(err, "failed to list ingress classes for gateway proxy", "gatewayproxy", gp.GetName())
113-
return nil
114-
}
115-
116-
for _, ic := range icList.Items {
117-
requests = append(requests, r.listApisixUpstreamForIngressClass(ctx, &ic)...)
118-
}
119-
120-
return requests
121-
}
122-
123-
func (r *ApisixUpstreamReconciler) matchesIngressController(obj client.Object) bool {
124-
ingressClass, ok := obj.(*networkingv1.IngressClass)
125-
if !ok {
126-
return false
127-
}
128-
return matchesController(ingressClass.Spec.Controller)
129-
}
130-
131-
func (r *ApisixUpstreamReconciler) getIngressClass(au *apiv2.ApisixUpstream) (*networkingv1.IngressClass, error) {
132-
if au.Spec.IngressClassName == "" {
133-
return r.getDefaultIngressClass()
134-
}
135-
136-
var ic networkingv1.IngressClass
137-
if err := r.Get(context.Background(), client.ObjectKey{Name: au.Spec.IngressClassName}, &ic); err != nil {
138-
return nil, err
139-
}
140-
return &ic, nil
141-
}
142-
143-
func (r *ApisixUpstreamReconciler) processIngressClassParameters(ctx context.Context, au *apiv2.ApisixUpstream, ic *networkingv1.IngressClass) error {
144-
if ic == nil || ic.Spec.Parameters == nil {
145-
return nil
146-
}
147-
148-
var (
149-
parameters = ic.Spec.Parameters
150-
)
151-
if parameters.APIGroup == nil || *parameters.APIGroup != v1alpha1.GroupVersion.Group || parameters.Kind != KindGatewayProxy {
152-
return nil
153-
}
154-
155-
// check if the parameters reference GatewayProxy
156-
var (
157-
gp v1alpha1.GatewayProxy
158-
ns = cmp.Or(parameters.Namespace, &au.Namespace)
159-
)
160-
161-
return r.Get(ctx, client.ObjectKey{Namespace: *ns, Name: parameters.Name}, &gp)
162-
}
163-
164-
func (r *ApisixUpstreamReconciler) getDefaultIngressClass() (*networkingv1.IngressClass, error) {
165-
var icList networkingv1.IngressClassList
166-
if err := r.List(context.Background(), &icList); err != nil {
167-
r.Log.Error(err, "failed to list ingress classes")
168-
return nil, err
169-
}
170-
for _, ic := range icList.Items {
171-
if IsDefaultIngressClass(&ic) && matchesController(ic.Spec.Controller) {
172-
return &ic, nil
173-
}
174-
}
175-
return nil, ReasonError{
176-
Reason: string(metav1.StatusReasonNotFound),
177-
Message: "default ingress class not found or does not match the controller",
178-
}
179-
}
180-
18157
func (r *ApisixUpstreamReconciler) updateStatus(au *apiv2.ApisixUpstream, err error) {
18258
SetApisixCRDConditionAccepted(&au.Status, au.GetGeneration(), err)
18359
r.Updater.Update(status.Update{

0 commit comments

Comments
 (0)