Skip to content

Commit f2b6c31

Browse files
authored
Merge pull request #3403 from mxpv/labels
Better default label handling
2 parents 9d4fc1d + 550a6f1 commit f2b6c31

7 files changed

Lines changed: 91 additions & 55 deletions

File tree

client.go

Lines changed: 57 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,15 @@ func New(address string, opts ...ClientOpt) (*Client, error) {
8787
if copts.timeout == 0 {
8888
copts.timeout = 10 * time.Second
8989
}
90-
rt := fmt.Sprintf("%s.%s", plugin.RuntimePlugin, runtime.GOOS)
90+
91+
c := &Client{}
92+
9193
if copts.defaultRuntime != "" {
92-
rt = copts.defaultRuntime
93-
}
94-
c := &Client{
95-
runtime: rt,
94+
c.runtime = copts.defaultRuntime
95+
} else {
96+
c.runtime = fmt.Sprintf("%s.%s", plugin.RuntimePlugin, runtime.GOOS)
9697
}
98+
9799
if copts.services != nil {
98100
c.services = *copts.services
99101
}
@@ -140,14 +142,11 @@ func New(address string, opts ...ClientOpt) (*Client, error) {
140142

141143
// check namespace labels for default runtime
142144
if copts.defaultRuntime == "" && copts.defaultns != "" {
143-
namespaces := c.NamespaceService()
144-
ctx := context.Background()
145-
if labels, err := namespaces.Labels(ctx, copts.defaultns); err == nil {
146-
if defaultRuntime, ok := labels[defaults.DefaultRuntimeNSLabel]; ok {
147-
c.runtime = defaultRuntime
148-
}
149-
} else {
145+
ctx := namespaces.WithNamespace(context.Background(), copts.defaultns)
146+
if label, err := c.GetLabel(ctx, defaults.DefaultRuntimeNSLabel); err != nil {
150147
return nil, err
148+
} else if label != "" {
149+
c.runtime = label
151150
}
152151
}
153152

@@ -170,14 +169,11 @@ func NewWithConn(conn *grpc.ClientConn, opts ...ClientOpt) (*Client, error) {
170169

171170
// check namespace labels for default runtime
172171
if copts.defaultRuntime == "" && copts.defaultns != "" {
173-
namespaces := c.NamespaceService()
174-
ctx := context.Background()
175-
if labels, err := namespaces.Labels(ctx, copts.defaultns); err == nil {
176-
if defaultRuntime, ok := labels[defaults.DefaultRuntimeNSLabel]; ok {
177-
c.runtime = defaultRuntime
178-
}
179-
} else {
172+
ctx := namespaces.WithNamespace(context.Background(), copts.defaultns)
173+
if label, err := c.GetLabel(ctx, defaults.DefaultRuntimeNSLabel); err != nil {
180174
return nil, err
175+
} else if label != "" {
176+
c.runtime = label
181177
}
182178
}
183179

@@ -340,7 +336,6 @@ func defaultRemoteContext() *RemoteContext {
340336
Resolver: docker.NewResolver(docker.ResolverOptions{
341337
Client: http.DefaultClient,
342338
}),
343-
Snapshotter: DefaultSnapshotter,
344339
}
345340
}
346341

@@ -491,6 +486,24 @@ func writeIndex(ctx context.Context, index *ocispec.Index, client *Client, ref s
491486
return writeContent(ctx, client.ContentStore(), ocispec.MediaTypeImageIndex, ref, bytes.NewReader(data), content.WithLabels(labels))
492487
}
493488

489+
// GetLabel gets a label value from namespace store
490+
// If there is no default label, an empty string returned with nil error
491+
func (c *Client) GetLabel(ctx context.Context, label string) (string, error) {
492+
ns, err := namespaces.NamespaceRequired(ctx)
493+
if err != nil {
494+
return "", err
495+
}
496+
497+
srv := c.NamespaceService()
498+
labels, err := srv.Labels(ctx, ns)
499+
if err != nil {
500+
return "", err
501+
}
502+
503+
value := labels[label]
504+
return value, nil
505+
}
506+
494507
// Subscribe to events that match one or more of the provided filters.
495508
//
496509
// Callers should listen on both the envelope and errs channels. If the errs
@@ -656,11 +669,34 @@ func (c *Client) Version(ctx context.Context) (Version, error) {
656669
}, nil
657670
}
658671

659-
func (c *Client) getSnapshotter(name string) (snapshots.Snapshotter, error) {
672+
func (c *Client) resolveSnapshotterName(ctx context.Context, name string) (string, error) {
673+
if name == "" {
674+
label, err := c.GetLabel(ctx, defaults.DefaultSnapshotterNSLabel)
675+
if err != nil {
676+
return "", err
677+
}
678+
679+
if label != "" {
680+
name = label
681+
} else {
682+
name = DefaultSnapshotter
683+
}
684+
}
685+
686+
return name, nil
687+
}
688+
689+
func (c *Client) getSnapshotter(ctx context.Context, name string) (snapshots.Snapshotter, error) {
690+
name, err := c.resolveSnapshotterName(ctx, name)
691+
if err != nil {
692+
return nil, err
693+
}
694+
660695
s := c.SnapshotService(name)
661696
if s == nil {
662697
return nil, errors.Wrapf(errdefs.ErrNotFound, "snapshotter %s was not found", name)
663698
}
699+
664700
return s, nil
665701
}
666702

cmd/ctr/commands/commands.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"runtime"
2525
"strings"
2626

27-
"github.com/containerd/containerd"
2827
"github.com/urfave/cli"
2928
)
3029

@@ -34,7 +33,6 @@ var (
3433
cli.StringFlag{
3534
Name: "snapshotter",
3635
Usage: "snapshotter name. Empty value stands for the default value.",
37-
Value: containerd.DefaultSnapshotter,
3836
EnvVar: "CONTAINERD_SNAPSHOTTER",
3937
},
4038
}

cmd/ctr/commands/namespaces/namespaces.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import (
3333
// Command is the cli command for managing namespaces
3434
var Command = cli.Command{
3535
Name: "namespaces",
36-
Aliases: []string{"namespace"},
36+
Aliases: []string{"namespace", "ns"},
3737
Usage: "manage namespaces",
3838
Subcommands: cli.Commands{
3939
createCommand,
@@ -45,6 +45,7 @@ var Command = cli.Command{
4545

4646
var createCommand = cli.Command{
4747
Name: "create",
48+
Aliases: []string{"c"},
4849
Usage: "create a new namespace",
4950
ArgsUsage: "<name> [<key>=<value]",
5051
Description: "create a new namespace. it must be unique",

container.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ func (c *container) NewTask(ctx context.Context, ioCreate cio.Creator, opts ...N
233233
}
234234

235235
// get the rootfs from the snapshotter and add it to the request
236-
s, err := c.client.getSnapshotter(r.Snapshotter)
236+
s, err := c.client.getSnapshotter(ctx, r.Snapshotter)
237237
if err != nil {
238238
return nil, err
239239
}

container_opts.go

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

2222
"github.com/containerd/containerd/containers"
23-
"github.com/containerd/containerd/defaults"
2423
"github.com/containerd/containerd/errdefs"
25-
"github.com/containerd/containerd/namespaces"
2624
"github.com/containerd/containerd/oci"
2725
"github.com/containerd/containerd/platforms"
2826
"github.com/containerd/containerd/snapshots"
@@ -118,9 +116,13 @@ func WithSnapshotter(name string) NewContainerOpts {
118116
// WithSnapshot uses an existing root filesystem for the container
119117
func WithSnapshot(id string) NewContainerOpts {
120118
return func(ctx context.Context, client *Client, c *containers.Container) error {
121-
setSnapshotterIfEmpty(ctx, client, c)
122119
// check that the snapshot exists, if not, fail on creation
123-
s, err := client.getSnapshotter(c.Snapshotter)
120+
var err error
121+
c.Snapshotter, err = client.resolveSnapshotterName(ctx, c.Snapshotter)
122+
if err != nil {
123+
return err
124+
}
125+
s, err := client.getSnapshotter(ctx, c.Snapshotter)
124126
if err != nil {
125127
return err
126128
}
@@ -140,9 +142,13 @@ func WithNewSnapshot(id string, i Image, opts ...snapshots.Opt) NewContainerOpts
140142
if err != nil {
141143
return err
142144
}
143-
setSnapshotterIfEmpty(ctx, client, c)
145+
144146
parent := identity.ChainID(diffIDs).String()
145-
s, err := client.getSnapshotter(c.Snapshotter)
147+
c.Snapshotter, err = client.resolveSnapshotterName(ctx, c.Snapshotter)
148+
if err != nil {
149+
return err
150+
}
151+
s, err := client.getSnapshotter(ctx, c.Snapshotter)
146152
if err != nil {
147153
return err
148154
}
@@ -161,7 +167,7 @@ func WithSnapshotCleanup(ctx context.Context, client *Client, c containers.Conta
161167
if c.Snapshotter == "" {
162168
return errors.Wrapf(errdefs.ErrInvalidArgument, "container.Snapshotter must be set to cleanup rootfs snapshot")
163169
}
164-
s, err := client.getSnapshotter(c.Snapshotter)
170+
s, err := client.getSnapshotter(ctx, c.Snapshotter)
165171
if err != nil {
166172
return err
167173
}
@@ -178,9 +184,13 @@ func WithNewSnapshotView(id string, i Image, opts ...snapshots.Opt) NewContainer
178184
if err != nil {
179185
return err
180186
}
181-
setSnapshotterIfEmpty(ctx, client, c)
187+
182188
parent := identity.ChainID(diffIDs).String()
183-
s, err := client.getSnapshotter(c.Snapshotter)
189+
c.Snapshotter, err = client.resolveSnapshotterName(ctx, c.Snapshotter)
190+
if err != nil {
191+
return err
192+
}
193+
s, err := client.getSnapshotter(ctx, c.Snapshotter)
184194
if err != nil {
185195
return err
186196
}
@@ -193,21 +203,6 @@ func WithNewSnapshotView(id string, i Image, opts ...snapshots.Opt) NewContainer
193203
}
194204
}
195205

196-
func setSnapshotterIfEmpty(ctx context.Context, client *Client, c *containers.Container) {
197-
if c.Snapshotter == "" {
198-
defaultSnapshotter := DefaultSnapshotter
199-
namespaceService := client.NamespaceService()
200-
if ns, err := namespaces.NamespaceRequired(ctx); err == nil {
201-
if labels, err := namespaceService.Labels(ctx, ns); err == nil {
202-
if snapshotLabel, ok := labels[defaults.DefaultSnapshotterNSLabel]; ok {
203-
defaultSnapshotter = snapshotLabel
204-
}
205-
}
206-
}
207-
c.Snapshotter = defaultSnapshotter
208-
}
209-
}
210-
211206
// WithContainerExtension appends extension data to the container object.
212207
// Use this to decorate the container object with additional data for the client
213208
// integration.

container_opts_unix.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,15 @@ func withRemappedSnapshotBase(id string, i Image, uid, gid uint32, readonly bool
5050
return err
5151
}
5252

53-
setSnapshotterIfEmpty(ctx, client, c)
54-
5553
var (
5654
parent = identity.ChainID(diffIDs).String()
5755
usernsID = fmt.Sprintf("%s-%d-%d", parent, uid, gid)
5856
)
59-
snapshotter, err := client.getSnapshotter(c.Snapshotter)
57+
c.Snapshotter, err = client.resolveSnapshotterName(ctx, c.Snapshotter)
58+
if err != nil {
59+
return err
60+
}
61+
snapshotter, err := client.getSnapshotter(ctx, c.Snapshotter)
6062
if err != nil {
6163
return err
6264
}

image.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
"github.com/containerd/containerd/images"
2626
"github.com/containerd/containerd/platforms"
2727
"github.com/containerd/containerd/rootfs"
28-
digest "github.com/opencontainers/go-digest"
28+
"github.com/opencontainers/go-digest"
2929
"github.com/opencontainers/image-spec/identity"
3030
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
3131
"github.com/pkg/errors"
@@ -108,7 +108,7 @@ func (i *image) Config(ctx context.Context) (ocispec.Descriptor, error) {
108108
}
109109

110110
func (i *image) IsUnpacked(ctx context.Context, snapshotterName string) (bool, error) {
111-
sn, err := i.client.getSnapshotter(snapshotterName)
111+
sn, err := i.client.getSnapshotter(ctx, snapshotterName)
112112
if err != nil {
113113
return false, err
114114
}
@@ -149,7 +149,11 @@ func (i *image) Unpack(ctx context.Context, snapshotterName string) error {
149149
chain []digest.Digest
150150
unpacked bool
151151
)
152-
sn, err := i.client.getSnapshotter(snapshotterName)
152+
snapshotterName, err = i.client.resolveSnapshotterName(ctx, snapshotterName)
153+
if err != nil {
154+
return err
155+
}
156+
sn, err := i.client.getSnapshotter(ctx, snapshotterName)
153157
if err != nil {
154158
return err
155159
}

0 commit comments

Comments
 (0)