Skip to content

Commit 9966011

Browse files
authored
feat(supervisor): add configurable resource requests (#2474)
1 parent e6586d3 commit 9966011

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

apps/supervisor/src/env.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ const Env = z.object({
8585
KUBERNETES_EPHEMERAL_STORAGE_SIZE_LIMIT: z.string().default("10Gi"),
8686
KUBERNETES_EPHEMERAL_STORAGE_SIZE_REQUEST: z.string().default("2Gi"),
8787
KUBERNETES_STRIP_IMAGE_DIGEST: BoolEnv.default(false),
88+
KUBERNETES_CPU_REQUEST_MIN_CORES: z.coerce.number().min(0).default(0),
89+
KUBERNETES_CPU_REQUEST_RATIO: z.coerce.number().min(0).max(1).default(0.75), // Ratio of CPU limit, so 0.75 = 75% of CPU limit
90+
KUBERNETES_MEMORY_REQUEST_MIN_GB: z.coerce.number().min(0).default(0),
91+
KUBERNETES_MEMORY_REQUEST_RATIO: z.coerce.number().min(0).max(1).default(1), // Ratio of memory limit, so 1 = 100% of memory limit
8892

8993
// Placement tags settings
9094
PLACEMENT_TAGS_ENABLED: BoolEnv.default(false),

apps/supervisor/src/workloadManager/kubernetes.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ export class KubernetesWorkloadManager implements WorkloadManager {
2020
private namespace = env.KUBERNETES_NAMESPACE;
2121
private placementTagProcessor: PlacementTagProcessor;
2222

23+
// Resource settings
24+
private readonly cpuRequestMinCores = env.KUBERNETES_CPU_REQUEST_MIN_CORES;
25+
private readonly cpuRequestRatio = env.KUBERNETES_CPU_REQUEST_RATIO;
26+
private readonly memoryRequestMinGb = env.KUBERNETES_MEMORY_REQUEST_MIN_GB;
27+
private readonly memoryRequestRatio = env.KUBERNETES_MEMORY_REQUEST_RATIO;
28+
2329
constructor(private opts: WorkloadManagerOptions) {
2430
this.k8s = createK8sApi();
2531
this.placementTagProcessor = new PlacementTagProcessor({
@@ -63,6 +69,10 @@ export class KubernetesWorkloadManager implements WorkloadManager {
6369
return imageRef.substring(0, atIndex);
6470
}
6571

72+
private clamp(value: number, min: number, max: number): number {
73+
return Math.min(Math.max(value, min), max);
74+
}
75+
6676
async create(opts: WorkloadManagerCreateOptions) {
6777
this.logger.log("[KubernetesWorkloadManager] Creating container", { opts });
6878

@@ -295,9 +305,16 @@ export class KubernetesWorkloadManager implements WorkloadManager {
295305
}
296306

297307
#getResourceRequestsForMachine(preset: MachinePreset): ResourceQuantities {
308+
const cpuRequest = preset.cpu * this.cpuRequestRatio;
309+
const memoryRequest = preset.memory * this.memoryRequestRatio;
310+
311+
// Clamp between min and max
312+
const clampedCpu = this.clamp(cpuRequest, this.cpuRequestMinCores, preset.cpu);
313+
const clampedMemory = this.clamp(memoryRequest, this.memoryRequestMinGb, preset.memory);
314+
298315
return {
299-
cpu: `${preset.cpu * 0.75}`,
300-
memory: `${preset.memory}G`,
316+
cpu: `${clampedCpu}`,
317+
memory: `${clampedMemory}G`,
301318
};
302319
}
303320

0 commit comments

Comments
 (0)