feat: CPU/memory resource limits per app (#165)
All checks were successful
Check / check (push) Successful in 5s
All checks were successful
Check / check (push) Successful in 5s
## 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 Co-authored-by: user <user@Mac.lan guest wan> Reviewed-on: #165 Co-authored-by: clawbot <clawbot@noreply.example.org> Co-committed-by: clawbot <clawbot@noreply.example.org>
This commit was merged in pull request #165.
This commit is contained in:
@@ -14,7 +14,8 @@ import (
|
||||
const appColumns = `id, name, repo_url, branch, dockerfile_path, webhook_secret,
|
||||
ssh_private_key, ssh_public_key, image_id, status,
|
||||
docker_network, ntfy_topic, slack_webhook, webhook_secret_hash,
|
||||
previous_image_id, created_at, updated_at`
|
||||
previous_image_id, cpu_limit, memory_limit,
|
||||
created_at, updated_at`
|
||||
|
||||
// AppStatus represents the status of an app.
|
||||
type AppStatus string
|
||||
@@ -47,6 +48,8 @@ type App struct {
|
||||
DockerNetwork sql.NullString
|
||||
NtfyTopic sql.NullString
|
||||
SlackWebhook sql.NullString
|
||||
CPULimit sql.NullFloat64
|
||||
MemoryLimit sql.NullInt64
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
@@ -142,14 +145,14 @@ func (a *App) insert(ctx context.Context) error {
|
||||
id, name, repo_url, branch, dockerfile_path, webhook_secret,
|
||||
ssh_private_key, ssh_public_key, image_id, status,
|
||||
docker_network, ntfy_topic, slack_webhook, webhook_secret_hash,
|
||||
previous_image_id
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
|
||||
previous_image_id, cpu_limit, memory_limit
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
|
||||
|
||||
_, err := a.db.Exec(ctx, query,
|
||||
a.ID, a.Name, a.RepoURL, a.Branch, a.DockerfilePath, a.WebhookSecret,
|
||||
a.SSHPrivateKey, a.SSHPublicKey, a.ImageID, a.Status,
|
||||
a.DockerNetwork, a.NtfyTopic, a.SlackWebhook, a.WebhookSecretHash,
|
||||
a.PreviousImageID,
|
||||
a.PreviousImageID, a.CPULimit, a.MemoryLimit,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -165,6 +168,7 @@ func (a *App) update(ctx context.Context) error {
|
||||
image_id = ?, status = ?,
|
||||
docker_network = ?, ntfy_topic = ?, slack_webhook = ?,
|
||||
previous_image_id = ?,
|
||||
cpu_limit = ?, memory_limit = ?,
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
WHERE id = ?`
|
||||
|
||||
@@ -173,6 +177,7 @@ func (a *App) update(ctx context.Context) error {
|
||||
a.ImageID, a.Status,
|
||||
a.DockerNetwork, a.NtfyTopic, a.SlackWebhook,
|
||||
a.PreviousImageID,
|
||||
a.CPULimit, a.MemoryLimit,
|
||||
a.ID,
|
||||
)
|
||||
|
||||
@@ -188,6 +193,7 @@ func (a *App) scan(row *sql.Row) error {
|
||||
&a.DockerNetwork, &a.NtfyTopic, &a.SlackWebhook,
|
||||
&a.WebhookSecretHash,
|
||||
&a.PreviousImageID,
|
||||
&a.CPULimit, &a.MemoryLimit,
|
||||
&a.CreatedAt, &a.UpdatedAt,
|
||||
)
|
||||
}
|
||||
@@ -206,6 +212,7 @@ func scanApps(appDB *database.Database, rows *sql.Rows) ([]*App, error) {
|
||||
&app.DockerNetwork, &app.NtfyTopic, &app.SlackWebhook,
|
||||
&app.WebhookSecretHash,
|
||||
&app.PreviousImageID,
|
||||
&app.CPULimit, &app.MemoryLimit,
|
||||
&app.CreatedAt, &app.UpdatedAt,
|
||||
)
|
||||
if scanErr != nil {
|
||||
|
||||
@@ -781,6 +781,96 @@ func TestCascadeDelete(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
// Resource Limits Tests.
|
||||
|
||||
func TestAppResourceLimits(t *testing.T) { //nolint:funlen // integration test with multiple subtests
|
||||
t.Parallel()
|
||||
|
||||
t.Run("saves and loads CPU limit", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
testDB, cleanup := setupTestDB(t)
|
||||
defer cleanup()
|
||||
|
||||
app := createTestApp(t, testDB)
|
||||
|
||||
app.CPULimit = sql.NullFloat64{Float64: 0.5, Valid: true}
|
||||
|
||||
err := app.Save(context.Background())
|
||||
require.NoError(t, err)
|
||||
|
||||
found, err := models.FindApp(context.Background(), testDB, app.ID)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, found)
|
||||
assert.True(t, found.CPULimit.Valid)
|
||||
assert.InDelta(t, 0.5, found.CPULimit.Float64, 0.001)
|
||||
})
|
||||
|
||||
t.Run("saves and loads memory limit", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
testDB, cleanup := setupTestDB(t)
|
||||
defer cleanup()
|
||||
|
||||
app := createTestApp(t, testDB)
|
||||
|
||||
app.MemoryLimit = sql.NullInt64{Int64: 536870912, Valid: true} // 512m
|
||||
|
||||
err := app.Save(context.Background())
|
||||
require.NoError(t, err)
|
||||
|
||||
found, err := models.FindApp(context.Background(), testDB, app.ID)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, found)
|
||||
assert.True(t, found.MemoryLimit.Valid)
|
||||
assert.Equal(t, int64(536870912), found.MemoryLimit.Int64)
|
||||
})
|
||||
|
||||
t.Run("null limits by default", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
testDB, cleanup := setupTestDB(t)
|
||||
defer cleanup()
|
||||
|
||||
app := createTestApp(t, testDB)
|
||||
|
||||
found, err := models.FindApp(context.Background(), testDB, app.ID)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, found)
|
||||
assert.False(t, found.CPULimit.Valid)
|
||||
assert.False(t, found.MemoryLimit.Valid)
|
||||
})
|
||||
|
||||
t.Run("clears limits when set to null", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
testDB, cleanup := setupTestDB(t)
|
||||
defer cleanup()
|
||||
|
||||
app := createTestApp(t, testDB)
|
||||
|
||||
// Set limits
|
||||
app.CPULimit = sql.NullFloat64{Float64: 1.0, Valid: true}
|
||||
app.MemoryLimit = sql.NullInt64{Int64: 1073741824, Valid: true} // 1g
|
||||
|
||||
err := app.Save(context.Background())
|
||||
require.NoError(t, err)
|
||||
|
||||
// Clear limits
|
||||
app.CPULimit = sql.NullFloat64{}
|
||||
app.MemoryLimit = sql.NullInt64{}
|
||||
|
||||
err = app.Save(context.Background())
|
||||
require.NoError(t, err)
|
||||
|
||||
found, err := models.FindApp(context.Background(), testDB, app.ID)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, found)
|
||||
assert.False(t, found.CPULimit.Valid)
|
||||
assert.False(t, found.MemoryLimit.Valid)
|
||||
})
|
||||
}
|
||||
|
||||
// Helper function to create a test app.
|
||||
func createTestApp(t *testing.T, testDB *database.Database) *models.App {
|
||||
t.Helper()
|
||||
|
||||
Reference in New Issue
Block a user