Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions apps/kubernetes-provider/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -545,8 +545,8 @@ class KubernetesTaskOperations implements TaskOperations {

#getResourceRequestsForMachine(preset: MachinePreset): ResourceQuantities {
return {
cpu: `${preset.cpu * 0.75}`,
memory: `${preset.memory}G`,
cpu: `${preset.cpuRequest ?? preset.cpu * 0.75}`,
memory: `${preset.memoryRequest ?? preset.memory}G`,
};
}
Comment on lines 546 to 551
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Clamp requests to limits (mirror supervisor change)

Prevents admission failures when overrides set requests above limits.

   #getResourceRequestsForMachine(preset: MachinePreset): ResourceQuantities {
-    return {
-      cpu: `${preset.cpuRequest ?? preset.cpu * 0.75}`,
-      memory: `${preset.memoryRequest ?? preset.memory}G`,
-    };
+    const cpuReq = Math.min(Math.max(0, preset.cpuRequest ?? preset.cpu * 0.75), preset.cpu);
+    const memReq = Math.min(Math.max(0, preset.memoryRequest ?? preset.memory), preset.memory);
+    return {
+      cpu: `${cpuReq}`,
+      memory: `${memReq}G`,
+    };
   }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
#getResourceRequestsForMachine(preset: MachinePreset): ResourceQuantities {
return {
cpu: `${preset.cpu * 0.75}`,
memory: `${preset.memory}G`,
cpu: `${preset.cpuRequest ?? preset.cpu * 0.75}`,
memory: `${preset.memoryRequest ?? preset.memory}G`,
};
}
#getResourceRequestsForMachine(preset: MachinePreset): ResourceQuantities {
const cpuReq = Math.min(
Math.max(0, preset.cpuRequest ?? preset.cpu * 0.75),
preset.cpu
);
const memReq = Math.min(
Math.max(0, preset.memoryRequest ?? preset.memory),
preset.memory
);
return {
cpu: `${cpuReq}`,
memory: `${memReq}G`,
};
}
🤖 Prompt for AI Agents
In apps/kubernetes-provider/src/index.ts around lines 546 to 551, the method
computing resource requests currently returns values that may exceed preset
limits; update it to clamp the computed cpu and memory requests to the
corresponding limits (e.g. cpuLimit and memoryLimit) so requests never exceed
limits. Compute cpuReq = min(preset.cpuRequest ?? preset.cpu * 0.75,
preset.cpuLimit ?? Infinity) and memoryReq = min(numeric(preset.memoryRequest ??
preset.memory), preset.memoryLimit ?? Infinity) then format cpu as string and
memory with trailing "G"; return those clamped values.


Expand Down
4 changes: 2 additions & 2 deletions apps/supervisor/src/workloadManager/kubernetes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,8 @@ export class KubernetesWorkloadManager implements WorkloadManager {

#getResourceRequestsForMachine(preset: MachinePreset): ResourceQuantities {
return {
cpu: `${preset.cpu * 0.75}`,
memory: `${preset.memory}G`,
cpu: `${preset.cpuRequest ?? preset.cpu * 0.75}`,
memory: `${preset.memoryRequest ?? preset.memory}G`,
};
}
Comment on lines 297 to 302
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Clamp requests to limits to avoid K8s 400s on misconfiguration

Even with schema checks, defensive clamping avoids failed pod creations when overrides drift or come from older configs.

   #getResourceRequestsForMachine(preset: MachinePreset): ResourceQuantities {
-    return {
-      cpu: `${preset.cpuRequest ?? preset.cpu * 0.75}`,
-      memory: `${preset.memoryRequest ?? preset.memory}G`,
-    };
+    const cpuReq = Math.min(Math.max(0, preset.cpuRequest ?? preset.cpu * 0.75), preset.cpu);
+    const memReq = Math.min(Math.max(0, preset.memoryRequest ?? preset.memory), preset.memory);
+    return {
+      cpu: `${cpuReq}`,
+      memory: `${memReq}G`,
+    };
   }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
#getResourceRequestsForMachine(preset: MachinePreset): ResourceQuantities {
return {
cpu: `${preset.cpu * 0.75}`,
memory: `${preset.memory}G`,
cpu: `${preset.cpuRequest ?? preset.cpu * 0.75}`,
memory: `${preset.memoryRequest ?? preset.memory}G`,
};
}
#getResourceRequestsForMachine(preset: MachinePreset): ResourceQuantities {
const cpuReq = Math.min(Math.max(0, preset.cpuRequest ?? preset.cpu * 0.75), preset.cpu);
const memReq = Math.min(Math.max(0, preset.memoryRequest ?? preset.memory), preset.memory);
return {
cpu: `${cpuReq}`,
memory: `${memReq}G`,
};
}


Expand Down
2 changes: 2 additions & 0 deletions apps/webapp/app/services/platform.v3.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ type Machines = typeof machinesFromPlatform;

const MachineOverrideValues = z.object({
cpu: z.number(),
cpuRequest: z.number().optional(), // Only used for k8s fallback to cpu
memory: z.number(),
memoryRequest: z.number().optional(), // Only used for k8s fallback to memory
});
type MachineOverrideValues = z.infer<typeof MachineOverrideValues>;

Expand Down
13 changes: 12 additions & 1 deletion docs/self-hosting/overview.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ While [limits](#limits) are generally configurable when self-hosting, some featu
| Community support | ✅ | ✅ | Access to our Discord community |
| ARM support | ✅ | ✅ | ARM-based deployments |


## Limits

Most of the [limits](/limits) are configurable when self-hosting, with some hardcoded exceptions. You can configure them via environment variables on the [webapp](/self-hosting/env/webapp) container.
Expand Down Expand Up @@ -92,6 +91,18 @@ All fields are optional. Partial overrides are supported:
}
```

You can also set cpu/memory requests for each machine type. This is used in k8s environments to set the minimum resources required for each machine type.
We do not recommend setting these values to zero, as it can cause the k8s scheduler to schedule an infinite number of pods in parallel and cause the pods to get killed due to resource exhaustion.

```json
{
"defaultMachine": "small-2x",
"machines": {
"small-1x": { "cpu": 0.5, "memory": 0.5, "cpuRequest": 0.25, "memoryRequest": 0.25 }
}
}
```

## Community support

It's dangerous to go alone! Join the self-hosting channel on our [Discord server](https://discord.gg/NQTxt5NA7s).
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/v3/schemas/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,10 @@ export const MachinePreset = z.object({
name: MachinePresetName,
/** unit: vCPU */
cpu: z.number(),
cpuRequest: z.number().optional(), // Only used for k8s fallback to cpu
/** unit: GB */
memory: z.number(),
memoryRequest: z.number().optional(), // Only used for k8s fallback to memory
centsPerMs: z.number(),
});

Expand Down
Loading