Skip to content

Commit c3ad689

Browse files
authored
fix: pass in bind allocator to build engine (#3179)
We already added bind allocator to build engine so that it can launch language plugins but we did not pass in the bind allocator. Added a task to clean this up here: #2452
1 parent c51b161 commit c3ad689

File tree

8 files changed

+58
-15
lines changed

8 files changed

+58
-15
lines changed

frontend/cli/cmd_box.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/TBD54566975/ftl"
1616
"github.com/TBD54566975/ftl/backend/controller/dsn"
1717
"github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1/ftlv1connect"
18+
"github.com/TBD54566975/ftl/internal/bind"
1819
"github.com/TBD54566975/ftl/internal/buildengine"
1920
"github.com/TBD54566975/ftl/internal/exec"
2021
"github.com/TBD54566975/ftl/internal/log"
@@ -123,7 +124,15 @@ func (b *boxCmd) Run(ctx context.Context, client ftlv1connect.ControllerServiceC
123124
if len(b.Build.Dirs) == 0 {
124125
return errors.New("no directories specified")
125126
}
126-
engine, err := buildengine.New(ctx, client, projConfig.Root(), b.Build.Dirs, buildengine.BuildEnv(b.Build.BuildEnv), buildengine.Parallelism(b.Build.Parallelism))
127+
128+
// use the cli endpoint to create the bind allocator, but leave the first port unused as it is meant to be reserved by a controller
129+
bindAllocator, err := bind.NewBindAllocator(cli.Endpoint)
130+
if err != nil {
131+
return fmt.Errorf("could not create bind allocator: %w", err)
132+
}
133+
_ = bindAllocator.Next()
134+
135+
engine, err := buildengine.New(ctx, client, projConfig.Root(), b.Build.Dirs, bindAllocator, buildengine.BuildEnv(b.Build.BuildEnv), buildengine.Parallelism(b.Build.Parallelism))
127136
if err != nil {
128137
return err
129138
}

frontend/cli/cmd_box_run.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func (b *boxRunCmd) Run(ctx context.Context, projConfig projectconfig.Config) er
7575
return fmt.Errorf("controller failed to start: %w", err)
7676
}
7777

78-
engine, err := buildengine.New(ctx, client, projConfig.Root(), []string{b.Dir})
78+
engine, err := buildengine.New(ctx, client, projConfig.Root(), []string{b.Dir}, runnerPortAllocator)
7979
if err != nil {
8080
return fmt.Errorf("failed to create build engine: %w", err)
8181
}

frontend/cli/cmd_build.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77

88
"github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1/ftlv1connect"
9+
"github.com/TBD54566975/ftl/internal/bind"
910
"github.com/TBD54566975/ftl/internal/buildengine"
1011
"github.com/TBD54566975/ftl/internal/projectconfig"
1112
)
@@ -23,7 +24,14 @@ func (b *buildCmd) Run(ctx context.Context, client ftlv1connect.ControllerServic
2324
if len(b.Dirs) == 0 {
2425
return errors.New("no directories specified")
2526
}
26-
engine, err := buildengine.New(ctx, client, projConfig.Root(), b.Dirs, buildengine.BuildEnv(b.BuildEnv), buildengine.Parallelism(b.Parallelism))
27+
// use the cli endpoint to create the bind allocator, but leave the first port unused as it is meant to be reserved by a controller
28+
bindAllocator, err := bind.NewBindAllocator(cli.Endpoint)
29+
if err != nil {
30+
return fmt.Errorf("could not create bind allocator: %w", err)
31+
}
32+
_ = bindAllocator.Next()
33+
34+
engine, err := buildengine.New(ctx, client, projConfig.Root(), b.Dirs, bindAllocator, buildengine.BuildEnv(b.BuildEnv), buildengine.Parallelism(b.Parallelism))
2735
if err != nil {
2836
return err
2937
}

frontend/cli/cmd_deploy.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package main
22

33
import (
44
"context"
5+
"fmt"
56

67
"github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1/ftlv1connect"
78
"github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1beta1/provisioner/provisionerconnect"
9+
"github.com/TBD54566975/ftl/internal/bind"
810
"github.com/TBD54566975/ftl/internal/buildengine"
911
"github.com/TBD54566975/ftl/internal/projectconfig"
1012
"github.com/TBD54566975/ftl/internal/rpc"
@@ -24,7 +26,15 @@ func (d *deployCmd) Run(ctx context.Context, projConfig projectconfig.Config) er
2426
} else {
2527
client = rpc.ClientFromContext[ftlv1connect.ControllerServiceClient](ctx)
2628
}
27-
engine, err := buildengine.New(ctx, client, projConfig.Root(), d.Build.Dirs, buildengine.BuildEnv(d.Build.BuildEnv), buildengine.Parallelism(d.Build.Parallelism))
29+
30+
// use the cli endpoint to create the bind allocator, but leave the first port unused as it is meant to be reserved by a controller
31+
bindAllocator, err := bind.NewBindAllocator(cli.Endpoint)
32+
if err != nil {
33+
return fmt.Errorf("could not create bind allocator: %w", err)
34+
}
35+
_ = bindAllocator.Next()
36+
37+
engine, err := buildengine.New(ctx, client, projConfig.Root(), d.Build.Dirs, bindAllocator, buildengine.BuildEnv(d.Build.BuildEnv), buildengine.Parallelism(d.Build.Parallelism))
2838
if err != nil {
2939
return err
3040
}

frontend/cli/cmd_dev.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
"github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1/ftlv1connect"
1414
"github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1beta1/provisioner/provisionerconnect"
15+
"github.com/TBD54566975/ftl/internal/bind"
1516
"github.com/TBD54566975/ftl/internal/buildengine"
1617
"github.com/TBD54566975/ftl/internal/dev"
1718
"github.com/TBD54566975/ftl/internal/log"
@@ -65,6 +66,12 @@ func (d *devCmd) Run(ctx context.Context, k *kong.Kong, projConfig projectconfig
6566
}
6667
sm := terminal.FromContext(ctx)
6768
starting := sm.NewStatus("\u001B[92mStarting FTL Server 🚀\u001B[39m")
69+
70+
bindAllocator, err := bind.NewBindAllocator(d.ServeCmd.Bind)
71+
if err != nil {
72+
return fmt.Errorf("could not create bind allocator: %w", err)
73+
}
74+
6875
// cmdServe will notify this channel when startup commands are complete and the controller is ready
6976
controllerReady := make(chan bool, 1)
7077
if !d.NoServe {
@@ -80,7 +87,7 @@ func (d *devCmd) Run(ctx context.Context, k *kong.Kong, projConfig projectconfig
8087
}
8188

8289
g.Go(func() error {
83-
return d.ServeCmd.run(ctx, projConfig, optional.Some(controllerReady), true)
90+
return d.ServeCmd.run(ctx, projConfig, optional.Some(controllerReady), true, bindAllocator)
8491
})
8592
}
8693

@@ -101,7 +108,7 @@ func (d *devCmd) Run(ctx context.Context, k *kong.Kong, projConfig projectconfig
101108
})
102109
}
103110

