From faf953db274464b33b8cca8da8db443b418ae106 Mon Sep 17 00:00:00 2001 From: elvilla Date: Wed, 8 Mar 2023 11:30:38 -0800 Subject: [PATCH 1/3] Use endpoint dns if hostname not specified for the address --- src/library.tests/WorkloadInformationProviderTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/library.tests/WorkloadInformationProviderTests.cs b/src/library.tests/WorkloadInformationProviderTests.cs index 5a9df9a2..95acc089 100644 --- a/src/library.tests/WorkloadInformationProviderTests.cs +++ b/src/library.tests/WorkloadInformationProviderTests.cs @@ -370,4 +370,4 @@ private List ConfigureHeadlessService(int numServices, Func return result; } } -} \ No newline at end of file +} From e19c831e638af69b7a71cb2be99841e5283cf06c Mon Sep 17 00:00:00 2001 From: elvilla Date: Thu, 16 Mar 2023 10:33:52 -0700 Subject: [PATCH 2/3] Add support for NodePort service type --- src/library.tests/WorkloadInformationProviderTests.cs | 2 +- src/library/Connect/WorkloadInformationProvider.cs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/library.tests/WorkloadInformationProviderTests.cs b/src/library.tests/WorkloadInformationProviderTests.cs index 95acc089..5a9df9a2 100644 --- a/src/library.tests/WorkloadInformationProviderTests.cs +++ b/src/library.tests/WorkloadInformationProviderTests.cs @@ -370,4 +370,4 @@ private List ConfigureHeadlessService(int numServices, Func return result; } } -} +} \ No newline at end of file diff --git a/src/library/Connect/WorkloadInformationProvider.cs b/src/library/Connect/WorkloadInformationProvider.cs index 1e90504d..f3c08345 100755 --- a/src/library/Connect/WorkloadInformationProvider.cs +++ b/src/library/Connect/WorkloadInformationProvider.cs @@ -452,7 +452,7 @@ private async Task> _CollectServicesToRouteAsync( } } - if ((s.Spec.Type == "ClusterIP" || s.Spec.Type == "LoadBalancer") && !string.IsNullOrWhiteSpace(s.Spec.ClusterIP) && (s.Spec.Ports?.Any() ?? false)) + if ((s.Spec.Type == "ClusterIP" || s.Spec.Type == "LoadBalancer" || s.Spec.Type == "NodePort") && !string.IsNullOrWhiteSpace(s.Spec.ClusterIP) && (s.Spec.Ports?.Any() ?? false)) { if (!servicesToRouteMap.ContainsKey(getMapKey(s.Metadata.Name, s.Metadata.NamespaceProperty))) { @@ -660,4 +660,4 @@ public int GetHashCode(V1Service service) #endregion Private members } -} \ No newline at end of file +} From 4d7b8f216afd372a39c721faf6368ecccadaad9d Mon Sep 17 00:00:00 2001 From: elvilla Date: Thu, 16 Mar 2023 10:37:43 -0700 Subject: [PATCH 3/3] forgot to add UT file --- .../WorkloadInformationProviderTests.cs | 43 +++++++++++++++++-- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/src/library.tests/WorkloadInformationProviderTests.cs b/src/library.tests/WorkloadInformationProviderTests.cs index 5a9df9a2..54aed1d4 100644 --- a/src/library.tests/WorkloadInformationProviderTests.cs +++ b/src/library.tests/WorkloadInformationProviderTests.cs @@ -109,7 +109,7 @@ public async void GetReachableServicesAsync_PortsToIgnore_Service(int numAddress servicePorts.Add("ServiceA", new List{234, 421, 300, 121}); servicePorts.Add("ServiceB", new List{89, 23, 111, 324}); - ConfigureServiceWithIgnorePorts(isHeadLessService: false, numAddresses, ignorePorts, servicePorts); + ConfigureServiceWithIgnorePorts(isHeadLessService: false, numAddresses, ignorePorts, servicePorts, "NodePort"); // Generate service endpoints var result = await _workloadInformationProvider.GetReachableEndpointsAsync(namespaceName: "", localProcessConfig: null, includeSameNamespaceServices: true, cancellationToken: default(CancellationToken)); @@ -209,6 +209,43 @@ public async void GetReachableServicesAsync_NoPortsToIgnore_Service(int numAddre } } + [Theory] + [InlineData(1)] + public async void GetReachableServicesAsync_NoPortsToIgnore_NodePortService(int numAddresses) + { + Dictionary ignorePorts = new Dictionary(); + ignorePorts.Add("ServiceA", null); + ignorePorts.Add("ServiceB", null); + + Dictionary> servicePorts = new Dictionary>(); + servicePorts.Add("ServiceA", new List{234, 421, 300, 121}); + servicePorts.Add("ServiceB", new List{89, 23, 111, 324}); + + ConfigureServiceWithIgnorePorts(isHeadLessService: false, numAddresses, ignorePorts, servicePorts, "NodePort"); + var result = await _workloadInformationProvider.GetReachableEndpointsAsync(namespaceName: "", localProcessConfig: null, includeSameNamespaceServices: true, cancellationToken: default(CancellationToken)); + + // Find all ports which were allocated to each service + Dictionary> portsAllocatedToService = new Dictionary>(); + foreach (var endpointInfo in result) + { + var ports = portsAllocatedToService.GetOrAdd(endpointInfo.DnsName, () => new List()); + ports.AddRange(endpointInfo.Ports.Select( p => p.RemotePort)); + } + + // For each service ensure that none of the ignore ports are in the ports allocated and all other servicePorts are allocated to the service + Assert.True(CompareLists(ignorePorts.Keys.Select( s => $"{s}.").ToList(), portsAllocatedToService.Keys.ToList())); + + foreach(var serviceName in ignorePorts.Keys) + { + // Identify expected ports for a service. + var ignorePortsList = ignorePorts.GetValueOrDefault(serviceName)?.Split(",").Select(p => int.Parse(p)).ToList() ?? new List(); + var expectedPortsForService = servicePorts.GetValueOrDefault(serviceName)?.Where( p => !ignorePortsList.Contains(p)) ?? new List(); + + // Check if the allocated ports (from result) are same as the expected ports for the service. + Assert.True(CompareLists(expectedPortsForService.ToList(), portsAllocatedToService.GetValueOrDefault($"{serviceName}."))); + } + } + [Theory] [InlineData(1)] public async void GetReachableServicesAsync_PortsToIgnoreIncorrectFormat_HeadlessService(int numAddresses) { @@ -237,7 +274,7 @@ private static bool CompareLists(List list1, List list2) return list1.All( item => list2.Contains(item)); } - private void ConfigureServiceWithIgnorePorts(bool isHeadLessService, int numAddresses, Dictionary ignorePorts, Dictionary> servicePorts = null) + private void ConfigureServiceWithIgnorePorts(bool isHeadLessService, int numAddresses, Dictionary ignorePorts, Dictionary> servicePorts = null, string serviceType = "ClusterIP") { var serviceList = new List(); // ignorePorts.Keys has all services used in the test. @@ -247,7 +284,7 @@ private void ConfigureServiceWithIgnorePorts(bool isHeadLessService, int numAddr { Spec = new V1ServiceSpec() { - Type = "ClusterIP", + Type = serviceType, ClusterIP = isHeadLessService ? "None" : "10.1.1.1", Ports = servicePorts?.GetValueOrDefault(serviceName).Select(p => new V1ServicePort(port: p, protocol: "TCP")).ToList() ?? new List { new V1ServicePort(port: 80, protocol: "TCP")}, Selector = new Dictionary { { "app", "myapp" } }