feat: CPU/memory resource limits per app #165

Merged
sneak merged 1 commits from feature/cpu-memory-resource-limits into main 2026-03-20 06:44:48 +01:00
Collaborator

Summary

Adds configurable Docker CPU and memory resource constraints per app, closes #72.

Changes

Database

  • Migration 007_add_resource_limits.sql: adds cpu_limit (REAL, nullable) and memory_limit (INTEGER in bytes, nullable) columns to the apps table

Model (internal/models/app.go)

  • Added CPULimit (sql.NullFloat64) and MemoryLimit (sql.NullInt64) fields to App struct
  • Updated insert, update, scan, and column list to include the new fields

Docker Client (internal/docker/client.go)

  • Added CPULimit (float64, CPU cores) and MemoryLimit (int64, bytes) to CreateContainerOptions
  • Added cpuLimitToNanoCPUs() conversion helper and buildResources() to construct container.Resources
  • Extracted buildEnvSlice() and buildMounts() helpers from CreateContainer for cleaner code
  • Resource limits are passed to Docker's HostConfig.Resources (NanoCPUs / Memory)

Deploy Service (internal/service/deploy/deploy.go)

  • buildContainerOptions reads CPULimit and MemoryLimit from the app and passes them to CreateContainerOptions

Handlers (internal/handlers/app.go)

  • HandleAppUpdate reads and validates cpu_limit and memory_limit form fields
  • Added parseOptionalFloat64() for CPU limit parsing (positive float or empty)
  • Added parseOptionalMemoryBytes() for memory parsing with unit suffixes (k/m/g) or plain bytes
  • Added optionalNullString() and applyResourceLimits() helpers to keep cyclomatic complexity in check

Templates

  • app_edit.html: Added "Resource Limits" section with CPU limit (cores) and memory limit (with unit suffix) fields
  • templates.go: Added formatMemoryBytes template function for display (converts bytes → human-readable like 256m, 1g)

Tests

  • internal/docker/resource_limits_test.go: Tests for cpuLimitToNanoCPUs conversion
  • internal/handlers/resource_limits_test.go: Tests for parseOptionalFloat64 and parseOptionalMemoryBytes (happy paths, edge cases, validation)
  • internal/models/models_test.go: Tests for App model resource limit persistence (save/load, null defaults, clearing)
  • internal/service/deploy/deploy_container_test.go: Tests for container options with/without resource limits
  • templates/templates_test.go: Tests for formatMemoryBytes formatting

README

  • Added "CPU and memory resource limits per app" to Features list

Behavior

  • CPU limit: Specified in cores (e.g. 0.5 = half a core, 2 = two cores). Converted to Docker NanoCPUs internally.
  • Memory limit: Accepts plain bytes or suffixed values (256m, 1g, 512k). Stored as bytes in the database.
  • Both fields are optional — empty/unset means unlimited (no Docker constraint applied).
  • Limits are applied on every container creation: new deploys, rollbacks, and restarts that recreate the container.

closes #72

