Allows container resources to be conditional in container recipes.

This commit is contained in:
ThatBen 2023-09-18 15:45:21 +02:00
parent 3d4370b154
commit 825200b386
2 changed files with 26 additions and 5 deletions

View File

@ -20,13 +20,13 @@ namespace CodexPlugin
public CodexContainerRecipe() public CodexContainerRecipe()
{ {
Image = GetDockerImage(); Image = GetDockerImage();
//Resources.Requests = new ContainerResourceSet(milliCPUs: 1000, memory: 6.GB());
//Resources.Limits = new ContainerResourceSet(milliCPUs: 4000, memory: 12.GB());
} }
protected override void Initialize(StartupConfig startupConfig) protected override void Initialize(StartupConfig startupConfig)
{ {
SetResourcesRequest(milliCPUs: 1000, memory: 6.GB());
SetResourceLimits(milliCPUs: 4000, memory: 12.GB());
var config = startupConfig.Get<CodexStartupConfig>(); var config = startupConfig.Get<CodexStartupConfig>();
AddExposedPortAndVar("CODEX_API_PORT"); AddExposedPortAndVar("CODEX_API_PORT");

View File

@ -12,6 +12,7 @@ namespace KubernetesWorkflow
private readonly List<VolumeMount> volumeMounts = new List<VolumeMount>(); private readonly List<VolumeMount> volumeMounts = new List<VolumeMount>();
private readonly List<object> additionals = new List<object>(); private readonly List<object> additionals = new List<object>();
private RecipeComponentFactory factory = null!; private RecipeComponentFactory factory = null!;
private ContainerResources resources = new ContainerResources();
public ContainerRecipe CreateRecipe(int index, int containerNumber, RecipeComponentFactory factory, StartupConfig config) public ContainerRecipe CreateRecipe(int index, int containerNumber, RecipeComponentFactory factory, StartupConfig config)
{ {
@ -21,7 +22,7 @@ namespace KubernetesWorkflow
Initialize(config); Initialize(config);
var recipe = new ContainerRecipe(containerNumber, config.NameOverride, Image, Resources, var recipe = new ContainerRecipe(containerNumber, config.NameOverride, Image, resources,
exposedPorts.ToArray(), exposedPorts.ToArray(),
internalPorts.ToArray(), internalPorts.ToArray(),
envVars.ToArray(), envVars.ToArray(),
@ -38,13 +39,13 @@ namespace KubernetesWorkflow
volumeMounts.Clear(); volumeMounts.Clear();
additionals.Clear(); additionals.Clear();
this.factory = null!; this.factory = null!;
resources = new ContainerResources();
return recipe; return recipe;
} }
public abstract string AppName { get; } public abstract string AppName { get; }
public abstract string Image { get; } public abstract string Image { get; }
public ContainerResources Resources { get; } = new ContainerResources();
protected int ContainerNumber { get; private set; } = 0; protected int ContainerNumber { get; private set; } = 0;
protected int Index { get; private set; } = 0; protected int Index { get; private set; } = 0;
protected abstract void Initialize(StartupConfig config); protected abstract void Initialize(StartupConfig config);
@ -109,6 +110,26 @@ namespace KubernetesWorkflow
additionals.Add(userData); additionals.Add(userData);
} }
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;
}
private Port AddExposedPort(Port port) private Port AddExposedPort(Port port)
{ {
if (exposedPorts.Any()) if (exposedPorts.Any())