diff --git a/client.go b/client.go index 99f9d86c2..be99e00b9 100644 --- a/client.go +++ b/client.go @@ -137,7 +137,7 @@ func New(address string, opts ...ClientOpt) (*Client, error) { c.conn, c.connector = conn, connector } if copts.services == nil && c.conn == nil { - return nil, errdefs.ErrNoGRPCAndService + return nil, ErrNoGRPCAndService } // check namespace labels for default runtime @@ -196,7 +196,7 @@ type Client struct { // Reconnect re-establishes the GRPC connection to the containerd daemon func (c *Client) Reconnect() error { if c.connector == nil { - return errdefs.ErrReconnectFailed + return ErrReconnectFailed } c.connMu.Lock() defer c.connMu.Unlock() @@ -219,7 +219,7 @@ func (c *Client) IsServing(ctx context.Context) (bool, error) { c.connMu.Lock() if c.conn == nil { c.connMu.Unlock() - return false, errdefs.ErrNoGRPC + return false, ErrNoGRPC } c.connMu.Unlock() r, err := c.HealthService().Check(ctx, &grpc_health_v1.HealthCheckRequest{}, grpc.WaitForReady(true)) @@ -350,7 +350,7 @@ func (c *Client) Fetch(ctx context.Context, ref string, opts ...RemoteOpt) (imag } if fetchCtx.Unpack { - return images.Image{}, errdefs.ErrUnpackNotSupported + return images.Image{}, ErrUnpackNotSupported } if fetchCtx.PlatformMatcher == nil { @@ -656,7 +656,7 @@ func (c *Client) Version(ctx context.Context) (Version, error) { c.connMu.Lock() if c.conn == nil { c.connMu.Unlock() - return Version{}, errdefs.ErrNoGRPC + return Version{}, ErrNoGRPC } c.connMu.Unlock() response, err := c.VersionService().Version(ctx, &ptypes.Empty{}) diff --git a/cmd/containerd/command/error.go b/cmd/containerd/command/error.go new file mode 100644 index 000000000..3e8572d63 --- /dev/null +++ b/cmd/containerd/command/error.go @@ -0,0 +1,32 @@ +/* + Copyright The containerd Authors. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package command + +import ( + "github.com/pkg/errors" +) + +var ( + // ErrUnknownLevel is returned when an unknown debugging level is encountered + ErrUnknownLevel = errors.New("unknown level") + // ErrRegisterAndUnregisterService is returned when both register and unregister flags are specified + ErrRegisterAndUnregisterService = errors.New("--register-service and --unregister-service cannot be used together") + // ErrEmptyTopic is returned when no topic is provided + ErrEmptyTopic = errors.New("topic required to publish event") + // ErrEmptyGRCPAddress is returned when the grpc address is empty + ErrEmptyGRCPAddress = errors.New("grpc address cannot be empty") +) diff --git a/cmd/containerd/command/main.go b/cmd/containerd/command/main.go index 8de29d4cf..ca0bc8d0b 100644 --- a/cmd/containerd/command/main.go +++ b/cmd/containerd/command/main.go @@ -27,7 +27,6 @@ import ( "runtime" "time" - "github.com/containerd/containerd/errdefs" "github.com/containerd/containerd/log" "github.com/containerd/containerd/mount" "github.com/containerd/containerd/services/server" @@ -153,7 +152,7 @@ func App() *cli.App { ttrpcAddress = fmt.Sprintf("%s.ttrpc", config.GRPC.Address) ) if address == "" { - return errdefs.ErrEmptyGRCPAddress + return ErrEmptyGRCPAddress } log.G(ctx).WithFields(logrus.Fields{ "version": version.Version, diff --git a/cmd/containerd/command/publish.go b/cmd/containerd/command/publish.go index edb0e9816..321ad43d0 100644 --- a/cmd/containerd/command/publish.go +++ b/cmd/containerd/command/publish.go @@ -51,7 +51,7 @@ var publishCommand = cli.Command{ ctx := namespaces.WithNamespace(gocontext.Background(), context.String("namespace")) topic := context.String("topic") if topic == "" { - return errdefs.ErrEmptyTopic + return ErrEmptyTopic } payload, err := getEventPayload(os.Stdin) if err != nil { diff --git a/cmd/containerd/command/service_windows.go b/cmd/containerd/command/service_windows.go index 319f9e095..4d70b9125 100644 --- a/cmd/containerd/command/service_windows.go +++ b/cmd/containerd/command/service_windows.go @@ -27,7 +27,6 @@ import ( "time" "unsafe" - "github.com/containerd/containerd/errdefs" "github.com/containerd/containerd/services/server" "github.com/sirupsen/logrus" "github.com/urfave/cli" @@ -162,7 +161,7 @@ func (h *etwHook) Fire(e *logrus.Entry) error { etype = windows.EVENTLOG_INFORMATION_TYPE eid = eventDebug default: - return errdefs.ErrUnknownLevel + return ErrUnknownLevel } // If there is additional data, include it as a second string. @@ -311,7 +310,7 @@ func registerUnregisterService(root string) (bool, error) { if unregisterServiceFlag { if registerServiceFlag { - return true, errdefs.ErrRegisterAndUnregisterService + return true, ErrRegisterAndUnregisterService } return true, unregisterService() } diff --git a/cmd/ctr/commands/containers/containers.go b/cmd/ctr/commands/containers/containers.go index a2d7afcca..1a1fa9b5d 100644 --- a/cmd/ctr/commands/containers/containers.go +++ b/cmd/ctr/commands/containers/containers.go @@ -28,7 +28,6 @@ import ( "github.com/containerd/containerd/cmd/ctr/commands" "github.com/containerd/containerd/cmd/ctr/commands/run" "github.com/containerd/containerd/containers" - "github.com/containerd/containerd/errdefs" "github.com/containerd/containerd/log" "github.com/containerd/typeurl" "github.com/urfave/cli" @@ -65,17 +64,17 @@ var createCommand = cli.Command{ if config { id = context.Args().First() if context.NArg() > 1 { - return errdefs.ErrArgConfigFile + return commands.ErrArgConfigFile } } else { id = context.Args().Get(1) ref = context.Args().First() if ref == "" { - return errdefs.ErrUnprovidedImageRef + return commands.ErrUnprovidedImageRef } } if id == "" { - return errdefs.ErrEmptyContainerID + return commands.ErrEmptyContainerID } client, ctx, cancel, err := commands.NewClient(context) if err != nil { @@ -168,7 +167,7 @@ var deleteCommand = cli.Command{ } if context.NArg() == 0 { - return errdefs.ErrDeleteNoneContainer + return commands.ErrDeleteNoneContainer } for _, arg := range context.Args() { if err := deleteContainer(ctx, client, arg, deleteOpts...); err != nil { @@ -214,7 +213,7 @@ var setLabelsCommand = cli.Command{ Action: func(context *cli.Context) error { containerID, labels := commands.ObjectWithLabelArgs(context) if containerID == "" { - return errdefs.ErrEmptyContainerID + return commands.ErrEmptyContainerID } client, ctx, cancel, err := commands.NewClient(context) if err != nil { @@ -250,7 +249,7 @@ var infoCommand = cli.Command{ Action: func(context *cli.Context) error { id := context.Args().First() if id == "" { - return errdefs.ErrEmptyContainerID + return commands.ErrEmptyContainerID } client, ctx, cancel, err := commands.NewClient(context) if err != nil { diff --git a/cmd/ctr/commands/error.go b/cmd/ctr/commands/error.go new file mode 100644 index 000000000..c5a1e9f90 --- /dev/null +++ b/cmd/ctr/commands/error.go @@ -0,0 +1,32 @@ +/* + Copyright The containerd Authors. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package commands + +import ( + "github.com/pkg/errors" +) + +var ( + // ErrArgConfigFile is returned when the configuration for a spec is provided + ErrArgConfigFile = errors.New("with spec config file, only container id should be provided") + // ErrUnprovidedImageRef is returned when no image reference is provided + ErrUnprovidedImageRef = errors.New("image ref must be provided") + // ErrEmptyContainerID is returned when no container id is provided + ErrEmptyContainerID = errors.New("container id must be provided") + // ErrDeleteNoneContainer is returned when no container ids are provided for deletion + ErrDeleteNoneContainer = errors.New("must specify at least one container to delete") +) diff --git a/errdefs/errors.go b/errdefs/errors.go index 94844df47..b5200afc0 100644 --- a/errdefs/errors.go +++ b/errdefs/errors.go @@ -40,25 +40,13 @@ import ( // For the most part, we just try to provide local grpc errors. Most conditions // map very well to those defined by grpc. var ( - ErrUnknown = errors.New("unknown") // used internally to represent a missed mapping. - ErrInvalidArgument = errors.New("invalid argument") - ErrNotFound = errors.New("not found") - ErrAlreadyExists = errors.New("already exists") - ErrFailedPrecondition = errors.New("failed precondition") - ErrUnavailable = errors.New("unavailable") - ErrNotImplemented = errors.New("not implemented") // represents not supported and unimplemented - ErrNoGRPCAndService = errors.New("no grpc connection and services is available") - ErrNoGRPC = errors.New("no grpc connection available") - ErrReconnectFailed = errors.New("unable to reconnect to containerd, no connector available") - ErrUnpackNotSupported = errors.New("unpack on fetch not supported, try pull") - ErrEmptyGRCPAddress = errors.New("grpc address cannot be empty") - ErrEmptyTopic = errors.New("topic required to publish event") - ErrUnknownLevel = errors.New("unknown level") - ErrRegisterAndUnregisterService = errors.New("--register-service and --unregister-service cannot be used together") - ErrArgConfigFile = errors.New("with spec config file, only container id should be provided") - ErrUnprovidedImageRef = errors.New("image ref must be provided") - ErrEmptyContainerID = errors.New("container id must be provided") - ErrDeleteNoneContainer = errors.New("must specify at least one container to delete") + ErrUnknown = errors.New("unknown") // used internally to represent a missed mapping. + ErrInvalidArgument = errors.New("invalid argument") + ErrNotFound = errors.New("not found") + ErrAlreadyExists = errors.New("already exists") + ErrFailedPrecondition = errors.New("failed precondition") + ErrUnavailable = errors.New("unavailable") + ErrNotImplemented = errors.New("not implemented") // represents not supported and unimplemented ) // IsInvalidArgument returns true if the error is due to an invalid argument diff --git a/error.go b/error.go new file mode 100644 index 000000000..e3ccba1b7 --- /dev/null +++ b/error.go @@ -0,0 +1,32 @@ +/* + Copyright The containerd Authors. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package containerd + +import ( + "github.com/pkg/errors" +) + +var ( + // ErrNoGRPCAndService is returned when no connection or service is available + ErrNoGRPCAndService = errors.New("no grpc connection and service is available") + // ErrReconnectFailed is returned when no connector is available to reconnect + ErrReconnectFailed = errors.New("unable to reconnect to containerd, no connector available") + // ErrNoGRPC is returned when no grpc connect is available + ErrNoGRPC = errors.New("no grpc connection available") + // ErrUnpackNotSupported is returned when a fetch cannot unpack + ErrUnpackNotSupported = errors.New("unpack on fetch not supported, try pull") +)