2023-09-08 07:39:56 +00:00
|
|
|
|
using Utils;
|
|
|
|
|
|
2023-11-12 09:07:23 +00:00
|
|
|
|
namespace KubernetesWorkflow.Recipe
|
2023-04-12 11:53:55 +00: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>();
|
2023-09-04 07:08:34 +00:00
|
|
|
|
private readonly PodLabels podLabels = new PodLabels();
|
|
|
|
|
private readonly PodAnnotations podAnnotations = new PodAnnotations();
|
2023-09-07 06:19:19 +00:00
|
|
|
|
private readonly List<VolumeMount> volumeMounts = new List<VolumeMount>();
|
2023-04-14 10:37:05 +00:00
|
|
|
|
private readonly List<object> additionals = new List<object>();
|
2023-04-12 11:53:55 +00:00
|
|
|
|
private RecipeComponentFactory factory = null!;
|
2023-09-18 13:45:21 +00:00
|
|
|
|
private ContainerResources resources = new ContainerResources();
|
2023-11-14 09:16:00 +00:00
|
|
|
|
private SchedulingAffinity schedulingAffinity = new SchedulingAffinity();
|
2024-03-13 09:01:14 +00:00
|
|
|
|
private CommandOverride commandOverride = new CommandOverride();
|
2023-11-23 13:50:54 +00:00
|
|
|
|
private bool setCriticalPriority;
|
2023-04-12 11:53:55 +00:00
|
|
|
|
|
2023-04-14 08:51:35 +00:00
|
|
|
|
public ContainerRecipe CreateRecipe(int index, int containerNumber, RecipeComponentFactory factory, StartupConfig config)
|
2023-04-12 11:53:55 +00:00
|
|
|
|
{
|
|
|
|
|
this.factory = factory;
|
|
|
|
|
ContainerNumber = containerNumber;
|
2023-04-14 08:51:35 +00:00
|
|
|
|
Index = index;
|
2023-04-12 11:53:55 +00:00
|
|
|
|
|
|
|
|
|
Initialize(config);
|
|
|
|
|
|
2024-03-13 09:01:14 +00:00
|
|
|
|
var recipe = new ContainerRecipe(containerNumber, config.NameOverride, Image, resources, schedulingAffinity, commandOverride, setCriticalPriority,
|
2023-09-04 07:08:34 +00:00
|
|
|
|
exposedPorts.ToArray(),
|
2023-09-07 06:19:19 +00:00
|
|
|
|
internalPorts.ToArray(),
|
|
|
|
|
envVars.ToArray(),
|
2023-09-04 07:08:34 +00:00
|
|
|
|
podLabels.Clone(),
|
|
|
|
|
podAnnotations.Clone(),
|
2023-09-07 06:19:19 +00:00
|
|
|
|
volumeMounts.ToArray(),
|
2023-09-20 08:13:29 +00:00
|
|
|
|
ContainerAdditionals.CreateFromUserData(additionals));
|
2023-04-13 08:11:33 +00:00
|
|
|
|
|
|
|
|
|
exposedPorts.Clear();
|
|
|
|
|
internalPorts.Clear();
|
|
|
|
|
envVars.Clear();
|
2023-09-04 07:08:34 +00:00
|
|
|
|
podLabels.Clear();
|
|
|
|
|
podAnnotations.Clear();
|
2023-09-07 06:19:19 +00:00
|
|
|
|
volumeMounts.Clear();
|
2023-04-14 10:37:05 +00:00
|
|
|
|
additionals.Clear();
|
2023-04-13 08:11:33 +00:00
|
|
|
|
this.factory = null!;
|
2023-09-18 13:45:21 +00:00
|
|
|
|
resources = new ContainerResources();
|
2023-11-14 09:16:00 +00:00
|
|
|
|
schedulingAffinity = new SchedulingAffinity();
|
2024-03-13 09:01:14 +00:00
|
|
|
|
commandOverride = new CommandOverride();
|
2023-11-23 13:50:54 +00:00
|
|
|
|
setCriticalPriority = false;
|
2023-04-13 08:11:33 +00:00
|
|
|
|
|
|
|
|
|
return recipe;
|
2023-04-12 11:53:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-07 13:51:44 +00:00
|
|
|
|
public abstract string AppName { get; }
|
2023-07-17 09:12:14 +00:00
|
|
|
|
public abstract string Image { get; }
|
2023-04-12 11:53:55 +00:00
|
|
|
|
protected int ContainerNumber { get; private set; } = 0;
|
2023-04-14 08:51:35 +00:00
|
|
|
|
protected int Index { get; private set; } = 0;
|
2023-04-12 11:53:55 +00:00
|
|
|
|
protected abstract void Initialize(StartupConfig config);
|
|
|
|
|
|
2023-10-25 12:23:07 +00:00
|
|
|
|
protected Port AddExposedPort(string tag, PortProtocol protocol = PortProtocol.TCP)
|
2023-04-12 11:53:55 +00:00
|
|
|
|
{
|
2023-11-13 14:27:52 +00:00
|
|
|
|
return AddExposedPort(factory.CreateExternalPort(tag, protocol));
|
2023-04-12 11:53:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-10-25 12:23:07 +00:00
|
|
|
|
protected Port AddExposedPort(int number, string tag, PortProtocol protocol = PortProtocol.TCP)
|
2023-08-11 07:37:30 +00:00
|
|
|
|
{
|
2023-11-14 09:49:14 +00:00
|
|
|
|
return AddExposedPort(factory.CreateExternalPort(number, tag, protocol));
|
2023-08-11 07:37:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-10-25 12:23:07 +00:00
|
|
|
|
protected Port AddInternalPort(string tag = "", PortProtocol protocol = PortProtocol.TCP)
|
2023-04-12 11:53:55 +00:00
|
|
|
|
{
|
2023-11-13 14:27:52 +00:00
|
|
|
|
var p = factory.CreateInternalPort(tag, protocol);
|
2023-04-12 11:53:55 +00:00
|
|
|
|
internalPorts.Add(p);
|
|
|
|
|
return p;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-25 12:23:07 +00:00
|
|
|
|
protected void AddExposedPortAndVar(string name, string tag, PortProtocol protocol = PortProtocol.TCP)
|
2023-04-12 11:53:55 +00:00
|
|
|
|
{
|
2023-10-25 12:23:07 +00:00
|
|
|
|
AddEnvVar(name, AddExposedPort(tag, protocol));
|
2023-04-12 11:53:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-10-25 12:23:07 +00:00
|
|
|
|
protected void AddInternalPortAndVar(string name, string tag = "", PortProtocol protocol = PortProtocol.TCP)
|
2023-04-12 11:53:55 +00:00
|
|
|
|
{
|
2023-10-25 12:23:07 +00:00
|
|
|
|
AddEnvVar(name, AddInternalPort(tag, protocol));
|
2023-04-12 11:53:55 +00: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 10:37:05 +00:00
|
|
|
|
|
2023-09-04 07:08:34 +00:00
|
|
|
|
protected void AddPodLabel(string name, string value)
|
|
|
|
|
{
|
|
|
|
|
podLabels.Add(name, value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void AddPodAnnotation(string name, string value)
|
|
|
|
|
{
|
|
|
|
|
podAnnotations.Add(name, value);
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-31 13:20:50 +00:00
|
|
|
|
protected void AddVolume(string name, string mountPath, string? subPath = null, string? secret = null, string? hostPath = null)
|
2023-10-31 10:38:54 +00:00
|
|
|
|
{
|
2023-10-31 13:20:50 +00:00
|
|
|
|
var size = 10.MB().ToSuffixNotation();
|
|
|
|
|
volumeMounts.Add(new VolumeMount(name, mountPath, subPath, size, secret, hostPath));
|
2023-10-31 10:38:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-09-08 07:39:56 +00:00
|
|
|
|
protected void AddVolume(string mountPath, ByteSize volumeSize)
|
2023-09-07 06:19:19 +00:00
|
|
|
|
{
|
|
|
|
|
volumeMounts.Add(new VolumeMount(
|
|
|
|
|
$"autovolume-{Guid.NewGuid().ToString().ToLowerInvariant()}",
|
2023-09-07 08:37:52 +00:00
|
|
|
|
mountPath,
|
2023-10-31 10:38:54 +00:00
|
|
|
|
resourceQuantity: volumeSize.ToSuffixNotation()));
|
2023-09-07 06:19:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-14 10:37:05 +00:00
|
|
|
|
protected void Additional(object userData)
|
|
|
|
|
{
|
|
|
|
|
additionals.Add(userData);
|
|
|
|
|
}
|
2023-09-15 13:52:02 +00:00
|
|
|
|
|
2023-09-18 13:45:21 +00:00
|
|
|
|
protected void SetResourcesRequest(int milliCPUs, ByteSize memory)
|
|
|
|
|
{
|
|
|
|
|
SetResourcesRequest(new ContainerResourceSet(milliCPUs, memory));
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-14 09:16:00 +00:00
|
|
|
|
protected void SetSchedulingAffinity(string notIn)
|
|
|
|
|
{
|
|
|
|
|
schedulingAffinity = new SchedulingAffinity(notIn);
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-13 09:01:14 +00:00
|
|
|
|
protected void OverrideCommand(params string[] command)
|
|
|
|
|
{
|
|
|
|
|
commandOverride = new CommandOverride(command);
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-23 13:50:54 +00:00
|
|
|
|
protected void SetSystemCriticalPriority()
|
|
|
|
|
{
|
|
|
|
|
setCriticalPriority = true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-07 08:25:51 +00:00
|
|
|
|
// Disabled following a possible bug in the k8s cluster that will throttle containers much more than is
|
|
|
|
|
// called for if they have resource limits defined.
|
|
|
|
|
//protected void SetResourceLimits(int milliCPUs, ByteSize memory)
|
|
|
|
|
//{
|
|
|
|
|
// SetResourceLimits(new ContainerResourceSet(milliCPUs, memory));
|
|
|
|
|
//}
|
2023-09-18 13:45:21 +00:00
|
|
|
|
|
|
|
|
|
protected void SetResourcesRequest(ContainerResourceSet requests)
|
|
|
|
|
{
|
|
|
|
|
resources.Requests = requests;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void SetResourceLimits(ContainerResourceSet limits)
|
|
|
|
|
{
|
|
|
|
|
resources.Limits = limits;
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-15 13:52:02 +00:00
|
|
|
|
private Port AddExposedPort(Port port)
|
|
|
|
|
{
|
|
|
|
|
exposedPorts.Add(port);
|
|
|
|
|
return port;
|
|
|
|
|
}
|
2023-04-12 11:53:55 +00:00
|
|
|
|
}
|
|
|
|
|
}
|