Skip to content

Commit

Permalink
docker graceful exits when image pull fails tinkerbell#382
Browse files Browse the repository at this point in the history
added check for Decode statement
  • Loading branch information
Cbkhare committed Mar 17, 2021
1 parent 1b178da commit f179393
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions cmd/tink-worker/internal/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/base64"
"encoding/json"
"io"
"os"

"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
Expand All @@ -22,6 +21,17 @@ type RegistryConnDetails struct {
logger log.Logger
}

// ImagePullStatus is the status of the downloaded Image chunk
type ImagePullStatus struct {
Status string `json:"status"`
Error string `json:"error"`
Progress string `json:"progress"`
ProgressDetail struct {
Current int `json:"current"`
Total int `json:"total"`
} `json:"progressDetail"`
}

// NewRegistryConnDetails creates a new RegistryConnDetails
func NewRegistryConnDetails(registry, user, pwd string, logger log.Logger) *RegistryConnDetails {
return &RegistryConnDetails{
Expand Down Expand Up @@ -64,8 +74,18 @@ func (r *RegistryConnDetails) pullImage(ctx context.Context, cli *client.Client,
return errors.Wrap(err, "DOCKER PULL")
}
defer out.Close()
if _, err := io.Copy(os.Stdout, out); err != nil {
return err
fd := json.NewDecoder(out)
var status *ImagePullStatus
for {
if err := fd.Decode(&status); err != nil {
if err == io.EOF {
break
}
return errors.Wrap(err, "DOCKER PULL")
}
if status.Error != "" {
return errors.Wrap(errors.New(status.Error), "DOCKER PULL")
}
}
return nil
}

0 comments on commit f179393

Please sign in to comment.