forked from elotl/virtual-kubelet
/
client.go
181 lines (160 loc) · 5.11 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
package cri
import (
"fmt"
log "github.com/sirupsen/logrus"
"golang.org/x/net/context"
criapi "k8s.io/kubernetes/pkg/kubelet/apis/cri/runtime/v1alpha2"
)
// Call RunPodSandbox on the CRI client
func runPodSandbox(client criapi.RuntimeServiceClient, config *criapi.PodSandboxConfig) (string, error) {
request := &criapi.RunPodSandboxRequest{Config: config}
log.Debugf("RunPodSandboxRequest: %v", request)
r, err := client.RunPodSandbox(context.Background(), request)
log.Debugf("RunPodSandboxResponse: %v", r)
if err != nil {
return "", err
}
log.Printf("New pod sandbox created: %v", r.PodSandboxId)
return r.PodSandboxId, nil
}
// Call StopPodSandbox on the CRI client
func stopPodSandbox(client criapi.RuntimeServiceClient, id string) error {
if id == "" {
return fmt.Errorf("ID cannot be empty")
}
request := &criapi.StopPodSandboxRequest{PodSandboxId: id}
log.Debugf("StopPodSandboxRequest: %v", request)
r, err := client.StopPodSandbox(context.Background(), request)
log.Debugf("StopPodSandboxResponse: %v", r)
if err != nil {
return err
}
log.Printf("Stopped sandbox %s\n", id)
return nil
}
// Call RemovePodSandbox on the CRI client
func removePodSandbox(client criapi.RuntimeServiceClient, id string) error {
if id == "" {
return fmt.Errorf("ID cannot be empty")
}
request := &criapi.RemovePodSandboxRequest{PodSandboxId: id}
log.Debugf("RemovePodSandboxRequest: %v", request)
r, err := client.RemovePodSandbox(context.Background(), request)
log.Debugf("RemovePodSandboxResponse: %v", r)
if err != nil {
return err
}
log.Printf("Removed sandbox %s\n", id)
return nil
}
// Call ListPodSandbox on the CRI client
func getPodSandboxes(client criapi.RuntimeServiceClient) ([]*criapi.PodSandbox, error) {
filter := &criapi.PodSandboxFilter{}
request := &criapi.ListPodSandboxRequest{
Filter: filter,
}
log.Debugf("ListPodSandboxRequest: %v", request)
r, err := client.ListPodSandbox(context.Background(), request)
log.Debugf("ListPodSandboxResponse: %v", r)
if err != nil {
return nil, err
}
return r.GetItems(), err
}
// Call PodSandboxStatus on the CRI client
func getPodSandboxStatus(client criapi.RuntimeServiceClient, psId string) (*criapi.PodSandboxStatus, error) {
if psId == "" {
return nil, fmt.Errorf("Pod ID cannot be empty in GPSS")
}
request := &criapi.PodSandboxStatusRequest{
PodSandboxId: psId,
Verbose: false,
}
log.Debugf("PodSandboxStatusRequest: %v", request)
r, err := client.PodSandboxStatus(context.Background(), request)
log.Debugf("PodSandboxStatusResponse: %v", r)
if err != nil {
return nil, err
}
return r.Status, nil
}
// Call CreateContainer on the CRI client
func createContainer(client criapi.RuntimeServiceClient, config *criapi.ContainerConfig, podConfig *criapi.PodSandboxConfig, pId string) (string, error) {
request := &criapi.CreateContainerRequest{
PodSandboxId: pId,
Config: config,
SandboxConfig: podConfig,
}
log.Debugf("CreateContainerRequest: %v", request)
r, err := client.CreateContainer(context.Background(), request)
log.Debugf("CreateContainerResponse: %v", r)
if err != nil {
return "", err
}
log.Printf("Container created: %s\n", r.ContainerId)
return r.ContainerId, nil
}
// Call StartContainer on the CRI client
func startContainer(client criapi.RuntimeServiceClient, cId string) error {
if cId == "" {
return fmt.Errorf("ID cannot be empty")
}
request := &criapi.StartContainerRequest{
ContainerId: cId,
}
log.Debugf("StartContainerRequest: %v", request)
r, err := client.StartContainer(context.Background(), request)
log.Debugf("StartContainerResponse: %v", r)
if err != nil {
return err
}
log.Printf("Container started: %s\n", cId)
return nil
}
// Call ContainerStatus on the CRI client
func getContainerCRIStatus(client criapi.RuntimeServiceClient, cId string) (*criapi.ContainerStatus, error) {
if cId == "" {
return nil, fmt.Errorf("Container ID cannot be empty in GCCS")
}
request := &criapi.ContainerStatusRequest{
ContainerId: cId,
Verbose: false,
}
log.Debugf("ContainerStatusRequest: %v", request)
r, err := client.ContainerStatus(context.Background(), request)
log.Debugf("ContainerStatusResponse: %v", r)
if err != nil {
return nil, err
}
return r.Status, nil
}
// Call ListContainers on the CRI client
func getContainersForSandbox(client criapi.RuntimeServiceClient, psId string) ([]*criapi.Container, error) {
filter := &criapi.ContainerFilter{}
filter.PodSandboxId = psId
request := &criapi.ListContainersRequest{
Filter: filter,
}
log.Debugf("ListContainerRequest: %v", request)
r, err := client.ListContainers(context.Background(), request)
log.Debugf("ListContainerResponse: %v", r)
if err != nil {
return nil, err
}
return r.Containers, nil
}
// Pull and image on the CRI client and return the image ref
func pullImage(client criapi.ImageServiceClient, image string) (string, error) {
request := &criapi.PullImageRequest{
Image: &criapi.ImageSpec{
Image: image,
},
}
log.Debugf("PullImageRequest: %v", request)
r, err := client.PullImage(context.Background(), request)
log.Debugf("PullImageResponse: %v", r)
if err != nil {
return "", err
}
return r.ImageRef, nil
}