Skip to content

Commit

Permalink
Merge pull request #282 from depot/remove-custom-progress
Browse files Browse the repository at this point in the history
Remove custom progress package
  • Loading branch information
jacobwgillespie authored May 3, 2024
2 parents 5e9e9f8 + bbba6de commit d15e405
Show file tree
Hide file tree
Showing 23 changed files with 254 additions and 540 deletions.
25 changes: 9 additions & 16 deletions examples/build/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (

"github.com/depot/cli/pkg/build"
"github.com/depot/cli/pkg/machine"
"github.com/depot/cli/pkg/progress"
"github.com/depot/cli/pkg/progresshelper"
cliv1 "github.com/depot/cli/pkg/proto/depot/cli/v1"
buildxprogress "github.com/docker/buildx/util/progress"
printer "github.com/docker/buildx/util/progress"
progress "github.com/docker/buildx/util/progress"
"github.com/moby/buildkit/client"
"github.com/moby/buildkit/client/llb"
)
Expand Down Expand Up @@ -44,24 +44,17 @@ func main() {
defer build.Finish(buildErr)

ctx, cancel := context.WithCancel(ctx)
buildxprinter, buildErr := buildxprogress.NewPrinter(ctx, os.Stderr, os.Stderr, "quiet")
printer, buildErr := progress.NewPrinter(ctx, os.Stderr, os.Stderr, "quiet")
if buildErr != nil {
return
}
// 2. Start progress reporter. This will report the build progress logs to the
// Depot API and print it to the terminal. You totally can skip this if you
// don't need to report the build progress to the API.
reporter, finishReporter, buildErr := progress.NewProgress(ctx, build.ID, build.Token, buildxprinter)
if buildErr != nil {
return
}

defer finishReporter() // Required to ensure that the printer is stopped before context `cancel()`.
defer cancel()

reportingWriter := progresshelper.NewReportingWriter(printer, build.ID, build.Token)

// 3. Acquire a buildkit machine.
var buildkit *machine.Machine
buildErr = reporter.WithLog("[depot] launching amd64 machine", func() error {
buildErr = progresshelper.WithLog(reportingWriter, "[depot] launching amd64 machine", func() error {
buildkit, buildErr = machine.Acquire(ctx, build.ID, build.Token, "amd64")
return buildErr
})
Expand All @@ -73,7 +66,7 @@ func main() {
// 4. Check buildkitd readiness. When the buildkitd starts, it may take
// quite a while to be ready to accept connections when it loads a large boltdb.
var buildkitClient *client.Client
buildErr = reporter.WithLog("[depot] connecting to amd64 machine", func() error {
buildErr = progresshelper.WithLog(reportingWriter, "[depot] connecting to amd64 machine", func() error {
ctx, cancel := context.WithTimeout(ctx, 5*time.Minute)
defer cancel()
buildkitClient, buildErr = buildkit.Connect(ctx)
Expand All @@ -84,13 +77,13 @@ func main() {
}

// 5. Use the buildkit client to build the image.
buildErr = buildImage(ctx, buildkitClient, reporter)
buildErr = buildImage(ctx, buildkitClient, printer)
if buildErr != nil {
return
}
}

func buildImage(ctx context.Context, buildkitClient *client.Client, reporter *progress.Progress) error {
func buildImage(ctx context.Context, buildkitClient *client.Client, reporter *progress.Printer) error {
statusCh, done := printer.NewChannel(reporter)
defer func() { <-done }()

Expand Down
1 change: 0 additions & 1 deletion examples/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,6 @@ github.com/spf13/viper v1.14.0/go.mod h1:WT//axPky3FdvXHzGw33dNdXXXfFQqmEalje+eg
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
Expand Down
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ require (
github.com/spf13/cobra v1.6.1
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.14.0
github.com/stretchr/testify v1.8.4
github.com/zclconf/go-cty v1.10.0
go.opentelemetry.io/otel/trace v1.20.0
go.opentelemetry.io/proto/otlp v0.12.0
Expand Down Expand Up @@ -146,7 +145,7 @@ require (
github.com/spf13/afero v1.9.2 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/stretchr/testify v1.8.4 // indirect
github.com/subosito/gotenv v1.4.1 // indirect
github.com/theupdateframework/notary v0.6.1 // indirect
github.com/tonistiigi/fsutil v0.0.0-20230105215944-fb433841cbfa // indirect
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -625,8 +625,6 @@ github.com/stoewer/go-strcase v1.2.0/go.mod h1:IBiWB2sKIp3wVVQ3Y035++gc+knqhUQag
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
Expand Down
4 changes: 2 additions & 2 deletions pkg/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (

"connectrpc.com/connect"
depotapi "github.com/depot/cli/pkg/api"
"github.com/depot/cli/pkg/progress"
cliv1 "github.com/depot/cli/pkg/proto/depot/cli/v1"
"github.com/docker/buildx/driver"
"github.com/docker/buildx/util/progress"
clitypes "github.com/docker/cli/cli/config/types"
"github.com/moby/buildkit/util/grpcerrors"
"google.golang.org/grpc/codes"
Expand All @@ -23,7 +23,7 @@ type Build struct {
// BuildURL is the URL to the build on the depot web UI.
BuildURL string
Finish func(error)
Reporter *progress.Progress
Reporter progress.Writer

Response *connect.Response[cliv1.CreateBuildResponse]
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/buildx/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
depotbuild "github.com/depot/cli/pkg/build"
"github.com/depot/cli/pkg/buildx/imagetools"
"github.com/depot/cli/pkg/debuglog"
depotprogress "github.com/depot/cli/pkg/progress"
"github.com/depot/cli/pkg/progresshelper"
"github.com/distribution/reference"
"github.com/docker/buildx/builder"
"github.com/docker/buildx/driver"
Expand Down Expand Up @@ -820,7 +820,7 @@ func BuildWithResultHandler(ctx context.Context, nodes []builder.Node, opt map[s
multiDriver := len(m[k]) > 1
hasMobyDriver := false
debuglog.Log("Fetching git attributes")
finishLog := depotprogress.StartLog(w, "[internal] fetching git attributes")
finishLog := progresshelper.StartLog(w, "[internal] fetching git attributes")
gitattrs, err := getGitAttributes(ctx, opt.Inputs.ContextPath, opt.Inputs.DockerfilePath)
finishLog(err)
if err != nil {
Expand Down
12 changes: 2 additions & 10 deletions pkg/buildx/commands/bake.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/depot/cli/pkg/compose"
"github.com/depot/cli/pkg/helpers"
"github.com/depot/cli/pkg/load"
depotprogress "github.com/depot/cli/pkg/progress"
"github.com/depot/cli/pkg/registry"
"github.com/depot/cli/pkg/sbom"
buildx "github.com/docker/buildx/build"
Expand Down Expand Up @@ -55,13 +54,7 @@ func RunBake(dockerCli command.Cli, in BakeOptions, validator BakeValidator) (er

ctx2, cancel := context.WithCancel(context.TODO())

buildxprinter, err := progress.NewPrinter(ctx2, os.Stderr, os.Stderr, in.progress)
if err != nil {
cancel()
return err
}

printer, finish, err := depotprogress.NewProgress(ctx2, in.buildID, in.token, buildxprinter)
printer, err := progress.NewPrinter(ctx2, os.Stderr, os.Stderr, in.progress)
if err != nil {
cancel()
return err
Expand All @@ -80,7 +73,6 @@ func RunBake(dockerCli command.Cli, in BakeOptions, validator BakeValidator) (er
}
}()

defer finish() // Required to ensure that the printer is stopped before the context is cancelled.
defer cancel()

if os.Getenv("DEPOT_NO_SUMMARY_LINK") == "" {
Expand Down Expand Up @@ -152,7 +144,7 @@ func RunBake(dockerCli command.Cli, in BakeOptions, validator BakeValidator) (er
}

linter := NewLinter(printer, NewLintFailureMode(in.lint, in.lintFailOn), clients, buildxNodes)
resp, err := build.DepotBuild(ctx, buildxNodes, buildOpts, dockerClient, dockerConfigDir, buildxprinter, linter, in.DepotOptions.build)
resp, err := build.DepotBuild(ctx, buildxNodes, buildOpts, dockerClient, dockerConfigDir, printer, linter, in.DepotOptions.build)
if err != nil {
if errors.Is(err, LintFailed) {
linter.Print(os.Stderr, in.progress)
Expand Down
13 changes: 2 additions & 11 deletions pkg/buildx/commands/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"github.com/depot/cli/pkg/debuglog"
"github.com/depot/cli/pkg/helpers"
"github.com/depot/cli/pkg/load"
depotprogress "github.com/depot/cli/pkg/progress"
"github.com/depot/cli/pkg/registry"
"github.com/depot/cli/pkg/sbom"
"github.com/distribution/reference"
Expand Down Expand Up @@ -205,19 +204,11 @@ func (c nopCloser) Close() error { return nil }
func buildTargets(ctx context.Context, dockerCli command.Cli, nodes []builder.Node, opts map[string]build.Options, depotOpts DepotOptions, progressMode, metadataFile string, exportLoad, allowNoOutput bool) (imageIDs []string, res *build.ResultContext, err error) {
ctx2, cancel := context.WithCancel(context.TODO())

buildxprinter, err := progress.NewPrinter(ctx2, os.Stderr, os.Stderr, progressMode)
printer, err := progress.NewPrinter(ctx2, os.Stderr, os.Stderr, progressMode)
if err != nil {
cancel()
return nil, nil, err
}

printer, finish, err := depotprogress.NewProgress(ctx2, depotOpts.buildID, depotOpts.token, buildxprinter)
if err != nil {
cancel()
return nil, nil, err
}

defer finish() // Required to ensure that the printer is stopped before the context is cancelled.
defer cancel()

if os.Getenv("DEPOT_NO_SUMMARY_LINK") == "" {
Expand Down Expand Up @@ -278,7 +269,7 @@ func buildTargets(ctx context.Context, dockerCli command.Cli, nodes []builder.No

linter := NewLinter(printer, NewLintFailureMode(depotOpts.lint, depotOpts.lintFailOn), clients, buildxNodes)

resp, err := depotbuildxbuild.DepotBuildWithResultHandler(ctx, buildxNodes, opts, dockerClient, dockerConfigDir, buildxprinter, linter, func(driverIndex int, gotRes *build.ResultContext) {
resp, err := depotbuildxbuild.DepotBuildWithResultHandler(ctx, buildxNodes, opts, dockerClient, dockerConfigDir, printer, linter, func(driverIndex int, gotRes *build.ResultContext) {
mu.Lock()
defer mu.Unlock()
if res == nil || driverIndex < idx {
Expand Down
12 changes: 5 additions & 7 deletions pkg/buildx/commands/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

"github.com/depot/cli/pkg/buildx/build"
"github.com/depot/cli/pkg/debuglog"
depotprogress "github.com/depot/cli/pkg/progress"
"github.com/depot/cli/pkg/progresshelper"
"github.com/docker/buildx/builder"
"github.com/docker/buildx/util/progress"
"github.com/moby/buildkit/client"
Expand Down Expand Up @@ -84,15 +84,13 @@ type Linter struct {
FailureMode LintFailure
Clients []*client.Client
BuildxNodes []builder.Node

// Our depot progress has special functionality to print and upload lint issues.
printer *depotprogress.Progress
printer *progress.Printer

mu sync.Mutex
issues map[string][]client.VertexWarning
}

func NewLinter(printer *depotprogress.Progress, failureMode LintFailure, clients []*client.Client, nodes []builder.Node) *Linter {
func NewLinter(printer *progress.Printer, failureMode LintFailure, clients []*client.Client, nodes []builder.Node) *Linter {
return &Linter{
FailureMode: failureMode,
Clients: clients,
Expand Down Expand Up @@ -146,7 +144,7 @@ func (l *Linter) Handle(ctx context.Context, target string, driverIndex int, doc
}
dgst := digest.Canonical.FromString(identity.NewID())
tm := time.Now()
l.printer.WriteLint(client.Vertex{Digest: dgst, Name: lintName, Started: &tm}, nil, nil)
progresshelper.WriteLint(l.printer, client.Vertex{Digest: dgst, Name: lintName, Started: &tm}, nil, nil)

output, err := RunHadolint(ctx, l.Clients[driverIndex], l.BuildxNodes[driverIndex].Platforms[0], dockerfile)
if err != nil {
Expand Down Expand Up @@ -280,7 +278,7 @@ func (l *Linter) Handle(ctx context.Context, target string, driverIndex int, doc
lintErr = LintFailed
}

l.printer.WriteLint(lintResults, statuses, logs)
progresshelper.WriteLint(l.printer, lintResults, statuses, logs)

l.mu.Lock()
defer l.mu.Unlock()
Expand Down
7 changes: 5 additions & 2 deletions pkg/buildxdriver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
depotbuild "github.com/depot/cli/pkg/build"
"github.com/depot/cli/pkg/debuglog"
"github.com/depot/cli/pkg/machine"
"github.com/depot/cli/pkg/progresshelper"
"github.com/docker/buildx/driver"
"github.com/docker/buildx/util/progress"
"github.com/moby/buildkit/client"
Expand Down Expand Up @@ -41,12 +42,14 @@ func (d *Driver) Bootstrap(ctx context.Context, reporter progress.Logger) error
d.cfg.Auth = depotbuild.NewAuthProvider(credentials, d.cfg.Auth)
}

reportingLogger := progresshelper.NewReportingLogger(reporter, buildID, token)

message := "[depot] launching " + platform + " machine"

// Try to acquire machine twice
var err error
for i := 0; i < 2; i++ {
finishLog := StartLog(message, reporter)
finishLog := StartLog(message, reportingLogger)
d.buildkit, err = machine.Acquire(ctx, buildID, token, platform)
finishLog(err)
if err == nil {
Expand All @@ -59,7 +62,7 @@ func (d *Driver) Bootstrap(ctx context.Context, reporter progress.Logger) error
}

message = "[depot] connecting to " + platform + " machine"
finishLog := StartLog(message, reporter)
finishLog := StartLog(message, reportingLogger)

ctx, cancel := context.WithTimeout(ctx, 5*time.Minute)
defer cancel()
Expand Down
22 changes: 8 additions & 14 deletions pkg/cmd/buildctl/dial-stdio.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ import (
depot "github.com/depot/cli/internal/build"
"github.com/depot/cli/pkg/helpers"
"github.com/depot/cli/pkg/machine"
"github.com/depot/cli/pkg/progress"
"github.com/depot/cli/pkg/progresshelper"
cliv1 "github.com/depot/cli/pkg/proto/depot/cli/v1"
buildxprogress "github.com/docker/buildx/util/progress"
"github.com/docker/buildx/util/progress"
"github.com/moby/buildkit/client"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -121,12 +121,6 @@ func run() error {
}()

status := make(chan *client.SolveStatus, 1024)
listener := func(s *client.SolveStatus) {
select {
case status <- s:
default:
}
}

acquireState := func() *ProxyState {
once.Do(func() {
Expand All @@ -143,25 +137,25 @@ func run() error {
ctx2 := context.TODO()
ctx2, cancelStatus = context.WithCancel(ctx2)

buildxprinter, err := buildxprogress.NewPrinter(ctx2, os.Stderr, os.Stderr, "quiet")
state.Reporter, err = progress.NewPrinter(ctx2, os.Stderr, os.Stderr, "quiet")
if err != nil {
state.Err = fmt.Errorf("unable to create buildx printer: %w", err)
cancel()
return
}
state.Reporter = progresshelper.Tee(state.Reporter, status)

state.Reporter, finishStatus, _ = progress.NewProgress(ctx2, build.ID, build.Token, buildxprinter)
state.Reporter.AddListener(listener)
reportingWriter := progresshelper.NewReportingWriter(state.Reporter, build.ID, build.Token)

state.SummaryURL = build.BuildURL
buildFinish = build.Finish

if os.Getenv("DEPOT_NO_SUMMARY_LINK") == "" {
state.Reporter.Log("[depot] build: "+state.SummaryURL, nil)
progresshelper.Log(state.Reporter, "[depot] build: "+state.SummaryURL, nil)
}

var builder *machine.Machine
state.Err = state.Reporter.WithLog("[depot] launching "+platform+" machine", func() error {
state.Err = progresshelper.WithLog(reportingWriter, "[depot] launching "+platform+" machine", func() error {
for i := 0; i < 2; i++ {
builder, state.Err = machine.Acquire(ctx, build.ID, build.Token, platform)
if state.Err == nil {
Expand All @@ -177,7 +171,7 @@ func run() error {

machineRelease = builder.Release

state.Err = state.Reporter.WithLog("[depot] connecting to "+platform+" machine", func() error {
state.Err = progresshelper.WithLog(reportingWriter, "[depot] connecting to "+platform+" machine", func() error {
buildkitConn, err := tlsConn(ctx, builder)
if err != nil {
state.Err = fmt.Errorf("unable to connect: %w", err)
Expand Down
13 changes: 7 additions & 6 deletions pkg/cmd/buildctl/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import (
content "github.com/containerd/containerd/api/services/content/v1"
"github.com/containerd/containerd/api/services/leases/v1"
"github.com/containerd/containerd/defaults"
"github.com/depot/cli/pkg/progress"
"github.com/depot/cli/pkg/progresshelper"
"github.com/docker/buildx/util/progress"
"github.com/gogo/protobuf/types"
control "github.com/moby/buildkit/api/services/control"
worker "github.com/moby/buildkit/api/types"
Expand Down Expand Up @@ -100,10 +101,10 @@ func Proxy(ctx context.Context, conn net.Conn, acquireState func() *ProxyState,

// ProxyState is created once usually during a Status API call.
type ProxyState struct {
Conn *grpc.ClientConn // Conn is the connection to the buildkitd server.
SummaryURL string // SummaryURL is the UI summary page.
Reporter *progress.Progress // Reporter forwards status events to the API.
Err error // Err is set when the connection cannot be established or the build fails.
Conn *grpc.ClientConn // Conn is the connection to the buildkitd server.
SummaryURL string // SummaryURL is the UI summary page.
Reporter progress.Writer // Reporter forwards status events to the API.
Err error // Err is set when the connection cannot be established or the build fails.
}

type ControlProxy struct {
Expand Down Expand Up @@ -205,7 +206,7 @@ func (p *ControlProxy) Status(in *control.StatusRequest, toBuildx control.Contro
msg, err := fromBuildkit.Recv()
if err != nil {
if os.Getenv("DEPOT_NO_SUMMARY_LINK") == "" {
state.Reporter.Log("Build summary: "+state.SummaryURL, nil)
progresshelper.Log(state.Reporter, "Build summary: "+state.SummaryURL, nil)
}

if errors.Is(err, io.EOF) || errors.Is(err, context.Canceled) {
Expand Down
Loading

0 comments on commit d15e405

Please sign in to comment.