Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update depot builder info #3899

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.20240828145546-91eddf9059f3
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.20240828145546-91eddf9059f3 h1:urWvRYS2YNe3QkMeS2TLuHI4QX5TafoJRuUrqU4TB40=
github.com/superfly/fly-go v0.1.28-0.20240828145546-91eddf9059f3/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
31 changes: 24 additions & 7 deletions internal/build/imgsrc/depot.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ func (s depotBuilderScope) String() string {
}

type DepotBuilder struct {
Scope depotBuilderScope
Scope depotBuilderScope
Size int
Region string
Update bool
}

func (d *DepotBuilder) Name() string { return "depot.dev" }
Expand Down Expand Up @@ -101,7 +104,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 +121,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 +134,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 +174,33 @@ 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 == "" {
closestRegion, err := apiClient.GetNearestRegion(ctx)
if err == nil {
region = closestRegion.Code
}
}

if region != "" {
region = "fly-" + region
}

defer buildState.BuilderInitFinish()

builderSizeBytes := depotSettings.Size
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'")

}

buildCacheSize := flag.GetInt(ctx, "depot-build-cache-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: buildCacheSize, 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.DepotBuildCacheSize(),
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 DepotBuildCacheSize() Int {
return Int{
Name: "depot-build-cache-size",
Description: "The size of the Depot builder's cache in GB",
Default: 50,
}
}

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 existing Depot project's settings",
Default: false,
}
}

func Nixpacks() Bool {
return Bool{
Name: "nixpacks",
Expand Down
Loading