Skip to content

Commit 507d22b

Browse files
n-gampagent
andauthored
nsc: add --builder_shape flag to buildx setup (#1805)
Allows requesting a specific builder shape (e.g. 4x16) when setting up the server-side buildx proxy. Wired through BuilderConfiguration.Shape to GetBuilderConfigurationRequest.BuilderShape. Requires builder customization to be enabled for the workspace. Amp-Thread-ID: https://ampcode.com/threads/T-019e2090-db2c-726b-b8a7-c25ee50d3a65 Co-authored-by: Amp <amp@ampcode.com>
1 parent 22cec92 commit 507d22b

2 files changed

Lines changed: 39 additions & 0 deletions

File tree

internal/cli/cmd/cluster/buildx.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ import (
1414
"os/signal"
1515
"path"
1616
"path/filepath"
17+
"strconv"
1718
"strings"
1819
"syscall"
1920
"time"
2021

22+
computev1beta "buf.build/gen/go/namespace/cloud/protocolbuffers/go/proto/namespace/cloud/compute/v1beta"
2123
"github.com/cenkalti/backoff"
2224
"github.com/docker/buildx/store"
2325
"github.com/docker/buildx/store/storeutil"
@@ -83,6 +85,8 @@ func newSetupBuildxCmd() *cobra.Command {
8385
_ = cmd.Flags().MarkHidden("use_server_side_proxy")
8486
experimental := cmd.Flags().String("experimental", "", "A set of experimental features to be passed at construction time.")
8587
_ = cmd.Flags().MarkHidden("experimental")
88+
builderShape := cmd.Flags().String("builder_shape", "", "If set, requests a specific builder shape in the form <vcpu>x<memGB> (e.g. 4x16). Requires builder customization to be enabled for your workspace — reach out to the Namespace team.")
89+
_ = cmd.Flags().MarkHidden("builder_shape")
8690

8791
cmd.RunE = fncobra.RunE(func(ctx context.Context, args []string) error {
8892
if *debugDir != "" && !*background {
@@ -125,10 +129,16 @@ func newSetupBuildxCmd() *cobra.Command {
125129
}
126130

127131
if *useServerSideProxy {
132+
shape, err := parseBuilderShape(*builderShape)
133+
if err != nil {
134+
return err
135+
}
136+
128137
if err := setupServerSideBuildxProxy(ctx, state, *name, *use, *defaultLoad, dockerCli, available, api.BuilderConfiguration{
129138
SkipPrespawn: !*createAtStartup,
130139
Name: *tag,
131140
Experimental: *experimental,
141+
Shape: shape,
132142
}); err != nil {
133143
return err
134144
}
@@ -792,6 +802,32 @@ func newStatusBuildxCommand() *cobra.Command {
792802
return cmd
793803
}
794804

805+
func parseBuilderShape(spec string) (*computev1beta.InstanceShape, error) {
806+
if spec == "" {
807+
return nil, nil
808+
}
809+
810+
parts := strings.Split(spec, "x")
811+
if len(parts) != 2 {
812+
return nil, fnerrors.Newf("invalid --builder_shape %q: expected format <vcpu>x<memGB> (e.g. 4x16)", spec)
813+
}
814+
815+
vcpu, err := strconv.Atoi(parts[0])
816+
if err != nil || vcpu <= 0 {
817+
return nil, fnerrors.Newf("invalid --builder_shape %q: vcpu must be a positive integer", spec)
818+
}
819+
820+
memGB, err := strconv.Atoi(parts[1])
821+
if err != nil || memGB <= 0 {
822+
return nil, fnerrors.Newf("invalid --builder_shape %q: memory (GB) must be a positive integer", spec)
823+
}
824+
825+
return &computev1beta.InstanceShape{
826+
VirtualCpu: int32(vcpu),
827+
MemoryMegabytes: int32(memGB) * 1024,
828+
}, nil
829+
}
830+
795831
func readRemoteBuilderConfigs(dockerCli *command.DockerCli, name string) ([]BuilderConfig, error) {
796832
var res []BuilderConfig
797833

internal/providers/nscloud/api/rpc.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"time"
1515

1616
builderv1beta "buf.build/gen/go/namespace/cloud/protocolbuffers/go/proto/namespace/cloud/builder/v1beta"
17+
computev1beta "buf.build/gen/go/namespace/cloud/protocolbuffers/go/proto/namespace/cloud/compute/v1beta"
1718
"github.com/bcicen/jstream"
1819
"github.com/cenkalti/backoff"
1920
"go.uber.org/atomic"
@@ -365,6 +366,7 @@ type BuilderConfiguration struct {
365366
SkipPrespawn bool
366367
Name string
367368
Experimental string
369+
Shape *computev1beta.InstanceShape
368370
}
369371

370372
func GetBuilderConfiguration(ctx context.Context, platform BuildPlatform, conf BuilderConfiguration) (*builderv1beta.GetBuilderConfigurationResponse, error) {
@@ -391,6 +393,7 @@ func GetBuilderConfiguration(ctx context.Context, platform BuildPlatform, conf B
391393
SkipBuilderPreSpawn: conf.SkipPrespawn,
392394
BuilderName: conf.Name,
393395
Experimental: conf.Experimental,
396+
BuilderShape: conf.Shape,
394397
})
395398
if err != nil {
396399
return nil, fnerrors.Newf("failed while creating %v build cluster: %w", platform, err)

0 commit comments

Comments
 (0)