Skip to content

Commit 38e3e12

Browse files
committed
feat(supervisor): add per-machine-preset resource request ratios
Adds support to configure CPU/memory request ratios per machine preset. Falls back to the global request ratio configs if no specific override is specified. Runs across different machine presets have different usage patters, so this enables use to manage the available capacity better.
1 parent 72594a4 commit 38e3e12

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

apps/supervisor/src/env.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,25 @@ const Env = z.object({
8989
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
9090
KUBERNETES_MEMORY_REQUEST_MIN_GB: z.coerce.number().min(0).default(0),
9191
KUBERNETES_MEMORY_REQUEST_RATIO: z.coerce.number().min(0).max(1).default(1), // Ratio of memory limit, so 1 = 100% of memory limit
92+
93+
// Per-preset overrides of the global KUBERNETES_CPU_REQUEST_RATIO
94+
KUBERNETES_CPU_REQUEST_RATIO_MICRO: z.coerce.number().min(0).max(1).optional(),
95+
KUBERNETES_CPU_REQUEST_RATIO_SMALL_1X: z.coerce.number().min(0).max(1).optional(),
96+
KUBERNETES_CPU_REQUEST_RATIO_SMALL_2X: z.coerce.number().min(0).max(1).optional(),
97+
KUBERNETES_CPU_REQUEST_RATIO_MEDIUM_1X: z.coerce.number().min(0).max(1).optional(),
98+
KUBERNETES_CPU_REQUEST_RATIO_MEDIUM_2X: z.coerce.number().min(0).max(1).optional(),
99+
KUBERNETES_CPU_REQUEST_RATIO_LARGE_1X: z.coerce.number().min(0).max(1).optional(),
100+
KUBERNETES_CPU_REQUEST_RATIO_LARGE_2X: z.coerce.number().min(0).max(1).optional(),
101+
102+
// Per-preset overrides of the global KUBERNETES_MEMORY_REQUEST_RATIO
103+
KUBERNETES_MEMORY_REQUEST_RATIO_MICRO: z.coerce.number().min(0).max(1).optional(),
104+
KUBERNETES_MEMORY_REQUEST_RATIO_SMALL_1X: z.coerce.number().min(0).max(1).optional(),
105+
KUBERNETES_MEMORY_REQUEST_RATIO_SMALL_2X: z.coerce.number().min(0).max(1).optional(),
106+
KUBERNETES_MEMORY_REQUEST_RATIO_MEDIUM_1X: z.coerce.number().min(0).max(1).optional(),
107+
KUBERNETES_MEMORY_REQUEST_RATIO_MEDIUM_2X: z.coerce.number().min(0).max(1).optional(),
108+
KUBERNETES_MEMORY_REQUEST_RATIO_LARGE_1X: z.coerce.number().min(0).max(1).optional(),
109+
KUBERNETES_MEMORY_REQUEST_RATIO_LARGE_2X: z.coerce.number().min(0).max(1).optional(),
110+
92111
KUBERNETES_MEMORY_OVERHEAD_GB: z.coerce.number().min(0).optional(), // Optional memory overhead to add to the limit in GB
93112
KUBERNETES_SCHEDULER_NAME: z.string().optional(), // Custom scheduler name for pods
94113
KUBERNETES_LARGE_MACHINE_POOL_LABEL: z.string().optional(), // if set, large-* presets affinity for machinepool=<value>

apps/supervisor/src/workloadManager/kubernetes.ts

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ import {
44
type WorkloadManagerCreateOptions,
55
type WorkloadManagerOptions,
66
} from "./types.js";
7-
import type { EnvironmentType, MachinePreset, PlacementTag } from "@trigger.dev/core/v3";
7+
import type {
8+
EnvironmentType,
9+
MachinePreset,
10+
MachinePresetName,
11+
PlacementTag,
12+
} from "@trigger.dev/core/v3";
813
import { PlacementTagProcessor } from "@trigger.dev/core/v3/serverOnly";
914
import { env } from "../env.js";
1015
import { type K8sApi, createK8sApi, type k8s } from "../clients/kubernetes.js";
@@ -14,6 +19,26 @@ type ResourceQuantities = {
1419
[K in "cpu" | "memory" | "ephemeral-storage"]?: string;
1520
};
1621

22+
const cpuRequestRatioByMachinePreset: Record<MachinePresetName, number | undefined> = {
23+
micro: env.KUBERNETES_CPU_REQUEST_RATIO_MICRO,
24+
"small-1x": env.KUBERNETES_CPU_REQUEST_RATIO_SMALL_1X,
25+
"small-2x": env.KUBERNETES_CPU_REQUEST_RATIO_SMALL_2X,
26+
"medium-1x": env.KUBERNETES_CPU_REQUEST_RATIO_MEDIUM_1X,
27+
"medium-2x": env.KUBERNETES_CPU_REQUEST_RATIO_MEDIUM_2X,
28+
"large-1x": env.KUBERNETES_CPU_REQUEST_RATIO_LARGE_1X,
29+
"large-2x": env.KUBERNETES_CPU_REQUEST_RATIO_LARGE_2X,
30+
};
31+
32+
const memoryRequestRatioByMachinePreset: Record<MachinePresetName, number | undefined> = {
33+
micro: env.KUBERNETES_MEMORY_REQUEST_RATIO_MICRO,
34+
"small-1x": env.KUBERNETES_MEMORY_REQUEST_RATIO_SMALL_1X,
35+
"small-2x": env.KUBERNETES_MEMORY_REQUEST_RATIO_SMALL_2X,
36+
"medium-1x": env.KUBERNETES_MEMORY_REQUEST_RATIO_MEDIUM_1X,
37+
"medium-2x": env.KUBERNETES_MEMORY_REQUEST_RATIO_MEDIUM_2X,
38+
"large-1x": env.KUBERNETES_MEMORY_REQUEST_RATIO_LARGE_1X,
39+
"large-2x": env.KUBERNETES_MEMORY_REQUEST_RATIO_LARGE_2X,
40+
};
41+
1742
export class KubernetesWorkloadManager implements WorkloadManager {
1843
private readonly logger = new SimpleStructuredLogger("kubernetes-workload-provider");
1944
private k8s: K8sApi;
@@ -321,8 +346,11 @@ export class KubernetesWorkloadManager implements WorkloadManager {
321346
}
322347

323348
#getResourceRequestsForMachine(preset: MachinePreset): ResourceQuantities {
324-
const cpuRequest = preset.cpu * this.cpuRequestRatio;
325-
const memoryRequest = preset.memory * this.memoryRequestRatio;
349+
const cpuRatio = cpuRequestRatioByMachinePreset[preset.name] ?? this.cpuRequestRatio;
350+
const memoryRatio = memoryRequestRatioByMachinePreset[preset.name] ?? this.memoryRequestRatio;
351+
352+
const cpuRequest = preset.cpu * cpuRatio;
353+
const memoryRequest = preset.memory * memoryRatio;
326354

327355
// Clamp between min and max
328356
const clampedCpu = this.clamp(cpuRequest, this.cpuRequestMinCores, preset.cpu);

0 commit comments

Comments
 (0)