Skip to content

Commit

Permalink
Named ports fix for headless services (#278)
Browse files Browse the repository at this point in the history
  • Loading branch information
hsubramanianaks authored Jun 21, 2023
1 parent d20afc2 commit e95236d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
40 changes: 39 additions & 1 deletion src/library.tests/LocalEnvironmentManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
// Licensed under the MIT license.
// --------------------------------------------------------------------------------------------

using System.Collections.Generic;
using Autofac;
using Microsoft.BridgeToKubernetes.Common.Models;
using Microsoft.BridgeToKubernetes.Common.Models.Settings;
using Microsoft.BridgeToKubernetes.Library.Connect;
using Microsoft.BridgeToKubernetes.Library.Models;
using Microsoft.BridgeToKubernetes.TestHelpers;
using System.Collections.Generic;
using System.Linq;
using Xunit;
using static Microsoft.BridgeToKubernetes.Common.Constants;
Expand Down Expand Up @@ -231,6 +231,44 @@ public static IEnumerable<object[]> TestData()
["FOO_K8SNS_SERVICE_PORT_TLS"] = "5051",
}
};

// endpoints with multiple named ports for headless services
yield return new object[]
{
new[]
{
new EndpointInfo
{
DnsName = "podname.servicename", // for headless it is combination of hostname.service from get endpoints
LocalIP = System.Net.IPAddress.Parse("127.0.0.1"),
Ports = new[]
{
new PortPair(localPort: 5050, remotePort: 80, name: "http"),
new PortPair(localPort: 5051, remotePort:443, name: "client", protocol: "client")
}
}
},
new Dictionary<string, string>
{
// backwards-compatible ports
["PODNAME_SERVICENAME_SERVICE_HOST"] = "127.0.0.1",
["PODNAME_SERVICENAME_SERVICE_PORT"] = "5050",
["PODNAME_SERVICENAME_PORT"] = "tcp://127.0.0.1:5050",
// named ports for first port pair
["PODNAME_SERVICENAME_PORT_5050_TCP_PROTO"] = "tcp",
["PODNAME_SERVICENAME_PORT_5050_TCP"] = "tcp://127.0.0.1:5050",
["PODNAME_SERVICENAME_PORT_5050_TCP_PORT"] = "5050",
["PODNAME_SERVICENAME_PORT_5050_TCP_ADDR"] = "127.0.0.1",
["PODNAME_SERVICENAME_SERVICE_PORT_HTTP"] = "5050",
// named ports for SECOND port pair
["PODNAME_SERVICENAME_PORT_5051_CLIENT_PROTO"] = "client",
["PODNAME_SERVICENAME_PORT_5051_CLIENT"] = "client://127.0.0.1:5051",
["PODNAME_SERVICENAME_PORT_5051_CLIENT_PORT"] = "5051",
["PODNAME_SERVICENAME_PORT_5051_CLIENT_ADDR"] = "127.0.0.1",
["PODNAME_SERVICENAME_SERVICE_PORT_CLIENT"] = "5051",

}
};
}

[Theory]
Expand Down
7 changes: 4 additions & 3 deletions src/library.tests/WorkloadInformationProviderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ public async void GetReachableServicesAsync_HeadlessService(int numServices, int
Assert.Equal((numServices-1) * (numAddresses), resultRechableEndpoints.Count());
foreach (var endpoint in resultRechableEndpoints) {
if (endpoint.Ports.Any()) {
Assert.Equal(endpoint.Ports.ElementAt(0).LocalPort, -1);
Assert.Equal(-1, endpoint.Ports.ElementAt(0).LocalPort);
Assert.Equal("http", endpoint.Ports.ElementAt(0).Name);
bool found = false;
foreach (var dns in expectedDnsList) {
if (string.Equals(endpoint.DnsName, dns))
Expand Down Expand Up @@ -342,7 +343,7 @@ private List<string> ConfigureHeadlessService(int numServices, Func<int, string>
{
Type = "ClusterIP",
ClusterIP = "None",
Ports = new List<V1ServicePort> { new V1ServicePort(port: 80, protocol: "TCP") },
Ports = new List<V1ServicePort> { new V1ServicePort(port: 80, protocol: "TCP", name: "http") },
Selector = new Dictionary<string, string> { { "app", "myapp" } }
},
Metadata = new V1ObjectMeta()
Expand All @@ -355,7 +356,7 @@ private List<string> ConfigureHeadlessService(int numServices, Func<int, string>
{
new V1EndpointSubset()
{
Ports = new List<Corev1EndpointPort> { new Corev1EndpointPort(port: 80, protocol: "TCP") },
Ports = new List<Corev1EndpointPort> { new Corev1EndpointPort(port: 80, protocol: "TCP", name: "http") },
Addresses = new List<V1EndpointAddress>()
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/library/Connect/WorkloadInformationProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ private async Task<IEnumerable<EndpointInfo>> _CollectServicesToRouteAsync(
DnsName = dns,
Ports = subset.Ports?
.Where(port => this._IsSupportedProtocol(port.Protocol, endpoint.Metadata.Name) && !(portToIgnoreForHeadlessServiceEndpoints.GetValueOrDefault(endpoint.Metadata.Name)?.Contains(port.Port) ?? false))
.Select(p => new PortPair(remotePort: p.Port, p.Name))
.Select(p => new PortPair(remotePort: p.Port,name : p.Name))
.ToArray() ?? new PortPair[] { },
IsInWorkloadNamespace = isInWorkloadNamespace
});
Expand Down

0 comments on commit e95236d

Please sign in to comment.