Merge pull request #19558 from LK4D4/reduce_cgroup_parsing
Refactor sysinfo usage
This commit is contained in:
commit
54ba7f08d5
1 changed files with 42 additions and 22 deletions
|
@ -1,6 +1,7 @@
|
||||||
package sysinfo
|
package sysinfo
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
@ -16,18 +17,37 @@ const (
|
||||||
SeccompModeFilter = uintptr(2)
|
SeccompModeFilter = uintptr(2)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func findCgroupMountpoints() (map[string]string, error) {
|
||||||
|
cgMounts, err := cgroups.GetCgroupMounts()
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
// New returns a new SysInfo, using the filesystem to detect which features
|
// New returns a new SysInfo, using the filesystem to detect which features
|
||||||
// the kernel supports. If `quiet` is `false` warnings are printed in logs
|
// the kernel supports. If `quiet` is `false` warnings are printed in logs
|
||||||
// whenever an error occurs or misconfigurations are present.
|
// whenever an error occurs or misconfigurations are present.
|
||||||
func New(quiet bool) *SysInfo {
|
func New(quiet bool) *SysInfo {
|
||||||
sysInfo := &SysInfo{}
|
sysInfo := &SysInfo{}
|
||||||
sysInfo.cgroupMemInfo = checkCgroupMem(quiet)
|
cgMounts, err := findCgroupMountpoints()
|
||||||
sysInfo.cgroupCPUInfo = checkCgroupCPU(quiet)
|
if err != nil {
|
||||||
sysInfo.cgroupBlkioInfo = checkCgroupBlkioInfo(quiet)
|
logrus.Warnf("Failed to parse cgroup information: %v", err)
|
||||||
sysInfo.cgroupCpusetInfo = checkCgroupCpusetInfo(quiet)
|
} else {
|
||||||
|
sysInfo.cgroupMemInfo = checkCgroupMem(cgMounts, quiet)
|
||||||
|
sysInfo.cgroupCPUInfo = checkCgroupCPU(cgMounts, quiet)
|
||||||
|
sysInfo.cgroupBlkioInfo = checkCgroupBlkioInfo(cgMounts, quiet)
|
||||||
|
sysInfo.cgroupCpusetInfo = checkCgroupCpusetInfo(cgMounts, quiet)
|
||||||
|
}
|
||||||
|
|
||||||
_, err := cgroups.FindCgroupMountpoint("devices")
|
_, ok := cgMounts["devices"]
|
||||||
sysInfo.CgroupDevicesEnabled = err == nil
|
sysInfo.CgroupDevicesEnabled = ok
|
||||||
|
|
||||||
sysInfo.IPv4ForwardingDisabled = !readProcBool("/proc/sys/net/ipv4/ip_forward")
|
sysInfo.IPv4ForwardingDisabled = !readProcBool("/proc/sys/net/ipv4/ip_forward")
|
||||||
sysInfo.BridgeNfCallIptablesDisabled = !readProcBool("/proc/sys/net/bridge/bridge-nf-call-iptables")
|
sysInfo.BridgeNfCallIptablesDisabled = !readProcBool("/proc/sys/net/bridge/bridge-nf-call-iptables")
|
||||||
|
@ -50,11 +70,11 @@ func New(quiet bool) *SysInfo {
|
||||||
}
|
}
|
||||||
|
|
||||||
// checkCgroupMem reads the memory information from the memory cgroup mount point.
|
// checkCgroupMem reads the memory information from the memory cgroup mount point.
|
||||||
func checkCgroupMem(quiet bool) cgroupMemInfo {
|
func checkCgroupMem(cgMounts map[string]string, quiet bool) cgroupMemInfo {
|
||||||
mountPoint, err := cgroups.FindCgroupMountpoint("memory")
|
mountPoint, ok := cgMounts["memory"]
|
||||||
if err != nil {
|
if !ok {
|
||||||
if !quiet {
|
if !quiet {
|
||||||
logrus.Warnf("Your kernel does not support cgroup memory limit: %v", err)
|
logrus.Warnf("Your kernel does not support cgroup memory limit")
|
||||||
}
|
}
|
||||||
return cgroupMemInfo{}
|
return cgroupMemInfo{}
|
||||||
}
|
}
|
||||||
|
@ -91,11 +111,11 @@ func checkCgroupMem(quiet bool) cgroupMemInfo {
|
||||||
}
|
}
|
||||||
|
|
||||||
// checkCgroupCPU reads the cpu information from the cpu cgroup mount point.
|
// checkCgroupCPU reads the cpu information from the cpu cgroup mount point.
|
||||||
func checkCgroupCPU(quiet bool) cgroupCPUInfo {
|
func checkCgroupCPU(cgMounts map[string]string, quiet bool) cgroupCPUInfo {
|
||||||
mountPoint, err := cgroups.FindCgroupMountpoint("cpu")
|
mountPoint, ok := cgMounts["cpu"]
|
||||||
if err != nil {
|
if !ok {
|
||||||
if !quiet {
|
if !quiet {
|
||||||
logrus.Warn(err)
|
logrus.Warnf("Unable to find cpu cgroup in mounts")
|
||||||
}
|
}
|
||||||
return cgroupCPUInfo{}
|
return cgroupCPUInfo{}
|
||||||
}
|
}
|
||||||
|
@ -122,11 +142,11 @@ func checkCgroupCPU(quiet bool) cgroupCPUInfo {
|
||||||
}
|
}
|
||||||
|
|
||||||
// checkCgroupBlkioInfo reads the blkio information from the blkio cgroup mount point.
|
// checkCgroupBlkioInfo reads the blkio information from the blkio cgroup mount point.
|
||||||
func checkCgroupBlkioInfo(quiet bool) cgroupBlkioInfo {
|
func checkCgroupBlkioInfo(cgMounts map[string]string, quiet bool) cgroupBlkioInfo {
|
||||||
mountPoint, err := cgroups.FindCgroupMountpoint("blkio")
|
mountPoint, ok := cgMounts["blkio"]
|
||||||
if err != nil {
|
if !ok {
|
||||||
if !quiet {
|
if !quiet {
|
||||||
logrus.Warn(err)
|
logrus.Warnf("Unable to find blkio cgroup in mounts")
|
||||||
}
|
}
|
||||||
return cgroupBlkioInfo{}
|
return cgroupBlkioInfo{}
|
||||||
}
|
}
|
||||||
|
@ -170,11 +190,11 @@ func checkCgroupBlkioInfo(quiet bool) cgroupBlkioInfo {
|
||||||
}
|
}
|
||||||
|
|
||||||
// checkCgroupCpusetInfo reads the cpuset information from the cpuset cgroup mount point.
|
// checkCgroupCpusetInfo reads the cpuset information from the cpuset cgroup mount point.
|
||||||
func checkCgroupCpusetInfo(quiet bool) cgroupCpusetInfo {
|
func checkCgroupCpusetInfo(cgMounts map[string]string, quiet bool) cgroupCpusetInfo {
|
||||||
mountPoint, err := cgroups.FindCgroupMountpoint("cpuset")
|
mountPoint, ok := cgMounts["cpuset"]
|
||||||
if err != nil {
|
if !ok {
|
||||||
if !quiet {
|
if !quiet {
|
||||||
logrus.Warn(err)
|
logrus.Warnf("Unable to find cpuset cgroup in mounts")
|
||||||
}
|
}
|
||||||
return cgroupCpusetInfo{}
|
return cgroupCpusetInfo{}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue