Skip to content

Commit

Permalink
bugfix: distinguish NotFound error with others about image
Browse files Browse the repository at this point in the history
Signed-off-by: Starnop <starnop@163.com>
  • Loading branch information
starnop committed Aug 23, 2018
1 parent d6a4571 commit 4b63b11
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 23 deletions.
13 changes: 7 additions & 6 deletions cri/v1alpha1/cri.go
Original file line number Diff line number Diff line change
Expand Up @@ -990,7 +990,8 @@ func (c *CriManager) ListImages(ctx context.Context, r *runtime.ListImagesReques
return &runtime.ListImagesResponse{Images: images}, nil
}

// ImageStatus returns the status of the image, returns nil if the image isn't present.
// ImageStatus returns the status of the image. If the image is not present,
// returns a response with ImageStatusResponse.Image set to nil.
func (c *CriManager) ImageStatus(ctx context.Context, r *runtime.ImageStatusRequest) (*runtime.ImageStatusResponse, error) {
imageRef := r.GetImage().GetImage()
ref, err := reference.Parse(imageRef)
Expand All @@ -1000,9 +1001,10 @@ func (c *CriManager) ImageStatus(ctx context.Context, r *runtime.ImageStatusRequ

imageInfo, err := c.ImageMgr.GetImage(ctx, ref.String())
if err != nil {
// TODO: separate ErrImageNotFound with others.
// Now we just return empty if the error occurred.
return &runtime.ImageStatusResponse{}, nil
if errtypes.IsNotfound(err) {
return &runtime.ImageStatusResponse{}, nil
}
return nil, err
}

image, err := imageToCriImage(imageInfo)
Expand Down Expand Up @@ -1046,8 +1048,7 @@ func (c *CriManager) RemoveImage(ctx context.Context, r *runtime.RemoveImageRequ

if err := c.ImageMgr.RemoveImage(ctx, imageRef, false); err != nil {
if errtypes.IsNotfound(err) {
// TODO: separate ErrImageNotFound with others.
// Now we just return empty if the error occurred.
// Now we just return empty if the ErrorNotFound occurred.
return &runtime.RemoveImageResponse{}, nil
}
return nil, err
Expand Down
13 changes: 8 additions & 5 deletions cri/v1alpha1/cri_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
apitypes "github.com/alibaba/pouch/apis/types"
anno "github.com/alibaba/pouch/cri/annotations"
"github.com/alibaba/pouch/daemon/mgr"
"github.com/alibaba/pouch/pkg/errtypes"
"github.com/alibaba/pouch/pkg/utils"

"github.com/containerd/cgroups"
Expand Down Expand Up @@ -822,17 +823,19 @@ func imageToCriImage(image *apitypes.ImageInfo) (*runtime.Image, error) {
// ensureSandboxImageExists pulls the image when it's not present.
func (c *CriManager) ensureSandboxImageExists(ctx context.Context, imageRef string) error {
_, _, _, err := c.ImageMgr.CheckReference(ctx, imageRef)
// TODO: maybe we should distinguish NotFound error with others.
if err == nil {
return nil
}

err = c.ImageMgr.PullImage(ctx, imageRef, nil, bytes.NewBuffer([]byte{}))
if err != nil {
return fmt.Errorf("pull sandbox image %q failed: %v", imageRef, err)
if errtypes.IsNotfound(err) {
err = c.ImageMgr.PullImage(ctx, imageRef, nil, bytes.NewBuffer([]byte{}))
if err != nil {
return fmt.Errorf("failed to pull sandbox image %q: %v", imageRef, err)
}
return nil
}

return nil
return fmt.Errorf("failed to check sandbox image %q: %v", imageRef, err)
}

// getUserFromImageUser gets uid or user name of the image user.
Expand Down
13 changes: 7 additions & 6 deletions cri/v1alpha2/cri.go
Original file line number Diff line number Diff line change
Expand Up @@ -1013,7 +1013,8 @@ func (c *CriManager) ListImages(ctx context.Context, r *runtime.ListImagesReques
return &runtime.ListImagesResponse{Images: images}, nil
}

// ImageStatus returns the status of the image, returns nil if the image isn't present.
// ImageStatus returns the status of the image. If the image is not present,
// returns a response with ImageStatusResponse.Image set to nil.
func (c *CriManager) ImageStatus(ctx context.Context, r *runtime.ImageStatusRequest) (*runtime.ImageStatusResponse, error) {
imageRef := r.GetImage().GetImage()
ref, err := reference.Parse(imageRef)
Expand All @@ -1023,9 +1024,10 @@ func (c *CriManager) ImageStatus(ctx context.Context, r *runtime.ImageStatusRequ

imageInfo, err := c.ImageMgr.GetImage(ctx, ref.String())
if err != nil {
// TODO: separate ErrImageNotFound with others.
// Now we just return empty if the error occurred.
return &runtime.ImageStatusResponse{}, nil
if errtypes.IsNotfound(err) {
return &runtime.ImageStatusResponse{}, nil
}
return nil, err
}

image, err := imageToCriImage(imageInfo)
Expand Down Expand Up @@ -1069,8 +1071,7 @@ func (c *CriManager) RemoveImage(ctx context.Context, r *runtime.RemoveImageRequ

if err := c.ImageMgr.RemoveImage(ctx, imageRef, false); err != nil {
if errtypes.IsNotfound(err) {
// TODO: separate ErrImageNotFound with others.
// Now we just return empty if the error occurred.
// Now we just return empty if the ErrorNotFound occurred.
return &runtime.RemoveImageResponse{}, nil
}
return nil, err
Expand Down
14 changes: 8 additions & 6 deletions cri/v1alpha2/cri_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
anno "github.com/alibaba/pouch/cri/annotations"
runtime "github.com/alibaba/pouch/cri/apis/v1alpha2"
"github.com/alibaba/pouch/daemon/mgr"
"github.com/alibaba/pouch/pkg/errtypes"
"github.com/alibaba/pouch/pkg/utils"

"github.com/containerd/cgroups"
Expand Down Expand Up @@ -822,17 +823,18 @@ func imageToCriImage(image *apitypes.ImageInfo) (*runtime.Image, error) {
// ensureSandboxImageExists pulls the image when it's not present.
func (c *CriManager) ensureSandboxImageExists(ctx context.Context, imageRef string) error {
_, _, _, err := c.ImageMgr.CheckReference(ctx, imageRef)
// TODO: maybe we should distinguish NotFound error with others.
if err == nil {
return nil
}

err = c.ImageMgr.PullImage(ctx, imageRef, nil, bytes.NewBuffer([]byte{}))
if err != nil {
return fmt.Errorf("pull sandbox image %q failed: %v", imageRef, err)
if errtypes.IsNotfound(err) {
err = c.ImageMgr.PullImage(ctx, imageRef, nil, bytes.NewBuffer([]byte{}))
if err != nil {
return fmt.Errorf("failed to pull sandbox image %q: %v", imageRef, err)
}
return nil
}

return nil
return fmt.Errorf("failed to check sandbox image %q: %v", imageRef, err)
}

// getUserFromImageUser gets uid or user name of the image user.
Expand Down

0 comments on commit 4b63b11

Please sign in to comment.