25
25
lxcIdRegexp = regexp .MustCompile (`/lxc/([^/]+)` )
26
26
systemSliceIdRegexp = regexp .MustCompile (`(/(system|runtime|reserved)\.slice/([^/]+))` )
27
27
talosIdRegexp = regexp .MustCompile (`/(system|podruntime)/([^/]+)` )
28
+ lxcPayloadRegexp = regexp .MustCompile (`/lxc\.payload\.([^/]+)` )
28
29
)
29
30
30
31
type ContainerType uint8
@@ -129,7 +130,14 @@ func NewFromProcessCgroupFile(filePath string) (*Cgroup, error) {
129
130
continue
130
131
}
131
132
for _ , cgType := range strings .Split (parts [1 ], "," ) {
132
- p := path .Join (baseCgroupPath , parts [2 ])
133
+ cgPath := parts [2 ]
134
+ if strings .HasPrefix (parts [2 ], "/lxc.payload." ) {
135
+ pp := strings .Split (cgPath , "/" )
136
+ if len (parts ) > 2 {
137
+ cgPath = "/" + pp [1 ]
138
+ }
139
+ }
140
+ p := path .Join (baseCgroupPath , cgPath )
133
141
switch p {
134
142
case "/" , "/init.scope" :
135
143
continue
@@ -147,24 +155,22 @@ func NewFromProcessCgroupFile(filePath string) (*Cgroup, error) {
147
155
148
156
func containerByCgroup (cgroupPath string ) (ContainerType , string , error ) {
149
157
parts := strings .Split (strings .TrimLeft (cgroupPath , "/" ), "/" )
150
- if cgroupPath == "/init" {
151
- return ContainerTypeTalosRuntime , "/talos/init" , nil
152
- }
153
- if len (parts ) < 2 {
158
+ if len (parts ) == 0 {
154
159
return ContainerTypeStandaloneProcess , "" , nil
155
160
}
156
161
prefix := parts [0 ]
157
- if prefix == "user.slice" || prefix == "init.scope" {
162
+ switch {
163
+ case cgroupPath == "/init" :
164
+ return ContainerTypeTalosRuntime , "/talos/init" , nil
165
+ case prefix == "user.slice" || prefix == "init.scope" :
158
166
return ContainerTypeStandaloneProcess , "" , nil
159
- }
160
- if prefix == "docker" || (prefix == "system.slice" && strings .HasPrefix (parts [1 ], "docker-" )) {
167
+ case prefix == "docker" || (prefix == "system.slice" && len (parts ) > 1 && strings .HasPrefix (parts [1 ], "docker-" )):
161
168
matches := dockerIdRegexp .FindStringSubmatch (cgroupPath )
162
169
if matches == nil {
163
170
return ContainerTypeUnknown , "" , fmt .Errorf ("invalid docker cgroup %s" , cgroupPath )
164
171
}
165
172
return ContainerTypeDocker , matches [1 ], nil
166
- }
167
- if strings .Contains (cgroupPath , "kubepods" ) {
173
+ case strings .Contains (cgroupPath , "kubepods" ):
168
174
crioMatches := crioIdRegexp .FindStringSubmatch (cgroupPath )
169
175
if crioMatches != nil {
170
176
return ContainerTypeCrio , crioMatches [1 ], nil
@@ -181,27 +187,33 @@ func containerByCgroup(cgroupPath string) (ContainerType, string, error) {
181
187
return ContainerTypeSandbox , "" , nil
182
188
}
183
189
return ContainerTypeDocker , matches [1 ], nil
184
- }
185
- if prefix == "lxc" {
186
- matches := lxcIdRegexp .FindStringSubmatch (cgroupPath )
187
- if matches == nil {
188
- return ContainerTypeUnknown , "" , fmt .Errorf ("invalid lxc cgroup %s" , cgroupPath )
189
- }
190
- return ContainerTypeLxc , matches [1 ], nil
191
- }
192
- if prefix == "system" || prefix == "podruntime" {
190
+ case prefix == "system" || prefix == "podruntime" :
193
191
matches := talosIdRegexp .FindStringSubmatch (cgroupPath )
194
192
if matches == nil {
195
193
return ContainerTypeUnknown , "" , fmt .Errorf ("invalid talos runtime cgroup %s" , cgroupPath )
196
194
}
197
195
return ContainerTypeTalosRuntime , path .Join ("/talos/" , matches [2 ]), nil
198
- }
199
- if prefix == "system.slice" || prefix == "runtime.slice" || prefix == "reserved.slice" {
196
+ case prefix == "system.slice" || prefix == "runtime.slice" || prefix == "reserved.slice" :
200
197
matches := systemSliceIdRegexp .FindStringSubmatch (cgroupPath )
201
198
if matches == nil {
202
199
return ContainerTypeUnknown , "" , fmt .Errorf ("invalid systemd cgroup %s" , cgroupPath )
203
200
}
204
201
return ContainerTypeSystemdService , strings .Replace (matches [1 ], "\\ x2d" , "-" , - 1 ), nil
202
+ case prefix == "lxc" :
203
+ matches := lxcIdRegexp .FindStringSubmatch (cgroupPath )
204
+ if matches == nil {
205
+ return ContainerTypeUnknown , "" , fmt .Errorf ("invalid lxc cgroup %s" , cgroupPath )
206
+ }
207
+ return ContainerTypeLxc , matches [1 ], nil
208
+ case strings .HasPrefix (prefix , "lxc.payload." ):
209
+ matches := lxcPayloadRegexp .FindStringSubmatch (cgroupPath )
210
+ if matches == nil {
211
+ return ContainerTypeUnknown , "" , fmt .Errorf ("invalid lxc payload cgroup %s" , cgroupPath )
212
+ }
213
+ return ContainerTypeLxc , "/lxc/" + matches [1 ], nil
214
+ case len (parts ) < 2 :
215
+ return ContainerTypeStandaloneProcess , "" , nil
205
216
}
217
+
206
218
return ContainerTypeUnknown , "" , fmt .Errorf ("unknown container: %s" , cgroupPath )
207
219
}
0 commit comments