From e518860df414e5d5946d7102fcd1427285cf0ec1 Mon Sep 17 00:00:00 2001 From: Nathan Coleman Date: Fri, 15 Dec 2023 16:08:41 -0500 Subject: [PATCH] Add test case for gateway pod reconciliation --- .../pod/pod_controller_ent_test.go | 28 +++---- .../controllers/pod/pod_controller_test.go | 79 ++++++++++++------- 2 files changed, 65 insertions(+), 42 deletions(-) diff --git a/control-plane/connect-inject/controllers/pod/pod_controller_ent_test.go b/control-plane/connect-inject/controllers/pod/pod_controller_ent_test.go index 1c074dc940..a116122a03 100644 --- a/control-plane/connect-inject/controllers/pod/pod_controller_ent_test.go +++ b/control-plane/connect-inject/controllers/pod/pod_controller_ent_test.go @@ -101,7 +101,7 @@ func TestReconcileCreatePodWithMirrorNamespaces(t *testing.T) { expectedConsulNamespace: constants.DefaultConsulNS, expectedWorkload: createWorkload(), expectedHealthStatus: createPassingHealthStatus(), - expectedProxyConfiguration: createProxyConfiguration(testPodName, pbmesh.ProxyMode_PROXY_MODE_TRANSPARENT), + expectedProxyConfiguration: createProxyConfiguration(testPodName, true, pbmesh.ProxyMode_PROXY_MODE_TRANSPARENT), }, { name: "kitchen sink new pod, non-default ns and partition", @@ -127,7 +127,7 @@ func TestReconcileCreatePodWithMirrorNamespaces(t *testing.T) { expectedConsulNamespace: "bar", expectedWorkload: createWorkload(), expectedHealthStatus: createPassingHealthStatus(), - expectedProxyConfiguration: createProxyConfiguration(testPodName, pbmesh.ProxyMode_PROXY_MODE_TRANSPARENT), + expectedProxyConfiguration: createProxyConfiguration(testPodName, true, pbmesh.ProxyMode_PROXY_MODE_TRANSPARENT), }, { name: "new pod with namespace prefix", @@ -194,7 +194,7 @@ func TestReconcileCreatePodWithMirrorNamespaces(t *testing.T) { expectedConsulNamespace: constants.DefaultConsulNS, expectedWorkload: createWorkload(), expectedHealthStatus: createPassingHealthStatus(), - expectedProxyConfiguration: createProxyConfiguration(testPodName, pbmesh.ProxyMode_PROXY_MODE_DEFAULT), + expectedProxyConfiguration: createProxyConfiguration(testPodName, true, pbmesh.ProxyMode_PROXY_MODE_DEFAULT), expectedDestinations: createDestinations(), }, { @@ -273,12 +273,12 @@ func TestReconcileUpdatePodWithMirrorNamespaces(t *testing.T) { existingConsulNamespace: "bar", existingWorkload: createWorkload(), existingHealthStatus: createPassingHealthStatus(), - existingProxyConfiguration: createProxyConfiguration(testPodName, pbmesh.ProxyMode_PROXY_MODE_TRANSPARENT), + existingProxyConfiguration: createProxyConfiguration(testPodName, true, pbmesh.ProxyMode_PROXY_MODE_TRANSPARENT), expectedConsulNamespace: "bar", expectedWorkload: createWorkload(), expectedHealthStatus: createPassingHealthStatus(), - expectedProxyConfiguration: createProxyConfiguration(testPodName, pbmesh.ProxyMode_PROXY_MODE_TRANSPARENT), + expectedProxyConfiguration: createProxyConfiguration(testPodName, true, pbmesh.ProxyMode_PROXY_MODE_TRANSPARENT), }, } @@ -311,7 +311,7 @@ func TestReconcileDeletePodWithMirrorNamespaces(t *testing.T) { existingConsulNamespace: "bar", existingWorkload: createWorkload(), existingHealthStatus: createPassingHealthStatus(), - existingProxyConfiguration: createProxyConfiguration(testPodName, pbmesh.ProxyMode_PROXY_MODE_TRANSPARENT), + existingProxyConfiguration: createProxyConfiguration(testPodName, true, pbmesh.ProxyMode_PROXY_MODE_TRANSPARENT), expectedConsulNamespace: "bar", }, @@ -330,7 +330,7 @@ func TestReconcileDeletePodWithMirrorNamespaces(t *testing.T) { existingConsulNamespace: "bar", existingWorkload: createWorkload(), existingHealthStatus: createPassingHealthStatus(), - existingProxyConfiguration: createProxyConfiguration(testPodName, pbmesh.ProxyMode_PROXY_MODE_DEFAULT), + existingProxyConfiguration: createProxyConfiguration(testPodName, true, pbmesh.ProxyMode_PROXY_MODE_DEFAULT), existingDestinations: createDestinations(), expectedConsulNamespace: "bar", @@ -415,7 +415,7 @@ func TestReconcileCreatePodWithDestinationNamespace(t *testing.T) { expectedConsulNamespace: constants.DefaultConsulNS, expectedWorkload: createWorkload(), expectedHealthStatus: createPassingHealthStatus(), - expectedProxyConfiguration: createProxyConfiguration(testPodName, pbmesh.ProxyMode_PROXY_MODE_TRANSPARENT), + expectedProxyConfiguration: createProxyConfiguration(testPodName, true, pbmesh.ProxyMode_PROXY_MODE_TRANSPARENT), }, { name: "new pod with explicit destinations, ns and partition", @@ -439,7 +439,7 @@ func TestReconcileCreatePodWithDestinationNamespace(t *testing.T) { expectedConsulNamespace: constants.DefaultConsulNS, expectedWorkload: createWorkload(), expectedHealthStatus: createPassingHealthStatus(), - expectedProxyConfiguration: createProxyConfiguration(testPodName, pbmesh.ProxyMode_PROXY_MODE_DEFAULT), + expectedProxyConfiguration: createProxyConfiguration(testPodName, true, pbmesh.ProxyMode_PROXY_MODE_DEFAULT), expectedDestinations: createDestinations(), }, { @@ -466,7 +466,7 @@ func TestReconcileCreatePodWithDestinationNamespace(t *testing.T) { expectedConsulNamespace: "a-penguin-walks-into-a-bar", expectedWorkload: createWorkload(), expectedHealthStatus: createPassingHealthStatus(), - expectedProxyConfiguration: createProxyConfiguration(testPodName, pbmesh.ProxyMode_PROXY_MODE_TRANSPARENT), + expectedProxyConfiguration: createProxyConfiguration(testPodName, true, pbmesh.ProxyMode_PROXY_MODE_TRANSPARENT), }, { name: "namespace in Consul does not exist", @@ -543,12 +543,12 @@ func TestReconcileUpdatePodWithDestinationNamespace(t *testing.T) { existingConsulNamespace: "a-penguin-walks-into-a-bar", existingWorkload: createWorkload(), existingHealthStatus: createPassingHealthStatus(), - existingProxyConfiguration: createProxyConfiguration(testPodName, pbmesh.ProxyMode_PROXY_MODE_TRANSPARENT), + existingProxyConfiguration: createProxyConfiguration(testPodName, true, pbmesh.ProxyMode_PROXY_MODE_TRANSPARENT), expectedConsulNamespace: "a-penguin-walks-into-a-bar", expectedWorkload: createWorkload(), expectedHealthStatus: createPassingHealthStatus(), - expectedProxyConfiguration: createProxyConfiguration(testPodName, pbmesh.ProxyMode_PROXY_MODE_TRANSPARENT), + expectedProxyConfiguration: createProxyConfiguration(testPodName, true, pbmesh.ProxyMode_PROXY_MODE_TRANSPARENT), }, } @@ -581,7 +581,7 @@ func TestReconcileDeletePodWithDestinationNamespace(t *testing.T) { existingConsulNamespace: "a-penguin-walks-into-a-bar", existingWorkload: createWorkload(), existingHealthStatus: createPassingHealthStatus(), - existingProxyConfiguration: createProxyConfiguration(testPodName, pbmesh.ProxyMode_PROXY_MODE_TRANSPARENT), + existingProxyConfiguration: createProxyConfiguration(testPodName, true, pbmesh.ProxyMode_PROXY_MODE_TRANSPARENT), expectedConsulNamespace: "a-penguin-walks-into-a-bar", }, @@ -600,7 +600,7 @@ func TestReconcileDeletePodWithDestinationNamespace(t *testing.T) { existingConsulNamespace: "a-penguin-walks-into-a-bar", existingWorkload: createWorkload(), existingHealthStatus: createPassingHealthStatus(), - existingProxyConfiguration: createProxyConfiguration(testPodName, pbmesh.ProxyMode_PROXY_MODE_DEFAULT), + existingProxyConfiguration: createProxyConfiguration(testPodName, true, pbmesh.ProxyMode_PROXY_MODE_DEFAULT), existingDestinations: createDestinations(), expectedConsulNamespace: "a-penguin-walks-into-a-bar", diff --git a/control-plane/connect-inject/controllers/pod/pod_controller_test.go b/control-plane/connect-inject/controllers/pod/pod_controller_test.go index 413f9ac49a..55b5fd5ba1 100644 --- a/control-plane/connect-inject/controllers/pod/pod_controller_test.go +++ b/control-plane/connect-inject/controllers/pod/pod_controller_test.go @@ -785,7 +785,7 @@ func TestProxyConfigurationDelete(t *testing.T) { { name: "proxy configuration delete", pod: createPod("foo", "", true, true), - existingProxyConfiguration: createProxyConfiguration("foo", pbmesh.ProxyMode_PROXY_MODE_TRANSPARENT), + existingProxyConfiguration: createProxyConfiguration("foo", true, pbmesh.ProxyMode_PROXY_MODE_TRANSPARENT), }, } @@ -1308,7 +1308,7 @@ func TestReconcileCreatePod(t *testing.T) { testCases := []testCase{ { - name: "vanilla new pod", + name: "vanilla new mesh-injected pod", podName: "foo", k8sObjects: func() []runtime.Object { pod := createPod("foo", "", true, true) @@ -1322,7 +1322,26 @@ func TestReconcileCreatePod(t *testing.T) { overwriteProbes: true, expectedWorkload: createWorkload(), expectedHealthStatus: createPassingHealthStatus(), - expectedProxyConfiguration: createProxyConfiguration("foo", pbmesh.ProxyMode_PROXY_MODE_TRANSPARENT), + expectedProxyConfiguration: createProxyConfiguration("foo", true, pbmesh.ProxyMode_PROXY_MODE_TRANSPARENT), + }, + { + name: "vanilla new gateway pod (not mesh-injected)", + podName: "foo", + k8sObjects: func() []runtime.Object { + pod := createPod("foo", "", false, true) + pod.Annotations[constants.AnnotationGatewayKind] = "mesh-gateway" + pod.Annotations[constants.AnnotationMeshInject] = "false" + pod.Annotations[constants.AnnotationTransparentProxyOverwriteProbes] = "false" + + return []runtime.Object{pod} + }, + tproxy: true, + telemetry: true, + metrics: true, + overwriteProbes: true, + expectedWorkload: createWorkload(), + expectedHealthStatus: createPassingHealthStatus(), + expectedProxyConfiguration: createProxyConfiguration("foo", false, pbmesh.ProxyMode_PROXY_MODE_TRANSPARENT), }, { name: "pod in ignored namespace", @@ -1380,7 +1399,7 @@ func TestReconcileCreatePod(t *testing.T) { overwriteProbes: true, expectedWorkload: createWorkload(), expectedHealthStatus: createPassingHealthStatus(), - expectedProxyConfiguration: createProxyConfiguration("foo", pbmesh.ProxyMode_PROXY_MODE_DEFAULT), + expectedProxyConfiguration: createProxyConfiguration("foo", true, pbmesh.ProxyMode_PROXY_MODE_DEFAULT), expectedDestinations: createDestinations(), }, { @@ -1827,14 +1846,14 @@ func TestReconcileDeletePod(t *testing.T) { podName: "foo", existingWorkload: createWorkload(), existingHealthStatus: createPassingHealthStatus(), - existingProxyConfiguration: createProxyConfiguration("foo", pbmesh.ProxyMode_PROXY_MODE_TRANSPARENT), + existingProxyConfiguration: createProxyConfiguration("foo", true, pbmesh.ProxyMode_PROXY_MODE_TRANSPARENT), }, { name: "annotated delete pod", podName: "foo", existingWorkload: createWorkload(), existingHealthStatus: createPassingHealthStatus(), - existingProxyConfiguration: createProxyConfiguration("foo", pbmesh.ProxyMode_PROXY_MODE_DEFAULT), + existingProxyConfiguration: createProxyConfiguration("foo", true, pbmesh.ProxyMode_PROXY_MODE_DEFAULT), existingDestinations: createDestinations(), }, { @@ -1842,7 +1861,7 @@ func TestReconcileDeletePod(t *testing.T) { podName: "foo", existingWorkload: createWorkload(), existingHealthStatus: createPassingHealthStatus(), - existingProxyConfiguration: createProxyConfiguration("foo", pbmesh.ProxyMode_PROXY_MODE_TRANSPARENT), + existingProxyConfiguration: createProxyConfiguration("foo", true, pbmesh.ProxyMode_PROXY_MODE_TRANSPARENT), aclsEnabled: true, }, } @@ -1989,32 +2008,14 @@ func createCriticalHealthStatus(name string, namespace string) *pbcatalog.Health // createProxyConfiguration creates a proxyConfiguration that matches the pod from createPod, // assuming that metrics, telemetry, and overwrite probes are enabled separately. -func createProxyConfiguration(podName string, mode pbmesh.ProxyMode) *pbmesh.ProxyConfiguration { +func createProxyConfiguration(podName string, overwriteProbes bool, mode pbmesh.ProxyMode) *pbmesh.ProxyConfiguration { mesh := &pbmesh.ProxyConfiguration{ Workloads: &pbcatalog.WorkloadSelector{ Names: []string{podName}, }, DynamicConfig: &pbmesh.DynamicConfig{ - Mode: mode, - ExposeConfig: &pbmesh.ExposeConfig{ - ExposePaths: []*pbmesh.ExposePath{ - { - ListenerPort: 20400, - LocalPathPort: 2001, - Path: "/livez", - }, - { - ListenerPort: 20300, - LocalPathPort: 2000, - Path: "/readyz", - }, - { - ListenerPort: 20500, - LocalPathPort: 2002, - Path: "/startupz", - }, - }, - }, + Mode: mode, + ExposeConfig: nil, }, BootstrapConfig: &pbmesh.BootstrapConfig{ PrometheusBindAddr: "0.0.0.0:1234", @@ -2022,6 +2023,28 @@ func createProxyConfiguration(podName string, mode pbmesh.ProxyMode) *pbmesh.Pro }, } + if overwriteProbes { + mesh.DynamicConfig.ExposeConfig = &pbmesh.ExposeConfig{ + ExposePaths: []*pbmesh.ExposePath{ + { + ListenerPort: 20400, + LocalPathPort: 2001, + Path: "/livez", + }, + { + ListenerPort: 20300, + LocalPathPort: 2000, + Path: "/readyz", + }, + { + ListenerPort: 20500, + LocalPathPort: 2002, + Path: "/startupz", + }, + }, + } + } + if mode == pbmesh.ProxyMode_PROXY_MODE_TRANSPARENT { mesh.DynamicConfig.TransparentProxy = &pbmesh.TransparentProxy{ OutboundListenerPort: 15001,