2023-04-26 00:32:59 +00:00
|
|
|
/*
|
|
|
|
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"
|
2024-05-18 00:44:18 +00:00
|
|
|
"path/filepath"
|
2023-04-26 00:32:59 +00:00
|
|
|
|
|
|
|
"github.com/onsi/ginkgo/v2"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
|
|
|
"k8s.io/ingress-nginx/test/e2e/framework"
|
|
|
|
|
|
|
|
"k8s.io/ingress-nginx/pkg/util/runtime"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ = framework.IngressNginxDescribeSerial("[CGroups] cgroups", func() {
|
|
|
|
f := framework.NewDefaultFramework("cgroups")
|
|
|
|
|
|
|
|
ginkgo.BeforeEach(func() {
|
|
|
|
f.NewEchoDeployment()
|
|
|
|
f.NewSlowEchoDeployment()
|
|
|
|
})
|
|
|
|
|
|
|
|
ginkgo.It("detects cgroups version v1", func() {
|
2024-05-20 12:37:46 +00:00
|
|
|
cgroupPath := "/testing/sys/fs/cgroup/"
|
2023-04-26 00:32:59 +00:00
|
|
|
|
2023-07-17 03:47:43 +00:00
|
|
|
quotaFile, err := os.Create(filepath.Join(cgroupPath, "cpu.cfs_quota_us"))
|
2023-07-16 22:58:58 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2023-07-17 03:47:43 +00:00
|
|
|
periodFile, err := os.Create(filepath.Join(cgroupPath, "cpu.cfs_period_us"))
|
2024-05-18 00:44:18 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2023-04-26 00:32:59 +00:00
|
|
|
|
2024-05-18 00:44:18 +00:00
|
|
|
_, err = quotaFile.WriteString("4")
|
2023-07-16 22:58:58 +00:00
|
|
|
if err != nil {
|
2023-04-26 00:32:59 +00:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2024-05-18 00:44:18 +00:00
|
|
|
err = quotaFile.Sync()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = periodFile.WriteString("2")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2023-07-16 22:58:58 +00:00
|
|
|
|
2024-05-18 00:44:18 +00:00
|
|
|
err = periodFile.Sync()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2023-07-17 03:47:43 +00:00
|
|
|
|
2024-05-20 12:37:46 +00:00
|
|
|
assert.Equal(ginkgo.GinkgoT(), runtime.GetCgroupVersion(cgroupPath), int64(1))
|
|
|
|
assert.Equal(ginkgo.GinkgoT(), runtime.NumCPUWithCustomPath(cgroupPath), 2)
|
2023-07-16 22:58:58 +00:00
|
|
|
|
2023-07-17 03:47:43 +00:00
|
|
|
os.Remove(filepath.Join(cgroupPath, "cpu.cfs_quota_us"))
|
|
|
|
os.Remove(filepath.Join(cgroupPath, "cpu.cfs_period_us"))
|
2023-04-26 00:32:59 +00:00
|
|
|
})
|
|
|
|
|
2023-07-16 22:58:58 +00:00
|
|
|
ginkgo.It("detect cgroups version v2", func() {
|
2024-05-20 12:37:46 +00:00
|
|
|
cgroupPath := "/testing/sys/fs/cgroup/"
|
|
|
|
if err := os.MkdirAll(cgroupPath, os.ModePerm); err != nil {
|
2023-07-16 22:58:58 +00:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2024-05-20 12:37:46 +00:00
|
|
|
_, err := os.Create(filepath.Join(cgroupPath, "cgroup.controllers"))
|
2024-05-18 00:44:18 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2024-05-20 12:37:46 +00:00
|
|
|
file, err := os.Create(filepath.Join(cgroupPath, "cpu.max"))
|
2024-05-18 00:44:18 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2023-07-16 22:58:58 +00:00
|
|
|
|
2024-05-18 00:44:18 +00:00
|
|
|
_, err = file.WriteString("4 2")
|
2023-07-16 22:58:58 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2024-05-18 00:44:18 +00:00
|
|
|
err = file.Sync()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2023-07-17 03:47:43 +00:00
|
|
|
|
2024-05-20 12:37:46 +00:00
|
|
|
assert.Equal(ginkgo.GinkgoT(), runtime.GetCgroupVersion(cgroupPath), int64(2))
|
|
|
|
assert.Equal(ginkgo.GinkgoT(), runtime.NumCPUWithCustomPath(cgroupPath), 2)
|
2023-07-16 22:58:58 +00:00
|
|
|
|
2024-05-20 12:37:46 +00:00
|
|
|
os.Remove(filepath.Join(cgroupPath, "cpu.max"))
|
|
|
|
os.Remove(filepath.Join(cgroupPath, "cgroup.controllers"))
|
2023-04-26 00:32:59 +00:00
|
|
|
})
|
|
|
|
})
|