Skip to content

Commit

Permalink
update depot builder info
Browse files Browse the repository at this point in the history
  • Loading branch information
billyb2 committed Aug 27, 2024
1 parent 307adcc commit 48c9342
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 12 deletions.
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
37 changes: 30 additions & 7 deletions internal/build/imgsrc/depot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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" }
Expand Down Expand Up @@ -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()
Expand All @@ -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, "", "")

Expand All @@ -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
Expand Down Expand Up @@ -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)

Check failure on line 200 in internal/build/imgsrc/depot.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest-m)

fmt.Errorf format %w has arg builderSizeBytes of wrong type int64

Check failure on line 200 in internal/build/imgsrc/depot.go

View workflow job for this annotation

GitHub Actions / test (macos-latest-xl)

fmt.Errorf format %w has arg builderSizeBytes of wrong type int64

Check failure on line 200 in internal/build/imgsrc/depot.go

View workflow job for this annotation

GitHub Actions / test (windows-latest-l)

fmt.Errorf format %w has arg builderSizeBytes of wrong type int64

Check failure on line 200 in internal/build/imgsrc/depot.go

View workflow job for this annotation

GitHub Actions / lint

printf: fmt.Errorf format %w has arg builderSizeBytes of wrong type int64 (govet)

Check failure on line 200 in internal/build/imgsrc/depot.go

View workflow job for this annotation

GitHub Actions / test / test (ubuntu-latest-m, ., test)

fmt.Errorf format %w has arg builderSizeBytes of wrong type int64

Check failure on line 200 in internal/build/imgsrc/depot.go

View workflow job for this annotation

GitHub Actions / test / test (macos-latest-xl, ., test)

fmt.Errorf format %w has arg builderSizeBytes of wrong type int64

Check failure on line 200 in internal/build/imgsrc/depot.go

View workflow job for this annotation

GitHub Actions / test / test (windows-latest-l, ., test)

fmt.Errorf format %w has arg builderSizeBytes of wrong type int64
}
}

buildInfo, err := apiClient.EnsureDepotRemoteBuilder(ctx, &fly.EnsureDepotRemoteBuilderInput{
AppName: &appName,
Region: &region,
BuilderScope: fly.StringPointer(builderScope.String()),
BuilderSize: &builderSizeBytes,
BuilderScope: fly.StringPointer(depotSettings.Scope.String()),
Update: &depotSettings.Update,
})
if err != nil {
streams.StopProgressIndicator()
Expand Down
7 changes: 5 additions & 2 deletions internal/build/imgsrc/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{},
Expand Down
3 changes: 3 additions & 0 deletions internal/command/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
24 changes: 24 additions & 0 deletions internal/flag/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 48c9342

Please sign in to comment.