Skip to content

Commit 49a1f3d

Browse files
committed
bugfix: convert container err into pouchd manager error
Signed-off-by: Allen Sun <[email protected]>
1 parent 952edf1 commit 49a1f3d

File tree

6 files changed

+274
-29
lines changed

6 files changed

+274
-29
lines changed

apis/swagger.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,10 @@ paths:
515515
description: "no error"
516516
404:
517517
$ref: "#/responses/404ErrorResponse"
518+
409:
519+
description: "container is paused"
520+
schema:
521+
$ref: "#/definitions/Error"
518522
500:
519523
$ref: "#/responses/500ErrorResponse"
520524
tags: ["Container"]

ctrd/container.go

Lines changed: 99 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ type containerPack struct {
4141

4242
// ContainerStats returns stats of the container.
4343
func (c *Client) ContainerStats(ctx context.Context, id string) (*containerdtypes.Metric, error) {
44+
metric, err := c.containerStats(ctx, id)
45+
if err != nil {
46+
return metric, convertCtrdErr(err)
47+
}
48+
return metric, nil
49+
}
50+
51+
// containerStats returns stats of the container.
52+
func (c *Client) containerStats(ctx context.Context, id string) (*containerdtypes.Metric, error) {
4453
if !c.lock.Trylock(id) {
4554
return nil, errtypes.ErrLockfailed
4655
}
@@ -56,6 +65,14 @@ func (c *Client) ContainerStats(ctx context.Context, id string) (*containerdtype
5665

5766
// ExecContainer executes a process in container.
5867
func (c *Client) ExecContainer(ctx context.Context, process *Process) error {
68+
if err := c.execContainer(ctx, process); err != nil {
69+
return convertCtrdErr(err)
70+
}
71+
return nil
72+
}
73+
74+
// execContainer executes a process in container.
75+
func (c *Client) execContainer(ctx context.Context, process *Process) error {
5976
pack, err := c.watch.get(process.ContainerID)
6077
if err != nil {
6178
return err
@@ -123,6 +140,15 @@ func (c *Client) ExecContainer(ctx context.Context, process *Process) error {
123140

124141
// ContainerPID returns the container's init process id.
125142
func (c *Client) ContainerPID(ctx context.Context, id string) (int, error) {
143+
pid, err := c.containerPID(ctx, id)
144+
if err != nil {
145+
return pid, convertCtrdErr(err)
146+
}
147+
return pid, nil
148+
}
149+
150+
// containerPID returns the container's init process id.
151+
func (c *Client) containerPID(ctx context.Context, id string) (int, error) {
126152
pack, err := c.watch.get(id)
127153
if err != nil {
128154
return -1, err
@@ -132,6 +158,15 @@ func (c *Client) ContainerPID(ctx context.Context, id string) (int, error) {
132158

133159
// ContainerPIDs returns the all processes's ids inside the container.
134160
func (c *Client) ContainerPIDs(ctx context.Context, id string) ([]int, error) {
161+
pids, err := c.containerPIDs(ctx, id)
162+
if err != nil {
163+
return pids, convertCtrdErr(err)
164+
}
165+
return pids, nil
166+
}
167+
168+
// containerPIDs returns the all processes's ids inside the container.
169+
func (c *Client) containerPIDs(ctx context.Context, id string) ([]int, error) {
135170
if !c.lock.Trylock(id) {
136171
return nil, errtypes.ErrLockfailed
137172
}
@@ -178,6 +213,14 @@ func (c *Client) ProbeContainer(ctx context.Context, id string, timeout time.Dur
178213

179214
// RecoverContainer reload the container from metadata and watch it, if program be restarted.
180215
func (c *Client) RecoverContainer(ctx context.Context, id string, io *containerio.IO) error {
216+
if err := c.recoverContainer(ctx, id, io); err != nil {
217+
return convertCtrdErr(err)
218+
}
219+
return nil
220+
}
221+
222+
// recoverContainer reload the container from metadata and watch it, if program be restarted.
223+
func (c *Client) recoverContainer(ctx context.Context, id string, io *containerio.IO) error {
181224
wrapperCli, err := c.Get(ctx)
182225
if err != nil {
183226
return fmt.Errorf("failed to get a containerd grpc client: %v", err)
@@ -226,6 +269,15 @@ func (c *Client) RecoverContainer(ctx context.Context, id string, io *containeri
226269

227270
// DestroyContainer kill container and delete it.
228271
func (c *Client) DestroyContainer(ctx context.Context, id string, timeout int64) (*Message, error) {
272+
msg, err := c.destroyContainer(ctx, id, timeout)
273+
if err != nil {
274+
return msg, convertCtrdErr(err)
275+
}
276+
return msg, nil
277+
}
278+
279+
// DestroyContainer kill container and delete it.
280+
func (c *Client) destroyContainer(ctx context.Context, id string, timeout int64) (*Message, error) {
229281
// TODO(ziren): if we just want to stop a container,
230282
// we may need lease to lock the snapshot of container,
231283
// in case, it be deleted by gc.
@@ -294,8 +346,16 @@ clean:
294346
return msg, c.watch.remove(ctx, id)
295347
}
296348

297-
// PauseContainer pause container.
349+
// PauseContainer pauses container.
298350
func (c *Client) PauseContainer(ctx context.Context, id string) error {
351+
if err := c.pauseContainer(ctx, id); err != nil {
352+
return convertCtrdErr(err)
353+
}
354+
return nil
355+
}
356+
357+
// pauseContainer pause container.
358+
func (c *Client) pauseContainer(ctx context.Context, id string) error {
299359
if !c.lock.Trylock(id) {
300360
return errtypes.ErrLockfailed
301361
}
@@ -317,8 +377,16 @@ func (c *Client) PauseContainer(ctx context.Context, id string) error {
317377
return nil
318378
}
319379

320-
// UnpauseContainer unpauses a container.
380+
// UnpauseContainer unpauses container.
321381
func (c *Client) UnpauseContainer(ctx context.Context, id string) error {
382+
if err := c.unpauseContainer(ctx, id); err != nil {
383+
return convertCtrdErr(err)
384+
}
385+
return nil
386+
}
387+
388+
// unpauseContainer unpauses a container.
389+
func (c *Client) unpauseContainer(ctx context.Context, id string) error {
322390
if !c.lock.Trylock(id) {
323391
return errtypes.ErrLockfailed
324392
}
@@ -352,7 +420,10 @@ func (c *Client) CreateContainer(ctx context.Context, container *Container) erro
352420
}
353421
defer c.lock.Unlock(id)
354422

355-
return c.createContainer(ctx, ref, id, container)
423+
if err := c.createContainer(ctx, ref, id, container); err != nil {
424+
return convertCtrdErr(err)
425+
}
426+
return nil
356427
}
357428

358429
func (c *Client) createContainer(ctx context.Context, ref, id string, container *Container) (err0 error) {
@@ -472,28 +543,16 @@ func (c *Client) createTask(ctx context.Context, id string, container containerd
472543
return pack, nil
473544
}
474545

475-
func (c *Client) listContainerStore(ctx context.Context) ([]string, error) {
476-
wrapperCli, err := c.Get(ctx)
477-
if err != nil {
478-
return nil, fmt.Errorf("failed to get a containerd grpc client: %v", err)
479-
}
480-
481-
containers, err := wrapperCli.client.ContainerService().List(ctx)
482-
if err != nil {
483-
return nil, err
484-
}
485-
486-
var cs []string
487-
488-
for _, c := range containers {
489-
cs = append(cs, c.ID)
546+
// UpdateResources updates the configurations of a container.
547+
func (c *Client) UpdateResources(ctx context.Context, id string, resources types.Resources) error {
548+
if err := c.updateResources(ctx, id, resources); err != nil {
549+
return convertCtrdErr(err)
490550
}
491-
492-
return cs, nil
551+
return nil
493552
}
494553

495-
// UpdateResources updates the configurations of a container.
496-
func (c *Client) UpdateResources(ctx context.Context, id string, resources types.Resources) error {
554+
// updateResources updates the configurations of a container.
555+
func (c *Client) updateResources(ctx context.Context, id string, resources types.Resources) error {
497556
if !c.lock.Trylock(id) {
498557
return errtypes.ErrLockfailed
499558
}
@@ -515,6 +574,15 @@ func (c *Client) UpdateResources(ctx context.Context, id string, resources types
515574
// ResizeContainer changes the size of the TTY of the init process running
516575
// in the container to the given height and width.
517576
func (c *Client) ResizeContainer(ctx context.Context, id string, opts types.ResizeOptions) error {
577+
if err := c.resizeContainer(ctx, id, opts); err != nil {
578+
return convertCtrdErr(err)
579+
}
580+
return nil
581+
}
582+
583+
// resizeContainer changes the size of the TTY of the init process running
584+
// in the container to the given height and width.
585+
func (c *Client) resizeContainer(ctx context.Context, id string, opts types.ResizeOptions) error {
518586
if !c.lock.Trylock(id) {
519587
return errtypes.ErrLockfailed
520588
}
@@ -530,6 +598,15 @@ func (c *Client) ResizeContainer(ctx context.Context, id string, opts types.Resi
530598

531599
// WaitContainer waits until container's status is stopped.
532600
func (c *Client) WaitContainer(ctx context.Context, id string) (types.ContainerWaitOKBody, error) {
601+
waitBody, err := c.waitContainer(ctx, id)
602+
if err != nil {
603+
return waitBody, convertCtrdErr(err)
604+
}
605+
return waitBody, nil
606+
}
607+
608+
// waitContainer waits until container's status is stopped.
609+
func (c *Client) waitContainer(ctx context.Context, id string) (types.ContainerWaitOKBody, error) {
533610
wrapperCli, err := c.Get(ctx)
534611
if err != nil {
535612
return types.ContainerWaitOKBody{}, fmt.Errorf("failed to get a containerd grpc client: %v", err)

ctrd/image.go

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ import (
2525

2626
// CreateImageReference creates the image in the meta data in the containerd.
2727
func (c *Client) CreateImageReference(ctx context.Context, img ctrdmetaimages.Image) (ctrdmetaimages.Image, error) {
28+
image, err := c.createImageReference(ctx, img)
29+
if err != nil {
30+
return image, convertCtrdErr(err)
31+
}
32+
return image, nil
33+
}
34+
35+
// createImageReference creates the image in the meta data in the containerd.
36+
func (c *Client) createImageReference(ctx context.Context, img ctrdmetaimages.Image) (ctrdmetaimages.Image, error) {
2837
wrapperCli, err := c.Get(ctx)
2938
if err != nil {
3039
return ctrdmetaimages.Image{}, fmt.Errorf("failed to get a containerd grpc client: %v", err)
@@ -35,6 +44,15 @@ func (c *Client) CreateImageReference(ctx context.Context, img ctrdmetaimages.Im
3544

3645
// GetImage returns the containerd's Image.
3746
func (c *Client) GetImage(ctx context.Context, ref string) (containerd.Image, error) {
47+
img, err := c.getImage(ctx, ref)
48+
if err != nil {
49+
return img, convertCtrdErr(err)
50+
}
51+
return img, nil
52+
}
53+
54+
// getImage returns the containerd's Image.
55+
func (c *Client) getImage(ctx context.Context, ref string) (containerd.Image, error) {
3856
wrapperCli, err := c.Get(ctx)
3957
if err != nil {
4058
return nil, fmt.Errorf("failed to get a containerd grpc client: %v", err)
@@ -45,6 +63,15 @@ func (c *Client) GetImage(ctx context.Context, ref string) (containerd.Image, er
4563

4664
// ListImages lists all images.
4765
func (c *Client) ListImages(ctx context.Context, filter ...string) ([]containerd.Image, error) {
66+
imgs, err := c.listImages(ctx, filter...)
67+
if err != nil {
68+
return imgs, convertCtrdErr(err)
69+
}
70+
return imgs, nil
71+
}
72+
73+
// listImages lists all images.
74+
func (c *Client) listImages(ctx context.Context, filter ...string) ([]containerd.Image, error) {
4875
wrapperCli, err := c.Get(ctx)
4976
if err != nil {
5077
return nil, fmt.Errorf("failed to get a containerd grpc client: %v", err)
@@ -55,6 +82,14 @@ func (c *Client) ListImages(ctx context.Context, filter ...string) ([]containerd
5582

5683
// RemoveImage deletes an image.
5784
func (c *Client) RemoveImage(ctx context.Context, ref string) error {
85+
if err := c.removeImage(ctx, ref); err != nil {
86+
return convertCtrdErr(err)
87+
}
88+
return nil
89+
}
90+
91+
// removeImage deletes an image.
92+
func (c *Client) removeImage(ctx context.Context, ref string) error {
5893
wrapperCli, err := c.Get(ctx)
5994
if err != nil {
6095
return fmt.Errorf("failed to get a containerd grpc client: %v", err)
@@ -68,6 +103,15 @@ func (c *Client) RemoveImage(ctx context.Context, ref string) error {
68103

69104
// SaveImage saves image to tarstream
70105
func (c *Client) SaveImage(ctx context.Context, exporter ctrdmetaimages.Exporter, ref string) (io.ReadCloser, error) {
106+
r, err := c.saveImage(ctx, exporter, ref)
107+
if err != nil {
108+
return r, convertCtrdErr(err)
109+
}
110+
return r, nil
111+
}
112+
113+
// saveImage saves image to tarstream
114+
func (c *Client) saveImage(ctx context.Context, exporter ctrdmetaimages.Exporter, ref string) (io.ReadCloser, error) {
71115
wrapperCli, err := c.Get(ctx)
72116
if err != nil {
73117
return nil, fmt.Errorf("failed to get a containerd grpc client: %v", err)
@@ -94,18 +138,24 @@ func (c *Client) SaveImage(ctx context.Context, exporter ctrdmetaimages.Exporter
94138
}
95139
}
96140

97-
exportedStream, err := wrapperCli.client.Export(ctx, exporter, desc)
98-
if err != nil {
99-
return nil, err
100-
}
101-
102-
return exportedStream, nil
141+
return wrapperCli.client.Export(ctx, exporter, desc)
103142
}
104143

105144
// ImportImage creates a set of images by tarstream.
106145
//
107146
// NOTE: One tar may have several manifests.
108147
func (c *Client) ImportImage(ctx context.Context, importer ctrdmetaimages.Importer, reader io.Reader) ([]containerd.Image, error) {
148+
imgs, err := c.importImage(ctx, importer, reader)
149+
if err != nil {
150+
return imgs, convertCtrdErr(err)
151+
}
152+
return imgs, nil
153+
}
154+
155+
// importImage creates a set of images by tarstream.
156+
//
157+
// NOTE: One tar may have several manifests.
158+
func (c *Client) importImage(ctx context.Context, importer ctrdmetaimages.Importer, reader io.Reader) ([]containerd.Image, error) {
109159
wrapperCli, err := c.Get(ctx)
110160
if err != nil {
111161
return nil, fmt.Errorf("failed to get a containerd grpc client: %v", err)

ctrd/utils.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,19 @@ import (
99
"time"
1010

1111
"github.com/alibaba/pouch/apis/types"
12+
"github.com/alibaba/pouch/pkg/errtypes"
1213
"github.com/alibaba/pouch/pkg/utils"
1314

1415
"github.com/containerd/containerd/containers"
16+
"github.com/containerd/containerd/errdefs"
1517
"github.com/containerd/containerd/namespaces"
1618
"github.com/containerd/containerd/oci"
1719
"github.com/containerd/containerd/remotes"
1820
"github.com/containerd/containerd/remotes/docker"
1921
"github.com/opencontainers/go-digest"
2022
"github.com/opencontainers/image-spec/specs-go/v1"
2123
specs "github.com/opencontainers/runtime-spec/specs-go"
24+
"github.com/pkg/errors"
2225
)
2326

2427
// NewDefaultSpec new a template spec with default.
@@ -166,3 +169,31 @@ func toLinuxResources(resources types.Resources) (*specs.LinuxResources, error)
166169

167170
return r, nil
168171
}
172+
173+
// convertCtrdErr converts containerd client error into a pouchd manager error.
174+
// containerd client error converts GRPC code from containerd API to containerd client error.
175+
// pouchd manager error is used in the whole managers and API layers to construct status code for API.
176+
// there should be a way convert the previous to the latter one.
177+
func convertCtrdErr(err error) error {
178+
if err == nil {
179+
return nil
180+
}
181+
182+
if errdefs.IsNotFound(err) {
183+
return errors.Wrap(errtypes.ErrNotfound, err.Error())
184+
}
185+
186+
if errdefs.IsAlreadyExists(err) {
187+
return errors.Wrap(errtypes.ErrAlreadyExisted, err.Error())
188+
}
189+
190+
if errdefs.IsInvalidArgument(err) {
191+
return errors.Wrap(errtypes.ErrInvalidParam, err.Error())
192+
}
193+
194+
if errdefs.IsNotImplemented(err) {
195+
return errors.Wrap(errtypes.ErrNotImplemented, err.Error())
196+
}
197+
198+
return err
199+
}

0 commit comments

Comments
 (0)