93 lines
2.8 KiB
C#
Raw Normal View History

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!;
public Address GetAddress(string portTag)
2023-09-11 16:57:57 +02:00
{
var containerAddress = Addresses.Single(a => a.PortTag == portTag);
if (containerAddress.IsInteral && RunningContainers.StartResult.RunnerLocation == RunnerLocation.ExternalToCluster)
2023-09-11 16:57:57 +02:00
{
throw new Exception("Attempt to access a container address created from an Internal port, " +
"while runner is located external to the cluster.");
2023-09-11 16:57:57 +02:00
}
return containerAddress.Address;
2023-09-11 16:57:57 +02:00
}
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()
{
return $"{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
}