Skip to content

Commit

Permalink
Merge pull request kata-containers#1518 from lifupan/fixtop
Browse files Browse the repository at this point in the history
virtcontainers: prepend a kata specific string to host cgroups path
  • Loading branch information
Julio Montes authored Apr 12, 2019
2 parents e15f3e4 + 1a1f93b commit d99693a
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 6 deletions.
28 changes: 24 additions & 4 deletions virtcontainers/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,11 @@ func TestStatusSandboxSuccessfulStateReady(t *testing.T) {
defer cleanUp()

config := newTestSandboxConfigNoop()
cgroupPath, err := renameCgroupPath(utils.DefaultCgroupPath)
if err != nil {
t.Fatal(err)
}

hypervisorConfig := HypervisorConfig{
KernelPath: filepath.Join(testDir, testKernel),
ImagePath: filepath.Join(testDir, testImage),
Expand All @@ -889,7 +894,7 @@ func TestStatusSandboxSuccessfulStateReady(t *testing.T) {
ID: containerID,
State: types.ContainerState{
State: types.StateReady,
CgroupPath: utils.DefaultCgroupPath,
CgroupPath: cgroupPath,
},
PID: 0,
RootFs: filepath.Join(testDir, testBundle),
Expand Down Expand Up @@ -922,6 +927,11 @@ func TestStatusSandboxSuccessfulStateRunning(t *testing.T) {
defer cleanUp()

config := newTestSandboxConfigNoop()
cgroupPath, err := renameCgroupPath(utils.DefaultCgroupPath)
if err != nil {
t.Fatal(err)
}

hypervisorConfig := HypervisorConfig{
KernelPath: filepath.Join(testDir, testKernel),
ImagePath: filepath.Join(testDir, testImage),
Expand All @@ -948,7 +958,7 @@ func TestStatusSandboxSuccessfulStateRunning(t *testing.T) {
ID: containerID,
State: types.ContainerState{
State: types.StateRunning,
CgroupPath: utils.DefaultCgroupPath,
CgroupPath: cgroupPath,
},
PID: 0,
RootFs: filepath.Join(testDir, testBundle),
Expand Down Expand Up @@ -1734,7 +1744,12 @@ func TestStatusContainerStateReady(t *testing.T) {

// (homage to a great album! ;)
contID := "101"

config := newTestSandboxConfigNoop()
cgroupPath, err := renameCgroupPath(utils.DefaultCgroupPath)
if err != nil {
t.Fatal(err)
}

ctx := context.Background()
p, err := CreateSandbox(ctx, config, nil)
Expand Down Expand Up @@ -1772,7 +1787,7 @@ func TestStatusContainerStateReady(t *testing.T) {
ID: contID,
State: types.ContainerState{
State: types.StateReady,
CgroupPath: utils.DefaultCgroupPath,
CgroupPath: cgroupPath,
},
PID: 0,
RootFs: filepath.Join(testDir, testBundle),
Expand Down Expand Up @@ -1800,7 +1815,12 @@ func TestStatusContainerStateRunning(t *testing.T) {

// (homage to a great album! ;)
contID := "101"

config := newTestSandboxConfigNoop()
cgroupPath, err := renameCgroupPath(utils.DefaultCgroupPath)
if err != nil {
t.Fatal(err)
}

ctx := context.Background()
p, err := CreateSandbox(ctx, config, nil)
Expand Down Expand Up @@ -1848,7 +1868,7 @@ func TestStatusContainerStateRunning(t *testing.T) {
ID: contID,
State: types.ContainerState{
State: types.StateRunning,
CgroupPath: utils.DefaultCgroupPath,
CgroupPath: cgroupPath,
},
PID: 0,
RootFs: filepath.Join(testDir, testBundle),
Expand Down
17 changes: 17 additions & 0 deletions virtcontainers/cgroups.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ type cgroupPather interface {
// where path is defined by the containers manager
const cgroupKataPath = "/kata/"

// prepend a kata specific string to oci cgroup path to
// form a different cgroup path, thus cAdvisor couldn't
// find kata containers cgroup path on host to prevent it
// from grabbing the stats data.
const cgroupKataPrefix = "kata"

var cgroupsLoadFunc = cgroups.Load
var cgroupsNewFunc = cgroups.New

Expand Down Expand Up @@ -355,3 +361,14 @@ func validCPUResources(cpuSpec *specs.LinuxCPU) *specs.LinuxCPU {

return &cpu
}

func renameCgroupPath(path string) (string, error) {
if path == "" {
return "", fmt.Errorf("Cgroup path is empty")
}

cgroupPathDir := filepath.Dir(path)
cgroupPathName := fmt.Sprintf("%s_%s", cgroupKataPrefix, filepath.Base(path))
return filepath.Join(cgroupPathDir, cgroupPathName), nil

}
9 changes: 7 additions & 2 deletions virtcontainers/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -1297,7 +1297,7 @@ func (c *Container) detachDevices() error {
}

// creates a new cgroup and return the cgroups path
func (c *Container) newCgroups() error {
func (c *Container) newCgroups() (err error) {
ann := c.GetAnnotations()

config, ok := ann[annotations.ConfigJSONKey]
Expand All @@ -1319,7 +1319,12 @@ func (c *Container) newCgroups() error {
resources.CPU = validCPUResources(spec.Linux.Resources.CPU)
}

c.state.CgroupPath = utils.ValidCgroupPath(spec.Linux.CgroupsPath)
cgroupPath := utils.ValidCgroupPath(spec.Linux.CgroupsPath)
c.state.CgroupPath, err = renameCgroupPath(cgroupPath)
if err != nil {
return err
}

cgroup, err := cgroupsNewFunc(cgroups.V1,
cgroups.StaticPath(c.state.CgroupPath), &resources)
if err != nil {
Expand Down

0 comments on commit d99693a

Please sign in to comment.