Browse code

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 authored on 2021/07/14 22:44:38
Showing 3 changed files
... ...
@@ -12,8 +12,6 @@ import (
12 12
 	"github.com/sirupsen/logrus"
13 13
 )
14 14
 
15
-type infoCollectorV2 func(info *SysInfo, controllers map[string]struct{}) (warnings []string)
16
-
17 15
 func newV2(quiet bool, options ...Opt) *SysInfo {
18 16
 	var warnings []string
19 17
 	sysInfo := &SysInfo{
... ...
@@ -23,38 +21,36 @@ func newV2(quiet bool, options ...Opt) *SysInfo {
23 23
 	for _, o := range options {
24 24
 		o(sysInfo)
25 25
 	}
26
+
27
+	ops := []infoCollector{
28
+		applyNetworkingInfo,
29
+		applyAppArmorInfo,
30
+		applySeccompInfo,
31
+		applyCgroupNsInfo,
32
+	}
33
+
26 34
 	m, err := cgroupsV2.LoadManager("/sys/fs/cgroup", sysInfo.cg2GroupPath)
27 35
 	if err != nil {
28 36
 		logrus.Warn(err)
29 37
 	} else {
30
-		controllersM := make(map[string]struct{})
38
+		sysInfo.cg2Controllers = make(map[string]struct{})
31 39
 		controllers, err := m.Controllers()
32 40
 		if err != nil {
33 41
 			logrus.Warn(err)
34 42
 		}
35 43
 		for _, c := range controllers {
36
-			controllersM[c] = struct{}{}
44
+			sysInfo.cg2Controllers[c] = struct{}{}
37 45
 		}
38
-		opsV2 := []infoCollectorV2{
46
+		ops = append(ops,
39 47
 			applyMemoryCgroupInfoV2,
40 48
 			applyCPUCgroupInfoV2,
41 49
 			applyIOCgroupInfoV2,
42 50
 			applyCPUSetCgroupInfoV2,
43 51
 			applyPIDSCgroupInfoV2,
44 52
 			applyDevicesCgroupInfoV2,
45
-		}
46
-		for _, o := range opsV2 {
47
-			w := o(sysInfo, controllersM)
48
-			warnings = append(warnings, w...)
49
-		}
53
+		)
50 54
 	}
51 55
 
52
-	ops := []infoCollector{
53
-		applyNetworkingInfo,
54
-		applyAppArmorInfo,
55
-		applySeccompInfo,
56
-		applyCgroupNsInfo,
57
-	}
58 56
 	for _, o := range ops {
59 57
 		w := o(sysInfo)
60 58
 		warnings = append(warnings, w...)
... ...
@@ -85,9 +81,9 @@ func getSwapLimitV2() bool {
85 85
 	return true
86 86
 }
87 87
 
88
-func applyMemoryCgroupInfoV2(info *SysInfo, controllers map[string]struct{}) []string {
88
+func applyMemoryCgroupInfoV2(info *SysInfo) []string {
89 89
 	var warnings []string
90
-	if _, ok := controllers["memory"]; !ok {
90
+	if _, ok := info.cg2Controllers["memory"]; !ok {
91 91
 		warnings = append(warnings, "Unable to find memory controller")
92 92
 		return warnings
93 93
 	}
... ...
@@ -102,9 +98,9 @@ func applyMemoryCgroupInfoV2(info *SysInfo, controllers map[string]struct{}) []s
102 102
 	return warnings
103 103
 }
104 104
 
105
-func applyCPUCgroupInfoV2(info *SysInfo, controllers map[string]struct{}) []string {
105
+func applyCPUCgroupInfoV2(info *SysInfo) []string {
106 106
 	var warnings []string
107
-	if _, ok := controllers["cpu"]; !ok {
107
+	if _, ok := info.cg2Controllers["cpu"]; !ok {
108 108
 		warnings = append(warnings, "Unable to find cpu controller")
109 109
 		return warnings
110 110
 	}
... ...
@@ -114,9 +110,9 @@ func applyCPUCgroupInfoV2(info *SysInfo, controllers map[string]struct{}) []stri
114 114
 	return warnings
115 115
 }
116 116
 
117
-func applyIOCgroupInfoV2(info *SysInfo, controllers map[string]struct{}) []string {
117
+func applyIOCgroupInfoV2(info *SysInfo) []string {
118 118
 	var warnings []string
119
-	if _, ok := controllers["io"]; !ok {
119
+	if _, ok := info.cg2Controllers["io"]; !ok {
120 120
 		warnings = append(warnings, "Unable to find io controller")
121 121
 		return warnings
122 122
 	}
... ...
@@ -130,9 +126,9 @@ func applyIOCgroupInfoV2(info *SysInfo, controllers map[string]struct{}) []strin
130 130
 	return warnings
131 131
 }
132 132
 
133
-func applyCPUSetCgroupInfoV2(info *SysInfo, controllers map[string]struct{}) []string {
133
+func applyCPUSetCgroupInfoV2(info *SysInfo) []string {
134 134
 	var warnings []string
135
-	if _, ok := controllers["cpuset"]; !ok {
135
+	if _, ok := info.cg2Controllers["cpuset"]; !ok {
136 136
 		warnings = append(warnings, "Unable to find cpuset controller")
137 137
 		return warnings
138 138
 	}
... ...
@@ -152,9 +148,9 @@ func applyCPUSetCgroupInfoV2(info *SysInfo, controllers map[string]struct{}) []s
152 152
 	return warnings
153 153
 }
154 154
 
155
-func applyPIDSCgroupInfoV2(info *SysInfo, controllers map[string]struct{}) []string {
155
+func applyPIDSCgroupInfoV2(info *SysInfo) []string {
156 156
 	var warnings []string
157
-	if _, ok := controllers["pids"]; !ok {
157
+	if _, ok := info.cg2Controllers["pids"]; !ok {
158 158
 		warnings = append(warnings, "Unable to find pids controller")
159 159
 		return warnings
160 160
 	}
... ...
@@ -162,7 +158,7 @@ func applyPIDSCgroupInfoV2(info *SysInfo, controllers map[string]struct{}) []str
162 162
 	return warnings
163 163
 }
164 164
 
165
-func applyDevicesCgroupInfoV2(info *SysInfo, controllers map[string]struct{}) []string {
165
+func applyDevicesCgroupInfoV2(info *SysInfo) []string {
166 166
 	info.CgroupDevicesEnabled = !userns.RunningInUserNS()
167 167
 	return nil
168 168
 }
... ...
@@ -43,6 +43,9 @@ type SysInfo struct {
43 43
 
44 44
 	// cg2GroupPath is the cgroup v2 group path to inspect availability of the controllers.
45 45
 	cg2GroupPath string
46
+
47
+	// cg2Controllers is an index of available cgroup v2 controllers.
48
+	cg2Controllers map[string]struct{}
46 49
 }
47 50
 
48 51
 type cgroupMemInfo struct {
... ...
@@ -57,31 +57,31 @@ func New(quiet bool, options ...Opt) *SysInfo {
57 57
 func newV1(quiet bool) *SysInfo {
58 58
 	var (
59 59
 		err      error
60
-		ops      []infoCollector
61 60
 		warnings []string
62 61
 		sysInfo  = &SysInfo{}
63 62
 	)
63
+
64
+	ops := []infoCollector{
65
+		applyNetworkingInfo,
66
+		applyAppArmorInfo,
67
+		applySeccompInfo,
68
+		applyCgroupNsInfo,
69
+	}
70
+
64 71
 	sysInfo.cgMounts, err = findCgroupMountpoints()
65 72
 	if err != nil {
66 73
 		logrus.Warn(err)
67 74
 	} else {
68
-		ops = append(ops, []infoCollector{
75
+		ops = append(ops,
69 76
 			applyMemoryCgroupInfo,
70 77
 			applyCPUCgroupInfo,
71 78
 			applyBlkioCgroupInfo,
72 79
 			applyCPUSetCgroupInfo,
73 80
 			applyPIDSCgroupInfo,
74 81
 			applyDevicesCgroupInfo,
75
-		}...)
82
+		)
76 83
 	}
77 84
 
78
-	ops = append(ops, []infoCollector{
79
-		applyNetworkingInfo,
80
-		applyAppArmorInfo,
81
-		applySeccompInfo,
82
-		applyCgroupNsInfo,
83
-	}...)
84
-
85 85
 	for _, o := range ops {
86 86
 		w := o(sysInfo)
87 87
 		warnings = append(warnings, w...)