Skip to content

Commit 2f60e38

Browse files
authored
Merge pull request containerd#2626 from krsoninikhil/defaults3
Uses namespace labels for default options
2 parents 22bb5eb + 6a21728 commit 2f60e38

File tree

6 files changed

+92
-9
lines changed

6 files changed

+92
-9
lines changed

client.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,20 @@ func New(address string, opts ...ClientOpt) (*Client, error) {
136136
if copts.services == nil && c.conn == nil {
137137
return nil, errors.New("no grpc connection or services is available")
138138
}
139+
140+
// check namespace labels for default runtime
141+
if copts.defaultRuntime == "" && copts.defaultns != "" {
142+
namespaces := c.NamespaceService()
143+
ctx := context.Background()
144+
if labels, err := namespaces.Labels(ctx, copts.defaultns); err == nil {
145+
if defaultRuntime, ok := labels[defaults.DefaultRuntimeNSLabel]; ok {
146+
c.runtime = defaultRuntime
147+
}
148+
} else {
149+
return nil, err
150+
}
151+
}
152+
139153
return c, nil
140154
}
141155

@@ -152,6 +166,20 @@ func NewWithConn(conn *grpc.ClientConn, opts ...ClientOpt) (*Client, error) {
152166
conn: conn,
153167
runtime: fmt.Sprintf("%s.%s", plugin.RuntimePlugin, runtime.GOOS),
154168
}
169+
170+
// check namespace labels for default runtime
171+
if copts.defaultRuntime == "" && copts.defaultns != "" {
172+
namespaces := c.NamespaceService()
173+
ctx := context.Background()
174+
if labels, err := namespaces.Labels(ctx, copts.defaultns); err == nil {
175+
if defaultRuntime, ok := labels[defaults.DefaultRuntimeNSLabel]; ok {
176+
c.runtime = defaultRuntime
177+
}
178+
} else {
179+
return nil, err
180+
}
181+
}
182+
155183
if copts.services != nil {
156184
c.services = *copts.services
157185
}

client_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"testing"
2929
"time"
3030

31+
"github.com/containerd/containerd/defaults"
3132
"github.com/containerd/containerd/images"
3233
"github.com/containerd/containerd/log"
3334
"github.com/containerd/containerd/namespaces"
@@ -396,3 +397,29 @@ func createShimDebugConfig() string {
396397

397398
return f.Name()
398399
}
400+
401+
func TestDefaultRuntimeWithNamespaceLabels(t *testing.T) {
402+
client, err := newClient(t, address)
403+
if err != nil {
404+
t.Fatal(err)
405+
}
406+
defer client.Close()
407+
408+
ctx, cancel := testContext()
409+
defer cancel()
410+
namespaces := client.NamespaceService()
411+
testRuntime := "testRuntime"
412+
runtimeLabel := defaults.DefaultRuntimeNSLabel
413+
if err := namespaces.SetLabel(ctx, testNamespace, runtimeLabel, testRuntime); err != nil {
414+
t.Fatal(err)
415+
}
416+
417+
testClient, err := New(address, WithDefaultNamespace(testNamespace))
418+
if err != nil {
419+
t.Fatal(err)
420+
}
421+
defer testClient.Close()
422+
if testClient.runtime != testRuntime {
423+
t.Error("failed to set default runtime from namespace labels")
424+
}
425+
}

