-
Notifications
You must be signed in to change notification settings - Fork 279
Add cgroup v2 support while maintaining v1 compatibility #2632
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
Open
jiechen0826
wants to merge
9
commits into
microsoft:main
Choose a base branch
from
jiechen0826:cgroup-v2-clean
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+45,322
−4,779
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9bc130f
add comprehensive cgroup v1/v2 dual compatibility
jiechen0826 ddca804
clean up go mod
jiechen0826 990508d
fix golang-cli formatting
jiechen0826 5df5974
Fix protobuild tools dependency
jiechen0826 9f85d63
Fix CI issues: vendor sync, lint errors, and protobuf generation
jiechen0826 bd4226a
Fix CI: sync vendor, regenerate protobuf and syscalls
jiechen0826 2672b79
fix: update vendor directory for CI consistency
jiechen0826 b2fd26a
fix: add missing tools for go generate
jiechen0826 da130ab
fix: update go:generate directives for CI compatibility
jiechen0826 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 @@ | ||
| //go:build linux | ||
| // +build linux | ||
|
|
||
| package main | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| cgroups2stats "github.com/containerd/cgroups/v3/cgroup2/stats" | ||
| oci "github.com/opencontainers/runtime-spec/specs-go" | ||
| ) | ||
|
|
||
| func TestCgroupManagerInterface_Compatibility(t *testing.T) { | ||
| // Test that both managers implement the CgroupManager interface | ||
| var v1mgr CgroupManager = &V1Manager{} | ||
| var v2mgr CgroupManager = &V2Manager{path: "/test/path"} | ||
|
|
||
| // Test that both managers implement the interface | ||
| _ = v1mgr | ||
| _ = v2mgr | ||
| } | ||
|
|
||
| func TestConvertToV2Resources_Basic(t *testing.T) { | ||
| limit := int64(1024 * 1024 * 1024) // 1GB | ||
|
|
||
| ociResources := &oci.LinuxResources{ | ||
| Memory: &oci.LinuxMemory{ | ||
| Limit: &limit, | ||
| }, | ||
| } | ||
|
|
||
| v2Resources := convertToV2Resources(ociResources) | ||
|
|
||
| if v2Resources == nil { | ||
| t.Fatal("v2Resources should not be nil") | ||
| } | ||
| if v2Resources.Memory == nil { | ||
| t.Fatal("v2Resources.Memory should not be nil") | ||
| } | ||
| if v2Resources.Memory.Max == nil || *v2Resources.Memory.Max != limit { | ||
| t.Errorf("Expected memory max %d, got %v", limit, v2Resources.Memory.Max) | ||
| } | ||
| } | ||
|
|
||
| func TestIsCgroupV2_Detection(t *testing.T) { | ||
| // Test the cgroup version detection | ||
| result1 := isCgroupV2() | ||
| result2 := isCgroupV2() | ||
|
|
||
| // Should be consistent | ||
| if result1 != result2 { | ||
| t.Error("isCgroupV2() should return consistent results") | ||
| } | ||
|
|
||
| t.Logf("Detected cgroup version: v%d", map[bool]int{false: 1, true: 2}[result1]) | ||
| } | ||
|
|
||
| // Additional Error Handling and Edge Cases | ||
| func TestCgroupManager_InvalidPath(t *testing.T) { | ||
| // Use a path that cannot be created due to read-only filesystem constraints | ||
| v2mgr := &V2Manager{path: "/proc/nonexistent/path"} | ||
| err := v2mgr.Create(1234) | ||
| if err == nil { | ||
| t.Error("Expected error for invalid cgroup path, got nil") | ||
| } else { | ||
| t.Logf("Expected error for invalid path: %v", err) | ||
| } | ||
| } | ||
|
|
||
| func TestCgroupManager_PermissionDenied(t *testing.T) { | ||
| // Test with root path that should require permissions | ||
| v2mgr := &V2Manager{path: "/sys/fs/cgroup/test-permission-denied"} | ||
| err := v2mgr.Create(1234) | ||
| // Error is expected, just ensure it doesn't panic | ||
| if err != nil { | ||
| t.Logf("Expected permission error: %v", err) | ||
| } | ||
| } | ||
|
|
||
| func TestConvertV2StatsToV1_InvalidInput(t *testing.T) { | ||
| // Test with nil input | ||
| result := convertV2StatsToV1Stats(nil) | ||
| if result == nil { | ||
| t.Error("convertV2StatsToV1Stats should handle nil input gracefully") | ||
| } | ||
|
|
||
| // Test with empty metrics | ||
| emptyMetrics := &cgroups2stats.Metrics{} | ||
| result = convertV2StatsToV1Stats(emptyMetrics) | ||
| if result == nil { | ||
| t.Error("convertV2StatsToV1Stats should handle empty metrics") | ||
| } | ||
| } | ||
|
|
||
| func TestConvertToV2Resources_ExtremeLimits(t *testing.T) { | ||
| // Test with maximum possible values | ||
| maxLimit := int64(^uint64(0) >> 1) // Max int64 | ||
| ociResources := &oci.LinuxResources{ | ||
| Memory: &oci.LinuxMemory{ | ||
| Limit: &maxLimit, | ||
| }, | ||
| Pids: &oci.LinuxPids{ | ||
| Limit: &maxLimit, | ||
| }, | ||
| } | ||
|
|
||
| v2Resources := convertToV2Resources(ociResources) | ||
| if v2Resources == nil { | ||
| t.Fatal("v2Resources should not be nil") | ||
| } | ||
|
|
||
| // Test with zero values | ||
| zeroLimit := int64(0) | ||
| ociResourcesZero := &oci.LinuxResources{ | ||
| Memory: &oci.LinuxMemory{ | ||
| Limit: &zeroLimit, | ||
| }, | ||
| } | ||
|
|
||
| v2ResourcesZero := convertToV2Resources(ociResourcesZero) | ||
| if v2ResourcesZero == nil || v2ResourcesZero.Memory == nil { | ||
| t.Error("Should handle zero limit values") | ||
| } | ||
| } | ||
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.
in
main.go: