builder: return image ID in API when using buildkit
Signed-off-by: Tibor Vass <tibor@docker.com>
This commit is contained in:
parent
b0e6eedcf4
commit
ca8022ec63
6 changed files with 14 additions and 29 deletions
|
@ -73,7 +73,7 @@ func (b *Backend) Build(ctx context.Context, config backend.BuildConfig) (string
|
|||
return "", err
|
||||
}
|
||||
if config.ProgressWriter.AuxFormatter != nil {
|
||||
if err = config.ProgressWriter.AuxFormatter.Emit(types.BuildResult{ID: imageID}); err != nil {
|
||||
if err = config.ProgressWriter.AuxFormatter.Emit("moby.image.id", types.BuildResult{ID: imageID}); err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
|
|
|
@ -243,6 +243,10 @@ func (br *buildRouter) postBuild(ctx context.Context, w http.ResponseWriter, r *
|
|||
return errdefs.InvalidParameter(errors.New("squash is only supported with experimental mode"))
|
||||
}
|
||||
|
||||
if buildOptions.Version == types.BuilderBuildKit && !br.daemon.HasExperimental() {
|
||||
return errdefs.InvalidParameter(errors.New("buildkit is only supported with experimental mode"))
|
||||
}
|
||||
|
||||
out := io.Writer(output)
|
||||
if buildOptions.SuppressOutput {
|
||||
out = notVerboseBuffer
|
||||
|
@ -255,10 +259,6 @@ func (br *buildRouter) postBuild(ctx context.Context, w http.ResponseWriter, r *
|
|||
return progress.NewProgressReader(in, progressOutput, r.ContentLength, "Downloading context", buildOptions.RemoteContext)
|
||||
}
|
||||
|
||||
if buildOptions.Version == types.BuilderBuildKit && !br.daemon.HasExperimental() {
|
||||
return errdefs.InvalidParameter(errors.New("buildkit is only supported with experimental mode"))
|
||||
}
|
||||
|
||||
wantAux := versions.GreaterThanOrEqualTo(version, "1.30")
|
||||
|
||||
imgID, err := br.backend.Build(ctx, backend.BuildConfig{
|
||||
|
|
|
@ -2,7 +2,6 @@ package buildkit
|
|||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"strings"
|
||||
"sync"
|
||||
|
@ -14,7 +13,7 @@ import (
|
|||
"github.com/docker/docker/api/types/backend"
|
||||
"github.com/docker/docker/builder"
|
||||
"github.com/docker/docker/daemon/images"
|
||||
"github.com/docker/docker/pkg/jsonmessage"
|
||||
"github.com/docker/docker/pkg/streamformatter"
|
||||
controlapi "github.com/moby/buildkit/api/services/control"
|
||||
"github.com/moby/buildkit/control"
|
||||
"github.com/moby/buildkit/identity"
|
||||
|
@ -228,6 +227,8 @@ func (b *Builder) Build(ctx context.Context, opt backend.BuildConfig) (*builder.
|
|||
Session: opt.Options.SessionID,
|
||||
}
|
||||
|
||||
aux := streamformatter.AuxFormatter{opt.ProgressWriter.Output}
|
||||
|
||||
eg, ctx := errgroup.WithContext(ctx)
|
||||
|
||||
eg.Go(func() error {
|
||||
|
@ -240,7 +241,7 @@ func (b *Builder) Build(ctx context.Context, opt backend.BuildConfig) (*builder.
|
|||
return errors.Errorf("missing image id")
|
||||
}
|
||||
out.ImageID = id
|
||||
return nil
|
||||
return aux.Emit("moby.image.id", types.BuildResult{ID: id})
|
||||
})
|
||||
|
||||
ch := make(chan *controlapi.StatusResponse)
|
||||
|
@ -258,25 +259,9 @@ func (b *Builder) Build(ctx context.Context, opt backend.BuildConfig) (*builder.
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
auxJSONBytes, err := json.Marshal(dt)
|
||||
if err != nil {
|
||||
if err := aux.Emit("moby.buildkit.trace", dt); err != nil {
|
||||
return err
|
||||
}
|
||||
auxJSON := new(json.RawMessage)
|
||||
*auxJSON = auxJSONBytes
|
||||
msgJSON, err := json.Marshal(&jsonmessage.JSONMessage{ID: "moby.buildkit.trace", Aux: auxJSON})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
msgJSON = append(msgJSON, []byte("\r\n")...)
|
||||
n, err := opt.ProgressWriter.Output.Write(msgJSON)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if n != len(msgJSON) {
|
||||
return io.ErrShortWrite
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
|
|
@ -257,7 +257,7 @@ func emitImageID(aux *streamformatter.AuxFormatter, state *dispatchState) error
|
|||
if aux == nil || state.imageID == "" {
|
||||
return nil
|
||||
}
|
||||
return aux.Emit(types.BuildResult{ID: state.imageID})
|
||||
return aux.Emit("", types.BuildResult{ID: state.imageID})
|
||||
}
|
||||
|
||||
func processMetaArg(meta instructions.ArgCommand, shlex *shell.Lex, args *BuildArgs) error {
|
||||
|
|
|
@ -139,14 +139,14 @@ type AuxFormatter struct {
|
|||
}
|
||||
|
||||
// Emit emits the given interface as an aux progress message
|
||||
func (sf *AuxFormatter) Emit(aux interface{}) error {
|
||||
func (sf *AuxFormatter) Emit(id string, aux interface{}) error {
|
||||
auxJSONBytes, err := json.Marshal(aux)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
auxJSON := new(json.RawMessage)
|
||||
*auxJSON = auxJSONBytes
|
||||
msgJSON, err := json.Marshal(&jsonmessage.JSONMessage{Aux: auxJSON})
|
||||
msgJSON, err := json.Marshal(&jsonmessage.JSONMessage{ID: id, Aux: auxJSON})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -106,7 +106,7 @@ func TestAuxFormatterEmit(t *testing.T) {
|
|||
sampleAux := &struct {
|
||||
Data string
|
||||
}{"Additional data"}
|
||||
err := aux.Emit(sampleAux)
|
||||
err := aux.Emit("", sampleAux)
|
||||
assert.NilError(t, err)
|
||||
assert.Check(t, is.Equal(`{"aux":{"Data":"Additional data"}}`+streamNewline, b.String()))
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue