forked from strangelove-ventures/interchaintest
/
container_lifecycle.go
149 lines (127 loc) · 3.43 KB
/
container_lifecycle.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package dockerutil
import (
"context"
"fmt"
"net"
"strings"
"github.com/Moonyongjung/interchaintest/v4/ibc"
dockertypes "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/network"
dockerclient "github.com/docker/docker/client"
"github.com/docker/docker/errdefs"
"github.com/docker/go-connections/nat"
"go.uber.org/zap"
)
type ContainerLifecycle struct {
log *zap.Logger
client *dockerclient.Client
containerName string
id string
preStartListeners Listeners
}
func NewContainerLifecycle(log *zap.Logger, client *dockerclient.Client, containerName string) *ContainerLifecycle {
return &ContainerLifecycle{
log: log,
client: client,
containerName: containerName,
}
}
func (c *ContainerLifecycle) CreateContainer(
ctx context.Context,
testName string,
networkID string,
image ibc.DockerImage,
ports nat.PortSet,
volumeBinds []string,
hostName string,
cmd []string,
) error {
imageRef := image.Ref()
c.log.Info(
"Will run command",
zap.String("image", imageRef),
zap.String("container", c.containerName),
zap.String("command", strings.Join(cmd, " ")),
)
pb, listeners, err := GeneratePortBindings(ports)
if err != nil {
return fmt.Errorf("failed to generate port bindings: %w", err)
}
c.preStartListeners = listeners
cc, err := c.client.ContainerCreate(
ctx,
&container.Config{
Image: imageRef,
Entrypoint: []string{},
Cmd: cmd,
Hostname: hostName,
Labels: map[string]string{CleanupLabel: testName},
ExposedPorts: ports,
},
&container.HostConfig{
Binds: volumeBinds,
PortBindings: pb,
PublishAllPorts: true,
AutoRemove: false,
DNS: []string{},
},
&network.NetworkingConfig{
EndpointsConfig: map[string]*network.EndpointSettings{
networkID: {},
},
},
nil,
c.containerName,
)
if err != nil {
listeners.CloseAll()
c.preStartListeners = []net.Listener{}
return err
}
c.id = cc.ID
return nil
}
func (c *ContainerLifecycle) StartContainer(ctx context.Context) error {
// lock port allocation for the time between freeing the ports from the
// temporary listeners to the consumption of the ports by the container
mu.RLock()
defer mu.RUnlock()
c.preStartListeners.CloseAll()
c.preStartListeners = []net.Listener{}
if err := StartContainer(ctx, c.client, c.id); err != nil {
return err
}
c.log.Info("Container started", zap.String("container", c.containerName))
return nil
}
func (c *ContainerLifecycle) StopContainer(ctx context.Context) error {
var timeout container.StopOptions
timeoutSec := 30
timeout.Timeout = &timeoutSec
return c.client.ContainerStop(ctx, c.id, timeout)
}
func (c *ContainerLifecycle) RemoveContainer(ctx context.Context) error {
err := c.client.ContainerRemove(ctx, c.id, dockertypes.ContainerRemoveOptions{
Force: true,
RemoveVolumes: true,
})
if err != nil && !errdefs.IsNotFound(err) {
return fmt.Errorf("remove container %s: %w", c.containerName, err)
}
return nil
}
func (c *ContainerLifecycle) ContainerID() string {
return c.id
}
func (c *ContainerLifecycle) GetHostPorts(ctx context.Context, portIDs ...string) ([]string, error) {
cjson, err := c.client.ContainerInspect(ctx, c.id)
if err != nil {
return nil, err
}
ports := make([]string, len(portIDs))
for i, p := range portIDs {
ports[i] = GetHostPort(cjson, p)
}
return ports, nil
}