mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-07-02 18:23:12 +00:00
Merge pull request #1366 from devimc/topic/fixRelativeCgroupPath
virtcontainers: honor OCI cgroupsPath
This commit is contained in:
commit
dbc5a32b74
@ -23,6 +23,7 @@ import (
|
|||||||
vcTypes "github.com/kata-containers/runtime/virtcontainers/pkg/types"
|
vcTypes "github.com/kata-containers/runtime/virtcontainers/pkg/types"
|
||||||
"github.com/kata-containers/runtime/virtcontainers/store"
|
"github.com/kata-containers/runtime/virtcontainers/store"
|
||||||
"github.com/kata-containers/runtime/virtcontainers/types"
|
"github.com/kata-containers/runtime/virtcontainers/types"
|
||||||
|
"github.com/kata-containers/runtime/virtcontainers/utils"
|
||||||
specs "github.com/opencontainers/runtime-spec/specs-go"
|
specs "github.com/opencontainers/runtime-spec/specs-go"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
@ -887,7 +888,8 @@ func TestStatusSandboxSuccessfulStateReady(t *testing.T) {
|
|||||||
{
|
{
|
||||||
ID: containerID,
|
ID: containerID,
|
||||||
State: types.State{
|
State: types.State{
|
||||||
State: types.StateReady,
|
State: types.StateReady,
|
||||||
|
CgroupPath: utils.DefaultCgroupPath,
|
||||||
},
|
},
|
||||||
PID: 0,
|
PID: 0,
|
||||||
RootFs: filepath.Join(testDir, testBundle),
|
RootFs: filepath.Join(testDir, testBundle),
|
||||||
@ -945,7 +947,8 @@ func TestStatusSandboxSuccessfulStateRunning(t *testing.T) {
|
|||||||
{
|
{
|
||||||
ID: containerID,
|
ID: containerID,
|
||||||
State: types.State{
|
State: types.State{
|
||||||
State: types.StateRunning,
|
State: types.StateRunning,
|
||||||
|
CgroupPath: utils.DefaultCgroupPath,
|
||||||
},
|
},
|
||||||
PID: 0,
|
PID: 0,
|
||||||
RootFs: filepath.Join(testDir, testBundle),
|
RootFs: filepath.Join(testDir, testBundle),
|
||||||
@ -1768,7 +1771,8 @@ func TestStatusContainerStateReady(t *testing.T) {
|
|||||||
expectedStatus := ContainerStatus{
|
expectedStatus := ContainerStatus{
|
||||||
ID: contID,
|
ID: contID,
|
||||||
State: types.State{
|
State: types.State{
|
||||||
State: types.StateReady,
|
State: types.StateReady,
|
||||||
|
CgroupPath: utils.DefaultCgroupPath,
|
||||||
},
|
},
|
||||||
PID: 0,
|
PID: 0,
|
||||||
RootFs: filepath.Join(testDir, testBundle),
|
RootFs: filepath.Join(testDir, testBundle),
|
||||||
@ -1843,7 +1847,8 @@ func TestStatusContainerStateRunning(t *testing.T) {
|
|||||||
expectedStatus := ContainerStatus{
|
expectedStatus := ContainerStatus{
|
||||||
ID: contID,
|
ID: contID,
|
||||||
State: types.State{
|
State: types.State{
|
||||||
State: types.StateRunning,
|
State: types.StateRunning,
|
||||||
|
CgroupPath: utils.DefaultCgroupPath,
|
||||||
},
|
},
|
||||||
PID: 0,
|
PID: 0,
|
||||||
RootFs: filepath.Join(testDir, testBundle),
|
RootFs: filepath.Join(testDir, testBundle),
|
||||||
|
@ -1288,14 +1288,14 @@ func (c *Container) newCgroups() error {
|
|||||||
resources.CPU = validCPUResources(spec.Linux.Resources.CPU)
|
resources.CPU = validCPUResources(spec.Linux.Resources.CPU)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
c.state.CgroupPath = utils.ValidCgroupPath(spec.Linux.CgroupsPath)
|
||||||
cgroup, err := cgroupsNewFunc(cgroups.V1,
|
cgroup, err := cgroupsNewFunc(cgroups.V1,
|
||||||
cgroups.StaticPath(spec.Linux.CgroupsPath), &resources)
|
cgroups.StaticPath(c.state.CgroupPath), &resources)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Could not create cgroup for %v: %v", spec.Linux.CgroupsPath, err)
|
return fmt.Errorf("Could not create cgroup for %v: %v", c.state.CgroupPath, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.state.Resources = resources
|
c.state.Resources = resources
|
||||||
c.state.CgroupPath = spec.Linux.CgroupsPath
|
|
||||||
|
|
||||||
// Add shim into cgroup
|
// Add shim into cgroup
|
||||||
if c.process.Pid > 0 {
|
if c.process.Pid > 0 {
|
||||||
|
@ -14,6 +14,9 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// DefaultCgroupPath runtime-determined location in the cgroups hierarchy.
|
||||||
|
const DefaultCgroupPath = "/vc"
|
||||||
|
|
||||||
const cpBinaryName = "cp"
|
const cpBinaryName = "cp"
|
||||||
|
|
||||||
const fileMode0755 = os.FileMode(0755)
|
const fileMode0755 = os.FileMode(0755)
|
||||||
@ -241,3 +244,18 @@ func SupportsVsocks() bool {
|
|||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ValidCgroupPath returns a valid cgroup path.
|
||||||
|
// see https://github.com/opencontainers/runtime-spec/blob/master/config-linux.md#cgroups-path
|
||||||
|
func ValidCgroupPath(path string) string {
|
||||||
|
// In the case of an absolute path (starting with /), the runtime MUST
|
||||||
|
// take the path to be relative to the cgroups mount point.
|
||||||
|
if filepath.IsAbs(path) {
|
||||||
|
return filepath.Clean(path)
|
||||||
|
}
|
||||||
|
|
||||||
|
// In the case of a relative path (not starting with /), the runtime MAY
|
||||||
|
// interpret the path relative to a runtime-determined location in the cgroups hierarchy.
|
||||||
|
// clean up path and return a new path relative to defaultCgroupPath
|
||||||
|
return filepath.Join(DefaultCgroupPath, filepath.Clean("/"+path))
|
||||||
|
}
|
||||||
|
@ -325,3 +325,18 @@ func TestSupportsVsocks(t *testing.T) {
|
|||||||
|
|
||||||
assert.True(SupportsVsocks())
|
assert.True(SupportsVsocks())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestValidCgroupPath(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
|
assert.Equal(DefaultCgroupPath, ValidCgroupPath("../../../"))
|
||||||
|
assert.Equal(filepath.Join(DefaultCgroupPath, "foo"), ValidCgroupPath("../../../foo"))
|
||||||
|
assert.Equal("/hi", ValidCgroupPath("/../hi"))
|
||||||
|
assert.Equal("/hi/foo", ValidCgroupPath("/../hi/foo"))
|
||||||
|
assert.Equal(DefaultCgroupPath, ValidCgroupPath(""))
|
||||||
|
assert.Equal(DefaultCgroupPath, ValidCgroupPath(""))
|
||||||
|
assert.Equal(DefaultCgroupPath, ValidCgroupPath("../"))
|
||||||
|
assert.Equal(DefaultCgroupPath, ValidCgroupPath("."))
|
||||||
|
assert.Equal(DefaultCgroupPath, ValidCgroupPath("./../"))
|
||||||
|
assert.Equal(filepath.Join(DefaultCgroupPath, "o / g"), ValidCgroupPath("o / m /../ g"))
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user