From 48c9342f5e2cfe0ce22b6df6477423ce237998fe Mon Sep 17 00:00:00 2001 From: William Batista Date: Tue, 27 Aug 2024 11:12:41 -0400 Subject: [PATCH] update depot builder info --- go.mod | 4 +++- go.sum | 4 ++-- internal/build/imgsrc/depot.go | 37 +++++++++++++++++++++++++------ internal/build/imgsrc/resolver.go | 7 ++++-- internal/command/deploy/deploy.go | 3 +++ internal/flag/flag.go | 24 ++++++++++++++++++++ 6 files changed, 67 insertions(+), 12 deletions(-) diff --git a/go.mod b/go.mod index ad697999f6..c5e32eb2d0 100644 --- a/go.mod +++ b/go.mod @@ -71,7 +71,7 @@ require ( github.com/spf13/pflag v1.0.5 github.com/spf13/viper v1.19.0 github.com/stretchr/testify v1.9.0 - github.com/superfly/fly-go v0.1.27 + github.com/superfly/fly-go v0.1.28-0.20240827141421-8c3a9a6cfa92 github.com/superfly/graphql v0.2.4 github.com/superfly/lfsc-go v0.1.1 github.com/superfly/macaroon v0.2.14-0.20240702184853-b8ac52a1fc77 @@ -270,3 +270,5 @@ replace ( github.com/loadsmart/calver-go => github.com/ndarilek/calver-go v0.0.0-20230710153822-893bbd83a936 github.com/nats-io/nats.go => github.com/btoews/nats.go v0.0.0-20240401180931-476bea7f4158 ) + +replace github.com/superfly/fly-go => github.com/superfly/fly-go v0.1.28-0.20240827141421-8c3a9a6cfa92 diff --git a/go.sum b/go.sum index 264eb6f408..b71c6e8b96 100644 --- a/go.sum +++ b/go.sum @@ -625,8 +625,8 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8= github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU= -github.com/superfly/fly-go v0.1.27 h1:dLevqMijR1aHyho7Ius8v2n2ZG6EiaZ9Y+Odl+g6P34= -github.com/superfly/fly-go v0.1.27/go.mod h1:JQke/BwoZqrWurqYkypSlcSo7bIUgCI3eVnqMC6AUj0= +github.com/superfly/fly-go v0.1.28-0.20240827141421-8c3a9a6cfa92 h1:xE7GUJXW4Gi5h1VLHgNdTwGZ4nujSlZEhwGhT75ELzA= +github.com/superfly/fly-go v0.1.28-0.20240827141421-8c3a9a6cfa92/go.mod h1:JQke/BwoZqrWurqYkypSlcSo7bIUgCI3eVnqMC6AUj0= github.com/superfly/graphql v0.2.4 h1:Av8hSk4x8WvKJ6MTnEwrLknSVSGPc7DWpgT3z/kt3PU= github.com/superfly/graphql v0.2.4/go.mod h1:CVfDl31srm8HnJ9udwLu6hFNUW/P6GUM2dKcG1YQ8jc= github.com/superfly/lfsc-go v0.1.1 h1:dGjLgt81D09cG+aR9lJZIdmonjZSR5zYCi7s54+ZU2Q= diff --git a/internal/build/imgsrc/depot.go b/internal/build/imgsrc/depot.go index 3155fcf73c..2f8215ad1d 100644 --- a/internal/build/imgsrc/depot.go +++ b/internal/build/imgsrc/depot.go @@ -12,6 +12,7 @@ import ( depotbuild "github.com/depot/depot-go/build" depotmachine "github.com/depot/depot-go/machine" + "github.com/docker/go-units" "github.com/moby/buildkit/client" "github.com/moby/buildkit/session/secrets/secretsprovider" "github.com/moby/buildkit/util/progress/progressui" @@ -52,7 +53,10 @@ func (s depotBuilderScope) String() string { } type DepotBuilder struct { - Scope depotBuilderScope + Scope depotBuilderScope + Size string + Region string + Update bool } func (d *DepotBuilder) Name() string { return "depot.dev" } @@ -101,7 +105,7 @@ func (d *DepotBuilder) Run(ctx context.Context, _ *dockerClientFactory, streams build.ImageBuildStart() - image, err := depotBuild(ctx, streams, opts, dockerfile, build, d.Scope) + image, err := depotBuild(ctx, streams, opts, dockerfile, build, d) if err != nil { metrics.SendNoData(ctx, "remote_builder_failure") build.ImageBuildFinish() @@ -118,7 +122,7 @@ func (d *DepotBuilder) Run(ctx context.Context, _ *dockerClientFactory, streams return image, "", nil } -func depotBuild(ctx context.Context, streams *iostreams.IOStreams, opts ImageOptions, dockerfilePath string, buildState *build, scope depotBuilderScope) (*DeploymentImage, error) { +func depotBuild(ctx context.Context, streams *iostreams.IOStreams, opts ImageOptions, dockerfilePath string, buildState *build, depotSettings *DepotBuilder) (*DeploymentImage, error) { buildState.BuilderInitStart() buildState.SetBuilderMetaPart1(depotBuilderType, "", "") @@ -131,7 +135,7 @@ func depotBuild(ctx context.Context, streams *iostreams.IOStreams, opts ImageOpt } } - buildkit, build, buildErr := initBuilder(ctx, buildState, opts.AppName, streams, scope) + buildkit, build, buildErr := initBuilder(ctx, buildState, opts.AppName, streams, depotSettings) if buildErr != nil { streams.StopProgressIndicator() return nil, buildErr @@ -171,19 +175,38 @@ func depotBuild(ctx context.Context, streams *iostreams.IOStreams, opts ImageOpt return newDeploymentImage(res, opts.Tag) } -func initBuilder(ctx context.Context, buildState *build, appName string, streams *iostreams.IOStreams, builderScope depotBuilderScope) (*depotmachine.Machine, *depotbuild.Build, error) { +func initBuilder(ctx context.Context, buildState *build, appName string, streams *iostreams.IOStreams, depotSettings *DepotBuilder) (*depotmachine.Machine, *depotbuild.Build, error) { apiClient := flyutil.ClientFromContext(ctx) - region := os.Getenv("FLY_REMOTE_BUILDER_REGION") + region := depotSettings.Region + if regionEnv := os.Getenv("FLY_REMOTE_BUILDER_REGION"); regionEnv != "" { + region = regionEnv + } + if region != "" { region = "fly-" + region } defer buildState.BuilderInitFinish() + var builderSizeBytes int64 + var err error + + switch depotSettings.Size { + case "": + builderSizeBytes = 50 * 1024 * 1024 * 1024 + default: + builderSizeBytes, err = units.FromHumanSize(depotSettings.Size) + if err != nil { + return nil, nil, fmt.Errorf("Depot builder with size %s is invalid due to %w", depotSettings.Size, builderSizeBytes) + } + } + buildInfo, err := apiClient.EnsureDepotRemoteBuilder(ctx, &fly.EnsureDepotRemoteBuilderInput{ AppName: &appName, Region: ®ion, - BuilderScope: fly.StringPointer(builderScope.String()), + BuilderSize: &builderSizeBytes, + BuilderScope: fly.StringPointer(depotSettings.Scope.String()), + Update: &depotSettings.Update, }) if err != nil { streams.StopProgressIndicator() diff --git a/internal/build/imgsrc/resolver.go b/internal/build/imgsrc/resolver.go index 1795c749da..db04711fdd 100644 --- a/internal/build/imgsrc/resolver.go +++ b/internal/build/imgsrc/resolver.go @@ -240,13 +240,16 @@ func (r *Resolver) BuildImage(ctx context.Context, streams *iostreams.IOStreams, builderScope = DepotBuilderScopeApp default: return nil, fmt.Errorf("invalid depot-scope value. must be 'org' or 'app'") - } + builderSize := flag.GetString(ctx, "depot-builder-size") + region := flag.GetString(ctx, "depot-builder-region") + update := flag.GetBool(ctx, "depot-builder-update") + if r.dockerFactory.mode.UseNixpacks() { strategies = append(strategies, &nixpacksBuilder{}) } else if r.dockerFactory.mode.UseDepot() { - strategies = append(strategies, &DepotBuilder{Scope: builderScope}) + strategies = append(strategies, &DepotBuilder{Scope: builderScope, Size: builderSize, Region: region, Update: update}) } else { strategies = []imageBuilder{ &buildpacksBuilder{}, diff --git a/internal/command/deploy/deploy.go b/internal/command/deploy/deploy.go index 21c58dfea2..3dbb39c456 100644 --- a/internal/command/deploy/deploy.go +++ b/internal/command/deploy/deploy.go @@ -52,6 +52,9 @@ var CommonFlags = flag.Set{ flag.NoCache(), flag.Depot(), flag.DepotScope(), + flag.DepotBuilderSize(), + flag.DepotBuilderRegion(), + flag.DepotBuilderUpdate(), flag.Nixpacks(), flag.BuildOnly(), flag.BpDockerHost(), diff --git a/internal/flag/flag.go b/internal/flag/flag.go index c63ee36b36..faeabd3f02 100644 --- a/internal/flag/flag.go +++ b/internal/flag/flag.go @@ -568,6 +568,30 @@ func DepotScope() String { } } +func DepotBuilderSize() String { + return String{ + Name: "depot-builder-size", + Description: "The size of the Depot builder's cache", + Default: "50GiB", + } +} + +func DepotBuilderRegion() String { + return String{ + Name: "depot-builder-region", + Description: "The region of the Depot builder", + Default: "50GiB", + } +} + +func DepotBuilderUpdate() Bool { + return Bool{ + Name: "depot-builder-update", + Description: "Update the Depot builder settings, if it doesn't already exist", + Default: false, + } +} + func Nixpacks() Bool { return Bool{ Name: "nixpacks",