Skip to content

Commit b52c26c

Browse files
authored
feat: support custom gatewayproxy namespace for ingressclass (#2701)
Signed-off-by: Ashing Zheng <axingfly@gmail.com>
1 parent d23f7bf commit b52c26c

File tree

6 files changed

+109
-23
lines changed

6 files changed

+109
-23
lines changed

internal/controller/indexer/indexer.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
apiv2 "github.com/apache/apisix-ingress-controller/api/v2"
3535
"github.com/apache/apisix-ingress-controller/internal/adc/translator/annotations"
3636
internaltypes "github.com/apache/apisix-ingress-controller/internal/types"
37+
k8sutils "github.com/apache/apisix-ingress-controller/internal/utils"
3738
"github.com/apache/apisix-ingress-controller/pkg/utils"
3839
)
3940

@@ -859,11 +860,8 @@ func IngressClassParametersRefIndexFunc(rawObj client.Object) []string {
859860
ingressClass.Spec.Parameters.APIGroup != nil &&
860861
*ingressClass.Spec.Parameters.APIGroup == v1alpha1.GroupVersion.Group &&
861862
ingressClass.Spec.Parameters.Kind == internaltypes.KindGatewayProxy {
862-
ns := ingressClass.GetNamespace()
863-
if ingressClass.Spec.Parameters.Namespace != nil {
864-
ns = *ingressClass.Spec.Parameters.Namespace
865-
}
866-
return []string{GenIndexKey(ns, ingressClass.Spec.Parameters.Name)}
863+
namespace := k8sutils.GetIngressClassParametersNamespace(*ingressClass)
864+
return []string{GenIndexKey(namespace, ingressClass.Spec.Parameters.Name)}
867865
}
868866
return nil
869867
}

internal/controller/ingressclass_controller.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,10 +187,7 @@ func (r *IngressClassReconciler) processInfrastructure(tctx *provider.TranslateC
187187
return nil
188188
}
189189

190-
namespace := ingressClass.Namespace
191-
if ingressClass.Spec.Parameters.Namespace != nil {
192-
namespace = *ingressClass.Spec.Parameters.Namespace
193-
}
190+
namespace := utils.GetIngressClassParametersNamespace(*ingressClass)
194191

