-
Notifications
You must be signed in to change notification settings - Fork 2
feat(config): add hostRequirements pre-flight validation #173
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
e2e/tests/readconfiguration/testdata-host-requirements/.devcontainer/devcontainer.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| { | ||
| "name": "Host Requirements Test", | ||
| "image": "mcr.microsoft.com/devcontainers/base:ubuntu", | ||
| "hostRequirements": { | ||
| "cpus": 4, | ||
| "memory": "8gb", | ||
| "storage": "32gb" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| package config | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "regexp" | ||
| "strconv" | ||
| "strings" | ||
|
|
||
| "github.com/devsy-org/devsy/pkg/log" | ||
| ) | ||
|
|
||
| // HostInfo provides system resource information for validation. | ||
| // Abstracted as an interface to allow testing with mock values. | ||
| type HostInfo interface { | ||
| NumCPU() int | ||
| TotalMemoryBytes() (uint64, error) | ||
| AvailableStorageBytes(path string) (uint64, error) | ||
| } | ||
|
|
||
| // ValidateHostRequirements checks whether the host satisfies the resource | ||
| // requirements declared in devcontainer.json. Returns warning strings for | ||
| // each unmet requirement; an empty slice means all requirements are met. | ||
| func ValidateHostRequirements( | ||
| reqs *HostRequirements, host HostInfo, workspacePath string, | ||
| ) []string { | ||
| if reqs == nil { | ||
| return nil | ||
| } | ||
|
|
||
| var warnings []string | ||
| warnings = appendCPUWarning(warnings, reqs.CPUs, host) | ||
| warnings = appendMemoryWarning(warnings, reqs.Memory, host) | ||
| warnings = appendStorageWarning(warnings, reqs.Storage, host, workspacePath) | ||
|
|
||
| for _, w := range warnings { | ||
| log.Warnw("hostRequirements not met", "warning", w) | ||
| } | ||
| return warnings | ||
| } | ||
|
|
||
| func appendCPUWarning(warnings []string, required int, host HostInfo) []string { | ||
| if required <= 0 { | ||
| return warnings | ||
| } | ||
| available := host.NumCPU() | ||
| if available < required { | ||
| return append(warnings, fmt.Sprintf( | ||
| "cpus: required %d, available %d", required, available, | ||
| )) | ||
| } | ||
| return warnings | ||
| } | ||
|
|
||
| func appendMemoryWarning(warnings []string, required string, host HostInfo) []string { | ||
| if required == "" { | ||
| return warnings | ||
| } | ||
| reqBytes, err := ParseSizeToBytes(required) | ||
| if err != nil { | ||
| return append(warnings, fmt.Sprintf("memory: invalid value %q: %v", required, err)) | ||
| } | ||
| available, err := host.TotalMemoryBytes() | ||
| if err != nil { | ||
| return append(warnings, fmt.Sprintf("memory: unable to detect: %v", err)) | ||
| } | ||
| if available < reqBytes { | ||
| return append(warnings, fmt.Sprintf( | ||
| "memory: required %s (%d bytes), available %d bytes", | ||
| required, reqBytes, available, | ||
| )) | ||
| } | ||
| return warnings | ||
| } | ||
|
|
||
| func appendStorageWarning( | ||
| warnings []string, required string, host HostInfo, path string, | ||
| ) []string { | ||
| if required == "" { | ||
| return warnings | ||
| } | ||
| reqBytes, err := ParseSizeToBytes(required) | ||
| if err != nil { | ||
| return append(warnings, fmt.Sprintf("storage: invalid value %q: %v", required, err)) | ||
| } | ||
| available, err := host.AvailableStorageBytes(path) | ||
| if err != nil { | ||
| return append(warnings, fmt.Sprintf("storage: unable to detect at %q: %v", path, err)) | ||
| } | ||
| if available < reqBytes { | ||
| return append(warnings, fmt.Sprintf( | ||
| "storage: required %s (%d bytes), available %d bytes at %q", | ||
| required, reqBytes, available, path, | ||
| )) | ||
| } | ||
| return warnings | ||
| } | ||
|
|
||
| var sizePattern = regexp.MustCompile(`(?i)^\s*(\d+)\s*(tb|gb|mb|kb)?\s*$`) | ||
|
|
||
| // ParseSizeToBytes converts a human-readable size string (e.g. "8gb", "512mb") | ||
| // to bytes. If no unit suffix is present, the value is treated as bytes. | ||
| func ParseSizeToBytes(s string) (uint64, error) { | ||
| matches := sizePattern.FindStringSubmatch(s) | ||
| if matches == nil { | ||
| return 0, fmt.Errorf("unrecognized size format: %q", s) | ||
| } | ||
| val, err := strconv.ParseUint(matches[1], 10, 64) | ||
| if err != nil { | ||
| return 0, err | ||
| } | ||
| unit := strings.ToLower(matches[2]) | ||
| switch unit { | ||
| case "tb": | ||
| return val * 1024 * 1024 * 1024 * 1024, nil | ||
| case "gb": | ||
| return val * 1024 * 1024 * 1024, nil | ||
| case "mb": | ||
| return val * 1024 * 1024, nil | ||
| case "kb": | ||
| return val * 1024, nil | ||
| default: | ||
| return val, nil | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| //go:build !windows | ||
|
|
||
| package config | ||
|
|
||
| import "syscall" | ||
|
|
||
| func availableStorageBytes(path string) (uint64, error) { | ||
| var stat syscall.Statfs_t | ||
| if err := syscall.Statfs(path, &stat); err != nil { | ||
| return 0, err | ||
| } | ||
| return stat.Bavail * uint64(stat.Bsize), nil //nolint:gosec // Bsize type varies by platform | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| //go:build windows | ||
|
|
||
| package config | ||
|
|
||
| import "fmt" | ||
|
|
||
| func availableStorageBytes(path string) (uint64, error) { | ||
| return 0, fmt.Errorf("storage detection not supported on windows") | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| package config | ||
|
|
||
| import ( | ||
| "bufio" | ||
| "fmt" | ||
| "os" | ||
| "runtime" | ||
| "strconv" | ||
| "strings" | ||
| ) | ||
|
|
||
| // SystemHostInfo provides real system resource information. | ||
| type SystemHostInfo struct { | ||
| WorkspacePath string | ||
| } | ||
|
|
||
| func (s SystemHostInfo) NumCPU() int { | ||
| return runtime.NumCPU() | ||
| } | ||
|
|
||
| func (s SystemHostInfo) TotalMemoryBytes() (uint64, error) { | ||
| return readMemTotalFromProc() | ||
| } | ||
|
|
||
| func (s SystemHostInfo) AvailableStorageBytes(path string) (uint64, error) { | ||
| if path == "" { | ||
| path = "/" | ||
| } | ||
| bytes, err := availableStorageBytes(path) | ||
| if err != nil { | ||
| return 0, fmt.Errorf("statfs %q: %w", path, err) | ||
| } | ||
| return bytes, nil | ||
| } | ||
|
|
||
| func readMemTotalFromProc() (uint64, error) { | ||
| f, err := os.Open("/proc/meminfo") | ||
| if err != nil { | ||
| return 0, fmt.Errorf("open /proc/meminfo: %w", err) | ||
| } | ||
| defer f.Close() //nolint:errcheck // best-effort close on read-only file | ||
|
|
||
| scanner := bufio.NewScanner(f) | ||
| for scanner.Scan() { | ||
| line := scanner.Text() | ||
| if !strings.HasPrefix(line, "MemTotal:") { | ||
| continue | ||
| } | ||
| fields := strings.Fields(line) | ||
| if len(fields) < 2 { | ||
| return 0, fmt.Errorf("unexpected MemTotal format: %q", line) | ||
| } | ||
| kb, err := strconv.ParseUint(fields[1], 10, 64) | ||
| if err != nil { | ||
| return 0, fmt.Errorf("parse MemTotal: %w", err) | ||
| } | ||
| return kb * 1024, nil | ||
| } | ||
| return 0, fmt.Errorf("MemTotal not found in /proc/meminfo") | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: devsy-org/devsy
Length of output: 1727
🏁 Script executed:
Repository: devsy-org/devsy
Length of output: 2627
🏁 Script executed:
Repository: devsy-org/devsy
Length of output: 2351
🏁 Script executed:
Repository: devsy-org/devsy
Length of output: 3148
🏁 Script executed:
Repository: devsy-org/devsy
Length of output: 1715
🏁 Script executed:
Repository: devsy-org/devsy
Length of output: 659
🏁 Script executed:
Repository: devsy-org/devsy
Length of output: 1069
Add build constraint to prevent Windows compilation errors —
syscall.Statfsand/proc/meminfoare Unix-only.host_requirements_system.gouses Unix-specific syscalls (syscall.Statfsat line 31 and/proc/meminfoat line 38) but has no//go:buildconstraint. Since Windows is a supported target (present in CI workflows), this file will fail to compile on Windows. Add//go:build !windowsat the top and create ahost_requirements_system_windows.gostub implementation following the existing pattern used byprepareprobe.go/prepareprobe_windows.goin the same package.🤖 Prompt for AI Agents