@@ -25,6 +25,12 @@ import (
25
25
"github.com/cnabio/cnab-go/driver"
26
26
)
27
27
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
+
28
34
// Driver is capable of running Docker invocation images using Docker itself.
29
35
type Driver struct {
30
36
config map [string ]string
@@ -93,6 +99,7 @@ func (d *Driver) Config() map[string]string {
93
99
"DOCKER_DRIVER_QUIET" : "Make the Docker driver quiet (only print container stdout/stderr)" ,
94
100
"OUTPUTS_MOUNT_PATH" : "Absolute path to where Docker driver can create temporary directories to bundle outputs. Defaults to temp dir." ,
95
101
"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" ,
96
103
}
97
104
}
98
105
@@ -199,21 +206,7 @@ func (d *Driver) exec(op *driver.Operation) (driver.OperationResult, error) {
199
206
return driver.OperationResult {}, errors .Wrap (err , "image digest validation failed" )
200
207
}
201
208
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 {
217
210
return driver.OperationResult {}, err
218
211
}
219
212
@@ -297,7 +290,7 @@ func (d *Driver) exec(op *driver.Operation) (driver.OperationResult, error) {
297
290
return opResult , err
298
291
}
299
292
300
- // ApplyConfigurationOptions applies the configuration options set on the driver
293
+ // ApplyConfigurationOptions applies the configuration options set on the driver by the user.
301
294
func (d * Driver ) ApplyConfigurationOptions () error {
302
295
for _ , opt := range d .dockerConfigurationOptions {
303
296
if err := opt (& d .containerCfg , & d .containerHostCfg ); err != nil {
@@ -307,6 +300,35 @@ func (d *Driver) ApplyConfigurationOptions() error {
307
300
return nil
308
301
}
309
302
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
+
310
332
func containerError (containerMessage string , containerErr , fetchErr error ) error {
311
333
if fetchErr != nil {
312
334
return fmt .Errorf ("%s: %v. fetching outputs failed: %s" , containerMessage , containerErr , fetchErr )
0 commit comments