104-
engine, err := buildengine.New(ctx, client, projConfig.Root(), d.Build.Dirs, opts...)
111+
engine, err := buildengine.New(ctx, client, projConfig.Root(), d.Build.Dirs, bindAllocator, opts...)
105112
if err != nil {
106113
return err
107114
}

frontend/cli/cmd_serve.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,15 @@ type serveCmd struct {
5656
const ftlRunningErrorMsg = "FTL is already running. Use 'ftl serve --stop' to stop it"
5757

5858
func (s *serveCmd) Run(ctx context.Context, projConfig projectconfig.Config) error {
59-
return s.run(ctx, projConfig, optional.None[chan bool](), false)
59+
bindAllocator, err := bind.NewBindAllocator(s.Bind)
60+
if err != nil {
61+
return fmt.Errorf("could not create bind allocator: %w", err)
62+
}
63+
return s.run(ctx, projConfig, optional.None[chan bool](), false, bindAllocator)
6064
}
6165

62-
func (s *serveCmd) run(ctx context.Context, projConfig projectconfig.Config, initialised optional.Option[chan bool], devMode bool) error {
66+
//nolint:maintidx
67+
func (s *serveCmd) run(ctx context.Context, projConfig projectconfig.Config, initialised optional.Option[chan bool], devMode bool, bindAllocator *bind.BindAllocator) error {
6368
logger := log.FromContext(ctx)
6469
controllerClient := rpc.ClientFromContext[ftlv1connect.ControllerServiceClient](ctx)
6570
provisionerClient := rpc.ClientFromContext[provisionerconnect.ProvisionerServiceClient](ctx)
@@ -112,11 +117,6 @@ func (s *serveCmd) run(ctx context.Context, projConfig projectconfig.Config, ini
112117

113118
wg, ctx := errgroup.WithContext(ctx)
114119

115-
bindAllocator, err := bind.NewBindAllocator(s.Bind)
116-
if err != nil {
117-
return err
118-
}
119-
120120
controllerAddresses := make([]*url.URL, 0, s.Controllers)
121121
controllerIngressAddresses := make([]*url.URL, 0, s.Controllers)
122122
for range s.Controllers {

internal/buildengine/engine.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ func WithStartTime(startTime time.Time) Option {
236236
// pull in missing schemas.
237237
//
238238
// "dirs" are directories to scan for local modules.
239-
func New(ctx context.Context, client DeployClient, projectRoot string, moduleDirs []string, options ...Option) (*Engine, error) {
239+
func New(ctx context.Context, client DeployClient, projectRoot string, moduleDirs []string, bindAllocator *bind.BindAllocator, options ...Option) (*Engine, error) {
240240
ctx = rpc.ContextWithClient(ctx, client)
241241
e := &Engine{
242242
client: client,
@@ -252,6 +252,7 @@ func New(ctx context.Context, client DeployClient, projectRoot string, moduleDir
252252
invalidateDeps: make(chan invalidateDependenciesEvent, 128),
253253
rawEngineUpdates: make(chan rawEngineEvent, 128),
254254
EngineUpdates: pubsub.New[EngineEvent](),
255+
bindAllocator: bindAllocator,
255256
}
256257
for _, option := range options {
257258
option(e)

internal/buildengine/engine_test.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,26 @@ package buildengine_test
22

33
import (
44
"context"
5+
"net/url"
56
"testing"
67

78
"github.com/alecthomas/assert/v2"
89

10+
"github.com/TBD54566975/ftl/internal/bind"
911
"github.com/TBD54566975/ftl/internal/buildengine"
1012
"github.com/TBD54566975/ftl/internal/log"
1113
"github.com/TBD54566975/ftl/internal/schema"
1214
)
1315

1416
func TestGraph(t *testing.T) {
1517
ctx := log.ContextWithNewDefaultLogger(context.Background())
16-
engine, err := buildengine.New(ctx, nil, t.TempDir(), []string{"testdata/alpha", "testdata/other", "testdata/another"})
18+
19+
bindURL, err := url.Parse("http://127.0.0.1:8893")
20+
assert.NoError(t, err)
21+
bindAllocator, err := bind.NewBindAllocator(bindURL)
22+
assert.NoError(t, err)
23+
24+
engine, err := buildengine.New(ctx, nil, t.TempDir(), []string{"testdata/alpha", "testdata/other", "testdata/another"}, bindAllocator)
1725
assert.NoError(t, err)
1826

1927
defer engine.Close()

0 commit comments

Comments
 (0)