@@ -8,34 +8,16 @@ import (
8
8
"context"
9
9
"errors"
10
10
"fmt"
11
- "os"
12
11
13
12
"github.com/containerd/cgroups/v3"
14
- "github.com/containerd/cgroups/v3/cgroup1"
15
- "github.com/containerd/cgroups/v3/cgroup2"
16
- "github.com/opencontainers/runtime-spec/specs-go"
17
- "github.com/siderolabs/go-debug"
18
- "github.com/siderolabs/go-pointer"
19
13
"go.uber.org/zap"
20
14
21
15
"github.com/siderolabs/talos/internal/app/machined/pkg/runtime"
22
16
"github.com/siderolabs/talos/internal/pkg/cgroup"
23
17
"github.com/siderolabs/talos/pkg/machinery/constants"
24
18
)
25
19
26
- func zeroIfRace [T any ](v T ) T {
27
- if debug .RaceEnabled {
28
- var zeroT T
29
-
30
- return zeroT
31
- }
32
-
33
- return v
34
- }
35
-
36
20
// CreateSystemCgroups creates system cgroups.
37
- //
38
- //nolint:gocyclo
39
21
func CreateSystemCgroups (ctx context.Context , log * zap.Logger , rt runtime.Runtime , next NextTaskFunc ) error {
40
22
// in container mode cgroups mode depends on cgroups provided by the container runtime
41
23
if ! rt .State ().Platform ().Mode ().InContainer () {
@@ -53,163 +35,16 @@ func CreateSystemCgroups(ctx context.Context, log *zap.Logger, rt runtime.Runtim
53
35
54
36
log .Info ("initializing cgroups" , zap .String ("root" , cgroup .Root ()))
55
37
56
- groups := []struct {
57
- name string
58
- resources * cgroup2.Resources
59
- }{
60
- {
61
- name : constants .CgroupInit ,
62
- resources : & cgroup2.Resources {
63
- Memory : & cgroup2.Memory {
64
- Min : pointer.To [int64 ](constants .CgroupInitReservedMemory ),
65
- Low : pointer.To [int64 ](constants .CgroupInitReservedMemory * 2 ),
66
- },
67
- CPU : & cgroup2.CPU {
68
- Weight : pointer.To [uint64 ](cgroup .MillicoresToCPUWeight (cgroup .MilliCores (constants .CgroupInitMillicores ))),
69
- },
70
- },
71
- },
72
- {
73
- name : constants .CgroupSystem ,
74
- resources : & cgroup2.Resources {
75
- Memory : & cgroup2.Memory {
76
- Min : pointer.To [int64 ](constants .CgroupSystemReservedMemory ),
77
- Low : pointer.To [int64 ](constants .CgroupSystemReservedMemory * 2 ),
78
- },
79
- CPU : & cgroup2.CPU {
80
- Weight : pointer.To [uint64 ](cgroup .MillicoresToCPUWeight (cgroup .MilliCores (constants .CgroupSystemMillicores ))),
81
- },
82
- },
83
- },
84
- {
85
- name : constants .CgroupSystemRuntime ,
86
- resources : & cgroup2.Resources {
87
- Memory : & cgroup2.Memory {
88
- Min : pointer.To [int64 ](constants .CgroupSystemRuntimeReservedMemory ),
89
- Low : pointer.To [int64 ](constants .CgroupSystemRuntimeReservedMemory * 2 ),
90
- },
91
- CPU : & cgroup2.CPU {
92
- Weight : pointer.To [uint64 ](cgroup .MillicoresToCPUWeight (cgroup .MilliCores (constants .CgroupSystemRuntimeMillicores ))),
93
- },
94
- },
95
- },
96
- {
97
- name : constants .CgroupUdevd ,
98
- resources : & cgroup2.Resources {
99
- Memory : & cgroup2.Memory {
100
- Min : pointer.To [int64 ](constants .CgroupUdevdReservedMemory ),
101
- Low : pointer.To [int64 ](constants .CgroupUdevdReservedMemory * 2 ),
102
- },
103
- CPU : & cgroup2.CPU {
104
- Weight : pointer.To [uint64 ](cgroup .MillicoresToCPUWeight (cgroup .MilliCores (constants .CgroupUdevdMillicores ))),
105
- },
106
- },
107
- },
108
- {
109
- name : constants .CgroupPodRuntimeRoot ,
110
- resources : & cgroup2.Resources {
111
- CPU : & cgroup2.CPU {
112
- Weight : pointer.To [uint64 ](cgroup .MillicoresToCPUWeight (cgroup .MilliCores (constants .CgroupPodRuntimeRootMillicores ))),
113
- },
114
- },
115
- },
116
- {
117
- name : constants .CgroupPodRuntime ,
118
- resources : & cgroup2.Resources {
119
- Memory : & cgroup2.Memory {
120
- Min : pointer.To [int64 ](constants .CgroupPodRuntimeReservedMemory ),
121
- Low : pointer.To [int64 ](constants .CgroupPodRuntimeReservedMemory * 2 ),
122
- },
123
- CPU : & cgroup2.CPU {
124
- Weight : pointer.To [uint64 ](cgroup .MillicoresToCPUWeight (cgroup .MilliCores (constants .CgroupPodRuntimeMillicores ))),
125
- },
126
- },
127
- },
128
- {
129
- name : constants .CgroupKubelet ,
130
- resources : & cgroup2.Resources {
131
- Memory : & cgroup2.Memory {
132
- Min : pointer.To [int64 ](constants .CgroupKubeletReservedMemory ),
133
- Low : pointer.To [int64 ](constants .CgroupKubeletReservedMemory * 2 ),
134
- },
135
- CPU : & cgroup2.CPU {
136
- Weight : pointer.To [uint64 ](cgroup .MillicoresToCPUWeight (cgroup .MilliCores (constants .CgroupKubeletMillicores ))),
137
- },
138
- },
139
- },
140
- {
141
- name : constants .CgroupDashboard ,
142
- resources : & cgroup2.Resources {
143
- Memory : & cgroup2.Memory {
144
- Max : zeroIfRace (pointer.To [int64 ](constants .CgroupDashboardMaxMemory )),
145
- },
146
- CPU : & cgroup2.CPU {
147
- Weight : pointer.To [uint64 ](cgroup .MillicoresToCPUWeight (cgroup .MilliCores (constants .CgroupDashboardMillicores ))),
148
- },
149
- },
150
- },
151
- {
152
- name : constants .CgroupApid ,
153
- resources : & cgroup2.Resources {
154
- Memory : & cgroup2.Memory {
155
- Min : pointer.To [int64 ](constants .CgroupApidReservedMemory ),
156
- Low : pointer.To [int64 ](constants .CgroupApidReservedMemory * 2 ),
157
- Max : zeroIfRace (pointer.To [int64 ](constants .CgroupApidMaxMemory )),
158
- Swap : pointer.To [int64 ](0 ),
159
- },
160
- CPU : & cgroup2.CPU {
161
- Weight : pointer.To [uint64 ](cgroup .MillicoresToCPUWeight (cgroup .MilliCores (constants .CgroupApidMillicores ))),
162
- },
163
- },
164
- },
165
- {
166
- name : constants .CgroupTrustd ,
167
- resources : & cgroup2.Resources {
168
- Memory : & cgroup2.Memory {
169
- Min : pointer.To [int64 ](constants .CgroupTrustdReservedMemory ),
170
- Low : pointer.To [int64 ](constants .CgroupTrustdReservedMemory * 2 ),
171
- Max : zeroIfRace (pointer.To [int64 ](constants .CgroupTrustdMaxMemory )),
172
- Swap : pointer.To [int64 ](0 ),
173
- },
174
- CPU : & cgroup2.CPU {
175
- Weight : pointer.To [uint64 ](cgroup .MillicoresToCPUWeight (cgroup .MilliCores (constants .CgroupTrustdMillicores ))),
176
- },
177
- },
178
- },
38
+ groups := []string {
39
+ constants .CgroupInit ,
40
+ constants .CgroupSystem ,
41
+ constants .CgroupPodRuntimeRoot ,
179
42
}
180
43
181
44
for _ , c := range groups {
182
- if cgroups .Mode () == cgroups .Unified {
183
- resources := c .resources
184
-
185
- if rt .State ().Platform ().Mode ().InContainer () {
186
- // don't attempt to set resources in container mode, as they might conflict with the parent cgroup tree
187
- resources = & cgroup2.Resources {}
188
- }
189
-
190
- cg , err := cgroup2 .NewManager (constants .CgroupMountPath , cgroup .Path (c .name ), resources )
191
- if err != nil {
192
- return fmt .Errorf ("failed to create cgroup: %w" , err )
193
- }
194
-
195
- if c .name == constants .CgroupInit {
196
- if err := cg .AddProc (uint64 (os .Getpid ())); err != nil {
197
- return fmt .Errorf ("failed to move init process to cgroup: %w" , err )
198
- }
199
- }
200
- } else {
201
- cg , err := cgroup1 .New (cgroup1 .StaticPath (c .name ), & specs.LinuxResources {})
202
- if err != nil {
203
- return fmt .Errorf ("failed to create cgroup: %w" , err )
204
- }
205
-
206
- if c .name == constants .CgroupInit {
207
- if err := cg .Add (cgroup1.Process {
208
- Pid : os .Getpid (),
209
- }); err != nil {
210
- return fmt .Errorf ("failed to move init process to cgroup: %w" , err )
211
- }
212
- }
45
+ _ , err := cgroup .CreateCgroup (c )
46
+ if err != nil {
47
+ return err
213
48
}
214
49
}
215
50
0 commit comments