Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

runtimetest: add validation of cgroups #93

Merged
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions cgroups/cgroups.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package cgroups

import (
"bufio"
"fmt"
"os"
"path/filepath"
"strings"

rspec "github.com/opencontainers/runtime-spec/specs-go"
)

// Cgroup represents interfaces for cgroup validation
type Cgroup interface {
GetBlockIOData(cgPath string) (*rspec.LinuxBlockIO, error)
GetCPUData(cgPath string) (*rspec.LinuxCPU, error)
GetDevicesData(cgPath string) ([]rspec.LinuxDeviceCgroup, error)
GetHugepageLimitData(cgPath string) ([]rspec.LinuxHugepageLimit, error)
GetMemoryData(cgPath string) (*rspec.LinuxMemory, error)
GetNetworkData(cgPath string) (*rspec.LinuxNetwork, error)
GetPidsData(cgPath string) (*rspec.LinuxPids, error)
}

// FindCgroup gets cgroup root mountpoint
func FindCgroup() (Cgroup, error) {
f, err := os.Open("/proc/self/mountinfo")
if err != nil {
return nil, err
}
defer f.Close()

cgroupv2 := false
scanner := bufio.NewScanner(f)
for scanner.Scan() {
text := scanner.Text()
fields := strings.Split(text, " ")
// Safe as mountinfo encodes mountpoints with spaces as \040.
index := strings.Index(text, " - ")
postSeparatorFields := strings.Fields(text[index+3:])
numPostFields := len(postSeparatorFields)

// This is an error as we can't detect if the mount is for "cgroup"
if numPostFields == 0 {
return nil, fmt.Errorf("Found no fields post '-' in %q", text)
}

if postSeparatorFields[0] == "cgroup" {
// Check that the mount is properly formated.
if numPostFields < 3 {
return nil, fmt.Errorf("Error found less than 3 fields post '-' in %q", text)
}

cg := &CgroupV1{
MountPath: filepath.Dir(fields[4]),
}
return cg, nil
} else if postSeparatorFields[0] == "cgroup2" {
cgroupv2 = true
continue
//TODO cgroupv2 unimplemented
}
}

if err := scanner.Err(); err != nil {
return nil, err
}

if cgroupv2 {
return nil, fmt.Errorf("cgroupv2 is not supported yet")
}
return nil, fmt.Errorf("cgroup is not found")
}
Loading