-
Notifications
You must be signed in to change notification settings - Fork 4
/
container_stop.go
63 lines (53 loc) · 1.8 KB
/
container_stop.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
package docker
import (
"context"
"time"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"k8s.io/klog/v2"
"github.com/openconfig/containerz/containers"
)
// ContainerStop stops a container. If the Force option is set and a timeout
// is specified in the context, the contains if forcefully terminated after that timeout.
// If the Force option is set but no timeout is provided the container's StopTimeout
// value is used, if set, otherwise the engine default.
// If the Force option is not set, no forceful termination is performed.
func (m *Manager) ContainerStop(ctx context.Context, instance string, opts ...options.Option) error {
optionz := options.ApplyOptions(opts...)
cnts, err := m.client.ContainerList(ctx, types.ContainerListOptions{
// TODO(alshabib): consider filtering for the image we care about
})
if err != nil {
return err
}
// check if the container exists.
if err := checkExistingInstanceAndPorts(instance, nil, cnts); err == nil {
return status.Errorf(codes.NotFound, "container %s was not found", instance)
}
// a negative timeout indicates to docker that no forceful termination should
// occur.
duration := -1
if optionz.Force {
// compute duration from context deadline
duration = 0
timeoutTime, ok := ctx.Deadline()
if ok {
duration = int(timeoutTime.Sub(time.Now()).Seconds())
}
}
pDuration := &duration
if duration == 0 {
pDuration = nil
}
if err := m.client.ContainerStop(ctx, instance, container.StopOptions{Timeout: pDuration}); err != nil {
klog.Warningf("container %s was not running", instance)
}
if err := m.client.ContainerRemove(ctx, instance, types.ContainerRemoveOptions{
Force: optionz.Force,
}); err != nil {
return err
}
return nil
}