Skip to content

Commit

Permalink
bugfix: pull image with digest
Browse files Browse the repository at this point in the history
Signed-off-by: YaoZengzeng <[email protected]>
  • Loading branch information
YaoZengzeng committed Mar 27, 2018
1 parent 9ae25a4 commit 00bc1a6
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 16 deletions.
2 changes: 1 addition & 1 deletion apis/server/image_bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (s *Server) pullImage(ctx context.Context, rw http.ResponseWriter, req *htt
}
}
// Error information has be sent to client, so no need call resp.Write
if err := s.ImageMgr.PullImage(ctx, image, tag, &authConfig, rw); err != nil {
if err := s.ImageMgr.PullImage(ctx, image + ":" + tag, &authConfig, rw); err != nil {
logrus.Errorf("failed to pull image %s:%s: %v", image, tag, err)
return nil
}
Expand Down
14 changes: 10 additions & 4 deletions daemon/mgr/cri.go
Original file line number Diff line number Diff line change
Expand Up @@ -809,18 +809,24 @@ func (c *CriManager) PullImage(ctx context.Context, r *runtime.PullImageRequest)
// TODO: authentication.
imageRef := r.GetImage().GetImage()

namedRef, err := reference.ParseNamedReference(imageRef)
refNamed, err := reference.ParseNamedReference(imageRef)
if err != nil {
return nil, err
}
taggedRef := reference.WithDefaultTagIfMissing(namedRef).(reference.Tagged)

err = c.ImageMgr.PullImage(ctx, taggedRef.Name(), taggedRef.Tag(), nil, bytes.NewBuffer([]byte{}))
_, ok := refNamed.(reference.Digested)
if !ok {
// If the imageRef is not a digest.
refTagged := reference.WithDefaultTagIfMissing(refNamed).(reference.Tagged)
imageRef = refTagged.String()
}

err = c.ImageMgr.PullImage(ctx, imageRef, nil, bytes.NewBuffer([]byte{}))
if err != nil {
return nil, err
}

imageInfo, err := c.ImageMgr.GetImage(ctx, taggedRef.String())
imageInfo, err := c.ImageMgr.GetImage(ctx, imageRef)
if err != nil {
return nil, err
}
Expand Down
20 changes: 13 additions & 7 deletions daemon/mgr/cri_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -686,22 +686,28 @@ func imageToCriImage(image *apitypes.ImageInfo) (*runtime.Image, error) {
}

// ensureSandboxImageExists pulls the image when it's not present.
func (c *CriManager) ensureSandboxImageExists(ctx context.Context, image string) error {
_, err := c.ImageMgr.GetImage(ctx, image)
func (c *CriManager) ensureSandboxImageExists(ctx context.Context, imageRef string) error {
_, err := c.ImageMgr.GetImage(ctx, imageRef)
// TODO: maybe we should distinguish NotFound error with others.
if err == nil {
return nil
}

namedRef, err := reference.ParseNamedReference(image)
refNamed, err := reference.ParseNamedReference(imageRef)
if err != nil {
return fmt.Errorf("parse image name failed: %v", err)
return nil, err
}

_, ok := refNamed.(reference.Digested)
if !ok {
// If the imageRef is not a digest.
refTagged := reference.WithDefaultTagIfMissing(refNamed).(reference.Tagged)
imageRef = refTagged.String()
}
taggedRef := reference.WithDefaultTagIfMissing(namedRef).(reference.Tagged)

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

return nil
Expand Down
8 changes: 4 additions & 4 deletions daemon/mgr/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
// ImageMgr as an interface defines all operations against images.
type ImageMgr interface {
// PullImage pulls images from specified registry.
PullImage(ctx context.Context, image, tag string, authConfig *types.AuthConfig, out io.Writer) error
PullImage(ctx context.Context, imageRef string, authConfig *types.AuthConfig, out io.Writer) error

// ListImages lists images stored by containerd.
ListImages(ctx context.Context, filters string) ([]types.ImageInfo, error)
Expand Down Expand Up @@ -73,7 +73,7 @@ func NewImageManager(cfg *config.Config, client *ctrd.Client) (*ImageManager, er
}

// PullImage pulls images from specified registry.
func (mgr *ImageManager) PullImage(pctx context.Context, image, tag string, authConfig *types.AuthConfig, out io.Writer) error {
func (mgr *ImageManager) PullImage(pctx context.Context, imageRef string, authConfig *types.AuthConfig, out io.Writer) error {
ctx, cancel := context.WithCancel(pctx)

stream := jsonstream.New(out)
Expand All @@ -86,8 +86,8 @@ func (mgr *ImageManager) PullImage(pctx context.Context, image, tag string, auth
close(wait)
}()

image = mgr.addRegistry(image)
img, err := mgr.client.PullImage(ctx, image+":"+tag, authConfig, stream)
imageRef = mgr.addRegistry(imageRef)
img, err := mgr.client.PullImage(ctx, imageRef, authConfig, stream)

// wait goroutine to exit.
<-wait
Expand Down

0 comments on commit 00bc1a6

Please sign in to comment.