Skip to content

Commit 098fe10

Browse files
AlinsRanronething
authored andcommitted
fix: status should not be recorded when ingressclass does not match (#2519)
Signed-off-by: Ashing Zheng <[email protected]>
1 parent 122e643 commit 098fe10

22 files changed

+118
-123
lines changed

internal/controller/apisixglobalrule_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func (r *ApisixGlobalRuleReconciler) Reconcile(ctx context.Context, req ctrl.Req
9898
// process IngressClass parameters if they reference GatewayProxy
9999
if err := ProcessIngressClassParameters(tctx, r.Client, r.Log, &globalRule, ingressClass); err != nil {
100100
r.Log.Error(err, "failed to process IngressClass parameters", "ingressClass", ingressClass.Name)
101-
return ctrl.Result{}, err
101+
return ctrl.Result{}, client.IgnoreNotFound(err)
102102
}
103103

104104
// Sync the global rule to APISIX

internal/controller/apisixpluginconfig_controller.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,16 @@ func (r *ApisixPluginConfigReconciler) Reconcile(ctx context.Context, req ctrl.R
6868
return ctrl.Result{}, nil
6969
}
7070

71+
tctx := provider.NewDefaultTranslateContext(ctx)
72+
73+
_, err := GetIngressClass(tctx, r.Client, r.Log, pc.Spec.IngressClassName)
74+
if err != nil {
75+
r.Log.V(1).Info("no matching IngressClass available",
76+
"ingressClassName", pc.Spec.IngressClassName,
77+
"error", err.Error())
78+
return ctrl.Result{}, nil
79+
}
80+
7181
// Only update status
7282
r.updateStatus(&pc, nil)
7383
return ctrl.Result{}, nil

internal/controller/apisixtls_controller.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ func (r *ApisixTlsReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
145145
Reason: string(apiv2.ConditionReasonInvalidSpec),
146146
Message: err.Error(),
147147
})
148-
return ctrl.Result{}, err
148+
return ctrl.Result{}, client.IgnoreNotFound(err)
149149
}
150150

151151
// process ApisixTls validation
@@ -159,7 +159,7 @@ func (r *ApisixTlsReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
159159
Reason: string(apiv2.ConditionReasonInvalidSpec),
160160
Message: err.Error(),
161161
})
162-
return ctrl.Result{}, err
162+
return ctrl.Result{}, client.IgnoreNotFound(err)
163163
}
164164

165165
if err := r.Provider.Update(ctx, tctx, &tls); err != 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/ingressclass_controller.go

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

9999
if err := ProcessIngressClassParameters(tctx, r.Client, r.Log, ingressClass, ingressClass); err != nil {
100100
r.Log.Error(err, "failed to process infrastructure for ingressclass", "ingressclass", ingressClass.GetName())
101-
return ctrl.Result{}, err
101+
return ctrl.Result{}, client.IgnoreNotFound(err)
102102
}
103103

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

