diff --git a/client.go b/client.go index 2d255c0e1..9b0888c79 100644 --- a/client.go +++ b/client.go @@ -87,13 +87,15 @@ func New(address string, opts ...ClientOpt) (*Client, error) { if copts.timeout == 0 { copts.timeout = 10 * time.Second } - rt := fmt.Sprintf("%s.%s", plugin.RuntimePlugin, runtime.GOOS) + + c := &Client{} + if copts.defaultRuntime != "" { - rt = copts.defaultRuntime - } - c := &Client{ - runtime: rt, + c.runtime = copts.defaultRuntime + } else { + c.runtime = fmt.Sprintf("%s.%s", plugin.RuntimePlugin, runtime.GOOS) } + if copts.services != nil { c.services = *copts.services } @@ -140,14 +142,11 @@ func New(address string, opts ...ClientOpt) (*Client, error) { // check namespace labels for default runtime if copts.defaultRuntime == "" && copts.defaultns != "" { - namespaces := c.NamespaceService() - ctx := context.Background() - if labels, err := namespaces.Labels(ctx, copts.defaultns); err == nil { - if defaultRuntime, ok := labels[defaults.DefaultRuntimeNSLabel]; ok { - c.runtime = defaultRuntime - } - } else { + ctx := namespaces.WithNamespace(context.Background(), copts.defaultns) + if label, err := c.GetLabel(ctx, defaults.DefaultRuntimeNSLabel); err != nil { return nil, err + } else if label != "" { + c.runtime = label } } @@ -170,14 +169,11 @@ func NewWithConn(conn *grpc.ClientConn, opts ...ClientOpt) (*Client, error) { // check namespace labels for default runtime if copts.defaultRuntime == "" && copts.defaultns != "" { - namespaces := c.NamespaceService() - ctx := context.Background() - if labels, err := namespaces.Labels(ctx, copts.defaultns); err == nil { - if defaultRuntime, ok := labels[defaults.DefaultRuntimeNSLabel]; ok { - c.runtime = defaultRuntime - } - } else { + ctx := namespaces.WithNamespace(context.Background(), copts.defaultns) + if label, err := c.GetLabel(ctx, defaults.DefaultRuntimeNSLabel); err != nil { return nil, err + } else if label != "" { + c.runtime = label } } @@ -340,7 +336,6 @@ func defaultRemoteContext() *RemoteContext { Resolver: docker.NewResolver(docker.ResolverOptions{ Client: http.DefaultClient, }), - Snapshotter: DefaultSnapshotter, } } @@ -491,6 +486,24 @@ func writeIndex(ctx context.Context, index *ocispec.Index, client *Client, ref s return writeContent(ctx, client.ContentStore(), ocispec.MediaTypeImageIndex, ref, bytes.NewReader(data), content.WithLabels(labels)) } +// GetLabel gets a label value from namespace store +// If there is no default label, an empty string returned with nil error +func (c *Client) GetLabel(ctx context.Context, label string) (string, error) { + ns, err := namespaces.NamespaceRequired(ctx) + if err != nil { + return "", err + } + + srv := c.NamespaceService() + labels, err := srv.Labels(ctx, ns) + if err != nil { + return "", err + } + + value := labels[label] + return value, nil +} + // Subscribe to events that match one or more of the provided filters. // // 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) { }, nil } -func (c *Client) getSnapshotter(name string) (snapshots.Snapshotter, error) { +func (c *Client) resolveSnapshotterName(ctx context.Context, name string) (string, error) { + if name == "" { + label, err := c.GetLabel(ctx, defaults.DefaultSnapshotterNSLabel) + if err != nil { + return "", err + } + + if label != "" { + name = label + } else { + name = DefaultSnapshotter + } + } + + return name, nil +} + +func (c *Client) getSnapshotter(ctx context.Context, name string) (snapshots.Snapshotter, error) { + name, err := c.resolveSnapshotterName(ctx, name) + if err != nil { + return nil, err + } + s := c.SnapshotService(name) if s == nil { return nil, errors.Wrapf(errdefs.ErrNotFound, "snapshotter %s was not found", name) } + return s, nil } diff --git a/cmd/ctr/commands/commands.go b/cmd/ctr/commands/commands.go index 6d9fb5488..234971d35 100644 --- a/cmd/ctr/commands/commands.go +++ b/cmd/ctr/commands/commands.go @@ -24,7 +24,6 @@ import ( "runtime" "strings" - "github.com/containerd/containerd" "github.com/urfave/cli" ) @@ -34,7 +33,6 @@ var ( cli.StringFlag{ Name: "snapshotter", Usage: "snapshotter name. Empty value stands for the default value.", - Value: containerd.DefaultSnapshotter, EnvVar: "CONTAINERD_SNAPSHOTTER", }, } diff --git a/cmd/ctr/commands/namespaces/namespaces.go b/cmd/ctr/commands/namespaces/namespaces.go index 81ce3dc0a..93455b14c 100644 --- a/cmd/ctr/commands/namespaces/namespaces.go +++ b/cmd/ctr/commands/namespaces/namespaces.go @@ -33,7 +33,7 @@ import ( // Command is the cli command for managing namespaces var Command = cli.Command{ Name: "namespaces", - Aliases: []string{"namespace"}, + Aliases: []string{"namespace", "ns"}, Usage: "manage namespaces", Subcommands: cli.Commands{ createCommand, @@ -45,6 +45,7 @@ var Command = cli.Command{ var createCommand = cli.Command{ Name: "create", + Aliases: []string{"c"}, Usage: "create a new namespace", ArgsUsage: " [=