forked from openshift/machine-api-operator
/
operator_expectations.go
610 lines (570 loc) · 19.6 KB
/
operator_expectations.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
package main
import (
"strings"
"time"
"context"
"errors"
"fmt"
"github.com/golang/glog"
osconfigv1 "github.com/openshift/api/config/v1"
caov1alpha1 "github.com/openshift/cluster-autoscaler-operator/pkg/apis/autoscaling/v1alpha1"
cvoresourcemerge "github.com/openshift/cluster-version-operator/lib/resourcemerge"
kappsapi "k8s.io/api/apps/v1"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
resource "k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/wait"
capiv1alpha1 "sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha1"
"sigs.k8s.io/controller-runtime/pkg/client"
)
const (
waitShort = 1 * time.Minute
waitMedium = 3 * time.Minute
waitLong = 10 * time.Minute
)
func (tc *testConfig) ExpectOperatorAvailable() error {
name := "machine-api-operator"
key := types.NamespacedName{
Namespace: namespace,
Name: name,
}
d := &kappsapi.Deployment{}
err := wait.PollImmediate(1*time.Second, waitShort, func() (bool, error) {
if err := tc.client.Get(context.TODO(), key, d); err != nil {
glog.Errorf("error querying api for Deployment object: %v, retrying...", err)
return false, nil
}
if d.Status.ReadyReplicas < 1 {
return false, nil
}
return true, nil
})
return err
}
func (tc *testConfig) ExpectOneClusterObject() error {
listOptions := client.ListOptions{
Namespace: namespace,
}
clusterList := capiv1alpha1.ClusterList{}
err := wait.PollImmediate(1*time.Second, waitShort, func() (bool, error) {
if err := tc.client.List(context.TODO(), &listOptions, &clusterList); err != nil {
glog.Errorf("error querying api for clusterList object: %v, retrying...", err)
return false, nil
}
if len(clusterList.Items) != 1 {
return false, errors.New("more than one cluster object found")
}
return true, nil
})
return err
}
func (tc *testConfig) ExpectClusterOperatorStatusAvailable() error {
name := "machine-api-operator"
key := types.NamespacedName{
Namespace: namespace,
Name: name,
}
clusterOperator := &osconfigv1.ClusterOperator{}
err := wait.PollImmediate(1*time.Second, waitShort, func() (bool, error) {
if err := tc.client.Get(context.TODO(), key, clusterOperator); err != nil {
glog.Errorf("error querying api for OperatorStatus object: %v, retrying...", err)
return false, nil
}
if available := cvoresourcemerge.FindOperatorStatusCondition(clusterOperator.Status.Conditions, osconfigv1.OperatorAvailable); available != nil {
if available.Status == osconfigv1.ConditionTrue {
return true, nil
}
}
return false, nil
})
return err
}
func (tc *testConfig) ExpectAllMachinesLinkedToANode() error {
machineAnnotationKey := "cluster.k8s.io/machine"
listOptions := client.ListOptions{
Namespace: namespace,
}
machineList := capiv1alpha1.MachineList{}
nodeList := corev1.NodeList{}
err := wait.PollImmediate(1*time.Second, waitShort, func() (bool, error) {
if err := tc.client.List(context.TODO(), &listOptions, &machineList); err != nil {
glog.Errorf("error querying api for machineList object: %v, retrying...", err)
return false, nil
}
if err := tc.client.List(context.TODO(), &listOptions, &nodeList); err != nil {
glog.Errorf("error querying api for nodeList object: %v, retrying...", err)
return false, nil
}
glog.Infof("Waiting for %d machines to become nodes", len(machineList.Items))
return len(machineList.Items) == len(nodeList.Items), nil
})
if err != nil {
return err
}
return wait.PollImmediate(1*time.Second, waitShort, func() (bool, error) {
nodeNameToMachineAnnotation := make(map[string]string)
for _, node := range nodeList.Items {
nodeNameToMachineAnnotation[node.Name] = node.Annotations[machineAnnotationKey]
}
for _, machine := range machineList.Items {
if machine.Status.NodeRef == nil {
glog.Errorf("machine %s has no NodeRef, retrying...", machine.Name)
return false, nil
}
nodeName := machine.Status.NodeRef.Name
if nodeNameToMachineAnnotation[nodeName] != fmt.Sprintf("%s/%s", namespace, machine.Name) {
glog.Errorf("node name %s does not match expected machine name %s, retrying...", nodeName, machine.Name)
return false, nil
}
}
return true, nil
})
}
func (tc *testConfig) ExpectReconcileControllersDeployment() error {
key := types.NamespacedName{
Namespace: namespace,
Name: "clusterapi-manager-controllers",
}
d := &kappsapi.Deployment{}
glog.Info("Get deployment")
err := wait.PollImmediate(1*time.Second, waitShort, func() (bool, error) {
if err := tc.client.Get(context.TODO(), key, d); err != nil {
glog.Errorf("error querying api for Deployment object: %v, retrying...", err)
return false, nil
}
return true, nil
})
if err != nil {
return err
}
glog.Info("Delete deployment")
err = wait.PollImmediate(1*time.Second, waitShort, func() (bool, error) {
if err := tc.client.Delete(context.TODO(), d); err != nil {
glog.Errorf("error querying api for Deployment object: %v, retrying...", err)
return false, nil
}
return true, nil
})
if err != nil {
return err
}
glog.Info("Verify deployment is recreated")
err = wait.PollImmediate(1*time.Second, waitLong, func() (bool, error) {
if err := tc.client.Get(context.TODO(), key, d); err != nil {
glog.Errorf("error querying api for Deployment object: %v, retrying...", err)
return false, nil
}
if d.Status.ReadyReplicas < 1 || !d.DeletionTimestamp.IsZero() {
return false, nil
}
return true, nil
})
return err
}
func (tc *testConfig) ExpectAdditiveReconcileMachineTaints() error {
glog.Info("Verify machine taints are getting applied to node")
listOptions := client.ListOptions{
Namespace: namespace,
}
machineList := capiv1alpha1.MachineList{}
if err := tc.client.List(context.TODO(), &listOptions, &machineList); err != nil {
return fmt.Errorf("error querying api for machineList object: %v", err)
}
glog.Info("Got the machine list")
machine := machineList.Items[0]
if machine.Status.NodeRef == nil {
return fmt.Errorf("machine %s has no NodeRef", machine.Name)
}
glog.Infof("Got the machine, %s", machine.Name)
nodeName := machine.Status.NodeRef.Name
nodeKey := types.NamespacedName{
Namespace: namespace,
Name: nodeName,
}
node := &corev1.Node{}
if err := tc.client.Get(context.TODO(), nodeKey, node); err != nil {
return fmt.Errorf("error querying api for node object: %v", err)
}
glog.Infof("Got the node, %s, from machine, %s", node.Name, machine.Name)
nodeTaint := corev1.Taint{
Key: "not-from-machine",
Value: "true",
Effect: corev1.TaintEffectNoSchedule,
}
node.Spec.Taints = []corev1.Taint{nodeTaint}
if err := tc.client.Update(context.TODO(), node); err != nil {
return fmt.Errorf("error updating node object with non-machine taint: %v", err)
}
glog.Info("Updated node object with taint")
machineTaint := corev1.Taint{
Key: "from-machine",
Value: "true",
Effect: corev1.TaintEffectNoSchedule,
}
machine.Spec.Taints = []corev1.Taint{machineTaint}
if err := tc.client.Update(context.TODO(), &machine); err != nil {
return fmt.Errorf("error updating machine object with taint: %v", err)
}
glog.Info("Updated machine object with taint")
var expectedTaints = sets.NewString("not-from-machine", "from-machine")
err := wait.PollImmediate(1*time.Second, waitLong, func() (bool, error) {
if err := tc.client.Get(context.TODO(), nodeKey, node); err != nil {
glog.Errorf("error querying api for node object: %v", err)
return false, nil
}
glog.Info("Got the node again for verification of taints")
var observedTaints = sets.NewString()
for _, taint := range node.Spec.Taints {
observedTaints.Insert(taint.Key)
}
if expectedTaints.Difference(observedTaints).HasAny("not-from-machine", "from-machine") == false {
glog.Infof("expected : %v, observed %v , difference %v, ", expectedTaints, observedTaints, expectedTaints.Difference(observedTaints))
return true, nil
}
glog.Infof("All expected taints not found on node. Missing: %v", expectedTaints.Difference(observedTaints))
return false, nil
})
return err
}
func (tc *testConfig) ExpectNewNodeWhenDeletingMachine() error {
listOptions := client.ListOptions{
Namespace: namespace,
}
machineList := capiv1alpha1.MachineList{}
nodeList := corev1.NodeList{}
glog.Info("Get machineList")
err := wait.PollImmediate(1*time.Second, waitShort, func() (bool, error) {
if err := tc.client.List(context.TODO(), &listOptions, &machineList); err != nil {
glog.Errorf("error querying api for machineList object: %v, retrying...", err)
return false, nil
}
return true, nil
})
if err != nil {
return err
}
glog.Info("Get nodeList")
err = wait.PollImmediate(1*time.Second, waitShort, func() (bool, error) {
if err := tc.client.List(context.TODO(), &listOptions, &nodeList); err != nil {
glog.Errorf("error querying api for nodeList object: %v, retrying...", err)
return false, nil
}
return true, nil
})
if err != nil {
return err
}
clusterInitialTotalNodes := len(nodeList.Items)
clusterInitialTotalMachines := len(machineList.Items)
var triagedWorkerMachine capiv1alpha1.Machine
var triagedWorkerNode corev1.Node
MachineLoop:
for _, m := range machineList.Items {
if m.Labels["sigs.k8s.io/cluster-api-machine-role"] == "worker" {
for _, n := range nodeList.Items {
if m.Status.NodeRef == nil {
glog.Errorf("no NodeRef found in machine %v", m.Name)
return errors.New("no NodeRef found in machine")
}
if n.Name == m.Status.NodeRef.Name {
triagedWorkerMachine = m
triagedWorkerNode = n
break MachineLoop
}
}
}
}
glog.Info("Delete machine")
err = wait.PollImmediate(1*time.Second, waitShort, func() (bool, error) {
if err := tc.client.Delete(context.TODO(), &triagedWorkerMachine); err != nil {
glog.Errorf("error querying api for Deployment object: %v, retrying...", err)
return false, nil
}
return true, nil
})
if err != nil {
return err
}
err = wait.PollImmediate(1*time.Second, waitMedium, func() (bool, error) {
if err := tc.client.List(context.TODO(), &listOptions, &machineList); err != nil {
glog.Errorf("error querying api for machineList object: %v, retrying...", err)
return false, nil
}
glog.Info("Expect new machine to come up")
return len(machineList.Items) == clusterInitialTotalMachines, nil
})
if err != nil {
return err
}
err = wait.PollImmediate(1*time.Second, waitLong, func() (bool, error) {
if err := tc.client.List(context.TODO(), &listOptions, &nodeList); err != nil {
glog.Errorf("error querying api for nodeList object: %v, retrying...", err)
return false, nil
}
glog.Info("Expect deleted machine node to go away")
for _, n := range nodeList.Items {
if n.Name == triagedWorkerNode.Name {
return false, nil
}
}
glog.Info("Expect new node to come up")
return len(nodeList.Items) == clusterInitialTotalNodes, nil
})
if err != nil {
return err
}
return nil
}
// ExpectAutoscalerScalesOut is an smoke test for the autoscaling feature
// Create a clusterAutoscaler object
// Create a machineAutoscaler object
// Create a workLoad to force autoscaling
// Validate the targeted machineSet scales out the field for the expected number of replicas
// Validate the number of nodes in the cluster is growing
// Delete the workLoad
// Delete the autoscaler object
// Ensure initial number of replicas and nodes
func (tc *testConfig) ExpectAutoscalerScalesOut() error {
listOptions := client.ListOptions{
Namespace: namespace,
}
glog.Info("Get one machineSet")
machineSetList := capiv1alpha1.MachineSetList{}
err := wait.PollImmediate(1*time.Second, waitMedium, func() (bool, error) {
if err := tc.client.List(context.TODO(), &listOptions, &machineSetList); err != nil {
glog.Errorf("error querying api for nodeList object: %v, retrying...", err)
return false, nil
}
return len(machineSetList.Items) > 0, nil
})
if err != nil {
return err
}
// When we add support for machineDeployments on the installer, cluster-autoscaler and cluster-autoscaler-operator
// we need to test against deployments instead so we skip this test.
targetMachineSet := machineSetList.Items[0]
if ownerReferences := targetMachineSet.GetOwnerReferences(); len(ownerReferences) > 0 {
glog.Infof("MachineSet %s is owned by a machineDeployment. Please run tests agains machineDeployment instead", targetMachineSet.Name)
return nil
}
glog.Infof("Create ClusterAutoscaler and MachineAutoscaler objects. Targeting machineSet %s", targetMachineSet.Name)
initialNumberOfReplicas := targetMachineSet.Spec.Replicas
clusterAutoscaler := caov1alpha1.ClusterAutoscaler{
ObjectMeta: metav1.ObjectMeta{
Name: "default",
Namespace: namespace,
},
TypeMeta: metav1.TypeMeta{
Kind: "ClusterAutoscaler",
APIVersion: "autoscaling.openshift.io/v1alpha1",
},
}
machineAutoscaler := caov1alpha1.MachineAutoscaler{
ObjectMeta: metav1.ObjectMeta{
GenerateName: fmt.Sprintf("autoscale-%s", targetMachineSet.Name),
Namespace: namespace,
},
TypeMeta: metav1.TypeMeta{
Kind: "MachineAutoscaler",
APIVersion: "autoscaling.openshift.io/v1alpha1",
},
Spec: caov1alpha1.MachineAutoscalerSpec{
MaxReplicas: 12,
MinReplicas: 1,
ScaleTargetRef: caov1alpha1.CrossVersionObjectReference{
Name: targetMachineSet.Name,
Kind: "MachineSet",
APIVersion: "cluster.k8s.io/v1alpha1",
},
},
}
err = wait.PollImmediate(1*time.Second, waitMedium, func() (bool, error) {
if err := tc.client.Create(context.TODO(), &clusterAutoscaler); err != nil {
if !strings.Contains(err.Error(), "already exists") {
glog.Errorf("error querying api for clusterAutoscaler object: %v, retrying...", err)
return false, nil
}
}
if err := tc.client.Create(context.TODO(), &machineAutoscaler); err != nil {
if !strings.Contains(err.Error(), "already exists") {
glog.Errorf("error querying api for machineAutoscaler object: %v, retrying...", err)
return false, nil
}
}
return true, nil
})
if err != nil {
return err
}
glog.Info("Get nodeList")
nodeList := corev1.NodeList{}
err = wait.PollImmediate(1*time.Second, waitMedium, func() (bool, error) {
if err := tc.client.List(context.TODO(), &listOptions, &nodeList); err != nil {
glog.Errorf("error querying api for nodeList object: %v, retrying...", err)
return false, nil
}
return true, nil
})
if err != nil {
return err
}
clusterInitialTotalNodes := len(nodeList.Items)
glog.Infof("Cluster initial number of nodes is %d", clusterInitialTotalNodes)
glog.Info("Create workload")
mem, err := resource.ParseQuantity("500Mi")
if err != nil {
glog.Fatal("failed to ParseQuantity %v", err)
}
cpu, err := resource.ParseQuantity("500m")
if err != nil {
glog.Fatal("failed to ParseQuantity %v", err)
}
backoffLimit := int32(4)
completions := int32(50)
parallelism := int32(50)
activeDeadlineSeconds := int64(100)
workLoad := batchv1.Job{
ObjectMeta: metav1.ObjectMeta{
Name: "workload",
Namespace: namespace,
},
TypeMeta: metav1.TypeMeta{
Kind: "Job",
APIVersion: "batch/v1",
},
Spec: batchv1.JobSpec{
Template: corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "workload",
Image: "busybox",
Command: []string{
"sleep",
"300",
},
Resources: corev1.ResourceRequirements{
Requests: corev1.ResourceList{
"memory": mem,
"cpu": cpu,
},
},
},
},
RestartPolicy: corev1.RestartPolicy("Never"),
},
},
ActiveDeadlineSeconds: &activeDeadlineSeconds,
BackoffLimit: &backoffLimit,
Completions: &completions,
Parallelism: ¶llelism,
},
}
err = wait.PollImmediate(1*time.Second, waitMedium, func() (bool, error) {
if err := tc.client.Create(context.TODO(), &workLoad); err != nil {
glog.Errorf("error querying api for workLoad object: %v, retrying...", err)
return false, nil
}
return true, nil
})
if err != nil {
return err
}
glog.Info("Wait for cluster to scale out number of replicas")
err = wait.PollImmediate(1*time.Second, waitLong, func() (bool, error) {
msKey := types.NamespacedName{
Namespace: namespace,
Name: targetMachineSet.Name,
}
ms := &capiv1alpha1.MachineSet{}
if err := tc.client.Get(context.TODO(), msKey, ms); err != nil {
glog.Errorf("error querying api for clusterAutoscaler object: %v, retrying...", err)
return false, nil
}
glog.Infof("MachineSet %s. Initial number of replicas: %d. New number of replicas: %d", targetMachineSet.Name, *initialNumberOfReplicas, *ms.Spec.Replicas)
return *ms.Spec.Replicas > *initialNumberOfReplicas, nil
})
if err != nil {
return err
}
glog.Info("Wait for cluster to scale out nodes")
err = wait.PollImmediate(1*time.Second, waitLong, func() (bool, error) {
nodeList := corev1.NodeList{}
if err := tc.client.List(context.TODO(), &listOptions, &nodeList); err != nil {
glog.Errorf("error querying api for nodeList object: %v, retrying...", err)
return false, nil
}
glog.Info("Expect at least a new node to come up")
glog.Infof("Initial number of nodes: %d. New number of nodes: %d", clusterInitialTotalNodes, len(nodeList.Items))
return len(nodeList.Items) > clusterInitialTotalNodes, nil
})
glog.Info("Delete workload")
err = wait.PollImmediate(1*time.Second, waitMedium, func() (bool, error) {
if err := tc.client.Delete(context.TODO(), &workLoad); err != nil {
glog.Errorf("error querying api for workLoad object: %v, retrying...", err)
return false, nil
}
return true, nil
})
if err != nil {
return err
}
// We delete the clusterAutoscaler and ensure the initial number of replicas to get the cluster to the initial number of nodes
// TODO: validate the autoscaler to scale down
glog.Info("Delete clusterAutoscaler object")
err = wait.PollImmediate(1*time.Second, waitShort, func() (bool, error) {
if err := tc.client.Delete(context.TODO(), &clusterAutoscaler); err != nil {
glog.Errorf("error querying api for clusterAutoscaler object: %v, retrying...", err)
return false, nil
}
return true, nil
})
if err != nil {
return err
}
glog.Info("Delete machineAutoscaler object")
err = wait.PollImmediate(1*time.Second, waitShort, func() (bool, error) {
if err := tc.client.Delete(context.TODO(), &machineAutoscaler); err != nil {
glog.Errorf("error querying api for machineAutoscaler object: %v, retrying...", err)
return false, nil
}
return true, nil
})
if err != nil {
return err
}
glog.Infof("Ensure initial number of replicas: %d", initialNumberOfReplicas)
err = wait.PollImmediate(1*time.Second, waitShort, func() (bool, error) {
msKey := types.NamespacedName{
Namespace: namespace,
Name: targetMachineSet.Name,
}
ms := &capiv1alpha1.MachineSet{}
if err := tc.client.Get(context.TODO(), msKey, ms); err != nil {
glog.Errorf("error querying api for machineSet object: %v, retrying...", err)
return false, nil
}
ms.Spec.Replicas = initialNumberOfReplicas
if err := tc.client.Update(context.TODO(), ms); err != nil {
glog.Errorf("error querying api for machineSet object: %v, retrying...", err)
return false, nil
}
return true, nil
})
if err != nil {
return err
}
glog.Info("Wait for cluster to match initial number of nodes")
return wait.PollImmediate(1*time.Second, waitLong, func() (bool, error) {
nodeList := corev1.NodeList{}
if err := tc.client.List(context.TODO(), &listOptions, &nodeList); err != nil {
glog.Errorf("error querying api for nodeList object: %v, retrying...", err)
return false, nil
}
glog.Infof("Initial number of nodes: %d. Current number of nodes: %d", clusterInitialTotalNodes, len(nodeList.Items))
return len(nodeList.Items) == clusterInitialTotalNodes, nil
})
}