Skip to content

Commit 3432398

Browse files
committed
Use labels only when default namespace is provided and prefer given
options. Implements same approach of setting defaults for `NewWithConn`. Signed-off-by: Nikhil Soni <[email protected]>
1 parent 59432aa commit 3432398

3 files changed

Lines changed: 27 additions & 14 deletions

File tree

client.go

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -140,18 +140,16 @@ func New(address string, opts ...ClientOpt) (*Client, error) {
140140
}
141141

142142
// 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
143+
if copts.defaultRuntime == "" && copts.defaultns != "" {
144+
namespaces := c.NamespaceService()
145+
ctx := context.Background()
146+
if labels, err := namespaces.Labels(ctx, copts.defaultns); err == nil {
147+
if defaultRuntime, ok := labels["containerd.io/defaults/runtime"]; ok {
148+
c.runtime = defaultRuntime
149+
}
150+
} else {
151+
return nil, err
152152
}
153-
} else {
154-
return nil, err
155153
}
156154

157155
return c, nil
@@ -170,6 +168,20 @@ func NewWithConn(conn *grpc.ClientConn, opts ...ClientOpt) (*Client, error) {
170168
conn: conn,
171169
runtime: fmt.Sprintf("%s.%s", plugin.RuntimePlugin, runtime.GOOS),
172170
}
171+
172+
// check namespace labels for default runtime
173+
if copts.defaultRuntime == "" && copts.defaultns != "" {
174+
namespaces := c.NamespaceService()
175+
ctx := context.Background()
176+
if labels, err := namespaces.Labels(ctx, copts.defaultns); err == nil {
177+
if defaultRuntime, ok := labels["containerd.io/defaults/runtime"]; ok {
178+
c.runtime = defaultRuntime
179+
}
180+
} else {
181+
return nil, err
182+
}
183+
}
184+
173185
if copts.services != nil {
174186
c.services = *copts.services
175187
}

client_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -405,11 +405,12 @@ func TestDefaultRuntimeWithNamespaceLabels(t *testing.T) {
405405
defer cancel()
406406
namespaces := client.NamespaceService()
407407
testRuntime := "testRuntime"
408-
if err := namespaces.SetLabel(ctx, testNamespace, "runtime", testRuntime); err != nil {
408+
runtimeLabel := "containerd.io/defaults/runtime"
409+
if err := namespaces.SetLabel(ctx, testNamespace, runtimeLabel, testRuntime); err != nil {
409410
t.Fatal(err)
410411
}
411412

412-
testClient, err := newClient(t, address, WithDefaultNamespace(testNamespace))
413+
testClient, err := New(address, WithDefaultNamespace(testNamespace))
413414
if err != nil {
414415
t.Fatal(err)
415416
}

container_opts.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ func setSnapshotterIfEmpty(ctx context.Context, client *Client, c *containers.Co
172172
namespaceService := client.NamespaceService()
173173
if ns, err := namespaces.NamespaceRequired(ctx); err == nil {
174174
if labels, err := namespaceService.Labels(ctx, ns); err == nil {
175-
if snapshotLabel, ok := labels["snapshotter"]; ok {
175+
if snapshotLabel, ok := labels["containerd.io/defaults/snapshotter"]; ok {
176176
defaultSnapshotter = snapshotLabel
177177
}
178178
}

0 commit comments

Comments
 (0)