-
Notifications
You must be signed in to change notification settings - Fork 0
/
pin_containerd.go
91 lines (78 loc) · 3.13 KB
/
pin_containerd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//go:build containerd
// +build containerd
/*
Copyright 2022 Christos Katsakioris
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package actipinner
import (
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
//"github.com/containerd/cgroups"
corev1 "k8s.io/api/core/v1"
"k8s.io/klog/v2"
)
const (
// TODO(ckatsak): This does not depend on CRI's build tag
cgroupsRoot = "/sys/fs/cgroup"
// XXX(ckatsak): Depends on CRI's build tag
kubePodsSlice = "kubepods.slice"
// TODO(ckatsak): Does this depend on CRI's build tag?
CPUSetFile = "cpuset.cpus"
)
// XXX(ckatsak): Implementation depends on CRI's build tag
func getCPUSetCgroupPath(pod *corev1.Pod) (path string, err error) {
qosClass := strings.ToLower(string(pod.Status.QOSClass))
podUID := strings.ReplaceAll(string(pod.UID), "-", "_")
path = filepath.Join(
cgroupsRoot,
// TODO(ckatsak): When is there a "unified" node here? Is it when both v1 and v2 are mounted? Does it depend on the distribution?
// TODO(ckatsak): Is there a "kubelet" node here, or is it present only in KinD?
kubePodsSlice,
fmt.Sprintf("kubepods-%s.slice", qosClass),
fmt.Sprintf("kubepods-%s-pod%s.slice", qosClass, podUID),
CPUSetFile,
)
if _, err = os.Stat(path); err != nil {
err = fmt.Errorf("Controller.getCPUSetCgroupPath: error stat(2)ing path %q: %v", path, err)
}
return
}
// XXX(ckatsak): Implementation depends on CRI's build tag
func (c *Controller) configureCPUSet(podPath, CPUs string) error {
if err := c.writeCPUs(podPath, CPUs); err != nil {
// First reset the assignment on Pod's cpuset:
// $ echo "${ALL_CPUS}" >/sys/fs/cgroup/kubepods.slice/.../..-pod${PodUID}.slice/cpuset.cpus
if err = c.writeCPUs(podPath, c.allCPUs); err != nil {
klog.Warningf("Controller.configureCPUSet: %v", err)
}
// Then, for all 'cri-containerd-*.scope/' SUBDIRs of `/sys/fs/cgroup/kubepods.slice/.../..-pod${PodUID}.slice/`:
// $ echo "${ALL_CPUS}" >/sys/fs/cgroup/kubepods.slice/.../..-pod${PodUID}.slice/$SUBDIR/cpuset.cpus
if err = filepath.WalkDir(filepath.Dir(podPath), func(path string, de fs.DirEntry, err error) error {
if err != nil {
return fmt.Errorf("Controller.configureCPUSet.WalkDirFunc(%q): %v", path, err)
}
if de.IsDir() && strings.HasPrefix(de.Name(), "cri-containerd-") && strings.HasSuffix(de.Name(), ".scope") {
if err = c.writeCPUs(filepath.Join(path, CPUSetFile), CPUs); err != nil {
klog.V(4).Infof("Controller.configureCPUSet.WalkDirFunc(%q): %v", path, err)
}
}
return nil
}); err != nil {
klog.Warningf("Controller.configureCPUSet: error walking filesystem tree under %q: %v",
filepath.Dir(podPath), err)
}
}
return c.writeCPUs(podPath, CPUs)
}