Skip to content

Commit 59432aa

Browse files
committed
Take default runtime and snapshotter from namespace labels
Signed-off-by: Nikhil Soni <[email protected]>
1 parent f7f24e2 commit 59432aa

4 files changed

Lines changed: 57 additions & 6 deletions

File tree

client.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,22 @@ func New(address string, opts ...ClientOpt) (*Client, error) {
138138
if copts.services == nil && c.conn == nil {
139139
return nil, errors.New("no grpc connection or services is available")
140140
}
141+
142+
// check namespace labels for default runtime
143+
defaultns := "default"
144+
if copts.defaultns != "" {
145+
defaultns = copts.defaultns
146+
}
147+
namespaces := c.NamespaceService()
148+
ctx := context.Background()
149+
if labels, err := namespaces.Labels(ctx, defaultns); err == nil {
150+
if defaultRuntime, ok := labels["runtime"]; ok {
151+
c.runtime = defaultRuntime
152+
}
153+
} else {
154+
return nil, err
155+
}
156+
141157
return c, nil
142158
}
143159

client_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,3 +393,28 @@ func createShimDebugConfig() string {
393393

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

container_opts.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121

2222
"github.com/containerd/containerd/containers"
2323
"github.com/containerd/containerd/errdefs"
24+
"github.com/containerd/containerd/namespaces"
2425
"github.com/containerd/containerd/oci"
2526
"github.com/containerd/containerd/platforms"
2627
"github.com/containerd/typeurl"
@@ -106,7 +107,7 @@ func WithSnapshotter(name string) NewContainerOpts {
106107
// WithSnapshot uses an existing root filesystem for the container
107108
func WithSnapshot(id string) NewContainerOpts {
108109
return func(ctx context.Context, client *Client, c *containers.Container) error {
109-
setSnapshotterIfEmpty(c)
110+
setSnapshotterIfEmpty(ctx, client, c)
110111
// check that the snapshot exists, if not, fail on creation
111112
if _, err := client.SnapshotService(c.Snapshotter).Mounts(ctx, id); err != nil {
112113
return err
@@ -124,7 +125,7 @@ func WithNewSnapshot(id string, i Image) NewContainerOpts {
124125
if err != nil {
125126
return err
126127
}
127-
setSnapshotterIfEmpty(c)
128+
setSnapshotterIfEmpty(ctx, client, c)
128129
parent := identity.ChainID(diffIDs).String()
129130
if _, err := client.SnapshotService(c.Snapshotter).Prepare(ctx, id, parent); err != nil {
130131
return err
@@ -154,7 +155,7 @@ func WithNewSnapshotView(id string, i Image) NewContainerOpts {
154155
if err != nil {
155156
return err
156157
}
157-
setSnapshotterIfEmpty(c)
158+
setSnapshotterIfEmpty(ctx, client, c)
158159
parent := identity.ChainID(diffIDs).String()
159160
if _, err := client.SnapshotService(c.Snapshotter).View(ctx, id, parent); err != nil {
160161
return err
@@ -165,9 +166,18 @@ func WithNewSnapshotView(id string, i Image) NewContainerOpts {
165166
}
166167
}
167168

168-
func setSnapshotterIfEmpty(c *containers.Container) {
169+
func setSnapshotterIfEmpty(ctx context.Context, client *Client, c *containers.Container) {
169170
if c.Snapshotter == "" {
170-
c.Snapshotter = DefaultSnapshotter
171+
defaultSnapshotter := DefaultSnapshotter
172+
namespaceService := client.NamespaceService()
173+
if ns, err := namespaces.NamespaceRequired(ctx); err == nil {
174+
if labels, err := namespaceService.Labels(ctx, ns); err == nil {
175+
if snapshotLabel, ok := labels["snapshotter"]; ok {
176+
defaultSnapshotter = snapshotLabel
177+
}
178+
}
179+
}
180+
c.Snapshotter = defaultSnapshotter
171181
}
172182
}
173183

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)

0 commit comments

Comments
 (0)