Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix: distinguish NotFound error with others about image #2139

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why return nil error here? could we check the IsNotfound out of this scope?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The CRI API declares this :

ImageStatus returns the status of the image. If the image is not present, returns a response with ImageStatusResponse.Image set to nil.

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