forked from kubermatic/kubermatic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom_tests.go
334 lines (298 loc) · 9.69 KB
/
custom_tests.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
/*
Copyright 2020 The Kubermatic Kubernetes Platform contributors.
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 main
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net"
"net/http"
"strings"
"time"
"go.uber.org/zap"
kubermaticv1 "github.com/kubermatic/kubermatic/api/pkg/crd/kubermatic/v1"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/wait"
ctrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client"
)
func (r *testRunner) testPVC(log *zap.SugaredLogger, userClusterClient ctrlruntimeclient.Client, attempt int) error {
log.Info("Testing support for PVC's...")
ns := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("pvc-test-%d", attempt),
},
}
if err := userClusterClient.Create(context.Background(), ns); err != nil {
return fmt.Errorf("failed to create namespace: %v", err)
}
log.Info("Creating a StatefulSet with PVC...")
labels := map[string]string{"app": "data-writer"}
// Creating a simple StatefulSet with 1 replica which writes to the PV. That way we know if storage can be provisioned and consumed
set := &appsv1.StatefulSet{
ObjectMeta: metav1.ObjectMeta{
Name: "data-writer",
Namespace: ns.Name,
},
Spec: appsv1.StatefulSetSpec{
Selector: &metav1.LabelSelector{
MatchLabels: labels,
},
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: labels,
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "busybox",
Image: "k8s.gcr.io/busybox",
Args: []string{
"/bin/sh",
"-c",
`echo "alive" > /data/healthy; sleep 3600`,
},
ReadinessProbe: &corev1.Probe{
InitialDelaySeconds: 0,
SuccessThreshold: 3,
PeriodSeconds: 5,
TimeoutSeconds: 1,
FailureThreshold: 1,
Handler: corev1.Handler{
Exec: &corev1.ExecAction{
Command: []string{
"cat",
"/data/healthy",
},
},
},
},
VolumeMounts: []corev1.VolumeMount{
{
Name: "data",
MountPath: "/data",
},
},
},
},
},
},
VolumeClaimTemplates: []corev1.PersistentVolumeClaim{
{
ObjectMeta: metav1.ObjectMeta{
Name: "data",
},
Spec: corev1.PersistentVolumeClaimSpec{
AccessModes: []corev1.PersistentVolumeAccessMode{corev1.ReadWriteOnce},
Resources: corev1.ResourceRequirements{
Requests: corev1.ResourceList{corev1.ResourceStorage: resource.MustParse("1Gi")},
},
},
},
},
},
}
if err := userClusterClient.Create(context.Background(), set); err != nil {
return fmt.Errorf("failed to create statefulset: %v", err)
}
log.Info("Waiting until the StatefulSet is ready...")
err := wait.Poll(10*time.Second, 10*time.Minute, func() (done bool, err error) {
currentSet := &appsv1.StatefulSet{}
name := types.NamespacedName{Namespace: ns.Name, Name: set.Name}
if err := userClusterClient.Get(context.Background(), name, currentSet); err != nil {
log.Warnf("Failed to load StatefulSet %s/%s from API server during PVC test: %v", ns.Name, set.Name, err)
return false, nil
}
if currentSet.Status.ReadyReplicas == 1 {
return true, nil
}
return false, nil
})
if err != nil {
return fmt.Errorf("failed to check if StatefulSet is running: %v", err)
}
log.Info("Successfully validated PVC support")
return nil
}
func (r *testRunner) testLB(log *zap.SugaredLogger, userClusterClient ctrlruntimeclient.Client, attempt int) error {
log.Info("Testing support for LB's...")
ns := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("lb-test-%d", attempt),
},
}
if err := userClusterClient.Create(context.Background(), ns); err != nil {
return fmt.Errorf("failed to create namespace: %v", err)
}
log.Info("Creating a Service of type LoadBalancer...")
labels := map[string]string{"app": "hello"}
service := &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: ns.Name,
},
Spec: corev1.ServiceSpec{
Type: corev1.ServiceTypeLoadBalancer,
Selector: labels,
Ports: []corev1.ServicePort{
{
Port: 80,
TargetPort: intstr.FromInt(8080),
},
},
},
}
if err := userClusterClient.Create(context.Background(), service); err != nil {
return fmt.Errorf("failed to create Service: %v", err)
}
pod := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "hello-kubernetes",
Namespace: ns.Name,
Labels: labels,
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "hello-kubernetes",
Image: "gcr.io/google-samples/node-hello:1.0",
Ports: []corev1.ContainerPort{
{
Name: "http",
ContainerPort: 8080,
Protocol: corev1.ProtocolTCP,
},
},
},
},
},
}
if err := userClusterClient.Create(context.Background(), pod); err != nil {
return fmt.Errorf("failed to create Pod: %v", err)
}
var host string
log.Debug("Waiting until the Service has a public IP/Name...")
err := wait.Poll(10*time.Second, defaultTimeout, func() (done bool, err error) {
currentService := &corev1.Service{}
if err := userClusterClient.Get(context.Background(), types.NamespacedName{Namespace: ns.Name, Name: service.Name}, currentService); err != nil {
log.Warnf("Failed to load Service %s/%s from API server during LB test: %v", ns.Name, service.Name, err)
return false, nil
}
if len(currentService.Status.LoadBalancer.Ingress) > 0 {
host = currentService.Status.LoadBalancer.Ingress[0].IP
if host == "" {
host = currentService.Status.LoadBalancer.Ingress[0].Hostname
// We wait until we can actually resolve the name
ips, err := net.LookupHost(host)
if err != nil || len(ips) == 0 {
return false, nil
}
}
return true, nil
}
return false, nil
})
if err != nil {
return fmt.Errorf("failed to check if Service is ready: %v", err)
}
log.Debug("The Service has an external IP/Name")
hostURL := fmt.Sprintf("http://%s:80", host)
log.Debug("Waiting until the pod is available via the LB...")
err = wait.Poll(30*time.Second, defaultTimeout, func() (done bool, err error) {
resp, err := http.Get(hostURL)
if err != nil {
log.Warnf("Failed to call Pod via LB (%s) during LB test: %v", hostURL, err)
return false, nil
}
defer func() {
if err := resp.Body.Close(); err != nil {
log.Warnf("Failed to close response body from Hello-Kubernetes Pod during LB test (%s): %v", hostURL, err)
}
}()
contents, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Warnf("Failed to read response body from Hello-Kubernetes Pod during LB test (%s): %v", hostURL, err)
return false, nil
}
if strings.Contains(string(contents), "Hello Kubernetes!") {
return true, nil
}
return false, nil
})
if err != nil {
return fmt.Errorf("failed to check if Pod is available via LB: %v", err)
}
log.Info("Successfully validated LB support")
return nil
}
type metricsData struct {
Status string `json:"status"`
Data []string `json:"data"`
}
// testUserClusterMetrics ensures all expected metrics are actually collected
// in Prometheus. Note that this assumes that some time has passed between
// Prometheus' eployment and this test, so it can scrape all targets. This
// includes kubelets, so nodes must have been ready for at least 30 seconds
// before this can succeed.
func (r *testRunner) testUserClusterMetrics(log *zap.SugaredLogger, cluster *kubermaticv1.Cluster, seedClient ctrlruntimeclient.Client) error {
log.Info("Testing user cluster metrics availability...")
res := r.seedGeneratedClient.CoreV1().RESTClient().Get().
Namespace(cluster.Status.NamespaceName).
Resource("pods").
Name("prometheus-0:9090").
SubResource("proxy").
Suffix("api/v1/label/__name__/values").
Do()
if err := res.Error(); err != nil {
return fmt.Errorf("request to Prometheus failed: %v", err)
}
code := 0
res.StatusCode(&code)
if code != http.StatusOK {
return fmt.Errorf("Prometheus returned HTTP %d", code)
}
body, err := res.Raw()
if err != nil {
return fmt.Errorf("failed to read response body: %v", err)
}
data := &metricsData{}
if err := json.Unmarshal(body, data); err != nil {
return fmt.Errorf("failed to unmarshal metrics response: %v", err)
}
if data.Status != "success" {
return fmt.Errorf("failed to get prometheus metrics with data status: %s", data.Status)
}
expected := sets.NewString(
"etcd_disk_backend_defrag_duration_seconds_sum",
"kube_daemonset_labels",
"kubelet_runtime_operations_duration_seconds_count",
"machine_controller_machines",
"replicaset_controller_rate_limiter_use",
"scheduler_e2e_scheduling_duration_seconds_count",
"ssh_tunnel_open_count",
"workqueue_retries_total",
)
fetched := sets.NewString(data.Data...)
missing := expected.Difference(fetched)
if missing.Len() > 0 {
return fmt.Errorf("failed to get all expected metrics: got: %v, %v are missing", fetched.List(), missing.List())
}
log.Info("Successfully validated user cluster metrics.")
return nil
}