2
0
mirror of synced 2025-01-12 17:44:08 +00:00

140 lines
4.4 KiB
C#
Raw Normal View History

2023-09-08 09:39:56 +02:00
using Utils;
namespace KubernetesWorkflow
2023-04-12 13:53:55 +02:00
{
public abstract class ContainerRecipeFactory
{
private readonly List<Port> exposedPorts = new List<Port>();
private readonly List<Port> internalPorts = new List<Port>();
private readonly List<EnvVar> envVars = new List<EnvVar>();
private readonly PodLabels podLabels = new PodLabels();
private readonly PodAnnotations podAnnotations = new PodAnnotations();
private readonly List<VolumeMount> volumeMounts = new List<VolumeMount>();
2023-04-14 12:37:05 +02:00
private readonly List<object> additionals = new List<object>();
2023-04-12 13:53:55 +02:00
private RecipeComponentFactory factory = null!;
private ContainerResources resources = new ContainerResources();
2023-04-12 13:53:55 +02:00
2023-04-14 10:51:35 +02:00
public ContainerRecipe CreateRecipe(int index, int containerNumber, RecipeComponentFactory factory, StartupConfig config)
2023-04-12 13:53:55 +02:00
{
this.factory = factory;
ContainerNumber = containerNumber;
2023-04-14 10:51:35 +02:00
Index = index;
2023-04-12 13:53:55 +02:00
Initialize(config);
var recipe = new ContainerRecipe(containerNumber, config.NameOverride, Image, resources,
exposedPorts.ToArray(),
internalPorts.ToArray(),
envVars.ToArray(),
podLabels.Clone(),
podAnnotations.Clone(),
volumeMounts.ToArray(),
2023-09-20 10:13:29 +02:00
ContainerAdditionals.CreateFromUserData(additionals));
2023-04-13 10:11:33 +02:00
exposedPorts.Clear();
internalPorts.Clear();
envVars.Clear();
podLabels.Clear();
podAnnotations.Clear();
volumeMounts.Clear();
2023-04-14 12:37:05 +02:00
additionals.Clear();
2023-04-13 10:11:33 +02:00
this.factory = null!;
resources = new ContainerResources();
2023-04-13 10:11:33 +02:00
return recipe;
2023-04-12 13:53:55 +02:00
}
2023-08-07 15:51:44 +02:00
public abstract string AppName { get; }
public abstract string Image { get; }
2023-04-12 13:53:55 +02:00
protected int ContainerNumber { get; private set; } = 0;
2023-04-14 10:51:35 +02:00
protected int Index { get; private set; } = 0;
2023-04-12 13:53:55 +02:00
protected abstract void Initialize(StartupConfig config);
2023-10-19 11:12:08 +02:00
protected Port AddExposedPort(string tag)
2023-04-12 13:53:55 +02:00
{
2023-09-15 15:52:02 +02:00
return AddExposedPort(factory.CreatePort(tag));
2023-04-12 13:53:55 +02:00
}
2023-10-19 11:12:08 +02:00
protected Port AddExposedPort(int number, string tag)
2023-08-11 09:37:30 +02:00
{
2023-09-15 15:52:02 +02:00
return AddExposedPort(factory.CreatePort(number, tag));
2023-08-11 09:37:30 +02:00
}
2023-04-13 14:36:17 +02:00
protected Port AddInternalPort(string tag = "")
2023-04-12 13:53:55 +02:00
{
2023-04-13 14:36:17 +02:00
var p = factory.CreatePort(tag);
2023-04-12 13:53:55 +02:00
internalPorts.Add(p);
return p;
}
2023-10-19 11:12:08 +02:00
protected void AddExposedPortAndVar(string name, string tag)
2023-04-12 13:53:55 +02:00
{
2023-04-13 14:36:17 +02:00
AddEnvVar(name, AddExposedPort(tag));
2023-04-12 13:53:55 +02:00
}
2023-04-13 14:36:17 +02:00
protected void AddInternalPortAndVar(string name, string tag = "")
2023-04-12 13:53:55 +02:00
{
2023-04-13 14:36:17 +02:00
AddEnvVar(name, AddInternalPort(tag));
2023-04-12 13:53:55 +02:00
}
protected void AddEnvVar(string name, string value)
{
envVars.Add(factory.CreateEnvVar(name, value));
}
protected void AddEnvVar(string name, Port value)
{
envVars.Add(factory.CreateEnvVar(name, value.Number));
}
2023-04-14 12:37:05 +02:00
protected void AddPodLabel(string name, string value)
{
podLabels.Add(name, value);
}
protected void AddPodAnnotation(string name, string value)
{
podAnnotations.Add(name, value);
}
2023-09-08 09:39:56 +02:00
protected void AddVolume(string mountPath, ByteSize volumeSize)
{
volumeMounts.Add(new VolumeMount(
$"autovolume-{Guid.NewGuid().ToString().ToLowerInvariant()}",
mountPath,
2023-09-08 09:39:56 +02:00
volumeSize.ToSuffixNotation()));
}
2023-04-14 12:37:05 +02:00
protected void Additional(object userData)
{
additionals.Add(userData);
}
2023-09-15 15:52:02 +02:00
protected void SetResourcesRequest(int milliCPUs, ByteSize memory)
{
SetResourcesRequest(new ContainerResourceSet(milliCPUs, memory));
}
protected void SetResourceLimits(int milliCPUs, ByteSize memory)
{
SetResourceLimits(new ContainerResourceSet(milliCPUs, memory));
}
protected void SetResourcesRequest(ContainerResourceSet requests)
{
resources.Requests = requests;
}
protected void SetResourceLimits(ContainerResourceSet limits)
{
resources.Limits = limits;
}
2023-09-15 15:52:02 +02:00
private Port AddExposedPort(Port port)
{
exposedPorts.Add(port);
return port;
}
2023-04-12 13:53:55 +02:00
}
}