1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

pkg/sysinfo: move cg2Controllers to be a field in SysInfo and unify v1/v2

We pass the SysInfo struct to all functions. Adding cg2Controllers as a
(non-exported) field makes passing around this information easier.

Now that infoCollector and infoCollectorV2 have the same signature, we can
simplify some bits and use a single slice for all "collectors".

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2021-07-14 15:44:38 +02:00
parent 5cc20ad9e5
commit 208d3c6efb
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C
3 changed files with 47 additions and 48 deletions

View file

@ -12,8 +12,6 @@ import (
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
type infoCollectorV2 func(info *SysInfo, controllers map[string]struct{}) (warnings []string)
func newV2(quiet bool, options ...Opt) *SysInfo { func newV2(quiet bool, options ...Opt) *SysInfo {
var warnings []string var warnings []string
sysInfo := &SysInfo{ sysInfo := &SysInfo{
@ -23,31 +21,6 @@ func newV2(quiet bool, options ...Opt) *SysInfo {
for _, o := range options { for _, o := range options {
o(sysInfo) o(sysInfo)
} }
m, err := cgroupsV2.LoadManager("/sys/fs/cgroup", sysInfo.cg2GroupPath)
if err != nil {
logrus.Warn(err)
} else {
controllersM := make(map[string]struct{})
controllers, err := m.Controllers()
if err != nil {
logrus.Warn(err)
}
for _, c := range controllers {
controllersM[c] = struct{}{}
}
opsV2 := []infoCollectorV2{
applyMemoryCgroupInfoV2,
applyCPUCgroupInfoV2,
applyIOCgroupInfoV2,
applyCPUSetCgroupInfoV2,
applyPIDSCgroupInfoV2,
applyDevicesCgroupInfoV2,
}
for _, o := range opsV2 {
w := o(sysInfo, controllersM)
warnings = append(warnings, w...)
}
}
ops := []infoCollector{ ops := []infoCollector{
applyNetworkingInfo, applyNetworkingInfo,
@ -55,6 +28,29 @@ func newV2(quiet bool, options ...Opt) *SysInfo {
applySeccompInfo, applySeccompInfo,
applyCgroupNsInfo, applyCgroupNsInfo,
} }
m, err := cgroupsV2.LoadManager("/sys/fs/cgroup", sysInfo.cg2GroupPath)
if err != nil {
logrus.Warn(err)
} else {
sysInfo.cg2Controllers = make(map[string]struct{})
controllers, err := m.Controllers()
if err != nil {
logrus.Warn(err)
}
for _, c := range controllers {
sysInfo.cg2Controllers[c] = struct{}{}
}
ops = append(ops,
applyMemoryCgroupInfoV2,
applyCPUCgroupInfoV2,
applyIOCgroupInfoV2,
applyCPUSetCgroupInfoV2,
applyPIDSCgroupInfoV2,
applyDevicesCgroupInfoV2,
)
}
for _, o := range ops { for _, o := range ops {
w := o(sysInfo) w := o(sysInfo)
warnings = append(warnings, w...) warnings = append(warnings, w...)
@ -85,9 +81,9 @@ func getSwapLimitV2() bool {
return true return true
} }
func applyMemoryCgroupInfoV2(info *SysInfo, controllers map[string]struct{}) []string { func applyMemoryCgroupInfoV2(info *SysInfo) []string {
var warnings []string var warnings []string
if _, ok := controllers["memory"]; !ok { if _, ok := info.cg2Controllers["memory"]; !ok {
warnings = append(warnings, "Unable to find memory controller") warnings = append(warnings, "Unable to find memory controller")
return warnings return warnings
} }
@ -102,9 +98,9 @@ func applyMemoryCgroupInfoV2(info *SysInfo, controllers map[string]struct{}) []s
return warnings return warnings
} }
func applyCPUCgroupInfoV2(info *SysInfo, controllers map[string]struct{}) []string { func applyCPUCgroupInfoV2(info *SysInfo) []string {
var warnings []string var warnings []string
if _, ok := controllers["cpu"]; !ok { if _, ok := info.cg2Controllers["cpu"]; !ok {
warnings = append(warnings, "Unable to find cpu controller") warnings = append(warnings, "Unable to find cpu controller")
return warnings return warnings
} }
@ -114,9 +110,9 @@ func applyCPUCgroupInfoV2(info *SysInfo, controllers map[string]struct{}) []stri
return warnings return warnings
} }
func applyIOCgroupInfoV2(info *SysInfo, controllers map[string]struct{}) []string { func applyIOCgroupInfoV2(info *SysInfo) []string {
var warnings []string var warnings []string
if _, ok := controllers["io"]; !ok { if _, ok := info.cg2Controllers["io"]; !ok {
warnings = append(warnings, "Unable to find io controller") warnings = append(warnings, "Unable to find io controller")
return warnings return warnings
} }
@ -130,9 +126,9 @@ func applyIOCgroupInfoV2(info *SysInfo, controllers map[string]struct{}) []strin
return warnings return warnings
} }
func applyCPUSetCgroupInfoV2(info *SysInfo, controllers map[string]struct{}) []string { func applyCPUSetCgroupInfoV2(info *SysInfo) []string {
var warnings []string var warnings []string
if _, ok := controllers["cpuset"]; !ok { if _, ok := info.cg2Controllers["cpuset"]; !ok {
warnings = append(warnings, "Unable to find cpuset controller") warnings = append(warnings, "Unable to find cpuset controller")
return warnings return warnings
} }
@ -152,9 +148,9 @@ func applyCPUSetCgroupInfoV2(info *SysInfo, controllers map[string]struct{}) []s
return warnings return warnings
} }
func applyPIDSCgroupInfoV2(info *SysInfo, controllers map[string]struct{}) []string { func applyPIDSCgroupInfoV2(info *SysInfo) []string {
var warnings []string var warnings []string
if _, ok := controllers["pids"]; !ok { if _, ok := info.cg2Controllers["pids"]; !ok {
warnings = append(warnings, "Unable to find pids controller") warnings = append(warnings, "Unable to find pids controller")
return warnings return warnings
} }
@ -162,7 +158,7 @@ func applyPIDSCgroupInfoV2(info *SysInfo, controllers map[string]struct{}) []str
return warnings return warnings
} }
func applyDevicesCgroupInfoV2(info *SysInfo, controllers map[string]struct{}) []string { func applyDevicesCgroupInfoV2(info *SysInfo) []string {
info.CgroupDevicesEnabled = !userns.RunningInUserNS() info.CgroupDevicesEnabled = !userns.RunningInUserNS()
return nil return nil
} }

View file

@ -43,6 +43,9 @@ type SysInfo struct {
// cg2GroupPath is the cgroup v2 group path to inspect availability of the controllers. // cg2GroupPath is the cgroup v2 group path to inspect availability of the controllers.
cg2GroupPath string cg2GroupPath string
// cg2Controllers is an index of available cgroup v2 controllers.
cg2Controllers map[string]struct{}
} }
type cgroupMemInfo struct { type cgroupMemInfo struct {

View file

@ -57,31 +57,31 @@ func New(quiet bool, options ...Opt) *SysInfo {
func newV1(quiet bool) *SysInfo { func newV1(quiet bool) *SysInfo {
var ( var (
err error err error
ops []infoCollector
warnings []string warnings []string
sysInfo = &SysInfo{} sysInfo = &SysInfo{}
) )
ops := []infoCollector{
applyNetworkingInfo,
applyAppArmorInfo,
applySeccompInfo,
applyCgroupNsInfo,
}
sysInfo.cgMounts, err = findCgroupMountpoints() sysInfo.cgMounts, err = findCgroupMountpoints()
if err != nil { if err != nil {
logrus.Warn(err) logrus.Warn(err)
} else { } else {
ops = append(ops, []infoCollector{ ops = append(ops,
applyMemoryCgroupInfo, applyMemoryCgroupInfo,
applyCPUCgroupInfo, applyCPUCgroupInfo,
applyBlkioCgroupInfo, applyBlkioCgroupInfo,
applyCPUSetCgroupInfo, applyCPUSetCgroupInfo,
applyPIDSCgroupInfo, applyPIDSCgroupInfo,
applyDevicesCgroupInfo, applyDevicesCgroupInfo,
}...) )
} }
ops = append(ops, []infoCollector{
applyNetworkingInfo,
applyAppArmorInfo,
applySeccompInfo,
applyCgroupNsInfo,
}...)
for _, o := range ops { for _, o := range ops {
w := o(sysInfo) w := o(sysInfo)
warnings = append(warnings, w...) warnings = append(warnings, w...)