## Summary Adds configurable Docker CPU and memory resource constraints per app, closes https://git.eeqj.de/sneak/upaas/issues/72. ## Changes ### Database - Migration `007_add_resource_limits.sql`: adds `cpu_limit` (REAL, nullable) and `memory_limit` (INTEGER in bytes, nullable) columns to the `apps` table ### Model (`internal/models/app.go`) - Added `CPULimit` (`sql.NullFloat64`) and `MemoryLimit` (`sql.NullInt64`) fields to `App` struct - Updated insert, update, scan, and column list to include the new fields ### Docker Client (`internal/docker/client.go`) - Added `CPULimit` (float64, CPU cores) and `MemoryLimit` (int64, bytes) to `CreateContainerOptions` - Added `cpuLimitToNanoCPUs()` conversion helper and `buildResources()` to construct `container.Resources` - Extracted `buildEnvSlice()` and `buildMounts()` helpers from `CreateContainer` for cleaner code - Resource limits are passed to Docker's `HostConfig.Resources` (NanoCPUs / Memory) ### Deploy Service (`internal/service/deploy/deploy.go`) - `buildContainerOptions` reads `CPULimit` and `MemoryLimit` from the app and passes them to `CreateContainerOptions` ### Handlers (`internal/handlers/app.go`) - `HandleAppUpdate` reads and validates `cpu_limit` and `memory_limit` form fields - Added `parseOptionalFloat64()` for CPU limit parsing (positive float or empty) - Added `parseOptionalMemoryBytes()` for memory parsing with unit suffixes (k/m/g) or plain bytes - Added `optionalNullString()` and `applyResourceLimits()` helpers to keep cyclomatic complexity in check ### Templates - `app_edit.html`: Added "Resource Limits" section with CPU limit (cores) and memory limit (with unit suffix) fields - `templates.go`: Added `formatMemoryBytes` template function for display (converts bytes → human-readable like `256m`, `1g`) ### Tests - `internal/docker/resource_limits_test.go`: Tests for `cpuLimitToNanoCPUs` conversion - `internal/handlers/resource_limits_test.go`: Tests for `parseOptionalFloat64` and `parseOptionalMemoryBytes` (happy paths, edge cases, validation) - `internal/models/models_test.go`: Tests for App model resource limit persistence (save/load, null defaults, clearing) - `internal/service/deploy/deploy_container_test.go`: Tests for container options with/without resource limits - `templates/templates_test.go`: Tests for `formatMemoryBytes` formatting ### README - Added "CPU and memory resource limits per app" to Features list ## Behavior - **CPU limit**: Specified in cores (e.g. `0.5` = half a core, `2` = two cores). Converted to Docker NanoCPUs internally. - **Memory limit**: Accepts plain bytes or suffixed values (`256m`, `1g`, `512k`). Stored as bytes in the database. - Both fields are **optional** — empty/unset means unlimited (no Docker constraint applied). - Limits are applied on every container creation: new deploys, rollbacks, and restarts that recreate the container. closes https://git.eeqj.de/sneak/upaas/issues/72
clawbot added 1 commit 2026-03-17 10:11:19 +01:00
feat: add CPU and memory resource limits per app
All checks were successful
Check / check (pull_request) Successful in 3m24s
d6f6cc3670
- Add cpu_limit (REAL) and memory_limit (INTEGER) columns to apps table
  via migration 007
- Add CPULimit and MemoryLimit fields to App model with full CRUD support
- Add resource limits fields to app edit form with human-friendly
  memory input (e.g. 256m, 1g, 512k)
- Pass CPU and memory limits to Docker container creation via
  NanoCPUs and Memory host config fields
- Extract Docker container creation helpers (buildEnvSlice, buildMounts,
  buildResources) for cleaner code
- Add formatMemoryBytes template function for display
- Add comprehensive tests for parsing, formatting, model persistence,
  and container options
clawbot reviewed 2026-03-17 10:32:25 +01:00
clawbot left a comment
Author
Collaborator

Review: PR #165 — feat: CPU/memory resource limits per app

Policy Compliance

No policy violations found.

  • All Docker base images pinned by @sha256: hash
  • .golangci.yml not modified
  • Makefile, CI config not modified
  • No weakened test assertions or suppressed linter rules
  • README updated to reflect new feature
  • Migration file (007_add_resource_limits.sql) follows the established numbered migration pattern already present on main (002–006)

