Skip to content

Commit

Permalink
get rid of host mapping; not useful yet
Browse files Browse the repository at this point in the history
this is a neat way we could implement service discovery, but right now
everything just runs on the host network, so there's no point.
  • Loading branch information
vito committed Aug 7, 2022
1 parent 0a7f919 commit ce3fd7a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 80 deletions.
32 changes: 9 additions & 23 deletions pkg/runtimes/buildkit.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ func (runtime *Buildkit) Run(ctx context.Context, thunk bass.Thunk) error {
)
}

func (runtime *Buildkit) Start(ctx context.Context, thunk bass.Thunk) (StartResult, error) {
func (runtime *Buildkit) Start(ctx context.Context, thunk bass.Thunk) (PortInfos, error) {
ctx, stop := context.WithCancel(ctx)
runs := bass.RunsFromContext(ctx)

Expand All @@ -235,21 +235,14 @@ func (runtime *Buildkit) Start(ctx context.Context, thunk bass.Thunk) (StartResu
return nil
})

result := StartResult{
Ports: PortInfos{},
}
ports := PortInfos{}

if thunk.Ports != nil {
host := "localhost" //thunk.Name()

// TODO: bridge networking?
result.Hosts = append(result.Hosts, CommandHost{
Host: host,
Target: net.IPv4(127, 0, 0, 1),
})
host := "localhost" //thunk.Name()

for _, port := range thunk.Ports {
result.Ports[port.Name] = bass.Bindings{
ports[port.Name] = bass.Bindings{
"host": bass.String(host),
"port": bass.Int(port.Port),
}.Scope()
Expand All @@ -263,12 +256,12 @@ func (runtime *Buildkit) Start(ctx context.Context, thunk bass.Thunk) (StartResu
err := pollForPort(zapctx.ToContext(ctx, logger), exited, port.Name, pollAddr)
if err != nil {
stop()
return result, err
return nil, err
}
}
}

return result, nil
return ports, nil
}

func pollForPort(ctx context.Context, exited <-chan error, svc string, addr string) error {
Expand Down Expand Up @@ -563,13 +556,6 @@ func (b *builder) llb(ctx context.Context, thunk bass.Thunk, captureStdout bool)
runOpt = append(runOpt, llb.AddEnv("_BASS_OUTPUT", outputFile))
}

for _, host := range cmd.Hosts {
runOpt = append(runOpt, llb.AddExtraHost(
host.Host,
host.Target,
))
}

if thunk.Insecure {
needsInsecure = true

Expand Down Expand Up @@ -637,16 +623,16 @@ func (r *Buildkit) ref(ctx context.Context, imageRef bass.ImageRef) (string, err
if imageRef.Repository.Addr != nil {
addr := imageRef.Repository.Addr

result, err := r.Start(ctx, addr.Thunk)
ports, err := r.Start(ctx, addr.Thunk)
if err != nil {
return "", err
}

info, found := result.Ports[addr.Port]
info, found := ports[addr.Port]
if !found {
zapctx.FromContext(ctx).Error("unknown port",
zap.Any("thunk", addr.Thunk),
zap.Any("ports", result.Ports))
zap.Any("ports", ports))
return "", fmt.Errorf("unknown port: %s", addr.Port)
}

Expand Down
22 changes: 4 additions & 18 deletions pkg/runtimes/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ type Command struct {
// these don't need to be marshaled, since they're part of the container
// setup and not passed to the shim
Mounts []CommandMount `json:"-"`
Hosts []CommandHost `json:"-"`

mounted map[string]bool
starter Starter
Expand All @@ -45,18 +44,7 @@ type CommandHost struct {

type Starter interface {
// Start starts the thunk and waits for its ports to be ready.
Start(context.Context, bass.Thunk) (StartResult, error)
}

type StartResult struct {
// A mapping from each port to its address info (host, port, etc.)
Ports PortInfos

// Hostname to IP mappings to configure on the container
//
// Order must be deterministic, hence this is not a map. There may be
// duplicates.
Hosts []CommandHost
Start(context.Context, bass.Thunk) (PortInfos, error)
}

type PortInfos map[string]*bass.Scope
Expand Down Expand Up @@ -351,23 +339,21 @@ func (cmd *Command) resolveValue(ctx context.Context, val bass.Value, dest any)

var addr bass.ThunkAddr
if err := val.Decode(&addr); err == nil {
result, err := cmd.starter.Start(ctx, addr.Thunk)
ports, err := cmd.starter.Start(ctx, addr.Thunk)
if err != nil {
return fmt.Errorf("start %s: %w", addr.Thunk, err)
}

info, found := result.Ports[addr.Port]
info, found := ports[addr.Port]
if !found {
return fmt.Errorf("no info for port '%s': %+v", addr.Port, result.Ports)
return fmt.Errorf("no info for port '%s': %+v", addr.Port, ports)
}

str, err := addr.Render(info)
if err != nil {
return err
}

cmd.Hosts = append(cmd.Hosts, result.Hosts...)

return bass.String(str).Decode(dest)
}

Expand Down
56 changes: 17 additions & 39 deletions pkg/runtimes/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package runtimes_test

import (
"context"
"net"
"testing"

"github.com/vito/bass/pkg/bass"
Expand Down Expand Up @@ -61,11 +60,11 @@ func init() {

type FakeStarter struct {
Started bass.Thunk
StartResult runtimes.StartResult
StartResult runtimes.PortInfos
}

// Start starts the thunk and waits for its ports to be ready.
func (starter *FakeStarter) Start(ctx context.Context, thunk bass.Thunk) (runtimes.StartResult, error) {
func (starter *FakeStarter) Start(ctx context.Context, thunk bass.Thunk) (runtimes.PortInfos, error) {
starter.Started = thunk
return starter.StartResult, nil
}
Expand Down Expand Up @@ -347,16 +346,11 @@ func TestNewCommand(t *testing.T) {
argsThunk.Args = []bass.Value{thunkAddr}

starter := &FakeStarter{
StartResult: runtimes.StartResult{
Ports: runtimes.PortInfos{
"http": bass.Bindings{
"host": bass.String("drew"),
"port": bass.Int(6455),
}.Scope(),
},
Hosts: []runtimes.CommandHost{
{"carey", net.IPv4(127, 0, 0, 1)},
},
StartResult: runtimes.PortInfos{
"http": bass.Bindings{
"host": bass.String("drew"),
"port": bass.Int(6455),
}.Scope(),
},
}

Expand All @@ -365,9 +359,6 @@ func TestNewCommand(t *testing.T) {
is.NoErr(err)
is.Equal(cmd, runtimes.Command{
Args: []string{"run", "some://drew:6455/addr"},
Hosts: []runtimes.CommandHost{
{"carey", net.IPv4(127, 0, 0, 1)},
},
})

is.Equal(starter.Started, svcThunk)
Expand All @@ -383,16 +374,11 @@ func TestNewCommand(t *testing.T) {
}

starter := &FakeStarter{
StartResult: runtimes.StartResult{
Ports: runtimes.PortInfos{
"http": bass.Bindings{
"host": bass.String("drew"),
"port": bass.Int(6455),
}.Scope(),
},
Hosts: []runtimes.CommandHost{
{"carey", net.IPv4(127, 0, 0, 1)},
},
StartResult: runtimes.PortInfos{
"http": bass.Bindings{
"host": bass.String("drew"),
"port": bass.Int(6455),
}.Scope(),
},
}

Expand All @@ -411,16 +397,11 @@ func TestNewCommand(t *testing.T) {
}.Scope()

starter := &FakeStarter{
StartResult: runtimes.StartResult{
Ports: runtimes.PortInfos{
"http": bass.Bindings{
"host": bass.String("drew"),
"port": bass.Int(6455),
}.Scope(),
},
Hosts: []runtimes.CommandHost{
{"carey", net.IPv4(127, 0, 0, 1)},
},
StartResult: runtimes.PortInfos{
"http": bass.Bindings{
"host": bass.String("drew"),
"port": bass.Int(6455),
}.Scope(),
},
}

Expand All @@ -430,9 +411,6 @@ func TestNewCommand(t *testing.T) {
is.Equal(cmd, runtimes.Command{
Args: []string{"run"},
Env: []string{"ADDR=some://drew:6455/addr"},
Hosts: []runtimes.CommandHost{
{"carey", net.IPv4(127, 0, 0, 1)},
},
})

is.Equal(starter.Started, svcThunk)
Expand Down

0 comments on commit ce3fd7a

Please sign in to comment.