2023-11-07 11:02:17 +00:00
|
|
|
|
using Logging;
|
|
|
|
|
using Newtonsoft.Json;
|
2023-11-06 13:33:47 +00:00
|
|
|
|
using Utils;
|
2023-06-21 06:28:40 +00:00
|
|
|
|
|
|
|
|
|
namespace KubernetesWorkflow
|
2023-04-12 11:53:55 +00:00
|
|
|
|
{
|
|
|
|
|
public class RunningContainers
|
|
|
|
|
{
|
2023-11-06 13:33:47 +00:00
|
|
|
|
public RunningContainers(StartupConfig startupConfig, StartResult startResult, RunningContainer[] containers)
|
2023-04-12 11:53:55 +00:00
|
|
|
|
{
|
|
|
|
|
StartupConfig = startupConfig;
|
2023-11-06 13:33:47 +00:00
|
|
|
|
StartResult = startResult;
|
2023-04-12 11:53:55 +00:00
|
|
|
|
Containers = containers;
|
2023-11-06 13:33:47 +00:00
|
|
|
|
|
|
|
|
|
foreach (var c in containers) c.RunningContainers = this;
|
2023-04-12 11:53:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public StartupConfig StartupConfig { get; }
|
2023-11-06 13:33:47 +00:00
|
|
|
|
public StartResult StartResult { get; }
|
2023-04-12 11:53:55 +00:00
|
|
|
|
public RunningContainer[] Containers { get; }
|
2023-04-13 12:36:17 +00:00
|
|
|
|
|
2023-11-06 13:33:47 +00:00
|
|
|
|
[JsonIgnore]
|
|
|
|
|
public string Name
|
|
|
|
|
{
|
|
|
|
|
get { return $"{Containers.Length}x '{Containers.First().Name}'"; }
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-13 12:36:17 +00:00
|
|
|
|
public string Describe()
|
|
|
|
|
{
|
2023-04-30 08:56:19 +00:00
|
|
|
|
return string.Join(",", Containers.Select(c => c.Name));
|
2023-04-13 12:36:17 +00:00
|
|
|
|
}
|
2023-04-12 11:53:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class RunningContainer
|
|
|
|
|
{
|
2023-11-06 13:33:47 +00:00
|
|
|
|
public RunningContainer(string name, ContainerRecipe recipe, ContainerAddress[] addresses)
|
2023-04-12 11:53:55 +00:00
|
|
|
|
{
|
2023-06-23 08:35:23 +00:00
|
|
|
|
Name = name;
|
2023-11-06 13:33:47 +00:00
|
|
|
|
Recipe = recipe;
|
|
|
|
|
Addresses = addresses;
|
2023-04-12 11:53:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-30 08:56:19 +00:00
|
|
|
|
public string Name { get; }
|
2023-04-12 11:53:55 +00:00
|
|
|
|
public ContainerRecipe Recipe { get; }
|
2023-11-06 13:33:47 +00:00
|
|
|
|
public ContainerAddress[] Addresses { get; }
|
2023-08-15 09:01:18 +00:00
|
|
|
|
|
2023-11-06 13:33:47 +00:00
|
|
|
|
[JsonIgnore]
|
|
|
|
|
public RunningContainers RunningContainers { get; internal set; } = null!;
|
2023-10-23 13:28:20 +00:00
|
|
|
|
|
2023-11-07 11:02:17 +00:00
|
|
|
|
public Address GetAddress(ILog log, string portTag)
|
2023-09-11 14:57:57 +00:00
|
|
|
|
{
|
2023-11-06 15:38:32 +00:00
|
|
|
|
var addresses = Addresses.Where(a => a.PortTag == portTag).ToArray();
|
|
|
|
|
if (!addresses.Any()) throw new Exception("No addresses found for portTag: " + portTag);
|
|
|
|
|
|
2023-11-07 12:06:49 +00:00
|
|
|
|
var select = SelectAddress(addresses);
|
2023-11-07 11:02:17 +00:00
|
|
|
|
log.Log($"Container '{Name}' selected for tag '{portTag}' address: '{select}'");
|
|
|
|
|
return select.Address;
|
2023-09-11 14:57:57 +00:00
|
|
|
|
}
|
2023-11-06 15:10:19 +00:00
|
|
|
|
|
|
|
|
|
public Address GetInternalAddress(string portTag)
|
|
|
|
|
{
|
2023-11-06 15:52:55 +00:00
|
|
|
|
var containerAddress = Addresses.Single(a => a.PortTag == portTag && a.IsInteral);
|
2023-11-06 15:10:19 +00:00
|
|
|
|
return containerAddress.Address;
|
|
|
|
|
}
|
2023-11-07 12:06:49 +00:00
|
|
|
|
|
|
|
|
|
private ContainerAddress SelectAddress(ContainerAddress[] addresses)
|
|
|
|
|
{
|
|
|
|
|
var location = RunnerLocationUtils.GetRunnerLocation();
|
|
|
|
|
if (location == RunnerLocation.InternalToCluster)
|
|
|
|
|
{
|
|
|
|
|
return addresses.Single(a => a.IsInteral);
|
|
|
|
|
}
|
|
|
|
|
if (location == RunnerLocation.ExternalToCluster)
|
|
|
|
|
{
|
|
|
|
|
return addresses.Single(a => !a.IsInteral);
|
|
|
|
|
}
|
|
|
|
|
throw new Exception("Running location not known.");
|
|
|
|
|
}
|
2023-04-12 11:53:55 +00:00
|
|
|
|
}
|
2023-08-07 08:44:48 +00:00
|
|
|
|
|
2023-11-06 13:33:47 +00:00
|
|
|
|
public class ContainerAddress
|
2023-10-19 09:08:30 +00:00
|
|
|
|
{
|
2023-11-06 13:33:47 +00:00
|
|
|
|
public ContainerAddress(string portTag, Address address, bool isInteral)
|
2023-10-19 09:08:30 +00:00
|
|
|
|
{
|
2023-11-06 13:33:47 +00:00
|
|
|
|
PortTag = portTag;
|
|
|
|
|
Address = address;
|
|
|
|
|
IsInteral = isInteral;
|
2023-10-19 09:08:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-06 13:33:47 +00:00
|
|
|
|
public string PortTag { get; }
|
|
|
|
|
public Address Address { get; }
|
|
|
|
|
public bool IsInteral { get; }
|
|
|
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
2023-11-07 11:02:17 +00:00
|
|
|
|
var indicator = IsInteral ? "int" : "ext";
|
|
|
|
|
return $"{indicator} {PortTag} -> '{Address}'";
|
2023-11-06 13:33:47 +00:00
|
|
|
|
}
|
2023-10-19 09:08:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-07 08:44:48 +00:00
|
|
|
|
public static class RunningContainersExtensions
|
|
|
|
|
{
|
|
|
|
|
public static RunningContainer[] Containers(this RunningContainers[] runningContainers)
|
|
|
|
|
{
|
|
|
|
|
return runningContainers.SelectMany(c => c.Containers).ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string Describe(this RunningContainers[] runningContainers)
|
|
|
|
|
{
|
|
|
|
|
return string.Join(",", runningContainers.Select(c => c.Describe()));
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-12 11:53:55 +00:00
|
|
|
|
}
|