This repository has been archived by the owner on Jun 16, 2023. It is now read-only.
forked from Mirantis/virtlet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pod_interface.go
350 lines (310 loc) · 9.59 KB
/
pod_interface.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*
Copyright 2017 Mirantis
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package framework
import (
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"os/signal"
"strings"
"time"
"github.com/davecgh/go-spew/spew"
"k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/tools/portforward"
"k8s.io/client-go/tools/remotecommand"
"k8s.io/client-go/transport/spdy"
"k8s.io/client-go/util/exec"
"github.com/Mirantis/virtlet/pkg/tools"
)
// PodInterface provides API to work with a pod
type PodInterface struct {
controller *Controller
hasService bool
Pod *v1.Pod
}
func newPodInterface(controller *Controller, pod *v1.Pod) *PodInterface {
return &PodInterface{
controller: controller,
Pod: pod,
}
}
// Create creates pod in the k8s
func (pi *PodInterface) Create() error {
updatedPod, err := pi.controller.client.Pods(pi.controller.Namespace()).Create(pi.Pod)
if err != nil {
return err
}
pi.Pod = updatedPod
return nil
}
// Delete deletes the pod and associated service, which was earlier created by `controller.Run()`
func (pi *PodInterface) Delete() error {
if pi.hasService {
pi.controller.client.Services(pi.Pod.Namespace).Delete(pi.Pod.Name, nil)
}
return pi.controller.client.Pods(pi.Pod.Namespace).Delete(pi.Pod.Name, nil)
}
// WaitForPodStatus waits for the pod to reach the specified status. If expectedContainerErrors
// is empty, the pod is expected to become Running and Ready. If it isn't, the pod is expected
// to have one of these errors among its container statuses.
func (pi *PodInterface) WaitForPodStatus(expectedContainerErrors []string, timing ...time.Duration) error {
timeout := time.Minute * 5
pollPeriond := time.Second
consistencyPeriod := time.Second * 5
if len(timing) > 0 {
timeout = timing[0]
}
if len(timing) > 1 {
pollPeriond = timing[1]
}
if len(timing) > 2 {
consistencyPeriod = timing[2]
}
return waitForConsistentState(func() error {
podUpdated, err := pi.controller.client.Pods(pi.Pod.Namespace).Get(pi.Pod.Name, metav1.GetOptions{})
if err != nil {
return err
}
pi.Pod = podUpdated
needErrors := len(expectedContainerErrors) > 0
phase := v1.PodRunning
if needErrors {
phase = v1.PodPending
}
if podUpdated.Status.Phase != phase {
return fmt.Errorf("pod %s is not %s phase: %s", podUpdated.Name, phase, podUpdated.Status.Phase)
}
gotExpectedError := false
for _, cs := range podUpdated.Status.ContainerStatuses {
switch {
case !needErrors && cs.State.Running == nil:
return fmt.Errorf("container %s in pod %s is not running: %s", cs.Name, podUpdated.Name, spew.Sdump(cs.State))
case !needErrors && !cs.Ready:
return fmt.Errorf("container %s in pod %s did not passed its readiness probe", cs.Name, podUpdated.Name)
case needErrors && cs.State.Waiting == nil:
return fmt.Errorf("container %s in pod %s not in waiting state", cs.Name, podUpdated.Name)
case needErrors:
for _, errStr := range expectedContainerErrors {
if cs.State.Waiting.Reason == errStr {
gotExpectedError = true
break
}
}
}
}
if needErrors && !gotExpectedError {
return fmt.Errorf("didn't get one of expected container errors: %s", strings.Join(expectedContainerErrors, " | "))
}
return nil
}, timeout, pollPeriond, consistencyPeriod)
}
// Wait waits for pod to start and checks that it doesn't fail immediately after that
func (pi *PodInterface) Wait(timing ...time.Duration) error {
return pi.WaitForPodStatus(nil, timing...)
}
// WaitForDestruction waits for the pod to be deleted
func (pi *PodInterface) WaitForDestruction(timing ...time.Duration) error {
timeout := time.Minute * 5
pollPeriond := time.Second
consistencyPeriod := time.Second * 5
if len(timing) > 0 {
timeout = timing[0]
}
if len(timing) > 1 {
pollPeriond = timing[1]
}
if len(timing) > 2 {
consistencyPeriod = timing[2]
}
return waitForConsistentState(func() error {
if _, err := pi.controller.client.Pods(pi.Pod.Namespace).Get(pi.Pod.Name, metav1.GetOptions{}); err != nil {
if k8serrors.IsNotFound(err) {
return nil
}
return err
}
return fmt.Errorf("pod %s was not deleted", pi.Pod.Name)
}, timeout, pollPeriond, consistencyPeriod)
}
// Container returns an interface to handle one of the pod's
// containers. If name is empty, it takes the first container
// of the pod.
func (pi *PodInterface) Container(name string) (Executor, error) {
if name == "" && len(pi.Pod.Spec.Containers) > 0 {
name = pi.Pod.Spec.Containers[0].Name
}
found := false
for _, c := range pi.Pod.Spec.Containers {
if c.Name == name {
found = true
break
}
}
if !found {
return nil, fmt.Errorf("container %s doesn't exist in pod %s in namespace %s", name, pi.Pod.Name, pi.Pod.Namespace)
}
return &containerInterface{
podInterface: pi,
name: name,
}, nil
}
// PortForward starts port forwarding to the specified ports to the specified pod
// in background. If a port entry has LocalPort = 0, it's updated with the real
// port number that was selected by the forwarder.
// Close returned channel to stop the port forwarder.
func (pi *PodInterface) PortForward(ports []*tools.ForwardedPort) (chan struct{}, error) {
if len(ports) == 0 {
return nil, errors.New("no ports specified")
}
signals := make(chan os.Signal, 1)
signal.Notify(signals, os.Interrupt)
defer signal.Stop(signals)
stopCh := make(chan struct{})
go func() {
<-signals
if stopCh != nil {
close(stopCh)
}
}()
restClient := pi.controller.client.RESTClient()
req := restClient.Post().
Resource("pods").
Name(pi.Pod.Name).
Namespace(pi.Pod.Namespace).
SubResource("portforward")
var buf bytes.Buffer
var portsStr []string
for _, p := range ports {
portsStr = append(portsStr, p.String())
}
errCh := make(chan error, 1)
readyCh := make(chan struct{})
go func() {
transport, upgrader, err := spdy.RoundTripperFor(pi.controller.restConfig)
if err != nil {
errCh <- err
return
}
dialer := spdy.NewDialer(upgrader, &http.Client{Transport: transport}, "POST", req.URL())
if err != nil {
errCh <- err
return
}
fw, err := portforward.New(dialer, portsStr, stopCh, readyCh, &buf, os.Stderr)
if err != nil {
errCh <- err
return
}
errCh <- fw.ForwardPorts()
}()
select {
case err := <-errCh:
return nil, err
case <-readyCh:
// FIXME: there appears to be no better way to get back the local ports as of now
if err := tools.ParsePortForwardOutput(buf.String(), ports); err != nil {
return nil, err
}
}
return stopCh, nil
}
// DinDNodeExecutor return DinD executor for node, where this pod is located
func (pi *PodInterface) DinDNodeExecutor() (Executor, error) {
return pi.controller.DinDNodeExecutor(pi.Pod.Spec.NodeName)
}
// LoadEvents retrieves the evnets for this pod as a list
// of strings of the form Type:Reason:Message
func (pi *PodInterface) LoadEvents() ([]string, error) {
events, err := pi.controller.client.Events(pi.controller.Namespace()).Search(scheme.Scheme, pi.Pod)
if err != nil {
return nil, err
}
var r []string
for _, e := range events.Items {
r = append(r, fmt.Sprintf("%s:%s:%s", e.Type, e.Reason, e.Message))
}
return r, nil
}
type containerInterface struct {
podInterface *PodInterface
name string
}
// Run executes commands in one of containers in the pod
func (ci *containerInterface) Run(stdin io.Reader, stdout, stderr io.Writer, command ...string) error {
restClient := ci.podInterface.controller.client.RESTClient()
req := restClient.Post().
Resource("pods").
Name(ci.podInterface.Pod.Name).
Namespace(ci.podInterface.Pod.Namespace).
SubResource("exec")
req.VersionedParams(&v1.PodExecOptions{
Container: ci.name,
Command: command,
Stdin: stdin != nil,
Stdout: stdout != nil,
Stderr: stderr != nil,
}, scheme.ParameterCodec)
executor, err := remotecommand.NewSPDYExecutor(ci.podInterface.controller.restConfig, "POST", req.URL())
if err != nil {
return err
}
options := remotecommand.StreamOptions{
Stdin: stdin,
Stdout: stdout,
Stderr: stderr,
}
if err := executor.Stream(options); err != nil {
if c, ok := err.(exec.CodeExitError); ok {
return CommandError{ExitCode: c.Code}
}
return err
}
return nil
}
// Close closes the executor
func (*containerInterface) Close() error {
return nil
}
// Start is a placeholder for fulfilling the Executor interface
func (*containerInterface) Start(stdin io.Reader, stdout, stderr io.Writer, command ...string) (Command, error) {
return nil, errors.New("Not Implemented")
}
// Logs returns the logs of the container as a string.
func (ci *containerInterface) Logs() (string, error) {
restClient := ci.podInterface.controller.client.RESTClient()
req := restClient.Get().
Name(ci.podInterface.Pod.Name).
Namespace(ci.podInterface.Pod.Namespace).
Resource("pods").
SubResource("log")
req.VersionedParams(&v1.PodLogOptions{
Container: ci.name,
}, scheme.ParameterCodec)
stream, err := req.Stream()
if err != nil {
return "", err
}
defer stream.Close()
bs, err := ioutil.ReadAll(stream)
if err != nil {
return "", fmt.Errorf("ReadAll(): %v", err)
}
return string(bs), nil
}