container_opts.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ import (
2020
"context"
2121

2222
"github.com/containerd/containerd/containers"
23+
"github.com/containerd/containerd/defaults"
2324
"github.com/containerd/containerd/errdefs"
25+
"github.com/containerd/containerd/namespaces"
2426
"github.com/containerd/containerd/oci"
2527
"github.com/containerd/containerd/platforms"
2628
"github.com/containerd/containerd/snapshots"
@@ -107,7 +109,7 @@ func WithSnapshotter(name string) NewContainerOpts {
107109
// WithSnapshot uses an existing root filesystem for the container
108110
func WithSnapshot(id string) NewContainerOpts {
109111
return func(ctx context.Context, client *Client, c *containers.Container) error {
110-
setSnapshotterIfEmpty(c)
112+
setSnapshotterIfEmpty(ctx, client, c)
111113
// check that the snapshot exists, if not, fail on creation
112114
if _, err := client.SnapshotService(c.Snapshotter).Mounts(ctx, id); err != nil {
113115
return err
@@ -125,7 +127,7 @@ func WithNewSnapshot(id string, i Image, opts ...snapshots.Opt) NewContainerOpts
125127
if err != nil {
126128
return err
127129
}
128-
setSnapshotterIfEmpty(c)
130+
setSnapshotterIfEmpty(ctx, client, c)
129131
parent := identity.ChainID(diffIDs).String()
130132
if _, err := client.SnapshotService(c.Snapshotter).Prepare(ctx, id, parent, opts...); err != nil {
131133
return err
@@ -155,7 +157,7 @@ func WithNewSnapshotView(id string, i Image, opts ...snapshots.Opt) NewContainer
155157
if err != nil {
156158
return err
157159
}
158-
setSnapshotterIfEmpty(c)
160+
setSnapshotterIfEmpty(ctx, client, c)
159161
parent := identity.ChainID(diffIDs).String()
160162
if _, err := client.SnapshotService(c.Snapshotter).View(ctx, id, parent, opts...); err != nil {
161163
return err
@@ -166,9 +168,18 @@ func WithNewSnapshotView(id string, i Image, opts ...snapshots.Opt) NewContainer
166168
}
167169
}
168170

169-
func setSnapshotterIfEmpty(c *containers.Container) {
171+
func setSnapshotterIfEmpty(ctx context.Context, client *Client, c *containers.Container) {
170172
if c.Snapshotter == "" {
171-
c.Snapshotter = DefaultSnapshotter
173+
defaultSnapshotter := DefaultSnapshotter
174+
namespaceService := client.NamespaceService()
175+
if ns, err := namespaces.NamespaceRequired(ctx); err == nil {
176+
if labels, err := namespaceService.Labels(ctx, ns); err == nil {
177+
if snapshotLabel, ok := labels[defaults.DefaultSnapshotterNSLabel]; ok {
178+
defaultSnapshotter = snapshotLabel
179+
}
180+
}
181+
}
182+
c.Snapshotter = defaultSnapshotter
172183
}
173184
}
174185

container_opts_unix.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func withRemappedSnapshotBase(id string, i Image, uid, gid uint32, readonly bool
5050
return err
5151
}
5252

53-
setSnapshotterIfEmpty(c)
53+
setSnapshotterIfEmpty(ctx, client, c)
5454

5555
var (
5656
snapshotter = client.SnapshotService(c.Snapshotter)

defaults/defaults.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,10 @@ const (
2323
// DefaultMaxSendMsgSize defines the default maximum message size for
2424
// sending protobufs passed over the GRPC API.
2525
DefaultMaxSendMsgSize = 16 << 20
26+
// DefaultRuntimeNSLabel defines the namespace label to check for
27+
// default runtime
28+
DefaultRuntimeNSLabel = "containerd.io/defaults/runtime"
29+
// DefaultSnapshotterNSLabel defines the namespances label to check for
30+
// default snapshotter
31+
DefaultSnapshotterNSLabel = "containerd.io/defaults/snapshotter"
2632
)

docs/namespaces.md

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@ Filesystem paths, IDs, and other system level resources must be namespaced for a
3434

3535
Simply create a new `context` and set your application's namespace on the `context`.
3636
Make sure to use a unique namespace for applications that does not conflict with existing namespaces. The namespaces
37-
API, or the `ctr namespaces` client command, can be used to query/list and create new namespaces. Note that namespaces
38-
can have a list of labels associated with the namespace. This can be useful for associating metadata with a particular
39-
namespace.
37+
API, or the `ctr namespaces` client command, can be used to query/list and create new namespaces.
4038

4139
```go
4240
ctx := context.Background()
@@ -49,6 +47,19 @@ var (
4947
)
5048
```
5149

50+
## Namespace Labels
51+
52+
Namespaces can have a list of labels associated with the namespace. This can be useful for associating metadata with a particular namespace.
53+
Labels can also be used to configure the defaults for containerd, for example:
54+
55+
```bash
56+
> sudo ctr namespaces label k8s.io containerd.io/defaults/snapshotter=btrfs
57+
> sudo ctr namespaces label k8s.io containerd.io/defaults/runtime=testRuntime
58+
```
59+
60+
This will set the default snapshotter as `btrfs` and runtime as `testRuntime`.
61+
Note that currently only these two labels are used to configure the defaults and labels of `default` namespace are not considered for the same.
62+
5263
## Inspecting Namespaces
5364

5465
If we need to inspect containers, images, or other resources in various namespaces the `ctr` tool allows you to do this.

0 commit comments

Comments
 (0)