-
Notifications
You must be signed in to change notification settings - Fork 463
/
containers.go
117 lines (97 loc) · 4.02 KB
/
containers.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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package cri
import (
"context"
"fmt"
runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
)
// CreateContainer creates a new container in the specified PodSandbox.
func (c *Client) CreateContainer(ctx context.Context, podSandBoxID string, config *runtimeapi.ContainerConfig, sandboxConfig *runtimeapi.PodSandboxConfig) (string, error) {
resp, err := c.runtimeClient.CreateContainer(ctx, &runtimeapi.CreateContainerRequest{
PodSandboxId: podSandBoxID,
Config: config,
SandboxConfig: sandboxConfig,
})
if err != nil {
return "", fmt.Errorf("CreateContainer in sandbox %q from runtime service failed: %w", podSandBoxID, err)
}
if resp.ContainerId == "" {
return "", fmt.Errorf("ContainerId is not set for container %q", config.GetMetadata())
}
return resp.ContainerId, nil
}
// StartContainer starts the container.
func (c *Client) StartContainer(ctx context.Context, containerID string) error {
_, err := c.runtimeClient.StartContainer(ctx, &runtimeapi.StartContainerRequest{
ContainerId: containerID,
})
if err != nil {
return fmt.Errorf("StartContainer %q from runtime service failed: %w", containerID, err)
}
return nil
}
// StopContainer stops a running container with a grace period (i.e., timeout).
func (c *Client) StopContainer(ctx context.Context, containerID string, timeout int64) error {
_, err := c.runtimeClient.StopContainer(ctx, &runtimeapi.StopContainerRequest{
ContainerId: containerID,
Timeout: timeout,
})
if err != nil {
return fmt.Errorf("StopContainer %q from runtime service failed: %w", containerID, err)
}
return nil
}
// RemoveContainer removes the container. If the container is running, the container
// should be forced to removal.
func (c *Client) RemoveContainer(ctx context.Context, containerID string) error {
_, err := c.runtimeClient.RemoveContainer(ctx, &runtimeapi.RemoveContainerRequest{
ContainerId: containerID,
})
if err != nil {
return fmt.Errorf("RemoveContainer %q from runtime service failed: %w", containerID, err)
}
return nil
}
// ListContainers lists containers by filters.
func (c *Client) ListContainers(ctx context.Context, filter *runtimeapi.ContainerFilter) ([]*runtimeapi.Container, error) {
resp, err := c.runtimeClient.ListContainers(ctx, &runtimeapi.ListContainersRequest{
Filter: filter,
})
if err != nil {
return nil, fmt.Errorf("ListContainers with filter %+v from runtime service failed: %w", filter, err)
}
return resp.Containers, nil
}
// ContainerStatus returns the container status.
func (c *Client) ContainerStatus(ctx context.Context, containerID string, verbose bool) (*runtimeapi.ContainerStatus, map[string]string, error) {
resp, err := c.runtimeClient.ContainerStatus(ctx, &runtimeapi.ContainerStatusRequest{
ContainerId: containerID,
Verbose: verbose,
})
if err != nil {
return nil, nil, fmt.Errorf("ContainerStatus %q from runtime service failed: %w", containerID, err)
}
return resp.Status, resp.Info, nil
}
// ContainerStats returns the stats of the container.
func (c *Client) ContainerStats(ctx context.Context, containerID string) (*runtimeapi.ContainerStats, error) {
resp, err := c.runtimeClient.ContainerStats(ctx, &runtimeapi.ContainerStatsRequest{
ContainerId: containerID,
})
if err != nil {
return nil, fmt.Errorf("ContainerStatus %q from runtime service failed: %w", containerID, err)
}
return resp.GetStats(), nil
}
// ListContainerStats returns stats for all the containers matching the filter.
func (c *Client) ListContainerStats(ctx context.Context, filter *runtimeapi.ContainerStatsFilter) ([]*runtimeapi.ContainerStats, error) {
resp, err := c.runtimeClient.ListContainerStats(ctx, &runtimeapi.ListContainerStatsRequest{
Filter: filter,
})
if err != nil {
return nil, fmt.Errorf("ListContainerStats with filter %+v from runtime service failed: %w", filter, err)
}
return resp.GetStats(), nil
}