Skip to content

Commit

Permalink
support dynamic network config via attachable
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Suraci <[email protected]>
  • Loading branch information
vito committed Jul 1, 2023
1 parent cdf28d6 commit 4711702
Show file tree
Hide file tree
Showing 42 changed files with 3,611 additions and 582 deletions.
97 changes: 97 additions & 0 deletions client/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
gatewayapi "github.com/moby/buildkit/frontend/gateway/pb"
"github.com/moby/buildkit/identity"
"github.com/moby/buildkit/session"
"github.com/moby/buildkit/session/networks"
"github.com/moby/buildkit/session/secrets/secretsprovider"
"github.com/moby/buildkit/session/sshforward/sshprovider"
"github.com/moby/buildkit/solver/errdefs"
Expand Down Expand Up @@ -45,6 +46,7 @@ func TestClientGatewayIntegration(t *testing.T) {
testClientGatewayContainerPID1Exit,
testClientGatewayContainerMounts,
testClientGatewayContainerSecretEnv,
testClientGatewayContainerNetworkConfig,
testClientGatewayContainerPID1Tty,
testClientGatewayContainerCancelPID1Tty,
testClientGatewayContainerExecTty,
Expand Down Expand Up @@ -913,6 +915,101 @@ func testClientGatewayContainerSecretEnv(t *testing.T, sb integration.Sandbox) {
checkAllReleasable(t, c, sb, true)
}

func testClientGatewayContainerNetworkConfig(t *testing.T, sb integration.Sandbox) {
requiresLinux(t)

ctx := sb.Context()

c, err := New(ctx, sb.Address())
require.NoError(t, err)
defer c.Close()

product := "buildkit_test"

outBuf := new(bytes.Buffer)
b := func(ctx context.Context, c client.Client) (*client.Result, error) {
mounts := map[string]llb.State{
"/": llb.Image("busybox:latest"),
}

var containerMounts []client.Mount
for mountpoint, st := range mounts {
def, err := st.Marshal(ctx)
if err != nil {
return nil, errors.Wrap(err, "failed to marshal state")
}

r, err := c.Solve(ctx, client.SolveRequest{
Definition: def.ToPB(),
})
if err != nil {
return nil, errors.Wrap(err, "failed to solve")
}
containerMounts = append(containerMounts, client.Mount{
Dest: mountpoint,
MountType: pb.MountType_BIND,
Ref: r.Ref,
})
}

ctr, err := c.NewContainer(ctx, client.NewContainerRequest{
Mounts: containerMounts,
NetworkConfigID: "my-network",
})
if err != nil {
return nil, err
}

pid, err := ctr.Start(ctx, client.StartRequest{
Args: []string{"sh", "-c", "echo hosts; cat /etc/hosts; echo resolv; cat /etc/resolv.conf"},
Stdout: &nopCloser{outBuf},
})
require.NoError(t, err)
err = pid.Wait()
require.NoError(t, err)

return &client.Result{}, ctr.Release(ctx)
}

_, err = c.Build(ctx, SolveOpt{
Session: []session.Attachable{
networks.NewConfigProvider(func(id string) *networks.Config {
switch id {
case "my-network":
return &networks.Config{
Dns: &networks.DNSConfig{
Nameservers: []string{"1.1.1.1"},
SearchDomains: []string{"zombo.com"},
Options: []string{"ndots:999"},
},
IpHosts: []*networks.IPHosts{
{
Ip: "1.2.3.4",
Hosts: []string{"example.com"},
},
},
}
default:
panic("unknown network ID")
}
}),
},
}, product, b, nil)
require.NoError(t, err)

require.Equal(t, `hosts
127.0.0.1 localhost buildkitsandbox
::1 localhost ip6-localhost ip6-loopback
1.2.3.4 example.com
resolv
search zombo.com
nameserver 1.1.1.1
options ndots:999
`, outBuf.String())

checkAllReleasable(t, c, sb, true)
}

// testClientGatewayContainerPID1Tty is testing that we can get a tty via
// a container pid1, executor.Run
func testClientGatewayContainerPID1Tty(t *testing.T, sb integration.Sandbox) {
Expand Down
Loading

0 comments on commit 4711702

Please sign in to comment.