Wires up resource limits

This commit is contained in:
benbierens 2023-09-08 10:14:52 +02:00
parent df6da29a69
commit 195527b650
No known key found for this signature in database
GPG Key ID: FE44815D96D0A1AA
6 changed files with 73 additions and 3 deletions

View File

@ -17,6 +17,7 @@ namespace DistTestCore.Codex
public override string AppName => "codex";
public override string Image { get; }
protected override ResourceLimits ResourceLimits => new ResourceLimits(milliCPUs: 500, memory: 2.GB());
public CodexContainerRecipe()
{

View File

@ -1,5 +1,6 @@
using DistTestCore.Codex;
using Logging;
using Utils;
using static DistTestCore.Helpers.FullConnectivityHelper;
namespace DistTestCore.Helpers

View File

@ -2,10 +2,11 @@
{
public class ContainerRecipe
{
public ContainerRecipe(int number, string image, Port[] exposedPorts, Port[] internalPorts, EnvVar[] envVars, PodLabels podLabels, PodAnnotations podAnnotations, VolumeMount[] volumes, object[] additionals)
public ContainerRecipe(int number, string image, ResourceLimits resourceLimits, Port[] exposedPorts, Port[] internalPorts, EnvVar[] envVars, PodLabels podLabels, PodAnnotations podAnnotations, VolumeMount[] volumes, object[] additionals)
{
Number = number;
Image = image;
ResourceLimits = resourceLimits;
ExposedPorts = exposedPorts;
InternalPorts = internalPorts;
EnvVars = envVars;
@ -17,6 +18,7 @@
public string Name { get { return $"ctnr{Number}"; } }
public int Number { get; }
public ResourceLimits ResourceLimits { get; }
public string Image { get; }
public Port[] ExposedPorts { get; }
public Port[] InternalPorts { get; }
@ -37,6 +39,7 @@
$"exposedPorts: {string.Join(",", ExposedPorts.Select(p => p.Number))}, " +
$"internalPorts: {string.Join(",", InternalPorts.Select(p => p.Number))}, " +
$"envVars: {string.Join(",", EnvVars.Select(v => v.Name + ":" + v.Value))}, " +
$"limits: {ResourceLimits}, " +
$"volumes: {string.Join(",", Volumes.Select(v => $"'{v.MountPath}'"))}";
}
}

View File

@ -21,7 +21,7 @@ namespace KubernetesWorkflow
Initialize(config);
var recipe = new ContainerRecipe(containerNumber, Image,
var recipe = new ContainerRecipe(containerNumber, Image, ResourceLimits,
exposedPorts.ToArray(),
internalPorts.ToArray(),
envVars.ToArray(),
@ -46,6 +46,7 @@ namespace KubernetesWorkflow
public abstract string Image { get; }
protected int ContainerNumber { get; private set; } = 0;
protected int Index { get; private set; } = 0;
protected virtual ResourceLimits ResourceLimits { get; } = new ResourceLimits();
protected abstract void Initialize(StartupConfig config);
protected Port AddExposedPort(string tag = "")

View File

@ -423,10 +423,33 @@ namespace KubernetesWorkflow
ImagePullPolicy = "Always",
Ports = CreateContainerPorts(recipe),
Env = CreateEnv(recipe),
VolumeMounts = CreateContainerVolumeMounts(recipe)
VolumeMounts = CreateContainerVolumeMounts(recipe),
Resources = CreateResourceLimits(recipe)
};
}
private V1ResourceRequirements CreateResourceLimits(ContainerRecipe recipe)
{
return new V1ResourceRequirements
{
Limits = CreateResourceLimit(recipe.ResourceLimits)
};
}
private Dictionary<string, ResourceQuantity> CreateResourceLimit(ResourceLimits limits)
{
var result = new Dictionary<string, ResourceQuantity>();
if (limits.MilliCPUs != 0)
{
result.Add("cpu", new ResourceQuantity($"{limits.MilliCPUs}m"));
}
if (limits.Memory.SizeInBytes != 0)
{
result.Add("memory", new ResourceQuantity(limits.Memory.ToSuffixNotation()));
}
return result;
}
private List<V1VolumeMount> CreateContainerVolumeMounts(ContainerRecipe recipe)
{
return recipe.Volumes.Select(CreateContainerVolumeMount).ToList();

View File

@ -0,0 +1,41 @@
using Utils;
namespace KubernetesWorkflow
{
public class ResourceLimits
{
public ResourceLimits(int milliCPUs, ByteSize memory)
{
MilliCPUs = milliCPUs;
Memory = memory;
}
public ResourceLimits(int milliCPUs)
: this(milliCPUs, new ByteSize(0))
{
}
public ResourceLimits(ByteSize memory)
: this(0, memory)
{
}
public ResourceLimits()
: this(0)
{
}
public int MilliCPUs { get; }
public ByteSize Memory { get; }
public override string ToString()
{
var result = new List<string>();
if (MilliCPUs == 0) result.Add("cpu: unlimited");
else result.Add($"cpu: {MilliCPUs} milliCPUs");
if (Memory.SizeInBytes == 0) result.Add("memory: unlimited");
else result.Add($"memory: {Memory}");
return string.Join(", ", result);
}
}
}