Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ref(docker.go): container config fields on Driver no longer pointers #206

Merged
merged 4 commits into from
May 2, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 14 additions & 35 deletions driver/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package docker
import (
"archive/tar"
"context"
"errors"
"fmt"
"io"
"io/ioutil"
Expand All @@ -21,7 +20,6 @@ import (
"github.com/docker/docker/pkg/jsonmessage"
"github.com/docker/docker/pkg/stdcopy"
"github.com/docker/docker/registry"
copystructure "github.com/mitchellh/copystructure"
)

// Driver is capable of running Docker invocation images using Docker itself.
Expand All @@ -33,8 +31,8 @@ type Driver struct {
dockerConfigurationOptions []ConfigurationOption
containerOut io.Writer
containerErr io.Writer
containerHostCfg *container.HostConfig
containerCfg *container.Config
containerHostCfg container.HostConfig
containerCfg container.Config
}

// Run executes the Docker driver
Expand All @@ -54,34 +52,14 @@ func (d *Driver) AddConfigurationOptions(opts ...ConfigurationOption) {

// GetContainerConfig returns a copy of the container configuration
// used by the driver during container exec
func (d *Driver) GetContainerConfig() (container.Config, error) {
cpy, err := copystructure.Copy(*d.containerCfg)
if err != nil {
return container.Config{}, err
}

containerCfg, ok := cpy.(container.Config)
if !ok {
return container.Config{}, errors.New("unable to process container config")
}

return containerCfg, nil
func (d *Driver) GetContainerConfig() container.Config {
return d.containerCfg
}

// GetContainerHostConfig returns a copy of the container host configuration
// used by the driver during container exec
func (d *Driver) GetContainerHostConfig() (container.HostConfig, error) {
cpy, err := copystructure.Copy(*d.containerHostCfg)
if err != nil {
return container.HostConfig{}, err
}

hostCfg, ok := cpy.(container.HostConfig)
if !ok {
return container.HostConfig{}, errors.New("unable to process container host config")
}

return hostCfg, nil
func (d *Driver) GetContainerHostConfig() container.HostConfig {
return d.containerHostCfg
vdice marked this conversation as resolved.
Show resolved Hide resolved
}

// Config returns the Docker driver configuration options
Expand Down Expand Up @@ -191,27 +169,27 @@ func (d *Driver) exec(op *driver.Operation) (driver.OperationResult, error) {
env = append(env, fmt.Sprintf("%s=%v", k, v))
}

d.containerCfg = &container.Config{
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 {
d.containerHostCfg = container.HostConfig{}
if err := d.ApplyConfigurationOptions(); err != nil {
return driver.OperationResult{}, err
}

resp, err := cli.Client().ContainerCreate(ctx, d.containerCfg, d.containerHostCfg, nil, "")
resp, err := cli.Client().ContainerCreate(ctx, &d.containerCfg, &d.containerHostCfg, nil, "")
switch {
case client.IsErrNotFound(err):
fmt.Fprintf(cli.Err(), "Unable to find image '%s' locally\n", op.Image.Image)
if err := pullImage(ctx, cli, op.Image.Image); err != nil {
return driver.OperationResult{}, err
}
if resp, err = cli.Client().ContainerCreate(ctx, d.containerCfg, d.containerHostCfg, nil, ""); err != nil {
if resp, err = cli.Client().ContainerCreate(ctx, &d.containerCfg, &d.containerHostCfg, nil, ""); err != nil {
return driver.OperationResult{}, fmt.Errorf("cannot create container: %v", err)
}
case err != nil:
Expand Down Expand Up @@ -293,9 +271,10 @@ func (d *Driver) exec(op *driver.Operation) (driver.OperationResult, error) {
return opResult, err
}

func (d *Driver) applyConfigurationOptions() error {
// ApplyConfigurationOptions applies the configuration options set on the driver
func (d *Driver) ApplyConfigurationOptions() error {
silvin-lubecki marked this conversation as resolved.
Show resolved Hide resolved
for _, opt := range d.dockerConfigurationOptions {
if err := opt(d.containerCfg, d.containerHostCfg); err != nil {
if err := opt(&d.containerCfg, &d.containerHostCfg); err != nil {
return err
}
}
Expand Down
23 changes: 7 additions & 16 deletions driver/docker/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,25 @@ func TestDriver_GetConfigurationOptions(t *testing.T) {
is.NotNil(d)
is.True(d.Handles(driver.ImageTypeDocker))

t.Run("no configuration options", func(t *testing.T) {
d.containerCfg = &container.Config{}
d.containerHostCfg = &container.HostConfig{}

err := d.applyConfigurationOptions()
t.Run("empty configuration options", func(t *testing.T) {
err := d.ApplyConfigurationOptions()
is.NoError(err)

cfg, err := d.GetContainerConfig()
is.NoError(err)
cfg := d.GetContainerConfig()
is.Equal(container.Config{}, cfg)

hostCfg, err := d.GetContainerHostConfig()
is.NoError(err)
hostCfg := d.GetContainerHostConfig()
is.Equal(container.HostConfig{}, hostCfg)
})

t.Run("configuration options", func(t *testing.T) {
d.containerCfg = &container.Config{}
d.containerHostCfg = &container.HostConfig{}
d.AddConfigurationOptions(func(cfg *container.Config, hostCfg *container.HostConfig) error {
cfg.User = "cnabby"
hostCfg.Privileged = true
return nil
})

err := d.applyConfigurationOptions()
err := d.ApplyConfigurationOptions()
is.NoError(err)

expectedContainerCfg := container.Config{
Expand All @@ -49,12 +42,10 @@ func TestDriver_GetConfigurationOptions(t *testing.T) {
Privileged: true,
}

cfg, err := d.GetContainerConfig()
is.NoError(err)
cfg := d.GetContainerConfig()
is.Equal(expectedContainerCfg, cfg)

hostCfg, err := d.GetContainerHostConfig()
is.NoError(err)
hostCfg := d.GetContainerHostConfig()
is.Equal(expectedHostCfg, hostCfg)
})
}