Skip to content

Commit

Permalink
build: set record status warnings in response
Browse files Browse the repository at this point in the history
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
  • Loading branch information
crazy-max committed Jun 11, 2024
1 parent d3a5318 commit aafd62e
Show file tree
Hide file tree
Showing 12 changed files with 318 additions and 186 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ jobs:
endpoint: tcp://localhost:1234
- driver: docker-container
metadata-provenance: max
- driver: docker-container
metadata-status: 1
exclude:
- driver: docker
multi-node: mnode-true
Expand Down Expand Up @@ -134,6 +136,9 @@ jobs:
if [ -n "${{ matrix.metadata-provenance }}" ]; then
echo "BUILDX_METADATA_PROVENANCE=${{ matrix.metadata-provenance }}" >> $GITHUB_ENV
fi
if [ -n "${{ matrix.metadata-status }}" ]; then
echo "BUILDX_METADATA_STATUS=${{ matrix.metadata-status }}" >> $GITHUB_ENV
fi
-
name: Install k3s
if: matrix.driver == 'kubernetes'
Expand Down
17 changes: 10 additions & 7 deletions build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ type Options struct {
Target string
Ulimits *opts.UlimitOpt

Session []session.Attachable
Linked bool // Linked marks this target as exclusively linked (not requested by the user).
PrintFunc *PrintFunc
WithProvenanceResponse bool
SourcePolicy *spb.Policy
GroupRef string
Session []session.Attachable
Linked bool // Linked marks this target as exclusively linked (not requested by the user).
PrintFunc *PrintFunc
WithDetailedResponse bool
SourcePolicy *spb.Policy
GroupRef string
}

type PrintFunc struct {
Expand Down Expand Up @@ -473,10 +473,13 @@ func BuildWithResultHandler(ctx context.Context, nodes []builder.Node, opt map[s
rr.ExporterResponse[k] = string(v)
}
rr.ExporterResponse["buildx.build.ref"] = buildRef
if opt.WithProvenanceResponse && node.Driver.HistoryAPISupported(ctx) {
if opt.WithDetailedResponse && node.Driver.HistoryAPISupported(ctx) {
if err := setRecordProvenance(ctx, c, rr, so.Ref, pw); err != nil {
return err
}
if err := setRecordStatus(ctx, c, rr, so.Ref, pw); err != nil {
return err
}
}

node := dp.Node().Driver
Expand Down
59 changes: 59 additions & 0 deletions build/status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package build

import (
"context"
"encoding/base64"
"encoding/json"
"io"

"github.com/docker/buildx/util/confutil"
"github.com/docker/buildx/util/progress"
controlapi "github.com/moby/buildkit/api/services/control"
"github.com/moby/buildkit/client"
"github.com/pkg/errors"
)

func setRecordStatus(ctx context.Context, c *client.Client, sr *client.SolveResponse, ref string, pw progress.Writer) error {
if !confutil.MetadataStatus() {
return nil
}
pw = progress.ResetTime(pw)
return progress.Wrap("resolving status for metadata file", pw.Write, func(l progress.SubLogger) error {
status, err := fetchStatus(ctx, c, ref)
if err != nil {
return err
} else if status == nil || len(status.Warnings) == 0 {
return nil
}

// we just want warnings for now
status.Vertexes = nil
status.Statuses = nil
status.Logs = nil

dt, err := json.Marshal(status)
if err != nil {
return errors.Wrapf(err, "failed to marshal status")
}
sr.ExporterResponse["buildx.build.status"] = base64.StdEncoding.EncodeToString(dt)

return nil
})
}

func fetchStatus(ctx context.Context, c *client.Client, ref string) (*client.SolveStatus, error) {
cl, err := c.ControlClient().Status(ctx, &controlapi.StatusRequest{
Ref: ref,
})
if err != nil {
return nil, err
}
resp, err := cl.Recv()
if err != nil {
if err == io.EOF {
return nil, nil
}
return nil, errors.Wrap(err, "failed to receive status")
}
return client.NewSolveStatus(resp), nil
}
2 changes: 1 addition & 1 deletion commands/bake.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func runBake(ctx context.Context, dockerCli command.Cli, targets []string, in ba
for k, b := range bo {
b.Ref = identity.NewID()
b.GroupRef = groupRef
b.WithProvenanceResponse = len(in.metadataFile) > 0
b.WithDetailedResponse = len(in.metadataFile) > 0
refs = append(refs, b.Ref)
bo[k] = b
}
Expand Down
2 changes: 1 addition & 1 deletion commands/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ func (o *buildOptions) toControllerOptions() (*controllerapi.BuildOptions, error
return nil, err
}

opts.WithProvenanceResponse = opts.PrintFunc == nil && len(o.metadataFile) > 0
opts.WithDetailedResponse = opts.PrintFunc == nil && len(o.metadataFile) > 0

return &opts, nil
}
Expand Down
30 changes: 15 additions & 15 deletions controller/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,21 @@ func RunBuild(ctx context.Context, dockerCli command.Cli, in controllerapi.Build
InStream: inStream,
NamedContexts: contexts,
},
Ref: in.Ref,
BuildArgs: in.BuildArgs,
CgroupParent: in.CgroupParent,
ExtraHosts: in.ExtraHosts,
Labels: in.Labels,
NetworkMode: in.NetworkMode,
NoCache: in.NoCache,
NoCacheFilter: in.NoCacheFilter,
Pull: in.Pull,
ShmSize: dockeropts.MemBytes(in.ShmSize),
Tags: in.Tags,
Target: in.Target,
Ulimits: controllerUlimitOpt2DockerUlimit(in.Ulimits),
GroupRef: in.GroupRef,
WithProvenanceResponse: in.WithProvenanceResponse,
Ref: in.Ref,
BuildArgs: in.BuildArgs,
CgroupParent: in.CgroupParent,
ExtraHosts: in.ExtraHosts,
Labels: in.Labels,
NetworkMode: in.NetworkMode,
NoCache: in.NoCache,
NoCacheFilter: in.NoCacheFilter,
Pull: in.Pull,
ShmSize: dockeropts.MemBytes(in.ShmSize),
Tags: in.Tags,
Target: in.Target,
Ulimits: controllerUlimitOpt2DockerUlimit(in.Ulimits),
GroupRef: in.GroupRef,
WithDetailedResponse: in.WithDetailedResponse,
}

platforms, err := platformutil.Parse(in.Platforms)
Expand Down
Loading

0 comments on commit aafd62e

Please sign in to comment.