From 6677ab6a63014237b8149b2aeba48fccf43ac7c1 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Wed, 14 Jul 2021 14:28:52 +0200 Subject: [PATCH] pkg/sysinfo: move cgMounts to be a field in SysInfo We pass the SysInfo struct to all functions. Adding cgMounts as a (non-exported) field makes passing around this information easier. Signed-off-by: Sebastiaan van Stijn --- pkg/sysinfo/cgroup2_linux.go | 2 +- pkg/sysinfo/sysinfo.go | 4 +++ pkg/sysinfo/sysinfo_linux.go | 47 +++++++++++++++++++----------------- 3 files changed, 30 insertions(+), 23 deletions(-) diff --git a/pkg/sysinfo/cgroup2_linux.go b/pkg/sysinfo/cgroup2_linux.go index 559f04adca..c3748fbb62 100644 --- a/pkg/sysinfo/cgroup2_linux.go +++ b/pkg/sysinfo/cgroup2_linux.go @@ -61,7 +61,7 @@ func newV2(quiet bool, options ...Opt) *SysInfo { applyCgroupNsInfo, } for _, o := range ops { - w := o(sysInfo, nil) + w := o(sysInfo) warnings = append(warnings, w...) } if !quiet { diff --git a/pkg/sysinfo/sysinfo.go b/pkg/sysinfo/sysinfo.go index 6e4b16847a..6d15acbe51 100644 --- a/pkg/sysinfo/sysinfo.go +++ b/pkg/sysinfo/sysinfo.go @@ -33,6 +33,10 @@ type SysInfo struct { // Whether the cgroup is in unified mode (v2). CgroupUnified bool + + // cgMounts is the list of cgroup v1 mount paths, indexed by subsystem, to + // inspect availability of subsystems. + cgMounts map[string]string } type cgroupMemInfo struct { diff --git a/pkg/sysinfo/sysinfo_linux.go b/pkg/sysinfo/sysinfo_linux.go index 30209f7dd7..7b7e5f896d 100644 --- a/pkg/sysinfo/sysinfo_linux.go +++ b/pkg/sysinfo/sysinfo_linux.go @@ -28,7 +28,7 @@ func findCgroupMountpoints() (map[string]string, error) { return mps, nil } -type infoCollector func(info *SysInfo, cgMounts map[string]string) (warnings []string) +type infoCollector func(info *SysInfo) (warnings []string) type opts struct { cg2GroupPath string @@ -60,10 +60,13 @@ func New(quiet bool, options ...Opt) *SysInfo { } func newV1(quiet bool) *SysInfo { - var ops []infoCollector - var warnings []string - sysInfo := &SysInfo{} - cgMounts, err := findCgroupMountpoints() + var ( + err error + ops []infoCollector + warnings []string + sysInfo = &SysInfo{} + ) + sysInfo.cgMounts, err = findCgroupMountpoints() if err != nil { logrus.Warn(err) } else { @@ -85,7 +88,7 @@ func newV1(quiet bool) *SysInfo { }...) for _, o := range ops { - w := o(sysInfo, cgMounts) + w := o(sysInfo) warnings = append(warnings, w...) } if !quiet { @@ -97,9 +100,9 @@ func newV1(quiet bool) *SysInfo { } // applyMemoryCgroupInfo adds the memory cgroup controller information to the info. -func applyMemoryCgroupInfo(info *SysInfo, cgMounts map[string]string) []string { +func applyMemoryCgroupInfo(info *SysInfo) []string { var warnings []string - mountPoint, ok := cgMounts["memory"] + mountPoint, ok := info.cgMounts["memory"] if !ok { warnings = append(warnings, "Your kernel does not support cgroup memory limit") return warnings @@ -135,9 +138,9 @@ func applyMemoryCgroupInfo(info *SysInfo, cgMounts map[string]string) []string { } // applyCPUCgroupInfo adds the cpu cgroup controller information to the info. -func applyCPUCgroupInfo(info *SysInfo, cgMounts map[string]string) []string { +func applyCPUCgroupInfo(info *SysInfo) []string { var warnings []string - mountPoint, ok := cgMounts["cpu"] + mountPoint, ok := info.cgMounts["cpu"] if !ok { warnings = append(warnings, "Unable to find cpu cgroup in mounts") return warnings @@ -162,9 +165,9 @@ func applyCPUCgroupInfo(info *SysInfo, cgMounts map[string]string) []string { } // applyBlkioCgroupInfo adds the blkio cgroup controller information to the info. -func applyBlkioCgroupInfo(info *SysInfo, cgMounts map[string]string) []string { +func applyBlkioCgroupInfo(info *SysInfo) []string { var warnings []string - mountPoint, ok := cgMounts["blkio"] + mountPoint, ok := info.cgMounts["blkio"] if !ok { warnings = append(warnings, "Unable to find blkio cgroup in mounts") return warnings @@ -203,9 +206,9 @@ func applyBlkioCgroupInfo(info *SysInfo, cgMounts map[string]string) []string { } // applyCPUSetCgroupInfo adds the cpuset cgroup controller information to the info. -func applyCPUSetCgroupInfo(info *SysInfo, cgMounts map[string]string) []string { +func applyCPUSetCgroupInfo(info *SysInfo) []string { var warnings []string - mountPoint, ok := cgMounts["cpuset"] + mountPoint, ok := info.cgMounts["cpuset"] if !ok { warnings = append(warnings, "Unable to find cpuset cgroup in mounts") return warnings @@ -230,9 +233,9 @@ func applyCPUSetCgroupInfo(info *SysInfo, cgMounts map[string]string) []string { } // applyPIDSCgroupInfo adds whether the pids cgroup controller is available to the info. -func applyPIDSCgroupInfo(info *SysInfo, cgMounts map[string]string) []string { +func applyPIDSCgroupInfo(info *SysInfo) []string { var warnings []string - _, ok := cgMounts["pids"] + _, ok := info.cgMounts["pids"] if !ok { warnings = append(warnings, "Unable to find pids cgroup in mounts") return warnings @@ -242,15 +245,15 @@ func applyPIDSCgroupInfo(info *SysInfo, cgMounts map[string]string) []string { } // applyDevicesCgroupInfo adds whether the devices cgroup controller is available to the info. -func applyDevicesCgroupInfo(info *SysInfo, cgMounts map[string]string) []string { +func applyDevicesCgroupInfo(info *SysInfo) []string { var warnings []string - _, ok := cgMounts["devices"] + _, ok := info.cgMounts["devices"] info.CgroupDevicesEnabled = ok return warnings } // applyNetworkingInfo adds networking information to the info. -func applyNetworkingInfo(info *SysInfo, _ map[string]string) []string { +func applyNetworkingInfo(info *SysInfo) []string { var warnings []string info.IPv4ForwardingDisabled = !readProcBool("/proc/sys/net/ipv4/ip_forward") info.BridgeNFCallIPTablesDisabled = !readProcBool("/proc/sys/net/bridge/bridge-nf-call-iptables") @@ -259,7 +262,7 @@ func applyNetworkingInfo(info *SysInfo, _ map[string]string) []string { } // applyAppArmorInfo adds whether AppArmor is enabled to the info. -func applyAppArmorInfo(info *SysInfo, _ map[string]string) []string { +func applyAppArmorInfo(info *SysInfo) []string { var warnings []string if _, err := os.Stat("/sys/kernel/security/apparmor"); !os.IsNotExist(err) { if _, err := ioutil.ReadFile("/sys/kernel/security/apparmor/profiles"); err == nil { @@ -270,7 +273,7 @@ func applyAppArmorInfo(info *SysInfo, _ map[string]string) []string { } // applyCgroupNsInfo adds whether cgroupns is enabled to the info. -func applyCgroupNsInfo(info *SysInfo, _ map[string]string) []string { +func applyCgroupNsInfo(info *SysInfo) []string { var warnings []string if _, err := os.Stat("/proc/self/ns/cgroup"); !os.IsNotExist(err) { info.CgroupNamespaces = true @@ -284,7 +287,7 @@ var ( ) // applySeccompInfo checks if Seccomp is supported, via CONFIG_SECCOMP. -func applySeccompInfo(info *SysInfo, _ map[string]string) []string { +func applySeccompInfo(info *SysInfo) []string { var warnings []string seccompOnce.Do(func() { // Check if Seccomp is supported, via CONFIG_SECCOMP.