Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support nodeport service #206

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 40 additions & 3 deletions src/library.tests/WorkloadInformationProviderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public async void GetReachableServicesAsync_PortsToIgnore_Service(int numAddress
servicePorts.Add("ServiceA", new List<int>{234, 421, 300, 121});
servicePorts.Add("ServiceB", new List<int>{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));

Expand Down Expand Up @@ -209,6 +209,43 @@ public async void GetReachableServicesAsync_NoPortsToIgnore_Service(int numAddre
}
}

[Theory]
[InlineData(1)]
public async void GetReachableServicesAsync_NoPortsToIgnore_NodePortService(int numAddresses)
{
Dictionary<string, string> ignorePorts = new Dictionary<string, string>();
ignorePorts.Add("ServiceA", null);
ignorePorts.Add("ServiceB", null);

Dictionary<string, List<int>> servicePorts = new Dictionary<string, List<int>>();
servicePorts.Add("ServiceA", new List<int>{234, 421, 300, 121});
servicePorts.Add("ServiceB", new List<int>{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<string, List<int>> portsAllocatedToService = new Dictionary<string, List<int>>();
foreach (var endpointInfo in result)
{
var ports = portsAllocatedToService.GetOrAdd(endpointInfo.DnsName, () => new List<int>());
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<int>();
var expectedPortsForService = servicePorts.GetValueOrDefault(serviceName)?.Where( p => !ignorePortsList.Contains(p)) ?? new List<int>();

// 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) {
Expand Down Expand Up @@ -237,7 +274,7 @@ private static bool CompareLists<T>(List<T> list1, List<T> list2)
return list1.All( item => list2.Contains(item));
}

private void ConfigureServiceWithIgnorePorts(bool isHeadLessService, int numAddresses, Dictionary<string, string> ignorePorts, Dictionary<string, List<int>> servicePorts = null)
private void ConfigureServiceWithIgnorePorts(bool isHeadLessService, int numAddresses, Dictionary<string, string> ignorePorts, Dictionary<string, List<int>> servicePorts = null, string serviceType = "ClusterIP")
{
var serviceList = new List<V1Service>();
// ignorePorts.Keys has all services used in the test.
Expand All @@ -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<V1ServicePort> { new V1ServicePort(port: 80, protocol: "TCP")},
Selector = new Dictionary<string, string> { { "app", "myapp" } }
Expand Down
4 changes: 2 additions & 2 deletions src/library/Connect/WorkloadInformationProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ private async Task<IEnumerable<EndpointInfo>> _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)))
{
Expand Down Expand Up @@ -660,4 +660,4 @@ public int GetHashCode(V1Service service)

#endregion Private members
}
}
}