2
0
mirror of synced 2025-01-12 09:34:40 +00:00

114 lines
3.5 KiB
C#
Raw Normal View History

2023-11-07 12:02:17 +01:00
using Logging;
using Newtonsoft.Json;
using Utils;
namespace KubernetesWorkflow
2023-04-12 13:53:55 +02:00
{
public class RunningContainers
{
public RunningContainers(StartupConfig startupConfig, StartResult startResult, RunningContainer[] containers)
2023-04-12 13:53:55 +02:00
{
StartupConfig = startupConfig;
StartResult = startResult;
2023-04-12 13:53:55 +02:00
Containers = containers;
foreach (var c in containers) c.RunningContainers = this;
2023-04-12 13:53:55 +02:00
}
public StartupConfig StartupConfig { get; }
public StartResult StartResult { get; }
2023-04-12 13:53:55 +02:00
public RunningContainer[] Containers { get; }
2023-04-13 14:36:17 +02:00
[JsonIgnore]
public string Name
{
get { return $"{Containers.Length}x '{Containers.First().Name}'"; }
}
2023-04-13 14:36:17 +02:00
public string Describe()
{
return string.Join(",", Containers.Select(c => c.Name));
2023-04-13 14:36:17 +02:00
}
2023-04-12 13:53:55 +02:00
}
public class RunningContainer
{
public RunningContainer(string name, ContainerRecipe recipe, ContainerAddress[] addresses)
2023-04-12 13:53:55 +02:00
{
Name = name;
Recipe = recipe;
Addresses = addresses;
2023-04-12 13:53:55 +02:00
}
public string Name { get; }
2023-04-12 13:53:55 +02:00
public ContainerRecipe Recipe { get; }
public ContainerAddress[] Addresses { get; }
2023-08-15 11:01:18 +02:00
[JsonIgnore]
public RunningContainers RunningContainers { get; internal set; } = null!;
2023-11-07 12:02:17 +01:00
public Address GetAddress(ILog log, string portTag)
2023-09-11 16:57:57 +02:00
{
var addresses = Addresses.Where(a => a.PortTag == portTag).ToArray();
if (!addresses.Any()) throw new Exception("No addresses found for portTag: " + portTag);
var select = SelectAddress(addresses);
2023-11-07 12:02:17 +01:00
log.Log($"Container '{Name}' selected for tag '{portTag}' address: '{select}'");
return select.Address;
2023-09-11 16:57:57 +02:00
}
2023-11-06 16:10:19 +01:00
public Address GetInternalAddress(string portTag)
{
var containerAddress = Addresses.Single(a => a.PortTag == portTag && a.IsInteral);
2023-11-06 16:10:19 +01:00
return containerAddress.Address;
}
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 13:53:55 +02:00
}
public class ContainerAddress
{
public ContainerAddress(string portTag, Address address, bool isInteral)
{
PortTag = portTag;
Address = address;
IsInteral = isInteral;
}
public string PortTag { get; }
public Address Address { get; }
public bool IsInteral { get; }
public override string ToString()
{
2023-11-07 12:02:17 +01:00
var indicator = IsInteral ? "int" : "ext";
return $"{indicator} {PortTag} -> '{Address}'";
}
}
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 13:53:55 +02:00
}