2023-09-11 14:57:57 +00:00
|
|
|
|
using System.Net.NetworkInformation;
|
|
|
|
|
using Utils;
|
|
|
|
|
|
|
|
|
|
namespace KubernetesWorkflow
|
|
|
|
|
{
|
|
|
|
|
internal enum RunnerLocation
|
|
|
|
|
{
|
|
|
|
|
ExternalToCluster,
|
|
|
|
|
InternalToCluster,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal static class RunnerLocationUtils
|
|
|
|
|
{
|
|
|
|
|
private static RunnerLocation? knownLocation = null;
|
|
|
|
|
|
|
|
|
|
internal static RunnerLocation DetermineRunnerLocation(RunningContainer container)
|
|
|
|
|
{
|
|
|
|
|
if (knownLocation != null) return knownLocation.Value;
|
2023-10-19 09:08:30 +00:00
|
|
|
|
knownLocation = PingForLocation(container);
|
|
|
|
|
return knownLocation.Value;
|
|
|
|
|
}
|
2023-09-11 14:57:57 +00:00
|
|
|
|
|
2023-10-19 09:08:30 +00:00
|
|
|
|
private static RunnerLocation PingForLocation(RunningContainer container)
|
|
|
|
|
{
|
2023-09-11 14:57:57 +00:00
|
|
|
|
if (PingHost(container.Pod.PodInfo.Ip))
|
|
|
|
|
{
|
2023-10-19 09:08:30 +00:00
|
|
|
|
return RunnerLocation.InternalToCluster;
|
2023-09-11 14:57:57 +00:00
|
|
|
|
}
|
2023-10-19 09:08:30 +00:00
|
|
|
|
|
|
|
|
|
foreach (var port in container.ContainerPorts)
|
2023-09-11 14:57:57 +00:00
|
|
|
|
{
|
2023-10-19 09:08:30 +00:00
|
|
|
|
if (PingHost(Format(port.ExternalAddress)))
|
|
|
|
|
{
|
|
|
|
|
return RunnerLocation.ExternalToCluster;
|
|
|
|
|
}
|
2023-09-11 14:57:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-10-19 09:08:30 +00:00
|
|
|
|
throw new Exception("Unable to determine location relative to kubernetes cluster.");
|
2023-09-11 14:57:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static string Format(Address host)
|
|
|
|
|
{
|
|
|
|
|
return host.Host
|
|
|
|
|
.Replace("http://", "")
|
|
|
|
|
.Replace("https://", "");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static bool PingHost(string host)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using var pinger = new Ping();
|
|
|
|
|
PingReply reply = pinger.Send(host);
|
|
|
|
|
return reply.Status == IPStatus.Success;
|
|
|
|
|
}
|
|
|
|
|
catch (PingException)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|