Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions internal/controller/indexer/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
apiv2 "github.com/apache/apisix-ingress-controller/api/v2"
"github.com/apache/apisix-ingress-controller/internal/adc/translator/annotations"
internaltypes "github.com/apache/apisix-ingress-controller/internal/types"
k8sutils "github.com/apache/apisix-ingress-controller/internal/utils"
"github.com/apache/apisix-ingress-controller/pkg/utils"
)

Expand Down Expand Up @@ -859,11 +860,8 @@ func IngressClassParametersRefIndexFunc(rawObj client.Object) []string {
ingressClass.Spec.Parameters.APIGroup != nil &&
*ingressClass.Spec.Parameters.APIGroup == v1alpha1.GroupVersion.Group &&
ingressClass.Spec.Parameters.Kind == internaltypes.KindGatewayProxy {
ns := ingressClass.GetNamespace()
if ingressClass.Spec.Parameters.Namespace != nil {
ns = *ingressClass.Spec.Parameters.Namespace
}
return []string{GenIndexKey(ns, ingressClass.Spec.Parameters.Name)}
namespace := k8sutils.GetIngressClassParametersNamespace(*ingressClass)
return []string{GenIndexKey(namespace, ingressClass.Spec.Parameters.Name)}
}
return nil
}
Expand Down
5 changes: 1 addition & 4 deletions internal/controller/ingressclass_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,7 @@ func (r *IngressClassReconciler) processInfrastructure(tctx *provider.TranslateC
return nil
}

namespace := ingressClass.Namespace
if ingressClass.Spec.Parameters.Namespace != nil {
namespace = *ingressClass.Spec.Parameters.Namespace
}
namespace := utils.GetIngressClassParametersNamespace(*ingressClass)