195192
gatewayProxy := new(v1alpha1.GatewayProxy)
196193
if err := r.Get(context.Background(), client.ObjectKey{

internal/controller/utils.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1354,10 +1354,7 @@ func ProcessIngressClassParameters(tctx *provider.TranslateContext, c client.Cli
13541354
parameters := ingressClass.Spec.Parameters
13551355
// check if the parameters reference GatewayProxy
13561356
if parameters.APIGroup != nil && *parameters.APIGroup == v1alpha1.GroupVersion.Group && parameters.Kind == KindGatewayProxy {
1357-
ns := object.GetNamespace()
1358-
if parameters.Namespace != nil {
1359-
ns = *parameters.Namespace
1360-
}
1357+
ns := utils.GetIngressClassParametersNamespace(*ingressClass)
13611358

13621359
gatewayProxy := &v1alpha1.GatewayProxy{}
13631360
if err := c.Get(tctx, client.ObjectKey{
@@ -1553,10 +1550,7 @@ func GetGatewayProxyByIngressClass(ctx context.Context, r client.Client, ingress
15531550
return nil, nil
15541551
}
15551552

1556-
namespace := ingressClass.Namespace
1557-
if ingressClass.Spec.Parameters.Namespace != nil {
1558-
namespace = *ingressClass.Spec.Parameters.Namespace
1559-
}
1553+
namespace := utils.GetIngressClassParametersNamespace(*ingressClass)
15601554

15611555
gatewayProxy := new(v1alpha1.GatewayProxy)
15621556
if err := r.Get(ctx, client.ObjectKey{

internal/utils/k8s.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"net"
2222
"regexp"
2323

24+
networkingv1 "k8s.io/api/networking/v1"
2425
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2526
k8stypes "k8s.io/apimachinery/pkg/types"
2627
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -99,3 +100,14 @@ func ConditionStatus(status bool) metav1.ConditionStatus {
99100
}
100101
return metav1.ConditionFalse
101102
}
103+
104+
func GetIngressClassParametersNamespace(ingressClass networkingv1.IngressClass) string {
105+
namespace := "default"
106+
if ingressClass.Spec.Parameters.Namespace != nil {
107+
namespace = *ingressClass.Spec.Parameters.Namespace
108+
}
109+
if annotationNamespace, exists := ingressClass.Annotations["apisix.apache.org/parameters-namespace"]; exists && annotationNamespace != "" {
110+
namespace = annotationNamespace
111+
}
112+
return namespace
113+
}

internal/webhook/v1/ingressclass_webhook.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
v1alpha1 "github.com/apache/apisix-ingress-controller/api/v1alpha1"
3232
"github.com/apache/apisix-ingress-controller/internal/controller/config"
3333
internaltypes "github.com/apache/apisix-ingress-controller/internal/types"
34+
"github.com/apache/apisix-ingress-controller/internal/utils"
3435
)
3536

3637
// nolint:unused
@@ -106,10 +107,7 @@ func (v *IngressClassCustomValidator) warnIfMissingGatewayProxyForIngressClass(c
106107
return nil
107108
}
108109

109-
ns := ingressClass.GetNamespace()
110-
if params.Namespace != nil && *params.Namespace != "" {
111-
ns = *params.Namespace
112-
}
110+
ns := utils.GetIngressClassParametersNamespace(*ingressClass)
113111
name := params.Name
114112

115113
var gp v1alpha1.GatewayProxy

test/e2e/crds/v2/basic.go

Lines changed: 89 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,26 @@
1818
package v2
1919

2020
import (
21+
"fmt"
22+
"net/http"
23+
"time"
24+
2125
. "github.com/onsi/ginkgo/v2"
2226
. "github.com/onsi/gomega"
27+
"k8s.io/apimachinery/pkg/types"
2328

29+
apiv2 "github.com/apache/apisix-ingress-controller/api/v2"
30+
"github.com/apache/apisix-ingress-controller/test/e2e/framework"
2431
"github.com/apache/apisix-ingress-controller/test/e2e/scaffold"
2532
)
2633

2734
var _ = Describe("APISIX Standalone Basic Tests", Label("apisix.apache.org", "v2", "basic"), func() {
28-
s := scaffold.NewDefaultScaffold()
35+
var (
36+
s = scaffold.NewDefaultScaffold()
37+
applier = framework.NewApplier(s.GinkgoT, s.K8sClient, s.CreateResourceFromString)
38+
)
2939

30-
Describe("APISIX HTTP Proxy", func() {
40+
Context("APISIX HTTP Proxy", func() {
3141
It("should handle basic HTTP requests", func() {
3242
httpClient := s.NewAPISIXClient()
3343
Expect(httpClient).NotTo(BeNil())
@@ -52,4 +62,81 @@ var _ = Describe("APISIX Standalone Basic Tests", Label("apisix.apache.org", "v2
5262
})
5363

5464
})
65+
66+
Context("IngressClass Annotations", func() {
67+
It("Basic tests", func() {
68+
const ingressClassYaml = `
69+
apiVersion: networking.k8s.io/v1
70+
kind: IngressClass
71+
metadata:
72+
name: %s
73+
annotations:
74+
apisix.apache.org/parameters-namespace: %s
75+
spec:
76+
controller: %s
77+
parameters:
78+
apiGroup: apisix.apache.org
79+
kind: GatewayProxy
80+
name: apisix-proxy-config
81+
`
82+
83+
By("create GatewayProxy")
84+
85+
err := s.CreateResourceFromString(s.GetGatewayProxySpec())
86+
Expect(err).NotTo(HaveOccurred(), "creating GatewayProxy")
87+
time.Sleep(5 * time.Second)
88+
89+
By("create IngressClass")
90+
ingressClass := fmt.Sprintf(ingressClassYaml, s.Namespace(), s.Namespace(), s.GetControllerName())
91+
err = s.CreateResourceFromString(ingressClass)
92+
Expect(err).NotTo(HaveOccurred(), "creating IngressClass")
93+
time.Sleep(5 * time.Second)
94+
95+
const apisixRouteSpec = `
96+
apiVersion: apisix.apache.org/v2
97+
kind: ApisixRoute
98+
metadata:
99+
name: default
100+
spec:
101+
ingressClassName: %s
102+
http:
103+
- name: rule0
104+
match:
105+
hosts:
106+
- httpbin
107+
paths:
108+
- %s
109+
backends:
110+
- serviceName: httpbin-service-e2e-test
111+
servicePort: 80
112+
`
113+
request := func(path string) int {
114+
return s.NewAPISIXClient().GET(path).WithHost("httpbin").Expect().Raw().StatusCode
115+
}
116+
117+
By("apply ApisixRoute")
118+
var apisixRoute apiv2.ApisixRoute
119+
applier.MustApplyAPIv2(types.NamespacedName{Namespace: s.Namespace(), Name: "default"}, &apisixRoute,
120+
fmt.Sprintf(apisixRouteSpec, s.Namespace(), "/get"))
121+
122+
By("verify ApisixRoute works")
123+
Eventually(request).WithArguments("/get").WithTimeout(8 * time.Second).ProbeEvery(time.Second).Should(Equal(http.StatusOK))
124+
125+
By("update ApisixRoute")
126+
applier.MustApplyAPIv2(types.NamespacedName{Namespace: s.Namespace(), Name: "default"}, &apisixRoute,
127+
fmt.Sprintf(apisixRouteSpec, s.Namespace(), "/headers"))
128+
Eventually(request).WithArguments("/get").WithTimeout(8 * time.Second).ProbeEvery(time.Second).Should(Equal(http.StatusNotFound))
129+
s.RequestAssert(&scaffold.RequestAssert{
130+
Method: "GET",
131+
Path: "/headers",
132+
Host: "httpbin",
133+
Check: scaffold.WithExpectedStatus(http.StatusOK),
134+
})
135+
136+
By("delete ApisixRoute")
137+
err = s.DeleteResource("ApisixRoute", "default")
138+
Expect(err).ShouldNot(HaveOccurred(), "deleting ApisixRoute")
139+
Eventually(request).WithArguments("/headers").WithTimeout(8 * time.Second).ProbeEvery(time.Second).Should(Equal(http.StatusNotFound))
140+
})
141+
})
55142
})

0 commit comments

Comments
 (0)