Skip to content

Commit

Permalink
Improve Podman compatibility
Browse files Browse the repository at this point in the history
- allow configuring the default network name - it's podman for Podman instead of bridge and 'bridge' is not a valid network name with Podman
- allow configuring an alternative Docker socket path for the reaper - this is also useful for rootless Docker
  • Loading branch information
prskr committed Mar 8, 2022
1 parent fea599d commit 1583528
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 35 deletions.
3 changes: 2 additions & 1 deletion container.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ type (

// GenericProviderOptions defines options applicable to all providers
GenericProviderOptions struct {
Logger Logging
Logger Logging
DefaultNetwork string
}

// GenericProviderOption defines a common interface to modify GenericProviderOptions
Expand Down
52 changes: 34 additions & 18 deletions docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,10 @@ func (c *DockerContainer) NetworkAliases(ctx context.Context) (map[string][]stri
func (c *DockerContainer) Exec(ctx context.Context, cmd []string) (int, error) {
cli := c.provider.client
response, err := cli.ContainerExecCreate(ctx, c.ID, types.ExecConfig{
Cmd: cmd,
Detach: false,
Cmd: cmd,
Detach: false,
AttachStderr: true,
AttachStdout: true,
})
if err != nil {
return 0, err
Expand Down Expand Up @@ -527,9 +529,9 @@ func (n *DockerNetwork) Remove(ctx context.Context) error {
// DockerProvider implements the ContainerProvider interface
type DockerProvider struct {
*DockerProviderOptions
client *client.Client
hostCache string
defaultNetwork string // default container network
client *client.Client
host string
hostCache string
}

var _ ContainerProvider = (*DockerProvider)(nil)
Expand Down Expand Up @@ -604,6 +606,10 @@ func NewDockerProvider(provOpts ...DockerProviderOption) (*DockerProvider, error

opts = append(opts, client.WithTLSClientConfig(cacertPath, certPath, keyPath))
}
} else if dockerHostEnv := os.Getenv("DOCKER_HOST"); dockerHostEnv != "" {
host = dockerHostEnv
} else {
host = "unix:///var/run/docker.sock"
}

c, err := client.NewClientWithOpts(opts...)
Expand All @@ -623,6 +629,7 @@ func NewDockerProvider(provOpts ...DockerProviderOption) (*DockerProvider, error
c.NegotiateAPIVersion(context.Background())
p := &DockerProvider{
DockerProviderOptions: o,
host: host,
client: c,
}

Expand Down Expand Up @@ -705,24 +712,26 @@ func (p *DockerProvider) CreateContainer(ctx context.Context, req ContainerReque

// Make sure that bridge network exists
// In case it is disabled we will create reaper_default network
p.defaultNetwork, err = getDefaultNetwork(ctx, p.client)
if err != nil {
return nil, err
if p.DefaultNetwork == "" {
p.DefaultNetwork, err = getDefaultNetwork(ctx, p.client)
if err != nil {
return nil, err
}
}

// If default network is not bridge make sure it is attached to the request
// as container won't be attached to it automatically
if p.defaultNetwork != Bridge {
if p.DefaultNetwork != Bridge {
isAttached := false
for _, net := range req.Networks {
if net == p.defaultNetwork {
if net == p.DefaultNetwork {
isAttached = true
break
}
}

if !isAttached {
req.Networks = append(req.Networks, p.defaultNetwork)
req.Networks = append(req.Networks, p.DefaultNetwork)
}
}

Expand All @@ -744,7 +753,7 @@ func (p *DockerProvider) CreateContainer(ctx context.Context, req ContainerReque

var termSignal chan bool
if !req.SkipReaper {
r, err := NewReaper(ctx, sessionID.String(), p, req.ReaperImage)
r, err := NewReaper(context.WithValue(ctx, dockerHostContextKey, p.host), sessionID.String(), p, req.ReaperImage)
if err != nil {
return nil, fmt.Errorf("%w: creating reaper failed", err)
}
Expand Down Expand Up @@ -828,6 +837,9 @@ func (p *DockerProvider) CreateContainer(ctx context.Context, req ContainerReque

// prepare mounts
mounts := mapToDockerMounts(req.Mounts)
if req.NetworkMode == "" {
req.NetworkMode = Bridge
}

hostConfig := &container.HostConfig{
PortBindings: exposedPortMap,
Expand Down Expand Up @@ -1001,7 +1013,11 @@ func (p *DockerProvider) CreateNetwork(ctx context.Context, req NetworkRequest)

// Make sure that bridge network exists
// In case it is disabled we will create reaper_default network
p.defaultNetwork, err = getDefaultNetwork(ctx, p.client)
if p.DefaultNetwork == "" {
if p.DefaultNetwork, err = getDefaultNetwork(ctx, p.client); err != nil {
return nil, err
}
}

if req.Labels == nil {
req.Labels = make(map[string]string)
Expand All @@ -1020,7 +1036,7 @@ func (p *DockerProvider) CreateNetwork(ctx context.Context, req NetworkRequest)

var termSignal chan bool
if !req.SkipReaper {
r, err := NewReaper(ctx, sessionID.String(), p, req.ReaperImage)
r, err := NewReaper(context.WithValue(ctx, dockerHostContextKey, p.host), sessionID.String(), p, req.ReaperImage)
if err != nil {
return nil, fmt.Errorf("%w: creating network reaper failed", err)
}
Expand Down Expand Up @@ -1065,14 +1081,14 @@ func (p *DockerProvider) GetNetwork(ctx context.Context, req NetworkRequest) (ty

func (p *DockerProvider) GetGatewayIP(ctx context.Context) (string, error) {
// Use a default network as defined in the DockerProvider
if p.defaultNetwork == "" {
if p.DefaultNetwork == "" {
var err error
p.defaultNetwork, err = getDefaultNetwork(ctx, p.client)
p.DefaultNetwork, err = getDefaultNetwork(ctx, p.client)
if err != nil {
return "", err
}
}
nw, err := p.GetNetwork(ctx, NetworkRequest{Name: p.defaultNetwork})
nw, err := p.GetNetwork(ctx, NetworkRequest{Name: p.DefaultNetwork})
if err != nil {
return "", err
}
Expand Down Expand Up @@ -1112,7 +1128,7 @@ func getDefaultGatewayIP() (string, error) {
if len(ip) == 0 {
return "", errors.New("Failed to parse default gateway IP")
}
return string(ip), nil
return ip, nil
}

func getDefaultNetwork(ctx context.Context, cli *client.Client) (string, error) {
Expand Down
Loading

0 comments on commit 1583528

Please sign in to comment.