forked from solo-io/gloo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kubernetes.go
197 lines (181 loc) · 5.12 KB
/
kubernetes.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
package services
import (
"bytes"
"fmt"
"os"
"os/exec"
"strings"
"time"
"github.com/onsi/ginkgo"
"github.com/pkg/errors"
"github.com/solo-io/solo-kit/pkg/utils/log"
)
const (
testrunner = "testrunner"
)
func SetupKubeForTest(namespace string) error {
context := os.Getenv("KUBECTL_CONTEXT")
if context == "" {
current, err := KubectlOut("config", "current-context")
if err != nil {
return errors.Wrap(err, "getting currrent context")
}
context = strings.TrimSuffix(current, "\n")
}
// TODO(yuval-k): this changes the context for the user? can we do this less intrusive? maybe add it to
// each kubectl command?
if err := Kubectl("config", "set-context", context, "--namespace="+namespace); err != nil {
return errors.Wrap(err, "setting context")
}
return Kubectl("create", "namespace", namespace)
}
func TeardownKube(namespace string) error {
return Kubectl("delete", "namespace", namespace)
}
func Kubectl(args ...string) error {
cmd := exec.Command("kubectl", args...)
log.Debugf("k command: %v", cmd.Args)
cmd.Env = os.Environ()
// disable DEBUG=1 from getting through to kube
for i, pair := range cmd.Env {
if strings.HasPrefix(pair, "DEBUG") {
cmd.Env = append(cmd.Env[:i], cmd.Env[i+1:]...)
break
}
}
cmd.Stdout = ginkgo.GinkgoWriter
cmd.Stderr = ginkgo.GinkgoWriter
return cmd.Run()
}
func KubectlOut(args ...string) (string, error) {
cmd := exec.Command("kubectl", args...)
cmd.Env = os.Environ()
// disable DEBUG=1 from getting through to kube
for i, pair := range cmd.Env {
if strings.HasPrefix(pair, "DEBUG") {
cmd.Env = append(cmd.Env[:i], cmd.Env[i+1:]...)
break
}
}
out, err := cmd.CombinedOutput()
if err != nil {
err = fmt.Errorf("%s (%v)", out, err)
}
return string(out), err
}
func KubectlOutAsync(args ...string) (*bytes.Buffer, chan struct{}, error) {
cmd := exec.Command("kubectl", args...)
cmd.Env = os.Environ()
// disable DEBUG=1 from getting through to kube
for i, pair := range cmd.Env {
if strings.HasPrefix(pair, "DEBUG") {
cmd.Env = append(cmd.Env[:i], cmd.Env[i+1:]...)
break
}
}
buf := &bytes.Buffer{}
cmd.Stdout = buf
cmd.Stderr = buf
err := cmd.Start()
if err != nil {
err = fmt.Errorf("%s (%v)", buf.Bytes(), err)
}
done := make(chan struct{})
go func() {
select {
case <-done:
cmd.Process.Kill()
}
}()
return buf, done, err
}
// WaitPodsRunning waits for all pods to be running
func WaitPodsRunning(podNames ...string) error {
finished := func(output string) bool {
return strings.Contains(output, "Running")
}
for _, pod := range podNames {
if err := WaitPodStatus(pod, "Running", finished); err != nil {
return err
}
}
return nil
}
// waitPodsTerminated waits for all pods to be terminated
func WaitPodsTerminated(podNames ...string) error {
for _, pod := range podNames {
finished := func(output string) bool {
return !strings.Contains(output, pod)
}
if err := WaitPodStatus(pod, "terminated", finished); err != nil {
return err
}
}
return nil
}
// TestRunner executes a command inside the TestRunner container
func TestRunner(args ...string) (string, error) {
args = append([]string{"exec", "-i", testrunner, "--"}, args...)
return KubectlOut(args...)
}
// TestRunnerAsync executes a command inside the TestRunner container
// returning a buffer that can be read from as it executes
func TestRunnerAsync(args ...string) (*bytes.Buffer, chan struct{}, error) {
args = append([]string{"exec", "-i", testrunner, "--"}, args...)
return KubectlOutAsync(args...)
}
func WaitPodStatus(pod, status string, finished func(output string) bool) error {
timeout := time.Second * 20
interval := time.Millisecond * 1000
tick := time.Tick(interval)
log.Debugf("waiting %v for pod %v to be %v...", timeout, pod, status)
for {
select {
case <-time.After(timeout):
return fmt.Errorf("timed out waiting for %v to be %v", pod, status)
case <-tick:
out, err := KubectlOut("get", "pod", "-l", "gloo="+pod)
if err != nil {
return fmt.Errorf("failed getting pod: %v", err)
}
if strings.Contains(out, "CrashLoopBackOff") {
out = KubeLogs(pod)
return errors.Errorf("%v in crash loop with logs %v", pod, out)
}
if strings.Contains(out, "ErrImagePull") || strings.Contains(out, "ImagePullBackOff") {
out, _ = KubectlOut("describe", "pod", "-l", "gloo="+pod)
return errors.Errorf("%v in ErrImagePull with description %v", pod, out)
}
if finished(out) {
return nil
}
}
}
}
func KubeLogs(pod string) string {
out, err := KubectlOut("logs", "-l", "gloo="+pod)
if err != nil {
out = err.Error()
}
return out
}
func WaitNamespaceStatus(namespace, status string, finished func(output string) bool) error {
timeout := time.Second * 20
interval := time.Millisecond * 1000
tick := time.Tick(interval)
log.Debugf("waiting %v for namespace %v to be %v...", timeout, namespace, status)
for {
select {
case <-time.After(timeout):
return fmt.Errorf("timed out waiting for %v to be %v", namespace, status)
case <-tick:
out, err := KubectlOut("get", "namespace", namespace)
if err != nil {
return fmt.Errorf("failed getting pod: %v", err)
}
if finished(out) {
return nil
}
}
}
}