Skip to content

Commit 0b6d46d

Browse files
authored
Merge pull request #265 from carolynvs/docker-network
Support DOCKER_NETWORK driver variable
2 parents fdc0505 + 020cc2a commit 0b6d46d

File tree

2 files changed

+78
-16
lines changed

2 files changed

+78
-16
lines changed

driver/docker/docker.go

+38-16
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ import (
2525
"github.com/cnabio/cnab-go/driver"
2626
)
2727

28+
const (
29+
// SettingNetwork is the environment variable for the driver that specifies
30+
// the docker network to which the invocation image should be attached.
31+
SettingNetwork = "DOCKER_NETWORK"
32+
)
33+
2834
// Driver is capable of running Docker invocation images using Docker itself.
2935
type Driver struct {
3036
config map[string]string
@@ -93,6 +99,7 @@ func (d *Driver) Config() map[string]string {
9399
"DOCKER_DRIVER_QUIET": "Make the Docker driver quiet (only print container stdout/stderr)",
94100
"OUTPUTS_MOUNT_PATH": "Absolute path to where Docker driver can create temporary directories to bundle outputs. Defaults to temp dir.",
95101
"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.",
102+
SettingNetwork: "Attach the invocation image to the specified docker network",
96103
}
97104
}
98105

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

202-
var env []string
203-
for k, v := range op.Environment {
204-
env = append(env, fmt.Sprintf("%s=%v", k, v))
205-
}
206-
207-
d.containerCfg = container.Config{
208-
Image: op.Image.Image,
209-
Env: env,
210-
Entrypoint: strslice.StrSlice{"/cnab/app/run"},
211-
AttachStderr: true,
212-
AttachStdout: true,
213-
}
214-
215-
d.containerHostCfg = container.HostConfig{}
216-
if err := d.ApplyConfigurationOptions(); err != nil {
209+
if err := d.setConfigurationOptions(op); err != nil {
217210
return driver.OperationResult{}, err
218211
}
219212

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

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

303+
// setConfigurationOptions initializes the container and host configuration options on the driver,
304+
// combining the default configuration with any overrides set by the user.
305+
func (d *Driver) setConfigurationOptions(op *driver.Operation) error {
306+
var env []string
307+
for k, v := range op.Environment {
308+
env = append(env, fmt.Sprintf("%s=%v", k, v))
309+
}
310+
311+
d.containerCfg = container.Config{
312+
Image: op.Image.Image,
313+
Env: env,
314+
Entrypoint: strslice.StrSlice{"/cnab/app/run"},
315+
AttachStderr: true,
316+
AttachStdout: true,
317+
}
318+
319+
d.containerHostCfg = container.HostConfig{}
320+
321+
if network, ok := d.config[SettingNetwork]; ok {
322+
d.containerHostCfg.NetworkMode = container.NetworkMode(network)
323+
}
324+
325+
if err := d.ApplyConfigurationOptions(); err != nil {
326+
return err
327+
}
328+
329+
return nil
330+
}
331+
310332
func containerError(containerMessage string, containerErr, fetchErr error) error {
311333
if fetchErr != nil {
312334
return fmt.Errorf("%s: %v. fetching outputs failed: %s", containerMessage, containerErr, fetchErr)

driver/docker/docker_test.go

+40
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,46 @@ func TestDriver_GetConfigurationOptions(t *testing.T) {
8787
})
8888
}
8989

90+
func TestDriver_setConfigurationOptions(t *testing.T) {
91+
img := "example.com/myimage"
92+
op := &driver.Operation{
93+
Image: bundle.InvocationImage{
94+
BaseImage: bundle.BaseImage{Image: img},
95+
},
96+
}
97+
98+
t.Run("defaults", func(t *testing.T) {
99+
d := &Driver{}
100+
101+
err := d.setConfigurationOptions(op)
102+
require.NoError(t, err)
103+
104+
cfg := d.containerCfg
105+
wantCfg := container.Config{
106+
Image: img,
107+
AttachStdout: true,
108+
AttachStderr: true,
109+
Entrypoint: []string{"/cnab/app/run"},
110+
}
111+
assert.Equal(t, wantCfg, cfg)
112+
113+
hostCfg := d.containerHostCfg
114+
assert.Equal(t, container.HostConfig{}, hostCfg)
115+
})
116+
117+
t.Run("docker network", func(t *testing.T) {
118+
net := "mynetwork"
119+
d := &Driver{}
120+
d.SetConfig(map[string]string{SettingNetwork: net})
121+
122+
err := d.setConfigurationOptions(op)
123+
require.NoError(t, err)
124+
125+
hostCfg := d.containerHostCfg
126+
assert.Equal(t, net, string(hostCfg.NetworkMode))
127+
})
128+
}
129+
90130
func TestDriver_SetConfig(t *testing.T) {
91131
testcases := []struct {
92132
name string

0 commit comments

Comments
 (0)