1
1
package containers
2
2
3
3
import (
4
+ "bytes"
4
5
"fmt"
5
6
"github.com/coroot/coroot-node-agent/cgroup"
6
7
"github.com/coroot/coroot-node-agent/common"
@@ -11,13 +12,15 @@ import (
11
12
"github.com/vishvananda/netns"
12
13
"k8s.io/klog/v2"
13
14
"os"
15
+ "regexp"
14
16
"time"
15
17
)
16
18
17
19
var (
18
- selfNetNs = netns .None ()
19
- hostNetNsId = netns .None ().UniqueId ()
20
- agentPid = uint32 (os .Getpid ())
20
+ selfNetNs = netns .None ()
21
+ hostNetNsId = netns .None ().UniqueId ()
22
+ agentPid = uint32 (os .Getpid ())
23
+ containerIdRegexp = regexp .MustCompile (`[a-z0-9]{64}` )
21
24
)
22
25
23
26
type Registry struct {
@@ -239,6 +242,16 @@ func (r *Registry) getOrCreateContainer(pid uint32) *Container {
239
242
r .containersByPid [pid ] = c
240
243
return c
241
244
}
245
+ if cg .ContainerType == cgroup .ContainerTypeSandbox {
246
+ cmdline := proc .GetCmdline (pid )
247
+ parts := bytes .Split (cmdline , []byte {0 })
248
+ if len (parts ) > 0 {
249
+ lastArg := parts [len (parts )- 1 ]
250
+ if bytes .HasSuffix (parts [0 ], []byte ("runsc-sandbox" )) && containerIdRegexp .Match (lastArg ) {
251
+ cg .ContainerId = string (lastArg )
252
+ }
253
+ }
254
+ }
242
255
md , err := getContainerMetadata (cg )
243
256
if err != nil {
244
257
klog .Warningf ("failed to get container metadata for pid %d -> %s: %s" , pid , cg .Id , err )
@@ -291,14 +304,19 @@ func calcId(cg *cgroup.Cgroup, md *ContainerMetadata) ContainerID {
291
304
if cg .ContainerType == cgroup .ContainerTypeSystemdService {
292
305
return ContainerID (cg .ContainerId )
293
306
}
294
- if cg .ContainerType != cgroup .ContainerTypeDocker && cg .ContainerType != cgroup .ContainerTypeContainerd {
307
+ switch cg .ContainerType {
308
+ case cgroup .ContainerTypeDocker , cgroup .ContainerTypeContainerd , cgroup .ContainerTypeSandbox :
309
+ default :
295
310
return ""
296
311
}
297
312
if md .labels ["io.kubernetes.pod.name" ] != "" {
298
313
pod := md .labels ["io.kubernetes.pod.name" ]
299
314
namespace := md .labels ["io.kubernetes.pod.namespace" ]
300
315
name := md .labels ["io.kubernetes.container.name" ]
301
- if name == "" || name == "POD" { // skip pause|sandbox containers
316
+ if cg .ContainerType == cgroup .ContainerTypeSandbox {
317
+ name = "sandbox"
318
+ }
319
+ if name == "" || name == "POD" { // skip pause containers
302
320
return ""
303
321
}
304
322
return ContainerID (fmt .Sprintf ("/k8s/%s/%s/%s" , namespace , pod , name ))
@@ -311,7 +329,12 @@ func calcId(cg *cgroup.Cgroup, md *ContainerMetadata) ContainerID {
311
329
}
312
330
313
331
func getContainerMetadata (cg * cgroup.Cgroup ) (* ContainerMetadata , error ) {
314
- if cg .ContainerType != cgroup .ContainerTypeDocker && cg .ContainerType != cgroup .ContainerTypeContainerd {
332
+ switch cg .ContainerType {
333
+ case cgroup .ContainerTypeDocker , cgroup .ContainerTypeContainerd , cgroup .ContainerTypeSandbox :
334
+ default :
335
+ return & ContainerMetadata {}, nil
336
+ }
337
+ if cg .ContainerId == "" {
315
338
return & ContainerMetadata {}, nil
316
339
}
317
340
var dockerdErr error
@@ -320,7 +343,6 @@ func getContainerMetadata(cg *cgroup.Cgroup) (*ContainerMetadata, error) {
320
343
if err == nil {
321
344
return md , nil
322
345
}
323
- klog .Warningf ("failed to inspect container %s: %s" , cg .ContainerId , err )
324
346
dockerdErr = err
325
347
}
326
348
var containerdErr error
@@ -329,7 +351,6 @@ func getContainerMetadata(cg *cgroup.Cgroup) (*ContainerMetadata, error) {
329
351
if err == nil {
330
352
return md , nil
331
353
}
332
- klog .Warningf ("failed to inspect container %s: %s" , cg .ContainerId , err )
333
354
containerdErr = err
334
355
}
335
356
return nil , fmt .Errorf ("failed to interact with dockerd (%s) or with containerd (%s)" , dockerdErr , containerdErr )
0 commit comments