-
Notifications
You must be signed in to change notification settings - Fork 485
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1994 from crazy-max/load-progress
build: sublogger to show docker load progress output
- Loading branch information
Showing
3 changed files
with
65 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,76 @@ | ||
package dockerutil | ||
|
||
import ( | ||
"errors" | ||
"encoding/json" | ||
"io" | ||
"time" | ||
|
||
"github.com/docker/buildx/util/progress" | ||
"github.com/docker/cli/cli/streams" | ||
"github.com/docker/docker/pkg/jsonmessage" | ||
"github.com/moby/buildkit/client" | ||
"github.com/moby/buildkit/identity" | ||
"github.com/opencontainers/go-digest" | ||
) | ||
|
||
func fromReader(w progress.Writer, name string, rc io.ReadCloser) error { | ||
dgst := digest.FromBytes([]byte(identity.NewID())) | ||
tm := time.Now() | ||
const minTimeDelta = 2 * time.Second | ||
|
||
vtx := client.Vertex{ | ||
Digest: dgst, | ||
Name: name, | ||
Started: &tm, | ||
} | ||
func fromReader(l progress.SubLogger, rc io.ReadCloser) error { | ||
started := map[string]client.VertexStatus{} | ||
|
||
w.Write(&client.SolveStatus{ | ||
Vertexes: []*client.Vertex{&vtx}, | ||
}) | ||
defer func() { | ||
for _, st := range started { | ||
st := st | ||
if st.Completed == nil { | ||
now := time.Now() | ||
st.Completed = &now | ||
l.SetStatus(&st) | ||
} | ||
} | ||
}() | ||
|
||
err := jsonmessage.DisplayJSONMessagesToStream(rc, streams.NewOut(io.Discard), nil) | ||
if err != nil { | ||
if jerr, ok := err.(*jsonmessage.JSONError); ok { | ||
err = errors.New(jerr.Message) | ||
dec := json.NewDecoder(rc) | ||
var parsedErr error | ||
var jm jsonmessage.JSONMessage | ||
for { | ||
if err := dec.Decode(&jm); err != nil { | ||
if parsedErr != nil { | ||
return parsedErr | ||
} | ||
if err == io.EOF { | ||
break | ||
} | ||
return err | ||
} | ||
if jm.Error != nil { | ||
parsedErr = jm.Error | ||
} | ||
if jm.ID == "" || jm.Progress == nil { | ||
continue | ||
} | ||
} | ||
|
||
tm2 := time.Now() | ||
vtx2 := vtx | ||
vtx2.Completed = &tm2 | ||
if err != nil { | ||
vtx2.Error = err.Error() | ||
id := "loading layer " + jm.ID | ||
st, ok := started[id] | ||
if !ok { | ||
now := time.Now() | ||
st = client.VertexStatus{ | ||
ID: id, | ||
Started: &now, | ||
} | ||
} | ||
timeDelta := time.Now().Sub(st.Timestamp) | ||
if timeDelta < minTimeDelta { | ||
continue | ||
} | ||
st.Timestamp = time.Now() | ||
if jm.Status == "Loading layer" { | ||
st.Current = jm.Progress.Current | ||
st.Total = jm.Progress.Total | ||
} | ||
if jm.Error != nil { | ||
now := time.Now() | ||
st.Completed = &now | ||
} | ||
started[id] = st | ||
l.SetStatus(&st) | ||
} | ||
w.Write(&client.SolveStatus{ | ||
Vertexes: []*client.Vertex{&vtx2}, | ||
}) | ||
return err | ||
|
||
return nil | ||
} |