Skip to content

Commit d7864eb

Browse files
committed
Use namespace in default cgroup path
By default, the generated spec will place containers in cgroups by their ids, we need to use the namespace as the cgroup root to avoid containers with the same name being placed in the same cgroup. ``` 11:perf_event:/to/redis 10:freezer:/to/redis 9:memory:/to/redis 8:devices:/to/redis 7:net_cls,net_prio:/to/redis 6:pids:/to/redis 5:hugetlb:/to/redis 4:cpuset:/to/redis 3:blkio:/to/redis 2:cpu,cpuacct:/to/redis 1:name=systemd:/to/redis 11:perf_event:/te/redis 10:freezer:/te/redis 9:memory:/te/redis 8:devices:/te/redis 7:net_cls,net_prio:/te/redis 6:pids:/te/redis 5:hugetlb:/te/redis 4:cpuset:/te/redis 3:blkio:/te/redis 2:cpu,cpuacct:/te/redis 1:name=systemd:/te/redis ``` Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
1 parent 72bb45a commit d7864eb

File tree

8 files changed

+37
-23
lines changed

8 files changed

+37
-23
lines changed

benchmark_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package containerd
33
import (
44
"fmt"
55
"testing"
6+
7+
"github.com/containerd/containerd/containers"
68
)
79

810
func BenchmarkContainerCreate(b *testing.B) {
@@ -20,7 +22,7 @@ func BenchmarkContainerCreate(b *testing.B) {
2022
b.Error(err)
2123
return
2224
}
23-
spec, err := GenerateSpec(ctx, client, nil, WithImageConfig(image), withTrue())
25+
spec, err := GenerateSpec(ctx, client, &containers.Container{ID: b.Name()}, WithImageConfig(image), withTrue())
2426
if err != nil {
2527
b.Error(err)
2628
return
@@ -63,7 +65,7 @@ func BenchmarkContainerStart(b *testing.B) {
6365
b.Error(err)
6466
return
6567
}
66-
spec, err := GenerateSpec(ctx, client, nil, WithImageConfig(image), withTrue())
68+
spec, err := GenerateSpec(ctx, client, &containers.Container{ID: b.Name()}, WithImageConfig(image), withTrue())
6769
if err != nil {
6870
b.Error(err)
6971
return

cmd/containerd-stress/main.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ import (
55
"fmt"
66
"os"
77
"os/signal"
8+
"path/filepath"
89
"runtime"
910
"strings"
1011
"sync"
1112
"syscall"
1213
"time"
1314

1415
"github.com/containerd/containerd"
16+
"github.com/containerd/containerd/containers"
1517
"github.com/containerd/containerd/namespaces"
1618
specs "github.com/opencontainers/runtime-spec/specs-go"
1719
"github.com/sirupsen/logrus"
@@ -98,7 +100,7 @@ func test(c config) error {
98100
return err
99101
}
100102
logrus.Info("generating spec from image")
101-
spec, err := containerd.GenerateSpec(ctx, client, nil, containerd.WithImageConfig(image), containerd.WithProcessArgs("true"))
103+
spec, err := containerd.GenerateSpec(ctx, client, &containers.Container{ID: ""}, containerd.WithImageConfig(image), containerd.WithProcessArgs("true"))
102104
if err != nil {
103105
return err
104106
}
@@ -120,7 +122,7 @@ func test(c config) error {
120122
w := &worker{
121123
id: i,
122124
wg: &wg,
123-
spec: spec,
125+
spec: *spec,
124126
image: image,
125127
client: client,
126128
}
@@ -158,7 +160,7 @@ type worker struct {
158160

159161
client *containerd.Client
160162
image containerd.Image
161-
spec *specs.Spec
163+
spec specs.Spec
162164
}
163165

164166
func (w *worker) run(ctx, tctx context.Context) {
@@ -194,8 +196,9 @@ func (w *worker) run(ctx, tctx context.Context) {
194196
}
195197

196198
func (w *worker) runContainer(ctx context.Context, id string) error {
199+
w.spec.Linux.CgroupsPath = filepath.Join("/", fmt.Sprint(w.id), id)
197200
c, err := w.client.NewContainer(ctx, id,
198-
containerd.WithSpec(w.spec),
201+
containerd.WithSpec(&w.spec),
199202
containerd.WithNewSnapshot(id, w.image),
200203
)
201204
if err != nil {

docs/getting-started.md

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -196,18 +196,11 @@ func redisExample() error {
196196
}
197197
log.Printf("Successfully pulled %s image\n", image.Name())
198198

199-
spec, err := containerd.GenerateSpec(ctx, client, nil, containerd.WithImageConfig(image))
200-
if err != nil {
201-
return err
202-
}
203-
log.Printf("Successfully generate an OCI spec version %s based on %s image", spec.Version, image.Name())
204-
205199
container, err := client.NewContainer(
206200
ctx,
207201
"redis-server",
208-
containerd.WithSpec(spec),
209-
containerd.WithImage(image),
210202
containerd.WithNewSnapshot("redis-server-snapshot", image),
203+
containerd.WithNewSpec(containerd.WithImageConfig(image)),
211204
)
212205
if err != nil {
213206
return err

spec.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
// GenerateSpec will generate a default spec from the provided image
1111
// for use as a containerd container
1212
func GenerateSpec(ctx context.Context, client *Client, c *containers.Container, opts ...SpecOpts) (*specs.Spec, error) {
13-
s, err := createDefaultSpec()
13+
s, err := createDefaultSpec(ctx, c.ID)
1414
if err != nil {
1515
return nil, err
1616
}

spec_opts.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func WithHostname(name string) SpecOpts {
3838
// WithNewSpec generates a new spec for a new container
3939
func WithNewSpec(opts ...SpecOpts) NewContainerOpts {
4040
return func(ctx context.Context, client *Client, c *containers.Container) error {
41-
s, err := createDefaultSpec()
41+
s, err := createDefaultSpec(ctx, c.ID)
4242
if err != nil {
4343
return err
4444
}

spec_unix.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package containerd
44

55
import (
6+
"context"
67
"io/ioutil"
78
"os"
89
"path/filepath"
@@ -11,6 +12,7 @@ import (
1112
"golang.org/x/sys/unix"
1213

1314
"github.com/containerd/containerd/mount"
15+
"github.com/containerd/containerd/namespaces"
1416
specs "github.com/opencontainers/runtime-spec/specs-go"
1517
)
1618

@@ -64,7 +66,11 @@ func defaultNamespaces() []specs.LinuxNamespace {
6466
}
6567
}
6668

67-
func createDefaultSpec() (*specs.Spec, error) {
69+
func createDefaultSpec(ctx context.Context, id string) (*specs.Spec, error) {
70+
ns, err := namespaces.NamespaceRequired(ctx)
71+
if err != nil {
72+
return nil, err
73+
}
6874
s := &specs.Spec{
6975
Version: specs.Version,
7076
Root: &specs.Root{
@@ -154,7 +160,7 @@ func createDefaultSpec() (*specs.Spec, error) {
154160
"/proc/sys",
155161
"/proc/sysrq-trigger",
156162
},
157-
// TODO (@crosbymichael) make sure we don't have have two containers in the same cgroup
163+
CgroupsPath: filepath.Join("/", ns, id),
158164
Resources: &specs.LinuxResources{
159165
Devices: []specs.LinuxDeviceCgroup{
160166
{

spec_unix_test.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@ import (
66
"context"
77
"testing"
88

9+
"github.com/containerd/containerd/containers"
10+
"github.com/containerd/containerd/namespaces"
911
specs "github.com/opencontainers/runtime-spec/specs-go"
1012
)
1113

1214
func TestGenerateSpec(t *testing.T) {
1315
t.Parallel()
1416

15-
s, err := GenerateSpec(context.Background(), nil, nil)
17+
ctx := namespaces.WithNamespace(context.Background(), "testing")
18+
s, err := GenerateSpec(ctx, nil, &containers.Container{ID: t.Name()})
1619
if err != nil {
1720
t.Fatal(err)
1821
}
@@ -52,7 +55,8 @@ func TestGenerateSpec(t *testing.T) {
5255
func TestSpecWithTTY(t *testing.T) {
5356
t.Parallel()
5457

55-
s, err := GenerateSpec(context.Background(), nil, nil, WithTTY)
58+
ctx := namespaces.WithNamespace(context.Background(), "testing")
59+
s, err := GenerateSpec(ctx, nil, &containers.Container{ID: t.Name()}, WithTTY)
5660
if err != nil {
5761
t.Fatal(err)
5862
}
@@ -68,8 +72,10 @@ func TestSpecWithTTY(t *testing.T) {
6872
func TestWithLinuxNamespace(t *testing.T) {
6973
t.Parallel()
7074

75+
ctx := namespaces.WithNamespace(context.Background(), "testing")
7176
replacedNS := specs.LinuxNamespace{Type: specs.NetworkNamespace, Path: "/var/run/netns/test"}
72-
s, err := GenerateSpec(context.Background(), nil, nil, WithLinuxNamespace(replacedNS))
77+
78+
s, err := GenerateSpec(ctx, nil, &containers.Container{ID: t.Name()}, WithLinuxNamespace(replacedNS))
7379
if err != nil {
7480
t.Fatal(err)
7581
}

spec_windows.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package containerd
22

3-
import specs "github.com/opencontainers/runtime-spec/specs-go"
3+
import (
4+
"context"
45

5-
func createDefaultSpec() (*specs.Spec, error) {
6+
specs "github.com/opencontainers/runtime-spec/specs-go"
7+
)
8+
9+
func createDefaultSpec(ctx context.Context, id string) (*specs.Spec, error) {
610
return &specs.Spec{
711
Version: specs.Version,
812
Root: &specs.Root{},

0 commit comments

Comments
 (0)