diff --git a/cri/v1alpha1/cri.go b/cri/v1alpha1/cri.go index 064871289..df4bb1280 100644 --- a/cri/v1alpha1/cri.go +++ b/cri/v1alpha1/cri.go @@ -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) @@ -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) @@ -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 diff --git a/cri/v1alpha1/cri_utils.go b/cri/v1alpha1/cri_utils.go index 23a7a0dd7..9d4679301 100644 --- a/cri/v1alpha1/cri_utils.go +++ b/cri/v1alpha1/cri_utils.go @@ -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" @@ -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. diff --git a/cri/v1alpha2/cri.go b/cri/v1alpha2/cri.go index e3462d1b1..a0076c00b 100644 --- a/cri/v1alpha2/cri.go +++ b/cri/v1alpha2/cri.go @@ -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) @@ -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) @@ -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 diff --git a/cri/v1alpha2/cri_utils.go b/cri/v1alpha2/cri_utils.go index a00399ecd..1fa80116c 100644 --- a/cri/v1alpha2/cri_utils.go +++ b/cri/v1alpha2/cri_utils.go @@ -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" @@ -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.