From 68e43ac7409860f500021f0677865bcbff6bec35 Mon Sep 17 00:00:00 2001 From: likun Date: Thu, 14 Aug 2025 10:45:50 -0400 Subject: [PATCH] iolimit set container cgroup instead of pod --- pkg/services/iolimit/iolimit.go | 13 +++++++++---- pkg/services/iolimit/iolimit_test.go | 16 ++++++++++++---- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/pkg/services/iolimit/iolimit.go b/pkg/services/iolimit/iolimit.go index 131ebda..12321f2 100644 --- a/pkg/services/iolimit/iolimit.go +++ b/pkg/services/iolimit/iolimit.go @@ -142,8 +142,11 @@ func (i *IOLimit) configIOLimit(podInfo *typedef.PodInfo) error { } // firstly clear all config - if err := clearAllBlkioThrottleFiles(podInfo.Path); err != nil { - return fmt.Errorf("failed to clear blkio throttle files for pod %s: %v", podInfo.Name, err) + // blkio cgroup hierarchical is not enabled default, only set container cgroups + for _, container := range podInfo.IDContainersMap { + if err := clearAllBlkioThrottleFiles(container.Path); err != nil { + return fmt.Errorf("failed to clear blkio throttle files for container %s of pod %s: %v", container.Name, podInfo.Name, err) + } } // secondly parse the config @@ -153,8 +156,10 @@ func (i *IOLimit) configIOLimit(podInfo *typedef.PodInfo) error { } // thirdly apply the config to cgroup files - if err := applyIOLimitConfig(podInfo.Path, cfg); err != nil { - return fmt.Errorf("failed to apply blkio config for pod %s: %v", podInfo.Name, err) + for _, container := range podInfo.IDContainersMap { + if err := applyIOLimitConfig(container.Path, cfg); err != nil { + return fmt.Errorf("failed to apply blkio config for container %s of pod %s: %v", container.Name, podInfo.Name, err) + } } return nil diff --git a/pkg/services/iolimit/iolimit_test.go b/pkg/services/iolimit/iolimit_test.go index 44ff9ec..15abb95 100644 --- a/pkg/services/iolimit/iolimit_test.go +++ b/pkg/services/iolimit/iolimit_test.go @@ -840,8 +840,12 @@ func TestConfigIOLimit(t *testing.T) { // Test 1: Empty config (should just clear files) podInfo := &typedef.PodInfo{ Name: "test-pod", - Hierarchy: cgroup.Hierarchy{Path: testPodPath}, Annotations: map[string]string{}, + IDContainersMap: map[string]*typedef.ContainerInfo{ + "test-container": { + Hierarchy: cgroup.Hierarchy{Path: testPodPath}, + }, + }, } err = iolimit.configIOLimit(podInfo) if err != nil { @@ -947,7 +951,7 @@ func TestConfigIOLimit(t *testing.T) { } // Test 4: Valid JSON config but invalid cgroup path - podInfo.Hierarchy.Path = "/invalid/path" + podInfo.IDContainersMap["test-container"].Hierarchy.Path = "/invalid/path" err = iolimit.configIOLimit(podInfo) if err == nil { t.Error("Expected error with invalid cgroup path") @@ -1372,11 +1376,15 @@ func TestConfigIOLimit_EdgeCases(t *testing.T) { // Test with valid JSON but applying config fails (no cgroup files) podInfo := &typedef.PodInfo{ - Name: "test-pod", - Hierarchy: cgroup.Hierarchy{Path: "nonexistent-pod"}, + Name: "test-pod", Annotations: map[string]string{ constant.BlkioKey: `{"device_read_bps":[{"device":"/dev/sda","value":"1048576"}]}`, }, + IDContainersMap: map[string]*typedef.ContainerInfo{ + "test-container": { + Hierarchy: cgroup.Hierarchy{Path: "nonexistent-container"}, + }, + }, } err := iolimit.configIOLimit(podInfo) -- Gitee