Skip to content

Commit

Permalink
Merge pull request #265 from carolynvs/docker-network
Browse files Browse the repository at this point in the history
Support DOCKER_NETWORK driver variable
  • Loading branch information
carolynvs authored Aug 2, 2021
2 parents fdc0505 + 020cc2a commit 0b6d46d
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 16 deletions.
54 changes: 38 additions & 16 deletions driver/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ import (
"github.com/cnabio/cnab-go/driver"
)

const (
// SettingNetwork is the environment variable for the driver that specifies
// the docker network to which the invocation image should be attached.
SettingNetwork = "DOCKER_NETWORK"
)

// Driver is capable of running Docker invocation images using Docker itself.
type Driver struct {
config map[string]string
Expand Down Expand Up @@ -93,6 +99,7 @@ func (d *Driver) Config() map[string]string {
"DOCKER_DRIVER_QUIET": "Make the Docker driver quiet (only print container stdout/stderr)",
"OUTPUTS_MOUNT_PATH": "Absolute path to where Docker driver can create temporary directories to bundle outputs. Defaults to temp dir.",
"CLEANUP_CONTAINERS": "If true, the docker container will be destroyed when it finishes running. If false, it will not be destroyed. The supported values are true and false. Defaults to true.",
SettingNetwork: "Attach the invocation image to the specified docker network",
}
}

Expand Down Expand Up @@ -199,21 +206,7 @@ func (d *Driver) exec(op *driver.Operation) (driver.OperationResult, error) {
return driver.OperationResult{}, errors.Wrap(err, "image digest validation failed")
}

var env []string
for k, v := range op.Environment {
env = append(env, fmt.Sprintf("%s=%v", k, v))
}

d.containerCfg = container.Config{
Image: op.Image.Image,
Env: env,
Entrypoint: strslice.StrSlice{"/cnab/app/run"},
AttachStderr: true,
AttachStdout: true,
}

d.containerHostCfg = container.HostConfig{}
if err := d.ApplyConfigurationOptions(); err != nil {
if err := d.setConfigurationOptions(op); err != nil {
return driver.OperationResult{}, err
}

Expand Down Expand Up @@ -297,7 +290,7 @@ func (d *Driver) exec(op *driver.Operation) (driver.OperationResult, error) {
return opResult, err
}

// ApplyConfigurationOptions applies the configuration options set on the driver
// ApplyConfigurationOptions applies the configuration options set on the driver by the user.
func (d *Driver) ApplyConfigurationOptions() error {
for _, opt := range d.dockerConfigurationOptions {
if err := opt(&d.containerCfg, &d.containerHostCfg); err != nil {
Expand All @@ -307,6 +300,35 @@ func (d *Driver) ApplyConfigurationOptions() error {
return nil
}

// setConfigurationOptions initializes the container and host configuration options on the driver,
// combining the default configuration with any overrides set by the user.
func (d *Driver) setConfigurationOptions(op *driver.Operation) error {
var env []string
for k, v := range op.Environment {
env = append(env, fmt.Sprintf("%s=%v", k, v))
}

d.containerCfg = container.Config{
Image: op.Image.Image,
Env: env,
Entrypoint: strslice.StrSlice{"/cnab/app/run"},
AttachStderr: true,
AttachStdout: true,
}

d.containerHostCfg = container.HostConfig{}

if network, ok := d.config[SettingNetwork]; ok {
d.containerHostCfg.NetworkMode = container.NetworkMode(network)
}

if err := d.ApplyConfigurationOptions(); err != nil {
return err
}

return nil
}

func containerError(containerMessage string, containerErr, fetchErr error) error {
if fetchErr != nil {
return fmt.Errorf("%s: %v. fetching outputs failed: %s", containerMessage, containerErr, fetchErr)
Expand Down
40 changes: 40 additions & 0 deletions driver/docker/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,46 @@ func TestDriver_GetConfigurationOptions(t *testing.T) {
})
}

func TestDriver_setConfigurationOptions(t *testing.T) {
img := "example.com/myimage"
op := &driver.Operation{
Image: bundle.InvocationImage{
BaseImage: bundle.BaseImage{Image: img},
},
}

t.Run("defaults", func(t *testing.T) {
d := &Driver{}

err := d.setConfigurationOptions(op)
require.NoError(t, err)

cfg := d.containerCfg
wantCfg := container.Config{
Image: img,
AttachStdout: true,
AttachStderr: true,
Entrypoint: []string{"/cnab/app/run"},
}
assert.Equal(t, wantCfg, cfg)

hostCfg := d.containerHostCfg
assert.Equal(t, container.HostConfig{}, hostCfg)
})

t.Run("docker network", func(t *testing.T) {
net := "mynetwork"
d := &Driver{}
d.SetConfig(map[string]string{SettingNetwork: net})

err := d.setConfigurationOptions(op)
require.NoError(t, err)

hostCfg := d.containerHostCfg
assert.Equal(t, net, string(hostCfg.NetworkMode))
})
}

func TestDriver_SetConfig(t *testing.T) {
testcases := []struct {
name string
Expand Down

0 comments on commit 0b6d46d

Please sign in to comment.