From 010bf533d11ac29a5632659cd2af2c5120a11f8e Mon Sep 17 00:00:00 2001 From: Nathan Coleman Date: Mon, 18 Dec 2023 16:54:41 -0500 Subject: [PATCH] NET-6663 Modify sidecarproxy controller to skip xGateway resources (#19902) * NET-6663 Modify sidecarproxy controller to skip xGateway resources * Check workload metadata after nil-check for workload * Add test asserting that workloads with meta gateway-kind are ignored * Use more common pattern for map access to increase readability --- .../controllers/sidecarproxy/controller.go | 7 ++++ .../sidecarproxy/controller_test.go | 32 +++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/internal/mesh/internal/controllers/sidecarproxy/controller.go b/internal/mesh/internal/controllers/sidecarproxy/controller.go index 733b811081..bf93f79d16 100644 --- a/internal/mesh/internal/controllers/sidecarproxy/controller.go +++ b/internal/mesh/internal/controllers/sidecarproxy/controller.go @@ -122,6 +122,7 @@ func (r *reconciler) Reconcile(ctx context.Context, rt controller.Runtime, req c rt.Logger.Error("error reading the associated workload", "error", err) return err } + if workload == nil { // If workload has been deleted, then return as ProxyStateTemplate should be cleaned up // by the garbage collector because of the owner reference. @@ -129,6 +130,12 @@ func (r *reconciler) Reconcile(ctx context.Context, rt controller.Runtime, req c return nil } + // If the workload is for a xGateway, then do nothing + let the gatewayproxy controller reconcile it + if gatewayKind, ok := workload.Metadata["gateway-kind"]; ok && gatewayKind != "" { + rt.Logger.Trace("workload is a gateway; skipping reconciliation", "workload", workloadID, "gateway-kind", gatewayKind) + return nil + } + proxyStateTemplate, err := dataFetcher.FetchProxyStateTemplate(ctx, req.ID) if err != nil { rt.Logger.Error("error reading proxy state template", "error", err) diff --git a/internal/mesh/internal/controllers/sidecarproxy/controller_test.go b/internal/mesh/internal/controllers/sidecarproxy/controller_test.go index 8a2fe05a46..3d93e70724 100644 --- a/internal/mesh/internal/controllers/sidecarproxy/controller_test.go +++ b/internal/mesh/internal/controllers/sidecarproxy/controller_test.go @@ -454,6 +454,38 @@ func (suite *controllerTestSuite) TestReconcile_NoWorkload() { }) } +func (suite *controllerTestSuite) TestReconcile_GatewayWorkload() { + suite.runTestCaseWithTenancies(func(tenancy *pbresource.Tenancy) { + // This test ensures that gateway workloads are ignored by the controller. + + gatewayWorkload := &pbcatalog.Workload{ + Addresses: []*pbcatalog.WorkloadAddress{ + { + Host: "10.0.0.1", + }, + }, + Identity: "mesh-gateway-identity", + Ports: map[string]*pbcatalog.WorkloadPort{ + "mesh": {Port: 20000, Protocol: pbcatalog.Protocol_PROTOCOL_MESH}, + "tcp": {Port: 8080, Protocol: pbcatalog.Protocol_PROTOCOL_TCP}, + }, + } + + resourcetest.Resource(pbcatalog.WorkloadType, "test-gateway-workload"). + WithData(suite.T(), gatewayWorkload). + WithTenancy(tenancy). + WithMeta("gateway-kind", "mesh-gateway"). + Write(suite.T(), suite.client.ResourceServiceClient) + + err := suite.ctl.Reconcile(context.Background(), suite.runtime, controller.Request{ + ID: resourceID(pbmesh.ProxyStateTemplateType, "test-gateway-workload", tenancy), + }) + + require.NoError(suite.T(), err) + suite.client.RequireResourceNotFound(suite.T(), resourceID(pbmesh.ProxyStateTemplateType, "test-non-mesh-api-workload", tenancy)) + }) +} + func (suite *controllerTestSuite) TestReconcile_NonMeshWorkload() { suite.runTestCaseWithTenancies(func(tenancy *pbresource.Tenancy) { // This test ensures that non-mesh workloads are ignored by the controller.