@@ -7,6 +7,9 @@ package container
7
7
import (
8
8
"context"
9
9
"encoding/json"
10
+ "fmt"
11
+ "path/filepath"
12
+ "regexp"
10
13
"strings"
11
14
"sync"
12
15
"time"
@@ -455,6 +458,8 @@ func (s *Containerd) IsContainerdReady(ctx context.Context) (bool, error) {
455
458
return s .Client .IsServing (ctx )
456
459
}
457
460
461
+ var kubepodsRegexp = regexp .MustCompile (`([^/]+)-([^/]+)-pod` )
462
+
458
463
// ExtractCGroupPathFromContainer retrieves the CGroupPath from the linux section
459
464
// in a container's OCI spec.
460
465
func ExtractCGroupPathFromContainer (container containers.Container ) (cgroupPath string , err error ) {
@@ -466,5 +471,24 @@ func ExtractCGroupPathFromContainer(container containers.Container) (cgroupPath
466
471
if spec .Linux == nil {
467
472
return "" , xerrors .Errorf ("container spec has no Linux section" )
468
473
}
474
+
475
+ // systemd: /kubepods.slice/kubepods-<QoS-class>.slice/kubepods-<QoS-class>-pod<pod-UID>.slice:<prefix>:<container-iD>
476
+ // cgroupfs: /kubepods/<QoS-class>/pod<pod-UID>/<container-iD>
477
+ fields := strings .SplitN (spec .Linux .CgroupsPath , ":" , 3 )
478
+ if len (fields ) != 3 {
479
+ return spec .Linux .CgroupsPath , nil
480
+ }
481
+
482
+ if match := kubepodsRegexp .FindStringSubmatch (fields [0 ]); len (match ) == 3 {
483
+ root , class := match [1 ], match [2 ]
484
+
485
+ return filepath .Join (
486
+ "/" ,
487
+ fmt .Sprintf ("%v.slice" , root ),
488
+ fmt .Sprintf ("%v-%v.slice" , root , class ),
489
+ fields [0 ],
490
+ ), nil
491
+ }
492
+
469
493
return spec .Linux .CgroupsPath , nil
470
494
}
0 commit comments