Skip to content

Commit

Permalink
Uses query pkg to process conditions and refactors e2e tests
Browse files Browse the repository at this point in the history
Signed-off-by: Daneyon Hansen <[email protected]>
  • Loading branch information
danehans committed Dec 2, 2024
1 parent cbdbac8 commit 1718452
Show file tree
Hide file tree
Showing 31 changed files with 65 additions and 113 deletions.
2 changes: 1 addition & 1 deletion changelog/v1.18.0-rc3/issue_7309.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ changelog:
issueLink: https://github.com/solo-io/solo-projects/issues/7309
resolvesIssue: true
description: >-
Adds ReferenceGRant support for the TCPRoute type.
Adds ReferenceGrant support for the TCPRoute type, allowing cross namespace references.
2 changes: 1 addition & 1 deletion pkg/schemes/extended_scheme.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
gwv1a2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
)

// AddGatewayV1A2Scheme adds the gwv1a2 scheme to the provided scheme if the TCPRoute CRD exists.
// AddGatewayV1A2Scheme adds the Gateway v1alpha2 scheme to the provided scheme if the TCPRoute CRD exists.
func AddGatewayV1A2Scheme(restConfig *rest.Config, scheme *runtime.Scheme) error {
exists, err := CRDExists(restConfig, gwv1a2.GroupVersion.Group, gwv1a2.GroupVersion.Version, wellknown.TCPRouteKind)
if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions pkg/schemes/scheme.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@ func DefaultScheme() *runtime.Scheme {
return s
}

// TestingScheme unconditionally includes the default and required Gateway API schemes.
func TestingScheme() *runtime.Scheme {
// GatewayScheme unconditionally includes the default and required Gateway API schemes.
// Use the Default scheme with AddGatewayV1A2Scheme to conditionally add the v1alpha2 scheme.
func GatewayScheme() *runtime.Scheme {
s := DefaultScheme()
if err := gwv1a2.Install(s); err != nil {
panic(fmt.Sprintf("Failed to install gateway v1alpha2 scheme: %v", err))
Expand Down
2 changes: 1 addition & 1 deletion projects/gateway2/controller/controller_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ var _ = BeforeSuite(func() {
cfg, err = testEnv.Start()
Expect(err).NotTo(HaveOccurred())
Expect(cfg).NotTo(BeNil())
scheme := schemes.DefaultScheme()
scheme := schemes.GatewayScheme()
k8sClient, err = client.New(cfg, client.Options{Scheme: scheme})
Expect(err).NotTo(HaveOccurred())
Expect(k8sClient).NotTo(BeNil())
Expand Down
2 changes: 1 addition & 1 deletion projects/gateway2/deployer/deployer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1438,7 +1438,7 @@ var _ = Describe("Deployer", func() {
// initialize a fake controller-runtime client with the given list of objects
func newFakeClientWithObjs(objs ...client.Object) client.Client {
return fake.NewClientBuilder().
WithScheme(schemes.TestingScheme()).
WithScheme(schemes.GatewayScheme()).
WithObjects(objs...).
Build()
}
Expand Down
2 changes: 1 addition & 1 deletion projects/gateway2/query/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var _ = Describe("Query", func() {
}

BeforeEach(func() {
scheme = schemes.TestingScheme()
scheme = schemes.GatewayScheme()
builder = fake.NewClientBuilder().WithScheme(scheme)
err := query.IterateIndices(func(o client.Object, f string, fun client.IndexerFunc) error {
builder.WithIndex(o, f, fun)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,21 +261,19 @@ func buildTcpHost(
tcpHost := &v1.TcpHost{Name: tcpRouteName}

var weightedDestinations []*v1.WeightedDestination
resolvedRefs := true
hasValidBackend := false

resolvedRefs := 0
for _, ref := range backendRefs {
// Try to get the backend object
obj, err := routeInfo.GetBackendForRef(ref.BackendObjectReference)
if err != nil {
// Process error and set ResolvedRefs condition to False
resolvedRefs = false
// Process error and set the appropriate status conditions
for _, parentRefReporter := range parentRefReporters {
query.ProcessBackendError(err, parentRefReporter)
}
continue
}
hasValidBackend = true
resolvedRefs += 1

// Process the backend object
var destination *v1.Destination
Expand All @@ -301,7 +299,6 @@ func buildTcpHost(
} else {
// Unsupported kind
err := query.ErrUnknownBackendKind
resolvedRefs = false
for _, parentRefReporter := range parentRefReporters {
query.ProcessBackendError(err, parentRefReporter)
}
Expand All @@ -314,34 +311,18 @@ func buildTcpHost(
})
}

if !hasValidBackend {
// No valid backends, do not create TcpHost
return nil
}

// Handle ResolvedRefs condition
if !resolvedRefs {
// Set ResolvedRefs condition to False
// All backendRefs are resolved, set the ResolvedRefs status condition to true
if resolvedRefs == len(backendRefs) {
for _, parentRefReporter := range parentRefReporters {
parentRefReporter.SetCondition(reports.RouteCondition{
Type: gwv1.RouteConditionResolvedRefs,
Status: metav1.ConditionFalse,
Reason: gwv1.RouteReasonRefNotPermitted,
Status: metav1.ConditionTrue,
Reason: gwv1.RouteReasonResolvedRefs,
})
}
// Do not create TcpHost
return nil
}

// Set ResolvedRefs condition to True
for _, parentRefReporter := range parentRefReporters {
parentRefReporter.SetCondition(reports.RouteCondition{
Type: gwv1.RouteConditionResolvedRefs,
Status: metav1.ConditionTrue,
Reason: gwv1.RouteReasonResolvedRefs,
})
}

// Set the TcpHost destination type
if len(weightedDestinations) == 0 {
// No valid destinations, return nil
return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ var _ = Describe("Query Get HttpListenerOptions", func() {
})

JustBeforeEach(func() {
builder := fake.NewClientBuilder().WithScheme(schemes.TestingScheme())
builder := fake.NewClientBuilder().WithScheme(schemes.GatewayScheme())
query.IterateIndices(func(o client.Object, f string, fun client.IndexerFunc) error {
builder.WithIndex(o, f, fun)
return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ var _ = Describe("Query Get ListenerOptions", func() {
})

JustBeforeEach(func() {
builder := fake.NewClientBuilder().WithScheme(schemes.TestingScheme())
builder := fake.NewClientBuilder().WithScheme(schemes.GatewayScheme())
query.IterateIndices(func(o client.Object, f string, fun client.IndexerFunc) error {
builder.WithIndex(o, f, fun)
return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var _ = Describe("Query", func() {
var builder *fake.ClientBuilder

BeforeEach(func() {
builder = fake.NewClientBuilder().WithScheme(schemes.TestingScheme())
builder = fake.NewClientBuilder().WithScheme(schemes.GatewayScheme())
query.IterateIndices(func(o client.Object, f string, fun client.IndexerFunc) error {
builder.WithIndex(o, f, fun)
return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ var _ = Describe("Query Get VirtualHostOptions", func() {
})

JustBeforeEach(func() {
builder := fake.NewClientBuilder().WithScheme(schemes.TestingScheme())
builder := fake.NewClientBuilder().WithScheme(schemes.GatewayScheme())
query.IterateIndices(func(o client.Object, f string, fun client.IndexerFunc) error {
builder.WithIndex(o, f, fun)
return nil
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Expected listeners when a TCPRoute contains an unsupported backendRef.
---
listeners:
- name: tcp
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Expected listeners when a TCPRoute references a non-existent Service as a backendRef.
---
listeners:
- aggregateListener:
Expand Down
2 changes: 1 addition & 1 deletion projects/gateway2/translator/testutils/test_file_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func LoadFromFiles(ctx context.Context, filename string) ([]client.Object, error
}

func parseFile(ctx context.Context, filename string) ([]runtime.Object, error) {
scheme := schemes.TestingScheme()
scheme := schemes.GatewayScheme()
file, err := os.ReadFile(filename)
if err != nil {
return nil, err
Expand Down
8 changes: 4 additions & 4 deletions projects/gateway2/translator/testutils/test_queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
type IndexIteratorFunc = func(f func(client.Object, string, client.IndexerFunc) error) error

func BuildIndexedFakeClient(objs []client.Object, funcs ...IndexIteratorFunc) client.Client {
builder := fake.NewClientBuilder().WithScheme(schemes.TestingScheme())
builder := fake.NewClientBuilder().WithScheme(schemes.GatewayScheme())
for _, f := range funcs {
f(func(o client.Object, s string, ifunc client.IndexerFunc) error {
builder.WithIndex(o, s, ifunc)
Expand All @@ -22,19 +22,19 @@ func BuildIndexedFakeClient(objs []client.Object, funcs ...IndexIteratorFunc) cl
}

func BuildGatewayQueriesWithClient(fakeClient client.Client, opts ...query.Option) query.GatewayQueries {
return query.NewData(fakeClient, schemes.TestingScheme(), opts...)
return query.NewData(fakeClient, schemes.GatewayScheme(), opts...)
}

func BuildGatewayQueries(
objs []client.Object,
) query.GatewayQueries {
builder := fake.NewClientBuilder().WithScheme(schemes.TestingScheme())
builder := fake.NewClientBuilder().WithScheme(schemes.GatewayScheme())
query.IterateIndices(func(o client.Object, f string, fun client.IndexerFunc) error {
builder.WithIndex(o, f, fun)
return nil
})

fakeClient := builder.WithObjects(objs...).Build()

return query.NewData(fakeClient, schemes.TestingScheme())
return query.NewData(fakeClient, schemes.GatewayScheme())
}
68 changes: 28 additions & 40 deletions test/kubernetes/e2e/features/services/tcproute/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ type tcpRouteTestCase struct {

func (s *testingSuite) TestConfigureTCPRouteBackingDestinations() {
testCases := []tcpRouteTestCase{
{
/*{
name: "SingleServiceTCPRoute",
nsManifest: singleSvcNsManifest,
gtwName: singleSvcGatewayName,
Expand Down Expand Up @@ -118,9 +118,9 @@ func (s *testingSuite) TestConfigureTCPRouteBackingDestinations() {
},
expectedRouteCounts: []int32{1, 1},
tcpRouteNames: []string{multiSvcTCPRouteName1, multiSvcTCPRouteName2},
},
},*/
{
name: "CrossNamespaceTCPRouteWithReferenceGrant",
name: crossNsTestName,
nsManifest: crossNsClientNsManifest,
gtwName: crossNsGatewayName,
gtwNs: crossNsClientName,
Expand All @@ -140,10 +140,10 @@ func (s *testingSuite) TestConfigureTCPRouteBackingDestinations() {
tcpRouteNames: []string{crossNsTCPRouteName},
},
{
name: "CrossNamespaceTCPRouteWithoutReferenceGrant",
name: crossNsNoRefGrantTestName,
nsManifest: crossNsNoRefGrantClientNsManifest,
gtwName: crossNsNoRefGrantGatewayName,
gtwNs: crossNsNoRefGrantClientName,
gtwNs: crossNsNoRefGrantClientNsName,
gtwManifest: crossNsNoRefGrantGatewayManifest,
svcManifest: crossNsNoRefGrantBackendSvcManifest,
tcpRouteManifest: crossNsNoRefGrantTCPRouteManifest,
Expand All @@ -164,33 +164,30 @@ func (s *testingSuite) TestConfigureTCPRouteBackingDestinations() {
s.Run(tc.name, func() {
// Cleanup function
s.T().Cleanup(func() {
err := s.deleteManifests(tc.nsManifest)
s.Require().NoError(err, fmt.Sprintf("Failed to delete manifest %s", tc.nsManifest))
s.deleteManifests(tc.nsManifest)

// Delete additional namespaces if any
if tc.name == "CrossNamespaceTCPRouteWithReferenceGrant" {
err = s.deleteManifests(crossNsBackendNsManifest)
s.Require().NoError(err, fmt.Sprintf("Failed to delete manifest %s", crossNsBackendNsManifest))
s.deleteManifests(crossNsBackendNsManifest)
}

if tc.name == "CrossNamespaceTCPRouteWithoutReferenceGrant" {
err = s.deleteManifests(crossNsNoRefGrantBackendNsManifest)
s.Require().NoError(err, fmt.Sprintf("Failed to delete manifest %s", crossNsNoRefGrantBackendNsManifest))
if tc.name == crossNsNoRefGrantTestName {
s.deleteManifests(crossNsNoRefGrantBackendNsManifest)
}

s.testInstallation.Assertions.EventuallyObjectsNotExist(s.ctx, &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: tc.gtwNs}})
})

// Setup environment for ReferenceGrant test cases
if tc.name == "CrossNamespaceTCPRouteWithReferenceGrant" {
s.applyManifests(crossNsBackendNsManifest)
s.applyManifests(crossNsBackendSvcManifest)
s.applyManifests(crossNsReferenceGrantManifest)
if tc.name == crossNsTestName {
s.applyManifests(crossNsBackendNsName, crossNsBackendNsManifest)
s.applyManifests(crossNsBackendNsName, crossNsBackendSvcManifest)
s.applyManifests(crossNsBackendNsName, crossNsReferenceGrantManifest)
}

if tc.name == "CrossNamespaceTCPRouteWithoutReferenceGrant" {
s.applyManifests(crossNsNoRefGrantBackendNsManifest)
s.applyManifests(crossNsNoRefGrantBackendSvcManifest)
if tc.name == crossNsNoRefGrantTestName {
s.applyManifests(crossNsNoRefGrantBackendNsName, crossNsNoRefGrantBackendNsManifest)
s.applyManifests(crossNsNoRefGrantBackendNsName, crossNsNoRefGrantBackendSvcManifest)
// ReferenceGrant not applied
}

Expand All @@ -206,15 +203,14 @@ func (s *testingSuite) TestConfigureTCPRouteBackingDestinations() {
)

// Apply TCPRoute manifest
err := s.testInstallation.Actions.Kubectl().ApplyFile(s.ctx, tc.tcpRouteManifest)
s.Require().NoError(err, fmt.Sprintf("Failed to apply manifest %s", tc.tcpRouteManifest))
s.applyManifests(tc.gtwNs, tc.tcpRouteManifest)

// Assert gateway conditions
s.testInstallation.Assertions.EventuallyGatewayCondition(s.ctx, tc.gtwName, tc.gtwNs, v1.GatewayConditionAccepted, metav1.ConditionTrue, timeout)

// Set the expected status conditions based on the test case
expected := metav1.ConditionTrue
if tc.name == "CrossNamespaceTCPRouteWithoutReferenceGrant" {
if tc.name == crossNsNoRefGrantTestName {
expected = metav1.ConditionFalse
}

Expand All @@ -235,7 +231,7 @@ func (s *testingSuite) TestConfigureTCPRouteBackingDestinations() {

// Assert expected responses
for i, port := range tc.ports {
if tc.name == "CrossNamespaceTCPRouteWithoutReferenceGrant" {
if tc.name == crossNsNoRefGrantTestName {
s.testInstallation.Assertions.AssertEventualCurlError(
s.ctx,
s.execOpts(tc.gtwNs),
Expand Down Expand Up @@ -269,35 +265,27 @@ func validateManifestFile(path string) error {
}

func (s *testingSuite) setupTestEnvironment(nsManifest, gtwName, gtwNs, gtwManifest, svcManifest string, proxySvc *corev1.Service, proxyDeploy *appsv1.Deployment) {
s.applyManifests(nsManifest)
s.applyManifests(gtwNs, nsManifest)

s.applyManifests(gtwManifest)
s.applyManifests(gtwNs, gtwManifest)
s.testInstallation.Assertions.EventuallyGatewayCondition(s.ctx, gtwName, gtwNs, v1.GatewayConditionAccepted, metav1.ConditionTrue, timeout)

s.applyManifests(svcManifest)
s.applyManifests(gtwNs, svcManifest)
s.testInstallation.Assertions.EventuallyObjectsExist(s.ctx, proxySvc, proxyDeploy)
}

func (s *testingSuite) applyManifests(manifests ...string) {
func (s *testingSuite) applyManifests(ns string, manifests ...string) {
for _, manifest := range manifests {
s.Eventually(func() bool {
err := s.testInstallation.Actions.Kubectl().ApplyFile(s.ctx, manifest)
if err != nil {
s.T().Logf("Retrying apply manifest: %s, error: %v", manifest, err)
return false
}
return true
}, waitTime, tickTime, fmt.Sprintf("Can apply manifest %s", manifest))
err := s.testInstallation.Actions.Kubectl().ApplyFile(s.ctx, manifest, "-n", ns)
s.Require().NoError(err, fmt.Sprintf("Failed to apply manifest %s", manifest))
}
}

func (s *testingSuite) deleteManifests(manifests ...string) error {
func (s *testingSuite) deleteManifests(manifests ...string) {
for _, manifest := range manifests {
if err := s.testInstallation.Actions.Kubectl().DeleteFile(s.ctx, manifest); err != nil {
return err
}
err := s.testInstallation.Actions.Kubectl().DeleteFileSafe(s.ctx, manifest)
s.Require().NoError(err, fmt.Sprintf("Failed to delete manifest %s", manifest))
}
return nil
}

func (s *testingSuite) execOpts(ns string) kubectl.PodExecOptions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ apiVersion: v1
kind: Service
metadata:
name: backend-svc
namespace: cross-namespace-allowed-backend-ns
spec:
selector:
app: backend-svc
Expand All @@ -15,7 +14,6 @@ apiVersion: apps/v1
kind: Deployment
metadata:
name: backend-svc
namespace: cross-namespace-allowed-backend-ns
labels:
app: backend-svc
spec:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
name: gateway
namespace: cross-namespace-allowed-client-ns
spec:
gatewayClassName: gloo-gateway
listeners:
Expand All @@ -14,7 +13,6 @@ apiVersion: v1
kind: Pod
metadata:
name: curl
namespace: cross-namespace-allowed-client-ns
labels:
app: curl
version: v1
Expand Down
Loading

0 comments on commit 1718452

Please sign in to comment.