diff --git a/pkg/util/runtime/cpu_linux.go b/pkg/util/runtime/cpu_linux.go index b4d6997b9..c72f47f01 100644 --- a/pkg/util/runtime/cpu_linux.go +++ b/pkg/util/runtime/cpu_linux.go @@ -38,16 +38,15 @@ import ( func NumCPU() int { cpus := runtime.NumCPU() - if err != nil { - return cpus - } - cgroupVersion := getCgroupVersion() cpuQuota := int64(-1) cpuPeriod := int64(-1) if cgroupVersion == 1 { cgroupPath, err := libcontainercgroups.FindCgroupMountpoint("", "cpu") + if err != nil { + return cpus + } cpuQuota = readCgroupFileToInt64(cgroupPath, "cpu.cfs_quota_us") cpuPeriod = readCgroupFileToInt64(cgroupPath, "cpu.cfs_period_us") } else if cgroupVersion == 2 { diff --git a/test/e2e/cgroups/cgroups.go b/test/e2e/cgroups/cgroups.go index 34d6c037a..4ee6bf927 100644 --- a/test/e2e/cgroups/cgroups.go +++ b/test/e2e/cgroups/cgroups.go @@ -17,6 +17,9 @@ limitations under the License. package cgroups import ( + "log" + "os" + "github.com/onsi/ginkgo/v2" "github.com/stretchr/testify/assert" @@ -33,15 +36,23 @@ var _ = framework.IngressNginxDescribeSerial("[CGroups] cgroups", func() { f.NewSlowEchoDeployment() }) + ginkgo.It("detects cgroups version v1", func() { + assert.Equal(ginkgo.GinkgoT(), runtime.getCgroupVersion(), 1) + }) + ginkgo.It("detects number of CPUs properly in cgroups v1", func() { assert.Equal(ginkgo.GinkgoT(), runtime.NumCPU(), -1) }) + ginkgo.It("detects cgroups version v2", func() { + // create cgroups2 files + if err := os.MkdirAll("a/b/c/d", os.ModePerm); err != nil { + log.Fatal(err) + } + + }) + ginkgo.It("detects number of CPUs properly in cgroups v2", func() { assert.Equal(ginkgo.GinkgoT(), runtime.NumCPU(), -1) }) - - ginkgo.It("detects cgroups version", func() { - - }) })