fix: conditional compiling for cgroups code
parent
947ff0435b
commit
f31a1d5020
2
go.mod
2
go.mod
|
|
@ -99,6 +99,7 @@ require (
|
|||
require (
|
||||
github.com/Xuanwo/go-locale v1.1.0
|
||||
github.com/docker/go-connections v0.4.0
|
||||
github.com/opencontainers/runc v1.0.2
|
||||
github.com/santhosh-tekuri/jsonschema/v5 v5.0.0
|
||||
)
|
||||
|
||||
|
|
@ -175,7 +176,6 @@ require (
|
|||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||
github.com/opencontainers/image-spec v1.0.1 // indirect
|
||||
github.com/opencontainers/runc v1.0.2 // indirect
|
||||
github.com/opencontainers/runtime-spec v1.0.3-0.20210326190908-1c3f411f0417 // indirect
|
||||
github.com/pelletier/go-toml v1.9.4 // indirect
|
||||
github.com/pelletier/go-toml/v2 v2.0.0-beta.8 // indirect
|
||||
|
|
|
|||
2
go.sum
2
go.sum
|
|
@ -331,7 +331,6 @@ github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3Ee
|
|||
github.com/coreos/go-systemd v0.0.0-20161114122254-48702e0da86b/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
|
||||
github.com/coreos/go-systemd v0.0.0-20180511133405-39ca1b05acc7/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
|
||||
github.com/coreos/go-systemd v0.0.0-20181012123002-c6f51f82210d/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
|
||||
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e h1:Wf6HqHfScWJN9/ZjdUKyjop4mf3Qdd+1TvvltAvM3m8=
|
||||
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
|
||||
github.com/coreos/go-systemd/v22 v22.0.0/go.mod h1:xO0FLkIi5MaZafQlIrOotqXZ90ih+1atmu1JpKERPPk=
|
||||
github.com/coreos/go-systemd/v22 v22.1.0/go.mod h1:xO0FLkIi5MaZafQlIrOotqXZ90ih+1atmu1JpKERPPk=
|
||||
|
|
@ -484,7 +483,6 @@ github.com/go-pdf/fpdf v0.6.0/go.mod h1:HzcnA+A23uwogo0tp9yU+l3V+KXhiESpt1PMayhO
|
|||
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
|
||||
github.com/godbus/dbus v0.0.0-20151105175453-c7fdd8b5cd55/go.mod h1:/YcGZj5zSblfDWMMoOzV4fas9FZnQYTkDnsGvmh2Grw=
|
||||
github.com/godbus/dbus v0.0.0-20180201030542-885f9cc04c9c/go.mod h1:/YcGZj5zSblfDWMMoOzV4fas9FZnQYTkDnsGvmh2Grw=
|
||||
github.com/godbus/dbus v0.0.0-20190422162347-ade71ed3457e h1:BWhy2j3IXJhjCbC68FptL43tDKIq8FladmaTs3Xs7Z8=
|
||||
github.com/godbus/dbus v0.0.0-20190422162347-ade71ed3457e/go.mod h1:bBOAhwG1umN6/6ZUMtDFBMQR8jRg9O75tm9K00oMsK4=
|
||||
github.com/godbus/dbus/v5 v5.0.3/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
|
||||
github.com/godbus/dbus/v5 v5.0.4 h1:9349emZab16e7zQvpmsbtjc18ykshndd8y2PG3sgJbA=
|
||||
|
|
|
|||
|
|
@ -19,9 +19,15 @@ limitations under the License.
|
|||
package oci
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
"syscall"
|
||||
|
||||
"github.com/opencontainers/runc/libcontainer/cgroups"
|
||||
"golang.org/x/sys/unix"
|
||||
|
||||
"k8s.io/klog/v2"
|
||||
)
|
||||
|
||||
// IsCgroup2UnifiedMode returns whether we are running in cgroup 2 cgroup2 mode.
|
||||
|
|
@ -32,3 +38,56 @@ func IsCgroup2UnifiedMode() (bool, error) {
|
|||
}
|
||||
return st.Type == unix.CGROUP2_SUPER_MAGIC, nil
|
||||
}
|
||||
|
||||
// findCgroupMountpoints returns the cgroups mount point
|
||||
// defined in docker engine engine/pkg/sysinfo/sysinfo_linux.go
|
||||
func findCgroupMountpoints() (map[string]string, error) {
|
||||
cgMounts, err := cgroups.GetCgroupMounts(false)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to parse cgroup information: %v", err)
|
||||
}
|
||||
mps := make(map[string]string)
|
||||
for _, m := range cgMounts {
|
||||
for _, ss := range m.Subsystems {
|
||||
mps[ss] = m.Mountpoint
|
||||
}
|
||||
}
|
||||
return mps, nil
|
||||
}
|
||||
|
||||
// HasMemoryCgroup checks whether it is possible to set memory limit for cgroup.
|
||||
func HasMemoryCgroup() bool {
|
||||
cgMounts, err := findCgroupMountpoints()
|
||||
if err != nil {
|
||||
klog.Warning("Your kernel does not support memory limit capabilities or the cgroup is not mounted.")
|
||||
return false
|
||||
}
|
||||
_, ok := cgMounts["memory"]
|
||||
if !ok {
|
||||
klog.Warning("Your kernel does not support memory limit capabilities or the cgroup is not mounted.")
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// hasMemorySwapCgroup checks whether it is possible to set swap limit for cgroup
|
||||
func hasMemorySwapCgroup() bool {
|
||||
cgMounts, err := findCgroupMountpoints()
|
||||
if err != nil {
|
||||
klog.Warning("Your kernel does not support swap limit capabilities or the cgroup is not mounted.")
|
||||
return false
|
||||
}
|
||||
mountPoint, ok := cgMounts["memory"]
|
||||
if !ok {
|
||||
klog.Warning("Your kernel does not support swap limit capabilities or the cgroup is not mounted.")
|
||||
return false
|
||||
}
|
||||
|
||||
_, err = os.Stat(path.Join(mountPoint, "memory.memsw.limit_in_bytesw"))
|
||||
if err != nil {
|
||||
klog.Warning("Your kernel does not support swap limit capabilities or the cgroup is not mounted.")
|
||||
return false
|
||||
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,3 +28,11 @@ import (
|
|||
func IsCgroup2UnifiedMode() (bool, error) {
|
||||
return false, errors.Errorf("Not supported on %s", runtime.GOOS)
|
||||
}
|
||||
|
||||
func HasMemoryCgroup() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func hasMemorySwapCgroup() bool {
|
||||
return true
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,7 +24,6 @@ import (
|
|||
"net/url"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
|
@ -32,7 +31,6 @@ import (
|
|||
|
||||
"github.com/blang/semver/v4"
|
||||
"github.com/docker/machine/libmachine/state"
|
||||
"github.com/opencontainers/runc/libcontainer/cgroups"
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"k8s.io/klog/v2"
|
||||
|
|
@ -42,22 +40,6 @@ import (
|
|||
"k8s.io/minikube/pkg/util/retry"
|
||||
)
|
||||
|
||||
// findCgroupMountpoints returns the cgroups mount point
|
||||
// defined in docker engine engine/pkg/sysinfo/sysinfo_linux.go
|
||||
func findCgroupMountpoints() (map[string]string, error) {
|
||||
cgMounts, err := cgroups.GetCgroupMounts(false)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to parse cgroup information: %v", err)
|
||||
}
|
||||
mps := make(map[string]string)
|
||||
for _, m := range cgMounts {
|
||||
for _, ss := range m.Subsystems {
|
||||
mps[ss] = m.Mountpoint
|
||||
}
|
||||
}
|
||||
return mps, nil
|
||||
}
|
||||
|
||||
// DeleteContainersByLabel deletes all containers that have a specific label
|
||||
// if there no containers found with the given label, it will return nil
|
||||
func DeleteContainersByLabel(ociBin string, label string) []error {
|
||||
|
|
@ -126,47 +108,6 @@ func PrepareContainerNode(p CreateParams) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// HasMemoryCgroup checks whether it is possible to set memory limit for cgroup.
|
||||
func HasMemoryCgroup() bool {
|
||||
if runtime.GOOS == "linux" {
|
||||
cgMounts, err := findCgroupMountpoints()
|
||||
if err != nil {
|
||||
klog.Warning("Your kernel does not support memory limit capabilities or the cgroup is not mounted.")
|
||||
return false
|
||||
}
|
||||
_, ok := cgMounts["memory"]
|
||||
if !ok {
|
||||
klog.Warning("Your kernel does not support memory limit capabilities or the cgroup is not mounted.")
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// hasMemorySwapCgroup checks whether it is possible to set swap limit for cgroup
|
||||
func hasMemorySwapCgroup() bool {
|
||||
if runtime.GOOS == "linux" {
|
||||
cgMounts, err := findCgroupMountpoints()
|
||||
if err != nil {
|
||||
klog.Warning("Your kernel does not support swap limit capabilities or the cgroup is not mounted.")
|
||||
return false
|
||||
}
|
||||
mountPoint, ok := cgMounts["memory"]
|
||||
if !ok {
|
||||
klog.Warning("Your kernel does not support swap limit capabilities or the cgroup is not mounted.")
|
||||
return false
|
||||
}
|
||||
|
||||
_, err = os.Stat(path.Join(mountPoint, "memory.memsw.limit_in_bytesw"))
|
||||
if err != nil {
|
||||
klog.Warning("Your kernel does not support swap limit capabilities or the cgroup is not mounted.")
|
||||
return false
|
||||
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// CreateContainerNode creates a new container node
|
||||
func CreateContainerNode(p CreateParams) error {
|
||||
// on windows os, if docker desktop is using Windows Containers. Exit early with error
|
||||
|
|
|
|||
Loading…
Reference in New Issue