1717package api
1818
1919import (
20- "os"
21- "path/filepath"
22- "strings"
2320 "time"
2421)
2522
@@ -46,92 +43,3 @@ func (x *Container) GetFinishedAtTime() time.Time {
4643 }
4744 return t
4845}
49-
50- // GetCgroupsV2AbsPath returns the absolute path to the cgroup v2 directory for this container.
51- // This method converts relative cgroup paths to absolute paths, for different cgroup managers,
52- // QoS classes, and custom cgroup hierarchies.
53- // It returns an empty string if the container has no Linux configuration or cgroups path.
54- func (x * Container ) GetCgroupsV2AbsPath () string {
55- if x == nil || x .Linux == nil || x .Linux .CgroupsPath == "" {
56- return ""
57- }
58-
59- cgroupPath := x .Linux .CgroupsPath
60- return getCGroupsV2Path (cgroupPath )
61- }
62-
63- // GetCgroupsV2AbsPath returns the absolute path to the cgroup v2 directory for this pod sandbox.
64- // This method converts relative cgroup paths to absolute paths, for different cgroup managers,
65- // QoS classes, and custom cgroup hierarchies.
66- // It returns an empty string if the container has no Linux configuration or cgroups path.
67- func (x * PodSandbox ) GetCgroupsV2AbsPath () string {
68- if x == nil || x .Linux == nil || x .Linux .CgroupsPath == "" {
69- return ""
70- }
71-
72- cgroupPath := x .Linux .CgroupsPath
73- return getCGroupsV2Path (cgroupPath )
74- }
75-
76- // Helper functions
77-
78- // getCGroupsV2Path helper
79- // Same implementation for both sandbox and container
80- func getCGroupsV2Path (cgroupPath string ) string {
81- if filepath .IsAbs (cgroupPath ) {
82- return cgroupPath
83- }
84-
85- cgroupV2Root := getCgroupV2Root ()
86- if cgroupV2Root == "" {
87- // Fallback to default cgroup v2 mount point
88- cgroupV2Root = "/sys/fs/cgroup"
89- }
90-
91- return filepath .Join (cgroupV2Root , cgroupPath )
92- }
93-
94- // getCgroupV2Root finds the cgroup v2 mount point by reading /proc/mounts
95- // It returns an empty string if no cgroup v2 mount point is found
96- func getCgroupV2Root () string {
97- commonPaths := []string {
98- "/sys/fs/cgroup" ,
99- "/cgroup2" ,
100- }
101-
102- if mountPoint := findCgroupV2Mount (); mountPoint != "" {
103- return mountPoint
104- }
105-
106- for _ , path := range commonPaths {
107- if isCgroupV2Mount (path ) {
108- return path
109- }
110- }
111-
112- return "/sys/fs/cgroup"
113- }
114-
115- // findCgroupV2Mount reads /proc/mounts to find the cgroup2 filesystem mount point
116- func findCgroupV2Mount () string {
117- data , err := os .ReadFile ("/proc/mounts" )
118- if err != nil {
119- return ""
120- }
121-
122- lines := strings .Split (string (data ), "\n " )
123- for _ , line := range lines {
124- fields := strings .Fields (line )
125- if len (fields ) >= 3 && fields [2 ] == "cgroup2" {
126- return fields [1 ]
127- }
128- }
129- return ""
130- }
131-
132- func isCgroupV2Mount (path string ) bool {
133- if _ , err := os .Stat (filepath .Join (path , "cgroup.controllers" )); err == nil {
134- return true
135- }
136- return false
137- }
0 commit comments