test/conformance/apisix/suite_test.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,7 @@ func TestMain(m *testing.M) {
125125
f := framework.NewFramework()
126126

127127
// init newDeployer function
128-
scaffold.NewDeployer = func(s *scaffold.Scaffold) scaffold.Deployer {
129-
return scaffold.NewAPISIXDeployer(s)
130-
}
128+
scaffold.NewDeployer = scaffold.NewAPISIXDeployer
131129

132130
// Check and delete specific namespaces if they exist
133131
kubectl := k8s.NewKubectlOptions("", "", "default")
@@ -140,13 +138,16 @@ func TestMain(m *testing.M) {
140138
k8s.CreateNamespace(GinkgoT(), kubectl, namespace)
141139
defer k8s.DeleteNamespace(GinkgoT(), kubectl, namespace)
142140

143-
s := scaffold.NewScaffold(&scaffold.Options{
144-
ControllerName: "apisix.apache.org/apisix-ingress-controller",
141+
adminkey := getEnvOrDefault("APISIX_ADMIN_KEY", "edd1c9f034335f136f87ad84b625c8f1")
142+
controllerName := "apisix.apache.org/apisix-ingress-controller"
143+
s := scaffold.NewScaffold(scaffold.Options{
144+
ControllerName: controllerName,
145145
SkipHooks: true,
146-
APISIXAdminAPIKey: getEnvOrDefault("APISIX_ADMIN_KEY", "edd1c9f034335f136f87ad84b625c8f1"),
146+
APISIXAdminAPIKey: adminkey,
147147
})
148148

149149
s.Deployer.DeployDataplane(scaffold.DeployDataplaneOptions{
150+
AdminKey: adminkey,
150151
Namespace: namespace,
151152
SkipCreateTunnels: true,
152153
ServiceType: "LoadBalancer",
@@ -162,7 +163,7 @@ func TestMain(m *testing.M) {
162163
address := svc.Status.LoadBalancer.Ingress[0].IP
163164

164165
f.DeployIngress(framework.IngressDeployOpts{
165-
ControllerName: s.GetControllerName(),
166+
ControllerName: controllerName,
166167
Namespace: namespace,
167168
StatusAddress: address,
168169
InitSyncDelay: 20 * time.Minute,
@@ -174,7 +175,7 @@ func TestMain(m *testing.M) {
174175

175176
defaultGatewayProxyOpts = GatewayProxyOpts{
176177
StatusAddress: address,
177-
AdminKey: s.AdminKey(),
178+
AdminKey: adminkey,
178179
AdminEndpoint: adminEndpoint,
179180
}
180181

test/e2e/crds/v1alpha1/backendtrafficpolicy.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ spec:
118118
By("create GatewayProxy")
119119
gatewayProxyName := gatewayName
120120
// TODO: change to gateway proxy spec
121-
err = s.CreateResourceFromString(fmt.Sprintf(defaultGatewayProxy, gatewayProxyName, s.Deployer.GetAdminEndpoint(), s.AdminKey()))
121+
err = s.CreateResourceFromString(s.GetGatewayProxySpec())
122+
s.GetGatewayProxySpec()
122123
Expect(err).NotTo(HaveOccurred(), "creating GatewayProxy")
123124
time.Sleep(time.Second)
124125

test/e2e/crds/v2/consumer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ var _ = Describe("Test ApisixConsumer", Label("apisix.apache.org", "v2", "apisix
4141

4242
BeforeEach(func() {
4343
By("create GatewayProxy")
44-
gatewayProxy := s.GetGatewayProxyYaml()
44+
gatewayProxy := s.GetGatewayProxySpec()
4545
err := s.CreateResourceFromString(gatewayProxy)
4646
Expect(err).NotTo(HaveOccurred(), "creating GatewayProxy")
4747
time.Sleep(5 * time.Second)

test/e2e/crds/v2/globalrule.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ spec:
5555
Context("ApisixGlobalRule Basic Operations", func() {
5656
BeforeEach(func() {
5757
By("create GatewayProxy")
58-
gatewayProxy := s.GetGatewayProxyYaml()
58+
gatewayProxy := s.GetGatewayProxySpec()
5959
err := s.CreateResourceFromString(gatewayProxy)
6060
Expect(err).NotTo(HaveOccurred(), "creating GatewayProxy")
6161
time.Sleep(5 * time.Second)

test/e2e/crds/v2/pluginconfig.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ var _ = Describe("Test ApisixPluginConfig", Label("apisix.apache.org", "v2", "ap
5353
Context("Test ApisixPluginConfig", func() {
5454
BeforeEach(func() {
5555
By("create GatewayProxy")
56-
gatewayProxy := s.GetGatewayProxyYaml()
56+
gatewayProxy := s.GetGatewayProxySpec()
5757
err := s.CreateResourceFromString(gatewayProxy)
5858
Expect(err).NotTo(HaveOccurred(), "creating GatewayProxy")
5959
time.Sleep(5 * time.Second)

0 commit comments

Comments
 (0)