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

226 - add ns to env vars when workload ns != service ns #227

Merged
merged 6 commits into from
May 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
62 changes: 62 additions & 0 deletions src/library.tests/LocalEnvironmentManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,68 @@ public static IEnumerable<object[]> TestData()
[ManagedIdentity.MSI_ENDPOINT_EnvironmentVariable] = "http://127.0.0.1:5050/metadata/identity/oauth2/token",
}
};

// single basic endpoint in another ns
yield return new object[]
{
new[] {
new EndpointInfo
{
DnsName = "foo.k8sns", // when !isInWorkloadNamespace
LocalIP = System.Net.IPAddress.Parse("127.0.0.1"),
Ports = new[] { new PortPair(5050, 80) }
}
},
new Dictionary<string, string>
{
// backwards-compatible ports
["FOO_K8SNS_SERVICE_HOST"] = "127.0.0.1",
["FOO_K8SNS_SERVICE_PORT"] = "5050",
["FOO_K8SNS_PORT"] = "tcp://127.0.0.1:5050",
// named ports
["FOO_K8SNS_PORT_5050_TCP_PROTO"] = "tcp",
["FOO_K8SNS_PORT_5050_TCP"] = "tcp://127.0.0.1:5050",
["FOO_K8SNS_PORT_5050_TCP_PORT"] = "5050",
["FOO_K8SNS_PORT_5050_TCP_ADDR"] = "127.0.0.1",
}
};

// single endpoint with multiple named ports in another ns
yield return new object[]
{
new[]
{
new EndpointInfo
{
DnsName = "foo.k8sns", // when !isInWorkloadNamespace
LocalIP = System.Net.IPAddress.Parse("127.0.0.1"),
Ports = new[]
{
new PortPair(5050, 80, "tcp", "http"),
new PortPair(5051, 443, "tcp", "tls")
}
}
},
new Dictionary<string, string>
{
// backwards-compatible ports
["FOO_K8SNS_SERVICE_HOST"] = "127.0.0.1",
["FOO_K8SNS_SERVICE_PORT"] = "5050",
["FOO_K8SNS_PORT"] = "tcp://127.0.0.1:5050",
// named ports for first port pair
["FOO_K8SNS_PORT_5050_TCP_PROTO"] = "tcp",
["FOO_K8SNS_PORT_5050_TCP"] = "tcp://127.0.0.1:5050",
["FOO_K8SNS_PORT_5050_TCP_PORT"] = "5050",
["FOO_K8SNS_PORT_5050_TCP_ADDR"] = "127.0.0.1",
["FOO_K8SNS_SERVICE_PORT_HTTP"] = "5050",
// named ports for second port pair
["FOO_K8SNS_PORT_5051_TCP_PROTO"] = "tcp",
["FOO_K8SNS_PORT_5051_TCP"] = "tcp://127.0.0.1:5051",
["FOO_K8SNS_PORT_5051_TCP_PORT"] = "5051",
["FOO_K8SNS_PORT_5051_TCP_ADDR"] = "127.0.0.1",
["FOO_K8SNS_SERVICE_PORT_TLS"] = "5051",
}
};
}

[Theory]
Expand Down
22 changes: 18 additions & 4 deletions src/library/Connect/LocalEnvironmentManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -436,12 +436,26 @@ public IDictionary<string, string> CreateEnvVariablesForK8s(WorkloadInfo workloa

// because we are using dns name instead of service we have to retrieve it by splitting when needed
// If this ever cause issues we should consider larger refactor where we add serviceName member variable to EndpointInfo class.
var serviceName = endpoint.DnsName
var dnsNameArray = endpoint.DnsName
.ToUpperInvariant()
.Split(".")
.Split(".");

var serviceName = dnsNameArray
.First()
.Replace("-", "_")
.Replace(".", "_");
cxznmhdcxz marked this conversation as resolved.
Show resolved Hide resolved
.Replace("-", "_");

// when !endpoint.IsInWorkloadNamespace
var serviceNs = dnsNameArray.Length == 2
hsubramanianaks marked this conversation as resolved.
Show resolved Hide resolved
? dnsNameArray
.Last()
.Replace("-", "_")
: null;

// sometimes cross talk is desired between namespaces, append those not matching workload
if (!string.IsNullOrWhiteSpace(serviceNs))
hsubramanianaks marked this conversation as resolved.
Show resolved Hide resolved
{
serviceName = $"{serviceName}_{serviceNs}";
hsubramanianaks marked this conversation as resolved.
Show resolved Hide resolved
}

var host = _useKubernetesServiceEnvironmentVariables || string.Equals(endpoint.DnsName, DAPR, StringComparison.OrdinalIgnoreCase)
? endpoint.LocalIP.ToString()
Expand Down