Requirements Checklist (issue #72)

Requirement Status
Add cpu_limit and memory_limit columns to apps table Met — migration 007 adds both columns (REAL nullable, INTEGER nullable)
Add fields to app edit form Met — app_edit.html has Resource Limits section with CPU and memory inputs
Pass limits to Docker container create Met — CreateContainerOptions carries limits → buildResources()HostConfig.Resources

Test Coverage

New code Test
cpuLimitToNanoCPUs() internal/docker/resource_limits_test.go
parseOptionalFloat64() internal/handlers/resource_limits_test.go
parseOptionalMemoryBytes() internal/handlers/resource_limits_test.go
formatMemoryBytes() templates/templates_test.go
App model persistence (save/load/null/clear) internal/models/models_test.go
Container options with/without limits internal/service/deploy/deploy_container_test.go

Build Result

docker build .PASS (fmt-check, lint, test, compile all green)

Rebase onto main — clean, no conflicts. Build still passes after rebase.

Code Quality Notes

  • Clean extraction of buildEnvSlice(), buildMounts(), buildResources() helpers improves readability of CreateContainer
  • optionalNullString() DRYs up repeated null-string handling in HandleAppUpdate
  • Input validation covers edge cases: empty strings, whitespace, zero, negative values, invalid formats
  • Memory parsing supports fractional units (1.5g) and case-insensitive suffixes
  • ErrInvalidMemoryFormat and ErrNegativeValue are properly defined as package-level error variables

Verdict: PASS

All issue requirements fully implemented. Tests cover every new code path. No policy violations. Build green.

## Review: [PR #165](https://git.eeqj.de/sneak/upaas/pulls/165) — feat: CPU/memory resource limits per app ### Policy Compliance No policy violations found. - All Docker base images pinned by `@sha256:` hash ✅ - `.golangci.yml` not modified ✅ - Makefile, CI config not modified ✅ - No weakened test assertions or suppressed linter rules ✅ - README updated to reflect new feature ✅ - Migration file (`007_add_resource_limits.sql`) follows the established numbered migration pattern already present on `main` (002–006) ✅ ### Requirements Checklist ([issue #72](https://git.eeqj.de/sneak/upaas/issues/72)) | Requirement | Status | |---|---| | Add `cpu_limit` and `memory_limit` columns to apps table | ✅ Met — migration 007 adds both columns (REAL nullable, INTEGER nullable) | | Add fields to app edit form | ✅ Met — `app_edit.html` has Resource Limits section with CPU and memory inputs | | Pass limits to Docker container create | ✅ Met — `CreateContainerOptions` carries limits → `buildResources()` → `HostConfig.Resources` | ### Test Coverage | New code | Test | |---|---| | `cpuLimitToNanoCPUs()` | `internal/docker/resource_limits_test.go` ✅ | | `parseOptionalFloat64()` | `internal/handlers/resource_limits_test.go` ✅ | | `parseOptionalMemoryBytes()` | `internal/handlers/resource_limits_test.go` ✅ | | `formatMemoryBytes()` | `templates/templates_test.go` ✅ | | App model persistence (save/load/null/clear) | `internal/models/models_test.go` ✅ | | Container options with/without limits | `internal/service/deploy/deploy_container_test.go` ✅ | ### Build Result `docker build .` — **PASS** (fmt-check, lint, test, compile all green) Rebase onto `main` — clean, no conflicts. Build still passes after rebase. ### Code Quality Notes - Clean extraction of `buildEnvSlice()`, `buildMounts()`, `buildResources()` helpers improves readability of `CreateContainer` - `optionalNullString()` DRYs up repeated null-string handling in `HandleAppUpdate` - Input validation covers edge cases: empty strings, whitespace, zero, negative values, invalid formats - Memory parsing supports fractional units (`1.5g`) and case-insensitive suffixes - `ErrInvalidMemoryFormat` and `ErrNegativeValue` are properly defined as package-level error variables ### Verdict: **PASS** ✅ All issue requirements fully implemented. Tests cover every new code path. No policy violations. Build green.
clawbot added the merge-ready label 2026-03-17 10:34:41 +01:00
sneak was assigned by clawbot 2026-03-17 10:34:42 +01:00
sneak merged commit 67361419f5 into main 2026-03-20 06:44:48 +01:00
sneak deleted branch feature/cpu-memory-resource-limits 2026-03-20 06:44:48 +01:00
Sign in to join this conversation.