Skip to content

Commit 227062d

Browse files
authored
fix: status should not be recorded when ingressclass does not match (#2519)
1 parent 5775f23 commit 227062d

34 files changed

+547
-356
lines changed

internal/controller/apisixconsumer_controller.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -83,23 +83,22 @@ func (r *ApisixConsumerReconciler) Reconcile(ctx context.Context, req ctrl.Reque
8383
ingressClass *networkingv1.IngressClass
8484
err error
8585
)
86-
defer func() {
87-
r.updateStatus(ac, err)
88-
}()
89-
90-
ingressClass, err = GetIngressClass(tctx, r.Client, r.Log, ac.Spec.IngressClassName)
91-
if err != nil {
92-
r.Log.Error(err, "failed to get IngressClass")
93-
return ctrl.Result{}, err
86+
if ingressClass, err = GetIngressClass(tctx, r.Client, r.Log, ac.Spec.IngressClassName); err != nil {
87+
r.Log.V(1).Info("no matching IngressClass available",
88+
"ingressClassName", ac.Spec.IngressClassName,
89+
"error", err.Error())
90+
return ctrl.Result{}, nil
9491
}
92+
defer func() { r.updateStatus(ac, err) }()
9593

9694
if err = ProcessIngressClassParameters(tctx, r.Client, r.Log, ac, ingressClass); err != nil {
9795
r.Log.Error(err, "failed to process IngressClass parameters", "ingressClass", ingressClass.Name)
98-
return ctrl.Result{}, err
96+
return ctrl.Result{}, client.IgnoreNotFound(err)
9997
}
10098

10199
if err = r.processSpec(ctx, tctx, ac); err != nil {
102-
return ctrl.Result{}, err
100+
r.Log.Error(err, "failed to process ApisixConsumer spec", "object", ac)
101+
return ctrl.Result{}, client.IgnoreNotFound(err)
103102
}
104103

105104
if err = r.Provider.Update(ctx, tctx, ac); err != nil {

internal/controller/apisixglobalrule_controller.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,22 +78,24 @@ func (r *ApisixGlobalRuleReconciler) Reconcile(ctx context.Context, req ctrl.Req
7878
return ctrl.Result{}, err
7979
}
8080

81-
r.Log.Info("reconciling global rule", "globalrule", globalRule.Name)
81+
r.Log.V(1).Info("reconciling ApisixGlobalRule", "object", globalRule)
8282

8383
// create a translate context
8484
tctx := provider.NewDefaultTranslateContext(ctx)
8585

8686
// get the ingress class
8787
ingressClass, err := GetIngressClass(tctx, r.Client, r.Log, globalRule.Spec.IngressClassName)
8888
if err != nil {
89-
r.Log.Error(err, "failed to get IngressClass")
90-
return ctrl.Result{}, err
89+
r.Log.V(1).Info("no matching IngressClass available",
90+
"ingressClassName", globalRule.Spec.IngressClassName,
91+
"error", err.Error())
92+
return ctrl.Result{}, nil
9193
}
9294

9395
// process IngressClass parameters if they reference GatewayProxy
9496
if err := ProcessIngressClassParameters(tctx, r.Client, r.Log, &globalRule, ingressClass); err != nil {
9597
r.Log.Error(err, "failed to process IngressClass parameters", "ingressClass", ingressClass.Name)
96-
return ctrl.Result{}, err
98+
return ctrl.Result{}, client.IgnoreNotFound(err)
9799
}
98100

99101
// Sync the global rule to APISIX

internal/controller/apisixpluginconfig_controller.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929

3030
apiv2 "github.com/apache/apisix-ingress-controller/api/v2"
3131
"github.com/apache/apisix-ingress-controller/internal/controller/status"
32+
"github.com/apache/apisix-ingress-controller/internal/provider"
3233
"github.com/apache/apisix-ingress-controller/internal/utils"
3334
)
3435

@@ -55,6 +56,16 @@ func (r *ApisixPluginConfigReconciler) Reconcile(ctx context.Context, req ctrl.R
5556
return ctrl.Result{}, client.IgnoreNotFound(err)
5657
}
5758

59+
tctx := provider.NewDefaultTranslateContext(ctx)
60+
61+
_, err := GetIngressClass(tctx, r.Client, r.Log, pc.Spec.IngressClassName)
62+
if err != nil {
63+
r.Log.V(1).Info("no matching IngressClass available",
64+
"ingressClassName", pc.Spec.IngressClassName,
65+
"error", err.Error())
66+
return ctrl.Result{}, nil
67+
}
68+
5869
// Only update status
5970
r.updateStatus(&pc, nil)
6071
return ctrl.Result{}, nil

internal/controller/apisixroute_controller.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,15 +124,18 @@ func (r *ApisixRouteReconciler) Reconcile(ctx context.Context, req ctrl.Request)
124124
ic *networkingv1.IngressClass
125125
err error
126126
)
127-
defer func() {
128-
r.updateStatus(&ar, err)
129-
}()
130127

131128
if ic, err = GetIngressClass(tctx, r.Client, r.Log, ar.Spec.IngressClassName); err != nil {
132-
return ctrl.Result{}, err
129+
r.Log.V(1).Info("no matching IngressClass available",
130+
"ingressClassName", ar.Spec.IngressClassName,
131+
"error", err.Error())
132+
return ctrl.Result{}, nil
133133
}
134+
defer func() { r.updateStatus(&ar, err) }()
135+
134136
if err = ProcessIngressClassParameters(tctx, r.Client, r.Log, &ar, ic); err != nil {
135-
return ctrl.Result{}, err
137+
r.Log.Error(err, "failed to process IngressClass parameters")
138+
return ctrl.Result{}, client.IgnoreNotFound(err)
136139
}
137140
if err = r.processApisixRoute(ctx, tctx, &ar); err != nil {
138141
return ctrl.Result{}, err

internal/controller/apisixtls_controller.go

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -117,16 +117,10 @@ func (r *ApisixTlsReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
117117
// get the ingress class
118118
ingressClass, err := GetIngressClass(tctx, r.Client, r.Log, tls.Spec.IngressClassName)
119119
if err != nil {
120-
r.Log.Error(err, "failed to get IngressClass")
121-
r.updateStatus(&tls, metav1.Condition{
122-
Type: string(apiv2.ConditionTypeAccepted),
123-
Status: metav1.ConditionFalse,
124-
ObservedGeneration: tls.Generation,
125-
LastTransitionTime: metav1.Now(),
126-
Reason: string(apiv2.ConditionReasonInvalidSpec),
127-
Message: err.Error(),
128-
})
129-
return ctrl.Result{}, err
120+
r.Log.V(1).Info("no matching IngressClass available, skip processing",
121+
"ingressClassName", tls.Spec.IngressClassName,
122+
"error", err.Error())
123+
return ctrl.Result{}, nil
130124
}
131125

132126
// process IngressClass parameters if they reference GatewayProxy
@@ -140,7 +134,7 @@ func (r *ApisixTlsReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
140134
Reason: string(apiv2.ConditionReasonInvalidSpec),
141135
Message: err.Error(),
142136
})
143-
return ctrl.Result{}, err
137+
return ctrl.Result{}, client.IgnoreNotFound(err)
144138
}
145139

146140
// process ApisixTls validation
@@ -154,7 +148,7 @@ func (r *ApisixTlsReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
154148
Reason: string(apiv2.ConditionReasonInvalidSpec),
155149
Message: err.Error(),
156150
})
157-
return ctrl.Result{}, err
151+
return ctrl.Result{}, client.IgnoreNotFound(err)
158152
}
159153

