forked from virtual-kubelet/cri
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
286 lines (254 loc) · 8.42 KB
/
client.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
package cri
import (
"fmt"
"github.com/virtual-kubelet/node-cli/manager"
"github.com/virtual-kubelet/virtual-kubelet/errdefs"
"github.com/virtual-kubelet/virtual-kubelet/log"
"github.com/virtual-kubelet/virtual-kubelet/trace"
"golang.org/x/net/context"
v1 "k8s.io/api/core/v1"
criapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
)
// Call RunPodSandbox on the CRI client
func runPodSandbox(ctx context.Context, client criapi.RuntimeServiceClient, config *criapi.PodSandboxConfig) (string, error) {
ctx, span := trace.StartSpan(ctx, "cri.getPodSandboxes")
defer span.End()
request := &criapi.RunPodSandboxRequest{Config: config}
log.G(ctx).Debug("RunPodSandboxRequest")
r, err := client.RunPodSandbox(context.Background(), request)
log.G(ctx).Debug("RunPodSandboxResponse")
if err != nil {
span.SetStatus(err)
return "", err
}
log.G(ctx).Debug("New pod sandbox created")
return r.PodSandboxId, nil
}
// Call StopPodSandbox on the CRI client
func stopPodSandbox(ctx context.Context, client criapi.RuntimeServiceClient, id string) error {
ctx, span := trace.StartSpan(ctx, "cri.getPodSandboxes")
defer span.End()
if id == "" {
err := errdefs.InvalidInput("ID cannot be empty")
span.SetStatus(err)
return err
}
request := &criapi.StopPodSandboxRequest{PodSandboxId: id}
log.G(ctx).Debug("StopPodSandboxRequest")
_, err := client.StopPodSandbox(context.Background(), request)
log.G(ctx).Debug("StopPodSandboxResponse")
if err != nil {
span.SetStatus(err)
return err
}
log.G(ctx).Debugf("Stopped sandbox %s", id)
return nil
}
// Call RemovePodSandbox on the CRI client
func removePodSandbox(ctx context.Context, client criapi.RuntimeServiceClient, id string) error {
ctx, span := trace.StartSpan(ctx, "cri.getPodSandboxes")
defer span.End()
if id == "" {
err := errdefs.InvalidInput("ID cannot be empty")
span.SetStatus(err)
return err
}
request := &criapi.RemovePodSandboxRequest{PodSandboxId: id}
log.G(ctx).Debug("RemovePodSandboxRequest")
_, err := client.RemovePodSandbox(context.Background(), request)
log.G(ctx).Debug("RemovePodSandboxResponse")
if err != nil {
span.SetStatus(err)
return err
}
log.G(ctx).Debugf("Removed sandbox %s", id)
return nil
}
// Call ListPodSandbox on the CRI client
func getPodSandboxes(ctx context.Context, client criapi.RuntimeServiceClient) ([]*criapi.PodSandbox, error) {
ctx, span := trace.StartSpan(ctx, "cri.getPodSandboxes")
defer span.End()
filter := &criapi.PodSandboxFilter{}
request := &criapi.ListPodSandboxRequest{
Filter: filter,
}
log.G(ctx).Debug("ListPodSandboxRequest")
r, err := client.ListPodSandbox(context.Background(), request)
log.G(ctx).Debug("ListPodSandboxResponse")
if err != nil {
span.SetStatus(err)
return nil, err
}
return r.GetItems(), err
}
// Call PodSandboxStatus on the CRI client
func getPodSandboxStatus(ctx context.Context, client criapi.RuntimeServiceClient, psId string) (*criapi.PodSandboxStatus, error) {
ctx, span := trace.StartSpan(ctx, "cri.getPodSandboxStatus")
defer span.End()
if psId == "" {
err := errdefs.InvalidInput("Pod ID cannot be empty in GPSS")
span.SetStatus(err)
return nil, err
}
request := &criapi.PodSandboxStatusRequest{
PodSandboxId: psId,
Verbose: false,
}
log.G(ctx).Debug("PodSandboxStatusRequest")
r, err := client.PodSandboxStatus(context.Background(), request)
log.G(ctx).Debug("PodSandboxStatusResponse")
if err != nil {
span.SetStatus(err)
return nil, err
}
return r.Status, nil
}
// Call CreateContainer on the CRI client
func createContainer(ctx context.Context, client criapi.RuntimeServiceClient, config *criapi.ContainerConfig, podConfig *criapi.PodSandboxConfig, pId string) (string, error) {
ctx, span := trace.StartSpan(ctx, "cri.createContainer")
defer span.End()
request := &criapi.CreateContainerRequest{
PodSandboxId: pId,
Config: config,
SandboxConfig: podConfig,
}
log.G(ctx).Debug("CreateContainerRequest")
r, err := client.CreateContainer(context.Background(), request)
log.G(ctx).Debug("CreateContainerResponse")
if err != nil {
span.SetStatus(err)
return "", err
}
log.G(ctx).Debugf("Container created: %s", r.ContainerId)
return r.ContainerId, nil
}
// Call StartContainer on the CRI client
func startContainer(ctx context.Context, client criapi.RuntimeServiceClient, cId string) error {
ctx, span := trace.StartSpan(ctx, "cri.startContainer")
defer span.End()
if cId == "" {
err := errdefs.InvalidInput("ID cannot be empty")
span.SetStatus(err)
return err
}
request := &criapi.StartContainerRequest{
ContainerId: cId,
}
log.G(ctx).Debug("StartContainerRequestv")
_, err := client.StartContainer(context.Background(), request)
log.G(ctx).Debug("StartContainerResponse")
if err != nil {
span.SetStatus(err)
return err
}
log.G(ctx).Debugf("Container started: %s", cId)
return nil
}
// Call ContainerStatus on the CRI client
func getContainerCRIStatus(ctx context.Context, client criapi.RuntimeServiceClient, cId string) (*criapi.ContainerStatus, error) {
ctx, span := trace.StartSpan(ctx, "cri.getContainerCRIStatus")
defer span.End()
if cId == "" {
err := errdefs.InvalidInput("Container ID cannot be empty in GCCS")
span.SetStatus(err)
return nil, err
}
request := &criapi.ContainerStatusRequest{
ContainerId: cId,
Verbose: false,
}
log.G(ctx).Debug("ContainerStatusRequest")
r, err := client.ContainerStatus(context.Background(), request)
log.G(ctx).Debug("ContainerStatusResponsev")
if err != nil {
span.SetStatus(err)
return nil, err
}
return r.Status, nil
}
// Call ListContainers on the CRI client
func getContainersForSandbox(ctx context.Context, client criapi.RuntimeServiceClient, psId string) ([]*criapi.Container, error) {
ctx, span := trace.StartSpan(ctx, "cri.getContainersForSandbox")
defer span.End()
filter := &criapi.ContainerFilter{}
filter.PodSandboxId = psId
request := &criapi.ListContainersRequest{
Filter: filter,
}
log.G(ctx).Debug("ListContainerRequest")
r, err := client.ListContainers(context.Background(), request)
log.G(ctx).Debug("ListContainerResponse")
if err != nil {
span.SetStatus(err)
return nil, err
}
return r.Containers, nil
}
// Pull and image on the CRI client and return the image ref
func pullImage(ctx context.Context, client criapi.ImageServiceClient, image string) (string, error) {
ctx, span := trace.StartSpan(ctx, "cri.pullImage")
defer span.End()
request := &criapi.PullImageRequest{
Image: &criapi.ImageSpec{
Image: image,
},
}
log.G(ctx).Debug("PullImageRequest")
r, err := client.PullImage(context.Background(), request)
log.G(ctx).Debug("PullImageResponse")
if err != nil {
span.SetStatus(err)
return "", err
}
return r.ImageRef, nil
}
// Generate the CRI ContainerConfig from the Pod and container specs
// TODO: Probably incomplete
func generateContainerConfig(ctx context.Context, container *v1.Container, pod *v1.Pod, imageRef, podVolRoot string, rm *manager.ResourceManager, attempt uint32) (*criapi.ContainerConfig, error) {
// TODO: Probably incomplete
config := &criapi.ContainerConfig{
Metadata: &criapi.ContainerMetadata{
Name: container.Name,
Attempt: attempt,
},
Image: &criapi.ImageSpec{Image: imageRef},
Command: container.Command,
Args: container.Args,
WorkingDir: container.WorkingDir,
Envs: createCtrEnvVars(container.Env),
Labels: createCtrLabels(container, pod),
Annotations: createCtrAnnotations(container, pod),
Linux: createCtrLinuxConfig(container, pod),
LogPath: fmt.Sprintf("%s-%d.log", container.Name, attempt),
Stdin: container.Stdin,
StdinOnce: container.StdinOnce,
Tty: container.TTY,
}
mounts, err := createCtrMounts(ctx, container, pod, podVolRoot, rm)
if err != nil {
return nil, err
}
config.Mounts = mounts
return config, nil
}
// Greate CRI PodSandboxConfig from the Pod spec
// TODO: This is probably incomplete
func generatePodSandboxConfig(ctx context.Context, pod *v1.Pod, logDir string, attempt uint32) (*criapi.PodSandboxConfig, error) {
podUID := string(pod.UID)
config := &criapi.PodSandboxConfig{
Metadata: &criapi.PodSandboxMetadata{
Name: pod.Name,
Namespace: pod.Namespace,
Uid: podUID,
Attempt: attempt,
},
Labels: createPodLabels(pod),
Annotations: pod.Annotations,
LogDirectory: logDir,
DnsConfig: createPodDnsConfig(pod),
Hostname: createPodHostname(pod),
PortMappings: createPortMappings(pod),
Linux: createPodSandboxLinuxConfig(pod),
}
return config, nil
}