-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request moby#38377 from rgulewich/38332-cgroup-ns
Start containers in their own cgroup namespaces
- Loading branch information
Showing
25 changed files
with
515 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package build // import "github.com/docker/docker/integration/build" | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"io" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/docker/docker/api/types" | ||
"github.com/docker/docker/integration/internal/requirement" | ||
"github.com/docker/docker/internal/test/daemon" | ||
"github.com/docker/docker/internal/test/fakecontext" | ||
"github.com/docker/docker/pkg/jsonmessage" | ||
"gotest.tools/assert" | ||
"gotest.tools/skip" | ||
) | ||
|
||
// Finds the output of `readlink /proc/<pid>/ns/cgroup` in build output | ||
func getCgroupFromBuildOutput(buildOutput io.Reader) (string, error) { | ||
const prefix = "cgroup:" | ||
|
||
dec := json.NewDecoder(buildOutput) | ||
for { | ||
m := jsonmessage.JSONMessage{} | ||
err := dec.Decode(&m) | ||
if err == io.EOF { | ||
return "", nil | ||
} | ||
if err != nil { | ||
return "", err | ||
} | ||
if ix := strings.Index(m.Stream, prefix); ix == 0 { | ||
return strings.TrimSpace(m.Stream), nil | ||
} | ||
} | ||
} | ||
|
||
// Runs a docker build against a daemon with the given cgroup namespace default value. | ||
// Returns the container cgroup and daemon cgroup. | ||
func testBuildWithCgroupNs(t *testing.T, daemonNsMode string) (string, string) { | ||
d := daemon.New(t, daemon.WithDefaultCgroupNamespaceMode(daemonNsMode)) | ||
d.StartWithBusybox(t) | ||
defer d.Stop(t) | ||
|
||
dockerfile := ` | ||
FROM busybox | ||
RUN readlink /proc/self/ns/cgroup | ||
` | ||
ctx := context.Background() | ||
source := fakecontext.New(t, "", fakecontext.WithDockerfile(dockerfile)) | ||
defer source.Close() | ||
|
||
client := d.NewClientT(t) | ||
resp, err := client.ImageBuild(ctx, | ||
source.AsTarReader(t), | ||
types.ImageBuildOptions{ | ||
Remove: true, | ||
ForceRemove: true, | ||
Tags: []string{"buildcgroupns"}, | ||
}) | ||
assert.NilError(t, err) | ||
defer resp.Body.Close() | ||
|
||
containerCgroup, err := getCgroupFromBuildOutput(resp.Body) | ||
assert.NilError(t, err) | ||
daemonCgroup := d.CgroupNamespace(t) | ||
|
||
return containerCgroup, daemonCgroup | ||
} | ||
|
||
func TestCgroupNamespacesBuild(t *testing.T) { | ||
skip.If(t, testEnv.DaemonInfo.OSType != "linux") | ||
skip.If(t, testEnv.IsRemoteDaemon()) | ||
skip.If(t, !requirement.CgroupNamespacesEnabled()) | ||
|
||
// When the daemon defaults to private cgroup namespaces, containers launched | ||
// should be in their own private cgroup namespace by default | ||
containerCgroup, daemonCgroup := testBuildWithCgroupNs(t, "private") | ||
assert.Assert(t, daemonCgroup != containerCgroup) | ||
} | ||
|
||
func TestCgroupNamespacesBuildDaemonHostMode(t *testing.T) { | ||
skip.If(t, testEnv.DaemonInfo.OSType != "linux") | ||
skip.If(t, testEnv.IsRemoteDaemon()) | ||
skip.If(t, !requirement.CgroupNamespacesEnabled()) | ||
|
||
// When the daemon defaults to host cgroup namespaces, containers | ||
// launched should not be inside their own cgroup namespaces | ||
containerCgroup, daemonCgroup := testBuildWithCgroupNs(t, "host") | ||
assert.Assert(t, daemonCgroup == containerCgroup) | ||
} |
Oops, something went wrong.