Skip to content

Commit

Permalink
fix: deal with pouch pull image Exception
Browse files Browse the repository at this point in the history
Signed-off-by: 万仔仁 <zirenwan@gmail.com>
  • Loading branch information
万仔仁 committed Nov 28, 2017
1 parent 83f89b6 commit 793b673
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
3 changes: 2 additions & 1 deletion apis/server/image_bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ func (s *Server) pullImage(ctx context.Context, resp http.ResponseWriter, req *h
metrics.ImagePullSummary.WithLabelValues(image + ":" + tag).Observe(metrics.SinceInMicroseconds(start))
}(time.Now())

// Error information has be sent to client, so no need call resp.Write
if err := s.ImageMgr.PullImage(ctx, image, tag, resp); err != nil {
logrus.Errorf("failed to pull image %s:%s: %v", image, tag, err)
return err
return nil
}

return nil
Expand Down
4 changes: 4 additions & 0 deletions cli/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ func renderOutput(responseBody io.ReadCloser) {
func display(w io.Writer, statuses []ctrd.ProgressInfo, start time.Time) {
var total int64
for _, status := range statuses {
if status.ErrorMessage != "" {
fmt.Fprintf(os.Stderr, "%s\n", status.ErrorMessage)
return
}
total += status.Offset
switch status.Status {
case "downloading", "uploading":
Expand Down
19 changes: 15 additions & 4 deletions ctrd/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ctrd

import (
"context"
"net/http"
"strconv"
"sync"
"time"
Expand Down Expand Up @@ -83,7 +84,6 @@ func (c *Client) PullImage(ctx context.Context, ref string, stream *jsonstream.J
if err := c.fetchProgress(pctx, ongoing, stream); err != nil {
logrus.Errorf("failed to get pull's progress: %v", err)
}
stream.Close()
close(wait)

logrus.Infof("fetch progress exited, ref: %s.", ref)
Expand All @@ -94,14 +94,21 @@ func (c *Client) PullImage(ctx context.Context, ref string, stream *jsonstream.J

// cancel fetch progress befor handle error.
cancelProgress()
defer stream.Close()

// wait fetch progress to finish.
<-wait

if err != nil {
// Send Error information to client through stream
messages := []ProgressInfo{
{Code: http.StatusInternalServerError, ErrorMessage: err.Error()},
}
stream.WriteObject(messages)

return err
}

// wait fetch progress to finish.
<-wait

logrus.Infof("success to pull image: %s", img.Name())
return nil
}
Expand All @@ -125,6 +132,10 @@ type ProgressInfo struct {
Total int64
StartedAt time.Time
UpdatedAt time.Time

// For Error handling
Code int // http response code
ErrorMessage string // detail error information
}

func (c *Client) fetchProgress(ctx context.Context, ongoing *jobs, stream *jsonstream.JSONStream) error {
Expand Down
6 changes: 2 additions & 4 deletions daemon/mgr/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,12 @@ func (mgr *ImageManager) PullImage(pctx context.Context, image, tag string, out
close(wait)
}()

if err := mgr.client.PullImage(ctx, image+":"+tag, stream); err != nil {
return err
}
err := mgr.client.PullImage(ctx, image+":"+tag, stream)

// wait goroutine to exit.
<-wait

return nil
return err
}

// ListImages lists images stored by containerd.
Expand Down

0 comments on commit 793b673

Please sign in to comment.