160154
if err := r.Provider.Update(ctx, tctx, &tls); err != nil {

internal/controller/apisixupstream_controller.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828

2929
apiv2 "github.com/apache/apisix-ingress-controller/api/v2"
3030
"github.com/apache/apisix-ingress-controller/internal/controller/status"
31+
"github.com/apache/apisix-ingress-controller/internal/provider"
3132
"github.com/apache/apisix-ingress-controller/internal/utils"
3233
)
3334

@@ -54,6 +55,16 @@ func (r *ApisixUpstreamReconciler) Reconcile(ctx context.Context, req ctrl.Reque
5455
return ctrl.Result{}, client.IgnoreNotFound(err)
5556
}
5657

58+
tctx := provider.NewDefaultTranslateContext(ctx)
59+
60+
_, err := GetIngressClass(tctx, r.Client, r.Log, au.Spec.IngressClassName)
61+
if err != nil {
62+
r.Log.V(1).Info("no matching IngressClass available, skip processing",
63+
"ingressClassName", au.Spec.IngressClassName,
64+
"error", err.Error())
65+
return ctrl.Result{}, nil
66+
}
67+
5768
// Only update status
5869
r.updateStatus(&au, nil)
5970
return ctrl.Result{}, nil

internal/controller/consumer_controller.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package controller
1919

