Skip to content

Commit

Permalink
Allow configuration of the initContainer resources
Browse files Browse the repository at this point in the history
  • Loading branch information
andersonshatch committed May 10, 2024
1 parent 4ba1048 commit 51ea0e8
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 5 deletions.
8 changes: 8 additions & 0 deletions manifests/helm/build/templates/overlays/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,11 @@ spec:
value: '{{ .Values.operator.enableEarlyChaining }}'
- name: CONTRAST_INSTALL_SOURCE
value: helm
- name: CONTRAST_INITCONTAINER_CPU_REQUEST
value: '{{ .Values.operator.initContainer.resources.requests.cpu }}'
- name: CONTRAST_INITCONTAINER_CPU_LIMIT
value: '{{ .Values.operator.initContainer.resources.limits.cpu }}'
- name: CONTRAST_INITCONTAINER_MEMORY_REQUEST
value: '{{ .Values.operator.initContainer.resources.requests.memory }}'
- name: CONTRAST_INITCONTAINER_MEMORY_LIMIT
value: '{{ .Values.operator.initContainer.resources.limits.memory }}'
9 changes: 9 additions & 0 deletions manifests/helm/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ operator:
webhookConfiguration: contrast-web-hook-configuration
# Enable early chaining. Should normally be disabled unless DynaKube is used in classicStack mode.
enableEarlyChaining: false
# Resource management for the agent initContainers
initContainer:
resources:
limits:
cpu: 100m
memory: 64Mi
requests:
cpu: 100m
memory: 64Mi

clusterDefaults:
# If enabled, configure cluster-wide defaults.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,14 +186,14 @@ private V1Container CreateInitContainer(PatchingContext context,
securityContent.Capabilities.Drop ??= MergeDropCapabilities(containerSecurityContext);

// https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/#resource-requests-and-limits-of-pod-and-container
const string cpuLimit = "100m";
const string memoryLimit = "64Mi";
var (cpuRequest, memoryRequest) = _operatorOptions.initRequests;
var (cpuLimit, memoryLimit) = _operatorOptions.initLimits;

var resources = new V1ResourceRequirements();

resources.Requests ??= new Dictionary<string, ResourceQuantity>(StringComparer.Ordinal);
resources.Requests.TryAdd("cpu", new ResourceQuantity(cpuLimit));
resources.Requests.TryAdd("memory", new ResourceQuantity(memoryLimit));
resources.Requests.TryAdd("cpu", new ResourceQuantity(cpuRequest));
resources.Requests.TryAdd("memory", new ResourceQuantity(memoryRequest));

resources.Limits ??= new Dictionary<string, ResourceQuantity>(StringComparer.Ordinal);
resources.Limits.TryAdd("cpu", new ResourceQuantity(cpuLimit));
Expand Down
32 changes: 31 additions & 1 deletion src/Contrast.K8s.AgentOperator/Modules/OptionsModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,34 @@ protected override void Load(ContainerBuilder builder)
chaosPercent = parsedChaosPercent;
}

var @cpuRequest = "100m";
var @cpuLimit = "100m";
if (GetEnvironmentVariableAsString("CONTRAST_INITCONTAINER_CPU_REQUEST", out var cpuRequestStr))
{
logger.LogOptionValue("initcontainer-cpu-request", @cpuRequest, cpuRequestStr);
@cpuRequest = cpuRequestStr;
}

if (GetEnvironmentVariableAsString("CONTRAST_INITCONTAINER_CPU_LIMIT", out var cpuLimitStr))
{
logger.LogOptionValue("initcontainer-cpu-limit", @cpuLimit, cpuLimitStr);
@cpuLimit = cpuLimitStr;
}

var @memoryLimit = "64Mi";
var @memoryRequest = "64Mi";
if (GetEnvironmentVariableAsString("CONTRAST_INITCONTAINER_MEMORY_REQUEST", out var memoryRequestStr))
{
logger.LogOptionValue("initcontainer-memory-request", @memoryRequest, memoryRequestStr);
@memoryRequest = memoryRequestStr;
}

if (GetEnvironmentVariableAsString("CONTRAST_INITCONTAINER_MEMORY_LIMIT", out var memoryLimitStr))
{
logger.LogOptionValue("initcontainer-memory-limit", @memoryLimit, memoryLimitStr);
@memoryLimit = memoryLimitStr;
}

return new OperatorOptions(
@namespace,
settleDuration,
Expand All @@ -99,7 +127,9 @@ protected override void Load(ContainerBuilder builder)
eventQueueMergeWindowSeconds,
runInitContainersAsNonRoot,
suppressSeccompProfile,
chaosPercent / 100m
chaosPercent / 100m,
(cpuRequest, memoryRequest),
(cpuLimit, memoryLimit)
);
}).SingleInstance();

Expand Down
2 changes: 2 additions & 0 deletions src/Contrast.K8s.AgentOperator/Options/OperatorOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ public record OperatorOptions(string Namespace,
bool RunInitContainersAsNonRoot,
bool SuppressSeccompProfile,
decimal ChaosRatio,
(string cpuRequest, string memoryRequest) initRequests,
(string cpuLimit, string memoryLimit) initLimits,
string FieldManagerName = "agents.contrastsecurity.com");

0 comments on commit 51ea0e8

Please sign in to comment.