From 5ba438d1e173654dd955d167052b65cf3220c2cd Mon Sep 17 00:00:00 2001 From: Thomas Schubart Date: Mon, 28 Feb 2022 17:24:40 +0000 Subject: [PATCH] Add runc-facade --- components/ws-daemon/gp-runc/main.go | 75 +++++++++++++++++++++ components/ws-daemon/pkg/cpulimit/cfs_cg.go | 20 ++++++ components/ws-daemon/pkg/cpulimit/cfs_v2.go | 1 + 3 files changed, 96 insertions(+) create mode 100644 components/ws-daemon/gp-runc/main.go create mode 100644 components/ws-daemon/pkg/cpulimit/cfs_cg.go diff --git a/components/ws-daemon/gp-runc/main.go b/components/ws-daemon/gp-runc/main.go new file mode 100644 index 00000000000000..c3413216b60edc --- /dev/null +++ b/components/ws-daemon/gp-runc/main.go @@ -0,0 +1,75 @@ +package main + +import ( + "encoding/json" + "os" + "os/exec" + "syscall" + + "github.com/opencontainers/runtime-spec/specs-go" + "github.com/sirupsen/logrus" + "golang.org/x/xerrors" +) + +var ( + fuseDeviceMajor int64 = 10 + fuseDeviceMinor int64 = 229 +) + +func main() { + log := logrus.New() + log.SetLevel(logrus.DebugLevel) + + var err error + runcPath, err := exec.LookPath("runc.orig") + if err != nil { + log.WithError(err).Fatal("runc not found") + } + + var useFacade bool + for _, arg := range os.Args { + if arg == "create" { + useFacade = true + break + } + } + + if useFacade { + err = createAndRunc(runcPath, log) + } else { + err = syscall.Exec(runcPath, os.Args, os.Environ()) + } + if err != nil { + log.WithError(err).Fatal("failed") + } +} + +func createAndRunc(runcPath string, log *logrus.Logger) error { + fc, err := os.ReadFile("config.json") + if err != nil { + return xerrors.Errorf("cannot read config.json: %w", err) + } + + var cfg specs.Spec + err = json.Unmarshal(fc, &cfg) + if err != nil { + return xerrors.Errorf("cannot decode config.json: %w", err) + } + + fuseDevice := specs.LinuxDeviceCgroup{ + Type: "c", + Minor: &fuseDeviceMinor, + Major: &fuseDeviceMajor, + Access: "rwm", + Allow: true, + } + + cfg.Linux.Resources.Devices = append(cfg.Linux.Resources.Devices, fuseDevice) + + err = syscall.Exec(runcPath, os.Args, os.Environ()) + if err != nil { + return xerrors.Errorf("exec %s: %w", runcPath, err) + } + + return nil +} diff --git a/components/ws-daemon/pkg/cpulimit/cfs_cg.go b/components/ws-daemon/pkg/cpulimit/cfs_cg.go new file mode 100644 index 00000000000000..ba2bfd5f904901 --- /dev/null +++ b/components/ws-daemon/pkg/cpulimit/cfs_cg.go @@ -0,0 +1,20 @@ +package cpulimit + +import ( +"github.com/containerd/cgroups" +) + +type CFSControllerCgroup struct { + cgroup cgroups.Cgroup +} + +func Load(base string) (CFSControllerCgroup, error) { + cgroup, err := cgroups.Load(cgroups.V1, cgroups.StaticPath(base)) + if err != nil { + return CFSControllerCgroup{}, err + } + + return CFSControllerCgroup{ + cgroup: cgroup, + }, nil +} \ No newline at end of file diff --git a/components/ws-daemon/pkg/cpulimit/cfs_v2.go b/components/ws-daemon/pkg/cpulimit/cfs_v2.go index 1b2a133171e554..4d3c9716697530 100644 --- a/components/ws-daemon/pkg/cpulimit/cfs_v2.go +++ b/components/ws-daemon/pkg/cpulimit/cfs_v2.go @@ -20,6 +20,7 @@ import ( type CgroupV2CFSController string func (basePath CgroupV2CFSController) Usage() (CPUTime, error) { + usage, err := basePath.getFlatKeyedValue("usage_usec") if err != nil { return 0, err