From 825200b3866ada645cd3662fc1b040f93d01f11e Mon Sep 17 00:00:00 2001 From: ThatBen Date: Mon, 18 Sep 2023 15:45:21 +0200 Subject: [PATCH] Allows container resources to be conditional in container recipes. --- CodexPlugin/CodexContainerRecipe.cs | 6 ++--- KubernetesWorkflow/ContainerRecipeFactory.cs | 25 ++++++++++++++++++-- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/CodexPlugin/CodexContainerRecipe.cs b/CodexPlugin/CodexContainerRecipe.cs index 15311c3..2225d59 100644 --- a/CodexPlugin/CodexContainerRecipe.cs +++ b/CodexPlugin/CodexContainerRecipe.cs @@ -20,13 +20,13 @@ namespace CodexPlugin public CodexContainerRecipe() { 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) { + SetResourcesRequest(milliCPUs: 1000, memory: 6.GB()); + SetResourceLimits(milliCPUs: 4000, memory: 12.GB()); + var config = startupConfig.Get(); AddExposedPortAndVar("CODEX_API_PORT"); diff --git a/KubernetesWorkflow/ContainerRecipeFactory.cs b/KubernetesWorkflow/ContainerRecipeFactory.cs index bfd0919..d606598 100644 --- a/KubernetesWorkflow/ContainerRecipeFactory.cs +++ b/KubernetesWorkflow/ContainerRecipeFactory.cs @@ -12,6 +12,7 @@ namespace KubernetesWorkflow private readonly List volumeMounts = new List(); private readonly List additionals = new List(); private RecipeComponentFactory factory = null!; + private ContainerResources resources = new ContainerResources(); public ContainerRecipe CreateRecipe(int index, int containerNumber, RecipeComponentFactory factory, StartupConfig config) { @@ -21,7 +22,7 @@ namespace KubernetesWorkflow Initialize(config); - var recipe = new ContainerRecipe(containerNumber, config.NameOverride, Image, Resources, + var recipe = new ContainerRecipe(containerNumber, config.NameOverride, Image, resources, exposedPorts.ToArray(), internalPorts.ToArray(), envVars.ToArray(), @@ -38,13 +39,13 @@ namespace KubernetesWorkflow volumeMounts.Clear(); additionals.Clear(); this.factory = null!; + resources = new ContainerResources(); return recipe; } public abstract string AppName { get; } public abstract string Image { get; } - public ContainerResources Resources { get; } = new ContainerResources(); protected int ContainerNumber { get; private set; } = 0; protected int Index { get; private set; } = 0; protected abstract void Initialize(StartupConfig config); @@ -109,6 +110,26 @@ namespace KubernetesWorkflow 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) { if (exposedPorts.Any())