-
Notifications
You must be signed in to change notification settings - Fork 51
/
cri_runtime_wrapper.go
334 lines (305 loc) · 12 KB
/
cri_runtime_wrapper.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package cri
import (
"context"
"fmt"
"time"
criruntimev1alpha2 "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
)
// NewCRIExtendedRuntimeServiceWrapper creates an ExtendedRuntimeService from a v1alpha2 runtime service client
// NOTE: the passed context is used for every subsequent call on the interface as the parent context with a timeout
// as passed through the argument. If the parent context gets canceled, this client becomes useless.
func NewCRIExtendedRuntimeServiceWrapper(ctx context.Context, timeout time.Duration, client criruntimev1alpha2.RuntimeServiceClient) (ExtendedRuntimeService, error) {
if client == nil {
return nil, fmt.Errorf("client cannot be nil")
}
if timeout == time.Duration(0) {
return nil, fmt.Errorf("timeout cannot be 0")
}
return &extendedServiceRuntimeWrapper{
ctx: ctx,
timeout: timeout,
rs: client,
}, nil
}
type extendedServiceRuntimeWrapper struct {
// well, this is stupid:
// the criapi.RuntimeService should take a context as first argument everywhere
// as it doesn't, the only sensible way is to be able to pass it from here
// however, be careful with this: if that passed context gets canceled, nothing will work anymore
ctx context.Context
timeout time.Duration
rs criruntimev1alpha2.RuntimeServiceClient
}
// Version returns the runtime name, runtime version and runtime API version
func (w *extendedServiceRuntimeWrapper) Version(apiVersion string) (*criruntimev1alpha2.VersionResponse, error) {
ctx, cancel := context.WithTimeout(w.ctx, w.timeout)
defer cancel()
return w.rs.Version(ctx, &criruntimev1alpha2.VersionRequest{Version: apiVersion})
}
// CreateContainer creates a new container in specified PodSandbox.
func (w *extendedServiceRuntimeWrapper) CreateContainer(podSandboxID string, config *criruntimev1alpha2.ContainerConfig, sandboxConfig *criruntimev1alpha2.PodSandboxConfig) (string, error) {
ctx, cancel := context.WithTimeout(w.ctx, w.timeout)
defer cancel()
resp, err := w.rs.CreateContainer(ctx, &criruntimev1alpha2.CreateContainerRequest{
PodSandboxId: podSandboxID,
Config: config,
SandboxConfig: sandboxConfig,
})
if err != nil {
return "", err
}
return resp.GetContainerId(), nil
}
// StartContainer starts the container.
func (w *extendedServiceRuntimeWrapper) StartContainer(containerID string) error {
ctx, cancel := context.WithTimeout(w.ctx, w.timeout)
defer cancel()
_, err := w.rs.StartContainer(ctx, &criruntimev1alpha2.StartContainerRequest{
ContainerId: containerID,
})
return err
}
// StopContainer stops a running container with a grace period (i.e., timeout).
func (w *extendedServiceRuntimeWrapper) StopContainer(containerID string, timeout int64) error {
ctx, cancel := context.WithTimeout(w.ctx, w.timeout)
defer cancel()
_, err := w.rs.StopContainer(ctx, &criruntimev1alpha2.StopContainerRequest{
ContainerId: containerID,
Timeout: timeout,
})
return err
}
// RemoveContainer removes the container.
func (w *extendedServiceRuntimeWrapper) RemoveContainer(containerID string) error {
ctx, cancel := context.WithTimeout(w.ctx, w.timeout)
defer cancel()
_, err := w.rs.RemoveContainer(ctx, &criruntimev1alpha2.RemoveContainerRequest{
ContainerId: containerID,
})
return err
}
// ListContainers lists all containers by filters.
func (w *extendedServiceRuntimeWrapper) ListContainers(filter *criruntimev1alpha2.ContainerFilter) ([]*criruntimev1alpha2.Container, error) {
ctx, cancel := context.WithTimeout(w.ctx, w.timeout)
defer cancel()
resp, err := w.rs.ListContainers(ctx, &criruntimev1alpha2.ListContainersRequest{
Filter: filter,
})
if err != nil {
return nil, err
}
return resp.GetContainers(), nil
}
// ContainerStatus returns the status of the container.
func (w *extendedServiceRuntimeWrapper) ContainerStatus(containerID string) (*criruntimev1alpha2.ContainerStatus, error) {
ctx, cancel := context.WithTimeout(w.ctx, w.timeout)
defer cancel()
resp, err := w.rs.ContainerStatus(ctx, &criruntimev1alpha2.ContainerStatusRequest{
ContainerId: containerID,
Verbose: false,
})
if err != nil {
return nil, err
}
return resp.GetStatus(), nil
}
// ContainerStatusVerbose returns the status of the container.
func (w *extendedServiceRuntimeWrapper) ContainerStatusVerbose(containerID string) (*criruntimev1alpha2.ContainerStatus, map[string]string, error) {
ctx, cancel := context.WithTimeout(w.ctx, w.timeout)
defer cancel()
resp, err := w.rs.ContainerStatus(ctx, &criruntimev1alpha2.ContainerStatusRequest{
ContainerId: containerID,
Verbose: true,
})
if err != nil {
return nil, nil, err
}
return resp.GetStatus(), resp.GetInfo(), nil
}
// UpdateContainerResources updates the cgroup resources for the container.
func (w *extendedServiceRuntimeWrapper) UpdateContainerResources(containerID string, resources *criruntimev1alpha2.LinuxContainerResources) error {
ctx, cancel := context.WithTimeout(w.ctx, w.timeout)
defer cancel()
_, err := w.rs.UpdateContainerResources(ctx, &criruntimev1alpha2.UpdateContainerResourcesRequest{
ContainerId: containerID,
Linux: resources,
})
return err
}
// ExecSync executes a command in the container, and returns the stdout output.
// If command exits with a non-zero exit code, an error is returned.
func (w *extendedServiceRuntimeWrapper) ExecSync(containerID string, cmd []string, timeout time.Duration) (stdout []byte, stderr []byte, err error) {
ctx, cancel := context.WithTimeout(w.ctx, w.timeout)
defer cancel()
resp, err := w.rs.ExecSync(ctx, &criruntimev1alpha2.ExecSyncRequest{
ContainerId: containerID,
Cmd: cmd,
Timeout: int64(timeout.Seconds()),
})
if err != nil {
return nil, nil, err
}
if resp.GetExitCode() != int32(0) {
return resp.GetStdout(), resp.GetStderr(), fmt.Errorf("exit code: %d", resp.GetExitCode())
}
return resp.GetStdout(), resp.GetStderr(), nil
}
// Exec prepares a streaming endpoint to execute a command in the container, and returns the address.
func (w *extendedServiceRuntimeWrapper) Exec(req *criruntimev1alpha2.ExecRequest) (*criruntimev1alpha2.ExecResponse, error) {
ctx, cancel := context.WithTimeout(w.ctx, w.timeout)
defer cancel()
return w.rs.Exec(ctx, req)
}
// Attach prepares a streaming endpoint to attach to a running container, and returns the address.
func (w *extendedServiceRuntimeWrapper) Attach(req *criruntimev1alpha2.AttachRequest) (*criruntimev1alpha2.AttachResponse, error) {
ctx, cancel := context.WithTimeout(w.ctx, w.timeout)
defer cancel()
return w.rs.Attach(ctx, req)
}
// ReopenContainerLog asks runtime to reopen the stdout/stderr log file
// for the container. If it returns error, new container log file MUST NOT
// be created.
func (w *extendedServiceRuntimeWrapper) ReopenContainerLog(containerID string) error {
ctx, cancel := context.WithTimeout(w.ctx, w.timeout)
defer cancel()
_, err := w.rs.ReopenContainerLog(ctx, &criruntimev1alpha2.ReopenContainerLogRequest{
ContainerId: containerID,
})
return err
}
// RunPodSandbox creates and starts a pod-level sandbox. Runtimes should ensure
// the sandbox is in ready state.
func (w *extendedServiceRuntimeWrapper) RunPodSandbox(config *criruntimev1alpha2.PodSandboxConfig, runtimeHandler string) (string, error) {
ctx, cancel := context.WithTimeout(w.ctx, w.timeout)
defer cancel()
resp, err := w.rs.RunPodSandbox(ctx, &criruntimev1alpha2.RunPodSandboxRequest{
Config: config,
RuntimeHandler: runtimeHandler,
})
if err != nil {
return "", err
}
return resp.GetPodSandboxId(), nil
}
// StopPodSandbox stops the sandbox. If there are any running containers in the
// sandbox, they should be force terminated.
func (w *extendedServiceRuntimeWrapper) StopPodSandbox(podSandboxID string) error {
ctx, cancel := context.WithTimeout(w.ctx, w.timeout)
defer cancel()
_, err := w.rs.StopPodSandbox(ctx, &criruntimev1alpha2.StopPodSandboxRequest{
PodSandboxId: podSandboxID,
})
return err
}
// RemovePodSandbox removes the sandbox. If there are running containers in the
// sandbox, they should be forcibly removed.
func (w *extendedServiceRuntimeWrapper) RemovePodSandbox(podSandboxID string) error {
ctx, cancel := context.WithTimeout(w.ctx, w.timeout)
defer cancel()
_, err := w.rs.RemovePodSandbox(ctx, &criruntimev1alpha2.RemovePodSandboxRequest{
PodSandboxId: podSandboxID,
})
return err
}
// PodSandboxStatus returns the Status of the PodSandbox.
func (w *extendedServiceRuntimeWrapper) PodSandboxStatus(podSandboxID string) (*criruntimev1alpha2.PodSandboxStatus, error) {
ctx, cancel := context.WithTimeout(w.ctx, w.timeout)
defer cancel()
resp, err := w.rs.PodSandboxStatus(ctx, &criruntimev1alpha2.PodSandboxStatusRequest{
PodSandboxId: podSandboxID,
Verbose: false,
})
if err != nil {
return nil, err
}
return resp.GetStatus(), nil
}
// PodSandboxStatusVerbose returns the Status of the PodSandbox.
func (w *extendedServiceRuntimeWrapper) PodSandboxStatusVerbose(podSandboxID string) (*criruntimev1alpha2.PodSandboxStatus, map[string]string, error) {
ctx, cancel := context.WithTimeout(w.ctx, w.timeout)
defer cancel()
resp, err := w.rs.PodSandboxStatus(ctx, &criruntimev1alpha2.PodSandboxStatusRequest{
PodSandboxId: podSandboxID,
Verbose: true,
})
if err != nil {
return nil, nil, err
}
return resp.GetStatus(), resp.GetInfo(), nil
}
// ListPodSandbox returns a list of Sandbox.
func (w *extendedServiceRuntimeWrapper) ListPodSandbox(filter *criruntimev1alpha2.PodSandboxFilter) ([]*criruntimev1alpha2.PodSandbox, error) {
ctx, cancel := context.WithTimeout(w.ctx, w.timeout)
defer cancel()
resp, err := w.rs.ListPodSandbox(ctx, &criruntimev1alpha2.ListPodSandboxRequest{
Filter: filter,
})
if err != nil {
return nil, err
}
return resp.GetItems(), nil
}
// PortForward prepares a streaming endpoint to forward ports from a PodSandbox, and returns the address.
func (w *extendedServiceRuntimeWrapper) PortForward(req *criruntimev1alpha2.PortForwardRequest) (*criruntimev1alpha2.PortForwardResponse, error) {
ctx, cancel := context.WithTimeout(w.ctx, w.timeout)
defer cancel()
return w.rs.PortForward(ctx, req)
}
// ContainerStats returns stats of the container. If the container does not
// exist, the call returns an error.
func (w *extendedServiceRuntimeWrapper) ContainerStats(containerID string) (*criruntimev1alpha2.ContainerStats, error) {
ctx, cancel := context.WithTimeout(w.ctx, w.timeout)
defer cancel()
resp, err := w.rs.ContainerStats(ctx, &criruntimev1alpha2.ContainerStatsRequest{
ContainerId: containerID,
})
if err != nil {
return nil, err
}
return resp.GetStats(), nil
}
// ListContainerStats returns stats of all running containers.
func (w *extendedServiceRuntimeWrapper) ListContainerStats(filter *criruntimev1alpha2.ContainerStatsFilter) ([]*criruntimev1alpha2.ContainerStats, error) {
ctx, cancel := context.WithTimeout(w.ctx, w.timeout)
defer cancel()
resp, err := w.rs.ListContainerStats(ctx, &criruntimev1alpha2.ListContainerStatsRequest{
Filter: filter,
})
if err != nil {
return nil, err
}
return resp.GetStats(), nil
}
// UpdateRuntimeConfig updates runtime configuration if specified
func (w *extendedServiceRuntimeWrapper) UpdateRuntimeConfig(runtimeConfig *criruntimev1alpha2.RuntimeConfig) error {
ctx, cancel := context.WithTimeout(w.ctx, w.timeout)
defer cancel()
_, err := w.rs.UpdateRuntimeConfig(ctx, &criruntimev1alpha2.UpdateRuntimeConfigRequest{
RuntimeConfig: runtimeConfig,
})
return err
}
// Status returns the status of the runtime.
func (w *extendedServiceRuntimeWrapper) Status() (*criruntimev1alpha2.RuntimeStatus, error) {
ctx, cancel := context.WithTimeout(w.ctx, w.timeout)
defer cancel()
resp, err := w.rs.Status(ctx, &criruntimev1alpha2.StatusRequest{
Verbose: false,
})
if err != nil {
return nil, err
}
return resp.GetStatus(), nil
}
// Status returns the status of the runtime.
func (w *extendedServiceRuntimeWrapper) StatusVerbose() (*criruntimev1alpha2.RuntimeStatus, map[string]string, error) {
ctx, cancel := context.WithTimeout(w.ctx, w.timeout)
defer cancel()
resp, err := w.rs.Status(ctx, &criruntimev1alpha2.StatusRequest{
Verbose: true,
})
if err != nil {
return nil, nil, err
}
return resp.GetStatus(), resp.GetInfo(), nil
}