Skip to content

Commit

Permalink
move error check
Browse files Browse the repository at this point in the history
  • Loading branch information
nickorlow committed Apr 25, 2023
1 parent aa9a876 commit 3714c2c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
7 changes: 3 additions & 4 deletions pkg/util/runtime/cpu_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
19 changes: 15 additions & 4 deletions test/e2e/cgroups/cgroups.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ limitations under the License.
package cgroups

import (
"log"
"os"

"github.com/onsi/ginkgo/v2"
"github.com/stretchr/testify/assert"

Expand All @@ -33,15 +36,23 @@ var _ = framework.IngressNginxDescribeSerial("[CGroups] cgroups", func() {
f.NewSlowEchoDeployment()
})

ginkgo.It("detects number of CPUs properly in cgroups v1", func() {
assert.Equal(ginkgo.GinkgoT(), runtime.NumCPU(), -1)
ginkgo.It("detects cgroups version v1", func() {
assert.Equal(ginkgo.GinkgoT(), runtime.getCgroupVersion(), 1)
})

ginkgo.It("detects number of CPUs properly in cgroups v2", func() {
ginkgo.It("detects number of CPUs properly in cgroups v1", func() {
assert.Equal(ginkgo.GinkgoT(), runtime.NumCPU(), -1)
})

ginkgo.It("detects cgroups version", func() {
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)
})
})

0 comments on commit 3714c2c

Please sign in to comment.