소스 검색

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>
Sebastiaan van Stijn 4 년 전
부모
커밋
208d3c6efb
3개의 변경된 파일36개의 추가작업 그리고 37개의 파일을 삭제
  1. 23 27
      pkg/sysinfo/cgroup2_linux.go
  2. 3 0
      pkg/sysinfo/sysinfo.go
  3. 10 10
      pkg/sysinfo/sysinfo_linux.go

+ 23 - 27
pkg/sysinfo/cgroup2_linux.go

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

+ 3 - 0
pkg/sysinfo/sysinfo.go

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

+ 10 - 10
pkg/sysinfo/sysinfo_linux.go

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