|
| 1 | +package test_gatewayapi |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/loft-sh/vcluster/e2e-next/constants" |
| 7 | + "github.com/loft-sh/vcluster/e2e-next/labels" |
| 8 | + "github.com/loft-sh/vcluster/pkg/util/random" |
| 9 | + "github.com/loft-sh/vcluster/pkg/util/translate" |
| 10 | + . "github.com/onsi/ginkgo/v2" |
| 11 | + . "github.com/onsi/gomega" |
| 12 | + corev1 "k8s.io/api/core/v1" |
| 13 | + kerrors "k8s.io/apimachinery/pkg/api/errors" |
| 14 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 15 | + "k8s.io/apimachinery/pkg/types" |
| 16 | + ctrlclient "sigs.k8s.io/controller-runtime/pkg/client" |
| 17 | + gatewayv1 "sigs.k8s.io/gateway-api/apis/v1" |
| 18 | + gatewayv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1" |
| 19 | +) |
| 20 | + |
| 21 | +const grantsDisabledGatewayClassSelectorValue = "gatewayapi-grants-disabled-vcluster" |
| 22 | + |
| 23 | +// GatewayAPIGrantsDisabledSpec registers route sync tests for a vCluster with |
| 24 | +// sync.toHost.gatewayApi.referenceGrants.enabled set to "false" while route |
| 25 | +// sync stays on. Disabling grant sync must not break the route controllers and |
| 26 | +// must keep virtual ReferenceGrants authoritative for cross-namespace refs. |
| 27 | +func GatewayAPIGrantsDisabledSpec() { |
| 28 | + Describe("Gateway API toHost with ReferenceGrant sync disabled", labels.GatewayAPI, func() { |
| 29 | + var ( |
| 30 | + hostClient ctrlclient.Client |
| 31 | + vClusterClient ctrlclient.Client |
| 32 | + vClusterName string |
| 33 | + vClusterHostNS string |
| 34 | + ) |
| 35 | + |
| 36 | + BeforeEach(func(ctx context.Context) { |
| 37 | + clients := newGatewayAPIClients(ctx) |
| 38 | + hostClient = clients.HostClient |
| 39 | + vClusterClient = clients.VClusterClient |
| 40 | + vClusterName = clients.VClusterName |
| 41 | + vClusterHostNS = clients.VClusterHostNS |
| 42 | + |
| 43 | + Eventually(func(g Gomega) { |
| 44 | + g.Expect(vClusterClient.List(ctx, &gatewayv1.HTTPRouteList{}, ctrlclient.InNamespace("default"))).To(Succeed()) |
| 45 | + }).WithPolling(constants.PollingInterval).WithTimeout(constants.PollingTimeout).Should(Succeed()) |
| 46 | + }) |
| 47 | + |
| 48 | + It("syncs HTTPRoutes to the host while ReferenceGrant sync is disabled", func(ctx context.Context) { |
| 49 | + suffix := random.String(6) |
| 50 | + class := createGatewayClass(ctx, hostClient, "gc-rgd-"+suffix, grantsDisabledGatewayClassSelectorValue, "grants disabled class") |
| 51 | + Eventually(func(g Gomega) { |
| 52 | + g.Expect(vClusterClient.Get(ctx, types.NamespacedName{Name: class.Name}, &gatewayv1.GatewayClass{})).To(Succeed()) |
| 53 | + }).WithPolling(constants.PollingInterval).WithTimeout(constants.PollingTimeout).Should(Succeed()) |
| 54 | + |
| 55 | + ns := createTenantNamespace(ctx, vClusterClient, "rgd-app-"+suffix) |
| 56 | + gw := tenantGateway(ns.Name, "gw-rgd-"+suffix, class.Name) |
| 57 | + Expect(vClusterClient.Create(ctx, gw)).To(Succeed()) |
| 58 | + DeferCleanup(func(ctx context.Context) { |
| 59 | + Expect(ctrlclient.IgnoreNotFound(vClusterClient.Delete(ctx, gw))).To(Succeed()) |
| 60 | + }) |
| 61 | + hostGatewayName := translate.SafeConcatName(gw.Name, "x", ns.Name, "x", vClusterName) |
| 62 | + Eventually(func(g Gomega) { |
| 63 | + g.Expect(hostClient.Get(ctx, types.NamespacedName{Namespace: vClusterHostNS, Name: hostGatewayName}, &gatewayv1.Gateway{})).To(Succeed()) |
| 64 | + }).WithPolling(constants.PollingInterval).WithTimeout(constants.PollingTimeout).Should(Succeed()) |
| 65 | + |
| 66 | + svc := &corev1.Service{ObjectMeta: metav1.ObjectMeta{Name: "rgd-backend-" + suffix, Namespace: ns.Name}, Spec: corev1.ServiceSpec{Ports: []corev1.ServicePort{{Port: 80}}}} |
| 67 | + Expect(vClusterClient.Create(ctx, svc)).To(Succeed()) |
| 68 | + |
| 69 | + route := tenantHTTPRoute(ns.Name, "route-rgd-"+suffix, gw.Name, svc.Name) |
| 70 | + Expect(vClusterClient.Create(ctx, route)).To(Succeed()) |
| 71 | + DeferCleanup(func(ctx context.Context) { |
| 72 | + Expect(ctrlclient.IgnoreNotFound(vClusterClient.Delete(ctx, route))).To(Succeed()) |
| 73 | + }) |
| 74 | + |
| 75 | + hostRouteName := translate.SafeConcatName(route.Name, "x", ns.Name, "x", vClusterName) |
| 76 | + Eventually(func(g Gomega) { |
| 77 | + g.Expect(hostClient.Get(ctx, types.NamespacedName{Namespace: vClusterHostNS, Name: hostRouteName}, &gatewayv1.HTTPRoute{})).To(Succeed()) |
| 78 | + }).WithPolling(constants.PollingInterval).WithTimeout(constants.PollingTimeout).Should(Succeed()) |
| 79 | + }) |
| 80 | + |
| 81 | + It("authorizes cross-namespace backendRefs via virtual ReferenceGrants without syncing grants to the host", func(ctx context.Context) { |
| 82 | + suffix := random.String(6) |
| 83 | + class := createGatewayClass(ctx, hostClient, "gc-rgd-xns-"+suffix, grantsDisabledGatewayClassSelectorValue, "grants disabled cross-namespace class") |
| 84 | + Eventually(func(g Gomega) { |
| 85 | + g.Expect(vClusterClient.Get(ctx, types.NamespacedName{Name: class.Name}, &gatewayv1.GatewayClass{})).To(Succeed()) |
| 86 | + }).WithPolling(constants.PollingInterval).WithTimeout(constants.PollingTimeout).Should(Succeed()) |
| 87 | + |
| 88 | + frontend := createTenantNamespace(ctx, vClusterClient, "rgd-frontend-"+suffix) |
| 89 | + backend := createTenantNamespace(ctx, vClusterClient, "rgd-backend-"+suffix) |
| 90 | + gw := tenantGateway(frontend.Name, "gw-rgd-xns-"+suffix, class.Name) |
| 91 | + Expect(vClusterClient.Create(ctx, gw)).To(Succeed()) |
| 92 | + DeferCleanup(func(ctx context.Context) { |
| 93 | + Expect(ctrlclient.IgnoreNotFound(vClusterClient.Delete(ctx, gw))).To(Succeed()) |
| 94 | + }) |
| 95 | + hostGatewayName := translate.SafeConcatName(gw.Name, "x", frontend.Name, "x", vClusterName) |
| 96 | + Eventually(func(g Gomega) { |
| 97 | + g.Expect(hostClient.Get(ctx, types.NamespacedName{Namespace: vClusterHostNS, Name: hostGatewayName}, &gatewayv1.Gateway{})).To(Succeed()) |
| 98 | + }).WithPolling(constants.PollingInterval).WithTimeout(constants.PollingTimeout).Should(Succeed()) |
| 99 | + |
| 100 | + svc := &corev1.Service{ObjectMeta: metav1.ObjectMeta{Name: "rgd-xns-backend-" + suffix, Namespace: backend.Name}, Spec: corev1.ServiceSpec{Ports: []corev1.ServicePort{{Port: 80}}}} |
| 101 | + Expect(vClusterClient.Create(ctx, svc)).To(Succeed()) |
| 102 | + |
| 103 | + var hostRouteName string |
| 104 | + var route *gatewayv1.HTTPRoute |
| 105 | + var grant *gatewayv1beta1.ReferenceGrant |
| 106 | + |
| 107 | + By("creating a route whose backendRef crosses into another namespace and expecting no host sync", func() { |
| 108 | + route = crossNamespaceRoute(frontend.Name, "route-rgd-xns-"+suffix, gw.Name, backend.Name, svc.Name) |
| 109 | + Expect(vClusterClient.Create(ctx, route)).To(Succeed()) |
| 110 | + DeferCleanup(func(ctx context.Context) { |
| 111 | + Expect(ctrlclient.IgnoreNotFound(vClusterClient.Delete(ctx, route))).To(Succeed()) |
| 112 | + }) |
| 113 | + |
| 114 | + hostRouteName = translate.SafeConcatName(route.Name, "x", frontend.Name, "x", vClusterName) |
| 115 | + Consistently(func(g Gomega) { |
| 116 | + err := hostClient.Get(ctx, types.NamespacedName{Namespace: vClusterHostNS, Name: hostRouteName}, &gatewayv1.HTTPRoute{}) |
| 117 | + g.Expect(kerrors.IsNotFound(err)).To(BeTrue(), "host route %s should stay absent until a grant permits it, got error: %v", hostRouteName, err) |
| 118 | + }).WithPolling(constants.PollingInterval).WithTimeout(constants.PollingTimeoutShort).Should(Succeed()) |
| 119 | + }) |
| 120 | + |
| 121 | + By("creating a virtual ReferenceGrant and expecting the route to sync", func() { |
| 122 | + // The ReferenceGrant CRD must be served in the tenant cluster even |
| 123 | + // with grant sync disabled — virtual grants still govern |
| 124 | + // cross-namespace authorization. |
| 125 | + Eventually(func(g Gomega) { |
| 126 | + g.Expect(vClusterClient.List(ctx, &gatewayv1beta1.ReferenceGrantList{}, ctrlclient.InNamespace(backend.Name))).To(Succeed()) |
| 127 | + }).WithPolling(constants.PollingInterval).WithTimeout(constants.PollingTimeout).Should(Succeed()) |
| 128 | + |
| 129 | + grant = &gatewayv1beta1.ReferenceGrant{ |
| 130 | + ObjectMeta: metav1.ObjectMeta{Name: "allow-rgd-" + suffix, Namespace: backend.Name}, |
| 131 | + Spec: gatewayv1beta1.ReferenceGrantSpec{ |
| 132 | + From: []gatewayv1beta1.ReferenceGrantFrom{{Group: gatewayv1.GroupName, Kind: "HTTPRoute", Namespace: gatewayv1.Namespace(frontend.Name)}}, |
| 133 | + To: []gatewayv1beta1.ReferenceGrantTo{{Group: "", Kind: "Service"}}, |
| 134 | + }, |
| 135 | + } |
| 136 | + Expect(vClusterClient.Create(ctx, grant)).To(Succeed()) |
| 137 | + DeferCleanup(func(ctx context.Context) { |
| 138 | + Expect(ctrlclient.IgnoreNotFound(vClusterClient.Delete(ctx, grant))).To(Succeed()) |
| 139 | + }) |
| 140 | + Eventually(func(g Gomega) { |
| 141 | + g.Expect(hostClient.Get(ctx, types.NamespacedName{Namespace: vClusterHostNS, Name: hostRouteName}, &gatewayv1.HTTPRoute{})).To(Succeed()) |
| 142 | + }).WithPolling(constants.PollingInterval).WithTimeout(constants.PollingTimeout).Should(Succeed()) |
| 143 | + }) |
| 144 | + |
| 145 | + By("expecting the virtual ReferenceGrant to never sync to the host", func() { |
| 146 | + hostGrantName := translate.SafeConcatName(grant.Name, "x", backend.Name, "x", vClusterName) |
| 147 | + Consistently(func(g Gomega) { |
| 148 | + err := hostClient.Get(ctx, types.NamespacedName{Namespace: vClusterHostNS, Name: hostGrantName}, &gatewayv1beta1.ReferenceGrant{}) |
| 149 | + g.Expect(kerrors.IsNotFound(err)).To(BeTrue(), "host grant %s should not exist with grant sync disabled, got error: %v", hostGrantName, err) |
| 150 | + }).WithPolling(constants.PollingInterval).WithTimeout(constants.PollingTimeoutShort).Should(Succeed()) |
| 151 | + }) |
| 152 | + }) |
| 153 | + }) |
| 154 | +} |
0 commit comments