未验证 提交 369f93f3 编写于 作者: M Medya Ghazizadeh 提交者: GitHub

Merge pull request #10468 from afbjorklund/cgroups-warning

Make sure to show debian warning also for cgroup 2
// +build linux
/*
Copyright 2021 The Kubernetes Authors All rights reserved.
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 oci
import (
"syscall"
"golang.org/x/sys/unix"
)
// IsCgroup2UnifiedMode returns whether we are running in cgroup 2 cgroup2 mode.
func IsCgroup2UnifiedMode() (bool, error) {
var st syscall.Statfs_t
if err := syscall.Statfs("/sys/fs/cgroup", &st); err != nil {
return false, err
}
return st.Type == unix.CGROUP2_SUPER_MAGIC, nil
}
// +build !linux
/*
Copyright 2021 The Kubernetes Authors All rights reserved.
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 oci
import (
"runtime"
"github.com/pkg/errors"
)
// IsCgroup2UnifiedMode returns whether we are running in cgroup 2 cgroup2 mode.
func IsCgroup2UnifiedMode() (bool, error) {
return false, errors.Errorf("Not supported on %s", runtime.GOOS)
}
...@@ -107,6 +107,24 @@ func PrepareContainerNode(p CreateParams) error { ...@@ -107,6 +107,24 @@ func PrepareContainerNode(p CreateParams) error {
return nil return nil
} }
func hasMemorySwapCgroup() bool {
memcgSwap := true
if runtime.GOOS == "linux" {
var memoryswap string
if cgroup2, err := IsCgroup2UnifiedMode(); err == nil && cgroup2 {
memoryswap = "/sys/fs/cgroup/memory/memory.swap.max"
} else {
memoryswap = "/sys/fs/cgroup/memory/memsw.limit_in_bytes"
}
if _, err := os.Stat(memoryswap); os.IsNotExist(err) {
// requires CONFIG_MEMCG_SWAP_ENABLED or cgroup_enable=memory in grub
klog.Warning("Your kernel does not support swap limit capabilities or the cgroup is not mounted.")
memcgSwap = false
}
}
return memcgSwap
}
// CreateContainerNode creates a new container node // CreateContainerNode creates a new container node
func CreateContainerNode(p CreateParams) error { func CreateContainerNode(p CreateParams) error {
// on windows os, if docker desktop is using Windows Containers. Exit early with error // on windows os, if docker desktop is using Windows Containers. Exit early with error
...@@ -152,14 +170,7 @@ func CreateContainerNode(p CreateParams) error { ...@@ -152,14 +170,7 @@ func CreateContainerNode(p CreateParams) error {
runArgs = append(runArgs, "--ip", p.IP) runArgs = append(runArgs, "--ip", p.IP)
} }
memcgSwap := true memcgSwap := hasMemorySwapCgroup()
if runtime.GOOS == "linux" {
if _, err := os.Stat("/sys/fs/cgroup/memory/memsw.limit_in_bytes"); os.IsNotExist(err) {
// requires CONFIG_MEMCG_SWAP_ENABLED or cgroup_enable=memory in grub
klog.Warning("Your kernel does not support swap limit capabilities or the cgroup is not mounted.")
memcgSwap = false
}
}
// https://www.freedesktop.org/wiki/Software/systemd/ContainerInterface/ // https://www.freedesktop.org/wiki/Software/systemd/ContainerInterface/
var virtualization string var virtualization string
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册