diff --git a/pkg/util/runtime/cpu_linux.go b/pkg/util/runtime/cpu_linux.go index dc24a1ad7..c97184bcf 100644 --- a/pkg/util/runtime/cpu_linux.go +++ b/pkg/util/runtime/cpu_linux.go @@ -38,7 +38,7 @@ import ( func NumCPU() int { cpus := runtime.NumCPU() - cgroupVersion := getCgroupVersion() + cgroupVersion := GetCgroupVersion() cpuQuota := int64(-1) cpuPeriod := int64(-1) @@ -60,7 +60,7 @@ func NumCPU() int { return int(math.Ceil(float64(cpuQuota) / float64(cpuPeriod))) } -func getCgroupVersion() int64 { +func GetCgroupVersion() int64 { // /sys/fs/cgroup/cgroup.controllers will not exist with cgroupsv1 if _, err := os.Stat("/sys/fs/cgroup/cgroup.controllers"); err == nil { return 2 diff --git a/pkg/util/runtime/cpu_notlinux.go b/pkg/util/runtime/cpu_notlinux.go index 2a1b48252..0441af957 100644 --- a/pkg/util/runtime/cpu_notlinux.go +++ b/pkg/util/runtime/cpu_notlinux.go @@ -26,4 +26,4 @@ import ( // NumCPU ... func NumCPU() int { return runtime.NumCPU() -} +} \ No newline at end of file diff --git a/test/e2e/cgroups/cgroups_linux.go b/test/e2e/cgroups/cgroups_linux.go new file mode 100644 index 000000000..76a18407d --- /dev/null +++ b/test/e2e/cgroups/cgroups_linux.go @@ -0,0 +1,97 @@ +//go:build linux +// +build linux + +/* +Copyright 2020 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package cgroups + +import ( + "log" + "os" + + "github.com/onsi/ginkgo/v2" + "github.com/stretchr/testify/assert" + + "k8s.io/ingress-nginx/test/e2e/framework" + + "path/filepath" + "k8s.io/ingress-nginx/pkg/util/runtime" + + libcontainercgroups "github.com/opencontainers/runc/libcontainer/cgroups" +) + +var _ = framework.IngressNginxDescribeSerial("[CGroups] cgroups", func() { + f := framework.NewDefaultFramework("cgroups") + + ginkgo.BeforeEach(func() { + f.NewEchoDeployment() + f.NewSlowEchoDeployment() + }) + + ginkgo.It("detects cgroups version v1", func() { + cgroupPath, err := libcontainercgroups.FindCgroupMountpoint("", "cpu") + if err != nil { + log.Fatal(err) + } + + quotaFile, err := os.Create(filepath.Join(cgroupPath,"cpu.cfs_quota_us")) + + if err != nil { + log.Fatal(err) + } + + periodFile, err := os.Create(filepath.Join(cgroupPath,"cpu.cfs_period_us")) + + if err != nil { + log.Fatal(err) + } + + quotaFile.WriteString("4"); + quotaFile.Sync(); + + periodFile.WriteString("2"); + periodFile.Sync(); + + assert.Equal(ginkgo.GinkgoT(), runtime.GetCgroupVersion(), int64(1)) + assert.Equal(ginkgo.GinkgoT(), runtime.NumCPU(), 2) + + os.Remove(filepath.Join(cgroupPath,"cpu.cfs_quota_us")) + os.Remove(filepath.Join(cgroupPath,"cpu.cfs_period_us")) + }) + + ginkgo.It("detect cgroups version v2", func() { + if err := os.MkdirAll("/sys/fs/cgroup/", os.ModePerm); err != nil { + log.Fatal(err) + } + + os.Create("/sys/fs/cgroup/cgroup.controllers") + file, err := os.Create("/sys/fs/cgroup/cpu.max") + + if err != nil { + log.Fatal(err) + } + + file.WriteString("4 2"); + file.Sync(); + + assert.Equal(ginkgo.GinkgoT(), runtime.GetCgroupVersion(), int64(2)) + assert.Equal(ginkgo.GinkgoT(), runtime.NumCPU(), 2) + + os.Remove("/sys/fs/cgroup/cpu.max") + os.Remove("/sys/fs/cgroup/cgroup.controllers") + }) +})