2023-11-06 15:12:18 +00:00
|
|
|
|
using Logging;
|
|
|
|
|
using System.Net.NetworkInformation;
|
2023-09-11 14:57:57 +00:00
|
|
|
|
|
|
|
|
|
namespace KubernetesWorkflow
|
|
|
|
|
{
|
|
|
|
|
internal enum RunnerLocation
|
|
|
|
|
{
|
2023-11-07 11:42:01 +00:00
|
|
|
|
Unknown,
|
2023-09-11 14:57:57 +00:00
|
|
|
|
ExternalToCluster,
|
|
|
|
|
InternalToCluster,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal static class RunnerLocationUtils
|
|
|
|
|
{
|
2023-11-07 12:06:49 +00:00
|
|
|
|
private static RunnerLocation location = RunnerLocation.Unknown;
|
2023-09-11 14:57:57 +00:00
|
|
|
|
|
2023-11-07 12:06:49 +00:00
|
|
|
|
internal static void DetermineRunnerLocation(ILog log, PodInfo info, K8sCluster cluster)
|
2023-09-11 14:57:57 +00:00
|
|
|
|
{
|
2023-11-07 12:06:49 +00:00
|
|
|
|
if (location != RunnerLocation.Unknown) return;
|
|
|
|
|
location = PingForLocation(info, cluster);
|
|
|
|
|
log.Log("Runner location set to: " + location);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal static bool IsKnown()
|
|
|
|
|
{
|
|
|
|
|
return location != RunnerLocation.Unknown;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal static RunnerLocation GetRunnerLocation()
|
|
|
|
|
{
|
|
|
|
|
if (location == RunnerLocation.Unknown) throw new Exception("Runner location is unknown.");
|
|
|
|
|
return location;
|
2023-10-19 09:08:30 +00:00
|
|
|
|
}
|
2023-09-11 14:57:57 +00:00
|
|
|
|
|
2023-11-06 13:33:47 +00:00
|
|
|
|
private static RunnerLocation PingForLocation(PodInfo podInfo, K8sCluster cluster)
|
2023-10-19 09:08:30 +00:00
|
|
|
|
{
|
2023-11-06 13:33:47 +00:00
|
|
|
|
if (PingHost(podInfo.Ip))
|
2023-09-11 14:57:57 +00:00
|
|
|
|
{
|
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
|
|
|
|
|
2023-11-06 13:33:47 +00:00
|
|
|
|
if (PingHost(Format(cluster.HostAddress)))
|
2023-09-11 14:57:57 +00:00
|
|
|
|
{
|
2023-11-06 13:33:47 +00:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2023-11-06 13:33:47 +00:00
|
|
|
|
private static string Format(string host)
|
2023-09-11 14:57:57 +00:00
|
|
|
|
{
|
2023-11-06 13:33:47 +00:00
|
|
|
|
return host
|
2023-09-11 14:57:57 +00:00
|
|
|
|
.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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|