2020
import (
2121
"context"
22+
"fmt"
2223

2324
"github.com/go-logr/logr"
2425
corev1 "k8s.io/api/core/v1"
@@ -36,6 +37,7 @@ import (
3637
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
3738

3839
"github.com/apache/apisix-ingress-controller/api/v1alpha1"
40+
"github.com/apache/apisix-ingress-controller/internal/controller/config"
3941
"github.com/apache/apisix-ingress-controller/internal/controller/indexer"
4042
"github.com/apache/apisix-ingress-controller/internal/controller/status"
4143
"github.com/apache/apisix-ingress-controller/internal/manager/readiness"
@@ -208,8 +210,10 @@ func (r *ConsumerReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
208210

209211
gateway, err := r.getGateway(ctx, consumer)
210212
if err != nil {
211-
r.Log.Error(err, "failed to get gateway", "consumer", consumer)
212-
statusErr = err
213+
r.Log.V(1).Info("no matching Gateway available",
214+
"gatewayRef", consumer.Spec.GatewayRef,
215+
"error", err.Error())
216+
return ctrl.Result{}, nil
213217
}
214218

215219
rk := utils.NamespacedNameKind(consumer)
@@ -295,8 +299,17 @@ func (r *ConsumerReconciler) getGateway(ctx context.Context, consumer *v1alpha1.
295299
Name: consumer.Spec.GatewayRef.Name,
296300
Namespace: ns,
297301
}, gateway); err != nil {
298-
r.Log.Error(err, "failed to get gateway", "gateway", consumer.Spec.GatewayRef.Name)
299-
return nil, err
302+
return nil, fmt.Errorf("failed to get gateway %s/%s: %w", ns, consumer.Spec.GatewayRef.Name, err)
303+
}
304+
gatewayClass := gatewayv1.GatewayClass{}
305+
if err := r.Get(ctx, client.ObjectKey{
306+
Name: string(gateway.Spec.GatewayClassName),
307+
}, &gatewayClass); err != nil {
308+
return nil, fmt.Errorf("failed to retrieve gatewayclass for gateway: %w", err)
309+
}
310+
311+
if string(gatewayClass.Spec.ControllerName) != config.ControllerConfig.ControllerName {
312+
return nil, fmt.Errorf("gateway %s/%s is not managed by this controller", gateway.Namespace, gateway.Name)
300313
}
301314
return gateway, nil
302315
}

internal/controller/gatewayproxy_controller.go

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
3636

3737
"github.com/apache/apisix-ingress-controller/api/v1alpha1"
38+
"github.com/apache/apisix-ingress-controller/internal/controller/config"
3839
"github.com/apache/apisix-ingress-controller/internal/controller/indexer"
3940
"github.com/apache/apisix-ingress-controller/internal/provider"
4041
"github.com/apache/apisix-ingress-controller/internal/utils"
@@ -67,6 +68,12 @@ func (r *GatewayProxyController) SetupWithManager(mrg ctrl.Manager) error {
6768
Watches(&corev1.Secret{},
6869
handler.EnqueueRequestsFromMapFunc(r.listGatewayProxiesForSecret),
6970
).
71+
Watches(&gatewayv1.Gateway{},
72+
handler.EnqueueRequestsFromMapFunc(r.listGatewayProxiesByGateway),
73+
).
74+
Watches(&networkingv1.IngressClass{},
75+
handler.EnqueueRequestsFromMapFunc(r.listGatewayProxiesForIngressClass),
76+
).
7077
Complete(r)
7178
}
7279

@@ -129,6 +136,16 @@ func (r *GatewayProxyController) Reconcile(ctx context.Context, req ctrl.Request
129136
return ctrl.Result{}, nil
130137
}
131138

139+
var gatewayclassList gatewayv1.GatewayClassList
140+
if err := r.List(ctx, &gatewayclassList, client.MatchingFields{indexer.ControllerName: config.GetControllerName()}); err != nil {
141+
r.Log.Error(err, "failed to list GatewayClassList")
142+
return ctrl.Result{}, nil
143+
}
144+
gcMatched := make(map[string]*gatewayv1.GatewayClass)
145+
for _, item := range gatewayclassList.Items {
146+
gcMatched[item.Name] = &item
147+
}
148+
132149
// list IngressClasses that reference the GatewayProxy
133150
if err := r.List(ctx, &ingressClassList, client.MatchingFields{indexer.IngressClassParametersRef: indexKey}); err != nil {
134151
r.Log.Error(err, "failed to list IngressClassList")
@@ -137,12 +154,27 @@ func (r *GatewayProxyController) Reconcile(ctx context.Context, req ctrl.Request
137154

138155
// append referrers to translate context
139156
for _, item := range gatewayList.Items {
140-
tctx.GatewayProxyReferrers[req.NamespacedName] = append(tctx.GatewayProxyReferrers[req.NamespacedName], utils.NamespacedNameKind(&item))
157+
gcName := string(item.Spec.GatewayClassName)
158+
if gcName == "" {
159+
continue
160+
}
161+
if _, ok := gcMatched[gcName]; ok {
162+
tctx.GatewayProxyReferrers[req.NamespacedName] = append(tctx.GatewayProxyReferrers[req.NamespacedName], utils.NamespacedNameKind(&item))
163+
}
141164
}
142165
for _, item := range ingressClassList.Items {
166+
if item.Spec.Controller != config.GetControllerName() {
167+
continue
168+
}
143169
tctx.GatewayProxyReferrers[req.NamespacedName] = append(tctx.GatewayProxyReferrers[req.NamespacedName], utils.NamespacedNameKind(&item))
144170
}
171+
r.Log.V(1).Info("found Gateways for GatewayProxy", "gatewayproxy", req.String(), "gateways", len(gatewayList.Items), "gatewayclasses", len(gatewayclassList.Items), "ingressclasses", len(ingressClassList.Items))
172+
173+
if len(tctx.GatewayProxyReferrers[req.NamespacedName]) == 0 {
174+
return ctrl.Result{}, nil
175+
}
145176

177+
r.Log.V(1).Info("references found for GatewayProxy", "gatewayproxy", req.String(), "references", tctx.GatewayProxyReferrers[req.NamespacedName])
146178
if err := r.Provider.Update(ctx, tctx, &gp); err != nil {
147179
return reconcile.Result{}, err
148180
}
@@ -184,3 +216,35 @@ func (r *GatewayProxyController) listGatewayProxiesForSecret(ctx context.Context
184216
indexer.SecretIndexRef: indexer.GenIndexKey(secret.GetNamespace(), secret.GetName()),
185217
})
186218
}
219+
220+
func (r *GatewayProxyController) listGatewayProxiesForIngressClass(ctx context.Context, object client.Object) []reconcile.Request {
221+
ingressClass, ok := object.(*networkingv1.IngressClass)
222+
if !ok {
223+
r.Log.Error(errors.New("unexpected object type"), "failed to convert object to IngressClass")
224+
return nil
225+
}
226+
reqs := []reconcile.Request{}
227+
gp, _ := GetGatewayProxyByIngressClass(ctx, r.Client, ingressClass)
228+
if gp != nil {
229+
reqs = append(reqs, reconcile.Request{
230+
NamespacedName: utils.NamespacedName(gp),
231+
})
232+
}
233+
return reqs
234+
}
235+
236+
func (r *GatewayProxyController) listGatewayProxiesByGateway(ctx context.Context, object client.Object) []reconcile.Request {
237+
gateway, ok := object.(*gatewayv1.Gateway)
238+
if !ok {
239+
r.Log.Error(errors.New("unexpected object type"), "failed to convert object to IngressClass")
240+
return nil
241+
}
242+
reqs := []reconcile.Request{}
243+
gp, _ := GetGatewayProxyByGateway(ctx, r.Client, gateway)
244+
if gp != nil {
245+
reqs = append(reqs, reconcile.Request{
246+
NamespacedName: utils.NamespacedName(gp),
247+
})
248+
}
249+
return reqs
250+
}

internal/controller/ingressclass_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func (r *IngressClassReconciler) Reconcile(ctx context.Context, req ctrl.Request
100100

101101
if err := r.processInfrastructure(tctx, ingressClass); err != nil {
102102
r.Log.Error(err, "failed to process infrastructure for ingressclass", "ingressclass", ingressClass.GetName())
103-
return ctrl.Result{}, err
103+
return ctrl.Result{}, client.IgnoreNotFound(err)
104104
}
105105

106106
if err := r.Provider.Update(ctx, tctx, ingressClass); err != nil {

internal/controller/utils.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1483,3 +1483,53 @@ func MatchConsumerGatewayRef(ctx context.Context, c client.Client, log logr.Logg
14831483
}
14841484
return matchesController(string(gatewayClass.Spec.ControllerName))
14851485
}
1486+
1487+
func GetGatewayProxyByIngressClass(ctx context.Context, r client.Client, ingressClass *networkingv1.IngressClass) (*v1alpha1.GatewayProxy, error) {
1488+
if ingressClass.Spec.Parameters == nil {
1489+
return nil, nil
1490+
}
1491+
1492+
if ingressClass.Spec.Parameters.APIGroup == nil ||
1493+
*ingressClass.Spec.Parameters.APIGroup != v1alpha1.GroupVersion.Group ||
1494+
ingressClass.Spec.Parameters.Kind != KindGatewayProxy {
1495+
return nil, nil
1496+
}
1497+
1498+
namespace := ingressClass.Namespace
1499+
if ingressClass.Spec.Parameters.Namespace != nil {
1500+
namespace = *ingressClass.Spec.Parameters.Namespace
1501+
}
1502+
1503+
gatewayProxy := new(v1alpha1.GatewayProxy)
1504+
if err := r.Get(ctx, client.ObjectKey{
1505+
Namespace: namespace,
1506+
Name: ingressClass.Spec.Parameters.Name,
1507+
}, gatewayProxy); err != nil {
1508+
return nil, fmt.Errorf("failed to get gateway proxy: %w", err)
1509+
}
1510+
return gatewayProxy, nil
1511+
}
1512+
1513+
func GetGatewayProxyByGateway(ctx context.Context, r client.Client, gateway *gatewayv1.Gateway) (*v1alpha1.GatewayProxy, error) {
1514+
if gateway == nil {
1515+
return nil, nil
1516+
}
1517+
infra := gateway.Spec.Infrastructure
1518+
if infra == nil || infra.ParametersRef == nil {
1519+
return nil, nil
1520+
}
1521+
1522+
ns := gateway.GetNamespace()
1523+
paramRef := infra.ParametersRef
1524+
if string(paramRef.Group) != v1alpha1.GroupVersion.Group || string(paramRef.Kind) != KindGatewayProxy {
1525+
return nil, nil
1526+
}
1527+
gatewayProxy := &v1alpha1.GatewayProxy{}
1528+
if err := r.Get(context.Background(), client.ObjectKey{
1529+
Namespace: ns,
1530+
Name: paramRef.Name,
1531+
}, gatewayProxy); err != nil {
1532+
return nil, fmt.Errorf("failed to get GatewayProxy: %w", err)
1533+
}
1534+
return gatewayProxy, nil
1535+
}

0 commit comments

Comments
 (0)