forked from GoogleContainerTools/skaffold
-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
269 lines (218 loc) · 7.25 KB
/
util.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
/*
Copyright 2019 The Skaffold Authors
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 integration
import (
"context"
"fmt"
"os"
"os/exec"
"testing"
"time"
"github.com/sirupsen/logrus"
appsv1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
typedappsv1 "k8s.io/client-go/kubernetes/typed/apps/v1"
corev1 "k8s.io/client-go/kubernetes/typed/core/v1"
pkgkubernetes "github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes"
k8s "github.com/GoogleContainerTools/skaffold/pkg/webhook/kubernetes"
)
func RunOnGCP() bool {
return os.Getenv("GCP_ONLY") == "true"
}
func Run(t *testing.T, dir, command string, args ...string) {
cmd := exec.Command(command, args...)
cmd.Dir = dir
if output, err := cmd.Output(); err != nil {
t.Fatalf("running command [%s %v]: %s %v", command, args, output, err)
}
}
// SetupNamespace creates a Kubernetes namespace to run a test.
func SetupNamespace(t *testing.T) (*v1.Namespace, *NSKubernetesClient, func()) {
client, err := pkgkubernetes.Client()
if err != nil {
t.Fatalf("Test setup error: getting Kubernetes client: %s", err)
}
ns, err := client.CoreV1().Namespaces().Create(&v1.Namespace{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "skaffold",
},
})
if err != nil {
t.Fatalf("creating namespace: %s", err)
}
logrus.Infoln("Namespace:", ns.Name)
nsClient := &NSKubernetesClient{
t: t,
client: client,
ns: ns.Name,
}
return ns, nsClient, func() {
client.CoreV1().Namespaces().Delete(ns.Name, &metav1.DeleteOptions{})
}
}
// NSKubernetesClient wraps a Kubernetes Client for a given namespace.
type NSKubernetesClient struct {
t *testing.T
client kubernetes.Interface
ns string
}
func (k *NSKubernetesClient) Pods() corev1.PodInterface {
return k.client.CoreV1().Pods(k.ns)
}
func (k *NSKubernetesClient) Secrets() corev1.SecretInterface {
return k.client.CoreV1().Secrets(k.ns)
}
func (k *NSKubernetesClient) Services() corev1.ServiceInterface {
return k.client.CoreV1().Services(k.ns)
}
func (k *NSKubernetesClient) Deployments() typedappsv1.DeploymentInterface {
return k.client.AppsV1().Deployments(k.ns)
}
func (k *NSKubernetesClient) DefaultSecrets() corev1.SecretInterface {
return k.client.CoreV1().Secrets("default")
}
func (k *NSKubernetesClient) CreateSecretFrom(ns, name string) {
secret, err := k.client.CoreV1().Secrets(ns).Get(name, metav1.GetOptions{})
if err != nil {
k.t.Fatalf("failed reading default/e2esecret: %s", err)
}
secret.Namespace = k.ns
secret.ResourceVersion = ""
if _, err = k.Secrets().Create(secret); err != nil {
k.t.Fatalf("failed creating %s/e2esecret: %s", k.ns, err)
}
}
// WaitForPodsReady waits for a list of pods to become ready.
func (k *NSKubernetesClient) WaitForPodsReady(podNames ...string) {
k.WaitForPodsInPhase(v1.PodRunning, podNames...)
}
// WaitForPodsInPhase waits for a list of pods to become ready.
func (k *NSKubernetesClient) WaitForPodsInPhase(expectedPhase v1.PodPhase, podNames ...string) {
if len(podNames) == 0 {
return
}
logrus.Infoln("Waiting for pods", podNames, "to be ready")
ctx, cancelTimeout := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancelTimeout()
pods := k.Pods()
w, err := pods.Watch(metav1.ListOptions{})
if err != nil {
k.t.Fatalf("Unable to watch pods: %v", err)
}
defer w.Stop()
phases := map[string]v1.PodPhase{}
for {
waitLoop:
select {
case <-ctx.Done():
k.printDiskFreeSpace()
//k.debug("nodes")
k.debug("pods")
k.t.Fatalf("Timed out waiting for pods %v ready in namespace %s", podNames, k.ns)
case event := <-w.ResultChan():
pod := event.Object.(*v1.Pod)
logrus.Infoln("Pod", pod.Name, "is", pod.Status.Phase)
if pod.Status.Phase == v1.PodFailed {
logs, err := pods.GetLogs(pod.Name, &v1.PodLogOptions{}).DoRaw()
if err != nil {
k.t.Fatalf("failed to get logs for failed pod %s: %s", pod.Name, err)
}
k.t.Fatalf("pod %s failed. Logs:\n %s", pod.Name, logs)
}
phases[pod.Name] = pod.Status.Phase
for _, podName := range podNames {
if phases[podName] != expectedPhase {
break waitLoop
}
}
logrus.Infoln("Pods", podNames, "ready")
return
}
}
}
// GetDeployment gets a deployment by name.
func (k *NSKubernetesClient) GetDeployment(depName string) *appsv1.Deployment {
k.WaitForDeploymentsToStabilize(depName)
dep, err := k.Deployments().Get(depName, metav1.GetOptions{})
if err != nil {
k.t.Fatalf("Could not find deployment: %s in namespace %s", depName, k.ns)
}
return dep
}
// WaitForDeploymentsToStabilize waits for a list of deployments to become stable.
func (k *NSKubernetesClient) WaitForDeploymentsToStabilize(depNames ...string) {
if len(depNames) == 0 {
return
}
logrus.Infoln("Waiting for deployments", depNames, "to stabilize")
ctx, cancelTimeout := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancelTimeout()
w, err := k.Deployments().Watch(metav1.ListOptions{})
if err != nil {
k.t.Fatalf("Unable to watch deployments: %v", err)
}
defer w.Stop()
deployments := map[string]*appsv1.Deployment{}
for {
waitLoop:
select {
case <-ctx.Done():
k.printDiskFreeSpace()
//k.debug("nodes")
k.debug("deployments.apps")
k.debug("pods")
k.t.Fatalf("Timed out waiting for deployments %v to stabilize in namespace %s", depNames, k.ns)
case event := <-w.ResultChan():
dp := event.Object.(*appsv1.Deployment)
logrus.Infof("Deployment %s: Generation %d/%d, Replicas %d/%d", dp.Name, dp.Status.ObservedGeneration, dp.Generation, dp.Status.Replicas, *(dp.Spec.Replicas))
deployments[dp.Name] = dp
for _, depName := range depNames {
if d, present := deployments[depName]; !present || !isStable(d) {
break waitLoop
}
}
logrus.Infoln("Deployments", depNames, "are stable")
return
}
}
}
// debug is used to print all the details about pods or deployments
func (k *NSKubernetesClient) debug(entities string) {
cmd := exec.Command("kubectl", "-n", k.ns, "get", entities, "-oyaml")
out, _ := cmd.CombinedOutput()
logrus.Warnln(cmd.Args)
// Use fmt.Println, not logrus, for prettier output
fmt.Println(string(out))
}
func (k *NSKubernetesClient) printDiskFreeSpace() {
cmd := exec.Command("df", "-h")
out, _ := cmd.CombinedOutput()
fmt.Println(string(out))
}
// ExternalIP waits for the external IP aof a given service.
func (k *NSKubernetesClient) ExternalIP(serviceName string) string {
svc, err := k.Services().Get(serviceName, metav1.GetOptions{})
if err != nil {
k.t.Fatalf("error getting registry service: %v", err)
}
ip, err := k8s.GetExternalIP(svc)
if err != nil {
k.t.Fatalf("error getting external ip: %v", err)
}
return ip
}
func isStable(dp *appsv1.Deployment) bool {
return dp.Generation <= dp.Status.ObservedGeneration && *(dp.Spec.Replicas) == dp.Status.Replicas
}