gatewayProxy := new(v1alpha1.GatewayProxy)
if err := r.Get(context.Background(), client.ObjectKey{
Expand Down
10 changes: 2 additions & 8 deletions internal/controller/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -1354,10 +1354,7 @@ func ProcessIngressClassParameters(tctx *provider.TranslateContext, c client.Cli
parameters := ingressClass.Spec.Parameters
// check if the parameters reference GatewayProxy
if parameters.APIGroup != nil && *parameters.APIGroup == v1alpha1.GroupVersion.Group && parameters.Kind == KindGatewayProxy {
ns := object.GetNamespace()
if parameters.Namespace != nil {
ns = *parameters.Namespace
}
ns := utils.GetIngressClassParametersNamespace(*ingressClass)

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

namespace := ingressClass.Namespace
if ingressClass.Spec.Parameters.Namespace != nil {
namespace = *ingressClass.Spec.Parameters.Namespace
}
namespace := utils.GetIngressClassParametersNamespace(*ingressClass)

gatewayProxy := new(v1alpha1.GatewayProxy)
if err := r.Get(ctx, client.ObjectKey{
Expand Down
12 changes: 12 additions & 0 deletions internal/utils/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"net"
"regexp"

networkingv1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8stypes "k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -99,3 +100,14 @@ func ConditionStatus(status bool) metav1.ConditionStatus {
}
return metav1.ConditionFalse
}

func GetIngressClassParametersNamespace(ingressClass networkingv1.IngressClass) string {
namespace := "default"
if ingressClass.Spec.Parameters.Namespace != nil {
namespace = *ingressClass.Spec.Parameters.Namespace
}
if annotationNamespace, exists := ingressClass.Annotations["apisix.apache.org/parameters-namespace"]; exists && annotationNamespace != "" {
namespace = annotationNamespace
}
return namespace
}
6 changes: 2 additions & 4 deletions internal/webhook/v1/ingressclass_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
v1alpha1 "github.com/apache/apisix-ingress-controller/api/v1alpha1"
"github.com/apache/apisix-ingress-controller/internal/controller/config"
internaltypes "github.com/apache/apisix-ingress-controller/internal/types"
"github.com/apache/apisix-ingress-controller/internal/utils"
)

// nolint:unused
Expand Down Expand Up @@ -106,10 +107,7 @@ func (v *IngressClassCustomValidator) warnIfMissingGatewayProxyForIngressClass(c
return nil
}

ns := ingressClass.GetNamespace()
if params.Namespace != nil && *params.Namespace != "" {
ns = *params.Namespace
}
ns := utils.GetIngressClassParametersNamespace(*ingressClass)
name := params.Name

var gp v1alpha1.GatewayProxy
Expand Down
91 changes: 89 additions & 2 deletions test/e2e/crds/v2/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,26 @@
package v2

import (
"fmt"
"net/http"
"time"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"k8s.io/apimachinery/pkg/types"

apiv2 "github.com/apache/apisix-ingress-controller/api/v2"
"github.com/apache/apisix-ingress-controller/test/e2e/framework"
"github.com/apache/apisix-ingress-controller/test/e2e/scaffold"
)

var _ = Describe("APISIX Standalone Basic Tests", Label("apisix.apache.org", "v2", "basic"), func() {
s := scaffold.NewDefaultScaffold()
var (
s = scaffold.NewDefaultScaffold()
applier = framework.NewApplier(s.GinkgoT, s.K8sClient, s.CreateResourceFromString)
)

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

})

Context("IngressClass Annotations", func() {
It("Basic tests", func() {
const ingressClassYaml = `
apiVersion: networking.k8s.io/v1
kind: IngressClass
metadata:
name: %s
annotations:
apisix.apache.org/parameters-namespace: %s
spec:
controller: %s
parameters:
apiGroup: apisix.apache.org
kind: GatewayProxy
name: apisix-proxy-config
`

By("create GatewayProxy")

err := s.CreateResourceFromString(s.GetGatewayProxySpec())
Expect(err).NotTo(HaveOccurred(), "creating GatewayProxy")
time.Sleep(5 * time.Second)

By("create IngressClass")
ingressClass := fmt.Sprintf(ingressClassYaml, s.Namespace(), s.Namespace(), s.GetControllerName())
err = s.CreateResourceFromString(ingressClass)
Expect(err).NotTo(HaveOccurred(), "creating IngressClass")
time.Sleep(5 * time.Second)

const apisixRouteSpec = `
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
name: default
spec:
ingressClassName: %s
http:
- name: rule0
match:
hosts:
- httpbin
paths:
- %s
backends:
- serviceName: httpbin-service-e2e-test
servicePort: 80
`
request := func(path string) int {
return s.NewAPISIXClient().GET(path).WithHost("httpbin").Expect().Raw().StatusCode
}

By("apply ApisixRoute")
var apisixRoute apiv2.ApisixRoute
applier.MustApplyAPIv2(types.NamespacedName{Namespace: s.Namespace(), Name: "default"}, &apisixRoute,
fmt.Sprintf(apisixRouteSpec, s.Namespace(), "/get"))

By("verify ApisixRoute works")
Eventually(request).WithArguments("/get").WithTimeout(8 * time.Second).ProbeEvery(time.Second).Should(Equal(http.StatusOK))

By("update ApisixRoute")
applier.MustApplyAPIv2(types.NamespacedName{Namespace: s.Namespace(), Name: "default"}, &apisixRoute,
fmt.Sprintf(apisixRouteSpec, s.Namespace(), "/headers"))
Eventually(request).WithArguments("/get").WithTimeout(8 * time.Second).ProbeEvery(time.Second).Should(Equal(http.StatusNotFound))
s.RequestAssert(&scaffold.RequestAssert{
Method: "GET",
Path: "/headers",
Host: "httpbin",
Check: scaffold.WithExpectedStatus(http.StatusOK),
})

By("delete ApisixRoute")
err = s.DeleteResource("ApisixRoute", "default")
Expect(err).ShouldNot(HaveOccurred(), "deleting ApisixRoute")
Eventually(request).WithArguments("/headers").WithTimeout(8 * time.Second).ProbeEvery(time.Second).Should(Equal(http.StatusNotFound))
})
})
})
Loading