Skip to content

Commit 0adf2fb

Browse files
committed
centralize harded-code message
Signed-off-by: Fahed DORGAA <[email protected]>
1 parent 290fc9e commit 0adf2fb

10 files changed

Lines changed: 49 additions & 29 deletions

File tree

client.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import (
4343
"github.com/containerd/containerd/content"
4444
contentproxy "github.com/containerd/containerd/content/proxy"
4545
"github.com/containerd/containerd/defaults"
46+
"github.com/containerd/containerd/errdefs"
4647
"github.com/containerd/containerd/events"
4748
"github.com/containerd/containerd/images"
4849
"github.com/containerd/containerd/leases"
@@ -134,7 +135,7 @@ func New(address string, opts ...ClientOpt) (*Client, error) {
134135
c.conn, c.connector = conn, connector
135136
}
136137
if copts.services == nil && c.conn == nil {
137-
return nil, errors.New("no grpc connection or services is available")
138+
return nil, errdefs.ErrNoGRPCAndService
138139
}
139140

140141
// check namespace labels for default runtime
@@ -199,7 +200,7 @@ type Client struct {
199200
// Reconnect re-establishes the GRPC connection to the containerd daemon
200201
func (c *Client) Reconnect() error {
201202
if c.connector == nil {
202-
return errors.New("unable to reconnect to containerd, no connector available")
203+
return errdefs.ErrReconnectFailed
203204
}
204205
c.connMu.Lock()
205206
defer c.connMu.Unlock()
@@ -222,7 +223,7 @@ func (c *Client) IsServing(ctx context.Context) (bool, error) {
222223
c.connMu.Lock()
223224
if c.conn == nil {
224225
c.connMu.Unlock()
225-
return false, errors.New("no grpc connection available")
226+
return false, errdefs.ErrNoGRPC
226227
}
227228
c.connMu.Unlock()
228229
r, err := c.HealthService().Check(ctx, &grpc_health_v1.HealthCheckRequest{}, grpc.WaitForReady(true))
@@ -354,7 +355,7 @@ func (c *Client) Fetch(ctx context.Context, ref string, opts ...RemoteOpt) (imag
354355
}
355356

356357
if fetchCtx.Unpack {
357-
return images.Image{}, errors.New("unpack on fetch not supported, try pull")
358+
return images.Image{}, errdefs.ErrUnpackNotSupported
358359
}
359360

360361
if fetchCtx.PlatformMatcher == nil {
@@ -642,7 +643,7 @@ func (c *Client) Version(ctx context.Context) (Version, error) {
642643
c.connMu.Lock()
643644
if c.conn == nil {
644645
c.connMu.Unlock()
645-
return Version{}, errors.New("no grpc connection available")
646+
return Version{}, errdefs.ErrNoGRPC
646647
}
647648
c.connMu.Unlock()
648649
response, err := c.VersionService().Version(ctx, &ptypes.Empty{})

cmd/containerd/command/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import (
3636
"github.com/pkg/errors"
3737
"github.com/sirupsen/logrus"
3838
"github.com/urfave/cli"
39+
"github.com/containerd/containerd/errdefs"
3940
"google.golang.org/grpc/grpclog"
4041
)
4142

@@ -152,7 +153,7 @@ func App() *cli.App {
152153
ttrpcAddress = fmt.Sprintf("%s.ttrpc", config.GRPC.Address)
153154
)
154155
if address == "" {
155-
return errors.New("grpc address cannot be empty")
156+
return errdefs.ErrEmptyGRCPAddress
156157
}
157158
log.G(ctx).WithFields(logrus.Fields{
158159
"version": version.Version,

cmd/containerd/command/publish.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ var publishCommand = cli.Command{
5151
ctx := namespaces.WithNamespace(gocontext.Background(), context.String("namespace"))
5252
topic := context.String("topic")
5353
if topic == "" {
54-
return errors.New("topic required to publish event")
54+
return errdefs.ErrEmptyTopic
5555
}
5656
payload, err := getEventPayload(os.Stdin)
5757
if err != nil {

cmd/containerd/command/service_windows.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package command
1818

1919
import (
2020
"bytes"
21-
"errors"
2221
"fmt"
2322
"io/ioutil"
2423
"log"
@@ -28,9 +27,11 @@ import (
2827
"time"
2928
"unsafe"
3029

30+
"github.com/containerd/containerd/errdefs"
3131
"github.com/containerd/containerd/services/server"
3232
"github.com/sirupsen/logrus"
3333
"github.com/urfave/cli"
34+
3435
"golang.org/x/sys/windows"
3536
"golang.org/x/sys/windows/svc"
3637
"golang.org/x/sys/windows/svc/debug"
@@ -162,7 +163,7 @@ func (h *etwHook) Fire(e *logrus.Entry) error {
162163
etype = windows.EVENTLOG_INFORMATION_TYPE
163164
eid = eventDebug
164165
default:
165-
return errors.New("unknown level")
166+
return errdefs.ErrUnknownLevel
166167
}
167168

168169
// If there is additional data, include it as a second string.
@@ -270,8 +271,8 @@ func registerService() error {
270271
Delay uint32
271272
}
272273
t := []scAction{
273-
{Type: scActionRestart, Delay: uint32(60 * time.Second / time.Millisecond)},
274-
{Type: scActionRestart, Delay: uint32(60 * time.Second / time.Millisecond)},
274+
{Type: scActionRestart, Delay: uint32(15 * time.Second / time.Millisecond)},
275+
{Type: scActionRestart, Delay: uint32(15 * time.Second / time.Millisecond)},
275276
{Type: scActionNone},
276277
}
277278
lpInfo := serviceFailureActions{ResetPeriod: uint32(24 * time.Hour / time.Second), ActionsCount: uint32(3), Actions: uintptr(unsafe.Pointer(&t[0]))}
@@ -311,7 +312,7 @@ func registerUnregisterService(root string) (bool, error) {
311312

312313
if unregisterServiceFlag {
313314
if registerServiceFlag {
314-
return true, errors.New("--register-service and --unregister-service cannot be used together")
315+
return true, errdefs.ErrRegisterAndUnregisterService
315316
}
316317
return true, unregisterService()
317318
}

cmd/ctr/commands/containers/containers.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ import (
2828
"github.com/containerd/containerd/cmd/ctr/commands"
2929
"github.com/containerd/containerd/cmd/ctr/commands/run"
3030
"github.com/containerd/containerd/containers"
31+
"github.com/containerd/containerd/errdefs"
3132
"github.com/containerd/containerd/log"
3233
"github.com/containerd/typeurl"
33-
"github.com/pkg/errors"
3434
"github.com/urfave/cli"
3535
)
3636

@@ -65,17 +65,17 @@ var createCommand = cli.Command{
6565
if config {
6666
id = context.Args().First()
6767
if context.NArg() > 1 {
68-
return errors.New("with spec config file, only container id should be provided")
68+
return errdefs.ErrArgConfigFile
6969
}
7070
} else {
7171
id = context.Args().Get(1)
7272
ref = context.Args().First()
7373
if ref == "" {
74-
return errors.New("image ref must be provided")
74+
return errdefs.ErrUnprovidedImageRef
7575
}
7676
}
7777
if id == "" {
78-
return errors.New("container id must be provided")
78+
return errdefs.ErrEmptyContainerId
7979
}
8080
client, ctx, cancel, err := commands.NewClient(context)
8181
if err != nil {
@@ -168,7 +168,7 @@ var deleteCommand = cli.Command{
168168
}
169169

170170
if context.NArg() == 0 {
171-
return errors.New("must specify at least one container to delete")
171+
return errdefs.ErrDeleteNoneContainer
172172
}
173173
for _, arg := range context.Args() {
174174
if err := deleteContainer(ctx, client, arg, deleteOpts...); err != nil {
@@ -214,7 +214,7 @@ var setLabelsCommand = cli.Command{
214214
Action: func(context *cli.Context) error {
215215
containerID, labels := commands.ObjectWithLabelArgs(context)
216216
if containerID == "" {
217-
return errors.New("container id must be provided")
217+
return errdefs.ErrEmptyContainerId
218218
}
219219
client, ctx, cancel, err := commands.NewClient(context)
220220
if err != nil {
@@ -250,7 +250,7 @@ var infoCommand = cli.Command{
250250
Action: func(context *cli.Context) error {
251251
id := context.Args().First()
252252
if id == "" {
253-
return errors.New("container id must be provided")
253+
return errdefs.ErrEmptyContainerId
254254
}
255255
client, ctx, cancel, err := commands.NewClient(context)
256256
if err != nil {

containerd.service

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ ExecStart=/usr/local/bin/containerd
99

1010
Delegate=yes
1111
KillMode=process
12+
Restart=always
1213
# Having non-zero Limit*s causes performance problems due to accounting overhead
1314
# in the kernel. We recommend using cgroups to do container-local accounting.
1415
LimitNPROC=infinity

diff/lcow/lcow.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,11 @@ func (s windowsLcowDiff) Apply(ctx context.Context, desc ocispec.Descriptor, mou
152152

153153
err = tar2ext4.Convert(rc, outFile, tar2ext4.ConvertWhiteout, tar2ext4.AppendVhdFooter, tar2ext4.MaximumDiskSize(maxLcowVhdSizeGB))
154154
if err != nil {
155-
return emptyDesc, errors.Wrapf(err, "failed to convert tar to ext4 vhd")
155+
return emptyDesc, errors.Wrapf(err, "failed to convert tar2ext4 vhd")
156+
}
157+
err = outFile.Sync()
158+
if err != nil {
159+
return emptyDesc, errors.Wrapf(err, "failed to sync tar2ext4 vhd to disk")
156160
}
157161
outFile.Close()
158162

errdefs/errors.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,25 @@ import "github.com/pkg/errors"
3636
// For the most part, we just try to provide local grpc errors. Most conditions
3737
// map very well to those defined by grpc.
3838
var (
39-
ErrUnknown = errors.New("unknown") // used internally to represent a missed mapping.
40-
ErrInvalidArgument = errors.New("invalid argument")
41-
ErrNotFound = errors.New("not found")
42-
ErrAlreadyExists = errors.New("already exists")
43-
ErrFailedPrecondition = errors.New("failed precondition")
44-
ErrUnavailable = errors.New("unavailable")
45-
ErrNotImplemented = errors.New("not implemented") // represents not supported and unimplemented
39+
ErrUnknown = errors.New("unknown") // used internally to represent a missed mapping.
40+
ErrInvalidArgument = errors.New("invalid argument")
41+
ErrNotFound = errors.New("not found")
42+
ErrAlreadyExists = errors.New("already exists")
43+
ErrFailedPrecondition = errors.New("failed precondition")
44+
ErrUnavailable = errors.New("unavailable")
45+
ErrNotImplemented = errors.New("not implemented") // represents not supported and unimplemented
46+
ErrNoGRPCAndService = errors.New("no grpc connection and services is available")
47+
ErrNoGRPC = errors.New("no grpc connection available")
48+
ErrReconnectFailed = errors.New("unable to reconnect to containerd, no connector available")
49+
ErrUnpackNotSupported = errors.New("unpack on fetch not supported, try pull")
50+
ErrEmptyGRCPAddress = errors.New("grpc address cannot be empty")
51+
ErrEmptyTopic = errors.New("topic required to publish event")
52+
ErrUnknownLevel = errors.New("unknown level")
53+
ErrRegisterAndUnregisterService = errors.New("--register-service and --unregister-service cannot be used together")
54+
ErrArgConfigFile = errors.New("with spec config file, only container id should be provided")
55+
ErrUnprovidedImageRef = errors.New("image ref must be provided")
56+
ErrEmptyContainerId = errors.New("container id must be provided")
57+
ErrDeleteNoneContainer = errors.New("must specify at least one container to delete")
4658
)
4759

4860
// IsInvalidArgument returns true if the error is due to an invalid argument

runtime/v1/shim/service.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ func (s *Service) Create(ctx context.Context, r *shimapi.CreateTaskRequest) (_ *
127127
rootfs := ""
128128
if len(mounts) > 0 {
129129
rootfs = filepath.Join(r.Bundle, "rootfs")
130-
if err := os.Mkdir(rootfs, 0711); err != nil {
130+
if err := os.Mkdir(rootfs, 0711); err != nil && !os.IsExist(err) {
131131
return nil, err
132132
}
133133
}

runtime/v2/runc/container.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func NewContainer(ctx context.Context, platform rproc.Platform, r *task.CreateTa
6868
rootfs := ""
6969
if len(mounts) > 0 {
7070
rootfs = filepath.Join(r.Bundle, "rootfs")
71-
if err := os.Mkdir(rootfs, 0711); err != nil {
71+
if err := os.Mkdir(rootfs, 0711); err != nil && !os.IsExist(err) {
7272
return nil, err
7373
}
7474
}

0 commit comments

Comments
 (0)