/
e2e.go
802 lines (695 loc) · 31.4 KB
/
e2e.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
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
package tests
import (
"context"
"fmt"
"os"
"time"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
metallbv1beta1 "github.com/metallb/metallb-operator/api/v1beta1"
"github.com/metallb/metallb-operator/pkg/params"
"github.com/metallb/metallb-operator/pkg/status"
"github.com/metallb/metallb-operator/test/consts"
testclient "github.com/metallb/metallb-operator/test/e2e/client"
"github.com/metallb/metallb-operator/test/e2e/metallb"
metallbutils "github.com/metallb/metallb-operator/test/e2e/metallb"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
schv1 "k8s.io/api/scheduling/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
goclient "sigs.k8s.io/controller-runtime/pkg/client"
)
var (
UseMetallbResourcesFromFile = false
OperatorNameSpace = consts.DefaultOperatorNameSpace
isOpenshift = false
)
func init() {
if len(os.Getenv("USE_LOCAL_RESOURCES")) != 0 {
UseMetallbResourcesFromFile = true
}
if ns := os.Getenv("OO_INSTALL_NAMESPACE"); len(ns) != 0 {
OperatorNameSpace = ns
}
if os.Getenv("IS_OPENSHIFT") != "" {
isOpenshift = true
}
}
var _ = Describe("metallb", func() {
Context("MetalLB deploy should work when deployed", Ordered, func() {
var metallb *metallbv1beta1.MetalLB
var metallbCRExisted bool
BeforeAll(func() {
var err error
metallb, err = metallbutils.Get(OperatorNameSpace, UseMetallbResourcesFromFile)
Expect(err).ToNot(HaveOccurred())
metallbCRExisted = true
err = testclient.Client.Get(context.Background(), goclient.ObjectKey{Namespace: metallb.Namespace, Name: metallb.Name}, metallb)
if errors.IsNotFound(err) {
metallbCRExisted = false
Expect(testclient.Client.Create(context.Background(), metallb)).Should(Succeed())
} else {
Expect(err).ToNot(HaveOccurred())
}
})
AfterAll(func() {
if !metallbCRExisted {
deployment, err := testclient.Client.Deployments(metallb.Namespace).Get(context.Background(), consts.MetalLBDeploymentName, metav1.GetOptions{})
Expect(err).ToNot(HaveOccurred())
Expect(deployment.OwnerReferences).ToNot(BeNil())
Expect(deployment.OwnerReferences[0].Kind).To(Equal("MetalLB"))
daemonset, err := testclient.Client.DaemonSets(metallb.Namespace).Get(context.Background(), consts.MetalLBDaemonsetName, metav1.GetOptions{})
Expect(err).ToNot(HaveOccurred())
Expect(daemonset.OwnerReferences).ToNot(BeNil())
Expect(daemonset.OwnerReferences[0].Kind).To(Equal("MetalLB"))
metallbutils.DeleteAndCheck(metallb)
}
})
DescribeTable("with BGP type", func(bgpType params.BGPType) {
if isOpenshift && bgpType == params.NativeMode {
Skip("Native mode not supported with openshift")
}
checkControllerBGPMode := func(mode params.BGPType) {
bgpTypeMatcher := ContainElement(corev1.EnvVar{Name: "METALLB_BGP_TYPE", Value: string(mode)})
if mode == params.NativeMode {
bgpTypeMatcher = Not(ContainElement(HaveField("Name", "METALLB_BGP_TYPE")))
}
EventuallyWithOffset(1, func() []corev1.Container {
controllerDeployment := &appsv1.Deployment{}
err := testclient.Client.Get(
context.Background(),
types.NamespacedName{Name: consts.MetalLBDeploymentName, Namespace: metallb.Namespace},
controllerDeployment)
if err != nil {
return nil
}
return controllerDeployment.Spec.Template.Spec.Containers
}, 2*time.Second, 200*time.Millisecond).Should(
ContainElement(
And(
WithTransform(nameGetter, Equal("controller")),
WithTransform(envGetter, bgpTypeMatcher),
)))
}
checkSpeakerBGPMode := func(mode params.BGPType) {
bgpTypeMatcher := ContainElement(corev1.EnvVar{Name: "METALLB_BGP_TYPE", Value: string(mode)})
if mode == params.NativeMode {
bgpTypeMatcher = Not(ContainElement(HaveField("Name", "METALLB_BGP_TYPE")))
}
EventuallyWithOffset(1, func() []corev1.Container {
speakerDaemonSet := &appsv1.DaemonSet{}
err := testclient.Client.Get(
context.Background(),
types.NamespacedName{Name: consts.MetalLBDaemonsetName, Namespace: metallb.Namespace},
speakerDaemonSet)
if err != nil {
return nil
}
return speakerDaemonSet.Spec.Template.Spec.Containers
}, 2*time.Second, 200*time.Millisecond).Should(
ContainElement(
And(
WithTransform(nameGetter, Equal("speaker")),
WithTransform(envGetter, bgpTypeMatcher),
)))
}
By("setting the bgpType")
Eventually(func() error {
err := testclient.Client.Get(context.Background(), goclient.ObjectKey{Namespace: metallb.Namespace, Name: metallb.Name}, metallb)
if !errors.IsNotFound(err) {
Expect(err).ToNot(HaveOccurred())
}
if errors.IsNotFound(err) {
metallb := &metallbv1beta1.MetalLB{
ObjectMeta: metav1.ObjectMeta{
Name: metallb.Name,
Namespace: metallb.Namespace,
},
Spec: metallbv1beta1.MetalLBSpec{
LogLevel: metallbv1beta1.LogLevelWarn,
BGPBackend: bgpType,
},
}
err := testclient.Client.Create(context.Background(), metallb)
return err
}
metallb.Spec.BGPBackend = bgpType
err = testclient.Client.Update(context.Background(), metallb)
return err
}, metallbutils.DeployTimeout, metallbutils.Interval).ShouldNot(HaveOccurred())
By("checking the controller is running in the right bgp mode")
checkControllerBGPMode(bgpType)
By("checking MetalLB controller deployment is in running state")
Eventually(func() error {
deploy, err := testclient.Client.Deployments(metallb.Namespace).Get(context.Background(), consts.MetalLBDeploymentName, metav1.GetOptions{})
if err != nil {
return err
}
pods, err := testclient.Client.Pods(OperatorNameSpace).List(context.Background(), metav1.ListOptions{
LabelSelector: "component=controller"})
if err != nil {
return err
}
if len(pods.Items) != int(deploy.Status.Replicas) {
return fmt.Errorf("deployment %s pods are not ready, expected %d replicas got %d pods", consts.MetalLBOperatorDeploymentName, deploy.Status.Replicas, len(pods.Items))
}
for _, pod := range pods.Items {
if pod.Status.Phase != corev1.PodRunning {
return fmt.Errorf("deployment %s pod %s is not running, expected status %s got %s", consts.MetalLBOperatorDeploymentName, pod.Name, corev1.PodRunning, pod.Status.Phase)
}
}
return nil
}, metallbutils.DeployTimeout, metallbutils.Interval).ShouldNot(HaveOccurred())
By("checking the speaker is running in the right bgp mode")
checkSpeakerBGPMode(bgpType)
By("checking MetalLB daemonset is in running state")
Eventually(func() error {
daemonset, err := testclient.Client.DaemonSets(metallb.Namespace).Get(context.Background(), consts.MetalLBDaemonsetName, metav1.GetOptions{})
if err != nil {
return err
}
pods, err := testclient.Client.Pods(OperatorNameSpace).List(context.Background(), metav1.ListOptions{
LabelSelector: "component=speaker"})
if err != nil {
return err
}
if len(pods.Items) != int(daemonset.Status.DesiredNumberScheduled) {
return fmt.Errorf("daemonset %s pods are not ready, expected %d generations got %d pods", consts.MetalLBDaemonsetName, int(daemonset.Status.DesiredNumberScheduled), len(pods.Items))
}
for _, pod := range pods.Items {
if pod.Status.Phase != corev1.PodRunning {
return fmt.Errorf("daemonset %s pod %s is not running, expected status %s got %s", consts.MetalLBDaemonsetName, pod.Name, corev1.PodRunning, pod.Status.Phase)
}
}
return nil
}, metallbutils.DeployTimeout, metallbutils.Interval).ShouldNot(HaveOccurred())
By("checking MetalLB CR status is set")
Eventually(func() bool {
config := &metallbv1beta1.MetalLB{}
err := testclient.Client.Get(context.Background(), goclient.ObjectKey{Namespace: metallb.Namespace, Name: metallb.Name}, config)
Expect(err).ToNot(HaveOccurred())
if config.Status.Conditions == nil {
return false
}
for _, condition := range config.Status.Conditions {
switch condition.Type {
case status.ConditionAvailable:
if condition.Status == metav1.ConditionFalse {
return false
}
case status.ConditionProgressing:
if condition.Status == metav1.ConditionTrue {
return false
}
case status.ConditionDegraded:
if condition.Status == metav1.ConditionTrue {
return false
}
case status.ConditionUpgradeable:
if condition.Status == metav1.ConditionFalse {
return false
}
}
}
return true
}, 5*time.Minute, 5*time.Second).Should(BeTrue())
if bgpType != params.FRRK8sMode {
return
}
By("checking frr-k8s daemonset is in running state")
Eventually(func() error {
daemonset, err := testclient.Client.DaemonSets(metallb.Namespace).Get(context.Background(), consts.FRRK8SDaemonsetName, metav1.GetOptions{})
if err != nil {
return err
}
pods, err := testclient.Client.Pods(OperatorNameSpace).List(context.Background(), metav1.ListOptions{
LabelSelector: "component=frr-k8s"})
if err != nil {
return err
}
if len(pods.Items) != int(daemonset.Status.DesiredNumberScheduled) {
return fmt.Errorf("daemonset %s pods are not ready, expected %d generations got %d pods", consts.MetalLBDaemonsetName, int(daemonset.Status.DesiredNumberScheduled), len(pods.Items))
}
for _, pod := range pods.Items {
if pod.Status.Phase != corev1.PodRunning {
return fmt.Errorf("daemonset %s pod %s is not running, expected status %s got %s", consts.MetalLBDaemonsetName, pod.Name, corev1.PodRunning, pod.Status.Phase)
}
}
return nil
}, metallbutils.DeployTimeout, metallbutils.Interval).ShouldNot(HaveOccurred())
By("checking frr-k8s webhook deployment is in running state")
Eventually(func() error {
deploy, err := testclient.Client.Deployments(metallb.Namespace).Get(context.Background(), consts.FRRK8SWebhookDeploymentName, metav1.GetOptions{})
if err != nil {
return err
}
pods, err := testclient.Client.Pods(OperatorNameSpace).List(context.Background(), metav1.ListOptions{
LabelSelector: "component=frr-k8s-webhook-server"})
if err != nil {
return err
}
if len(pods.Items) != int(deploy.Status.Replicas) {
return fmt.Errorf("deployment %s pods are not ready, expected %d replicas got %d pods", consts.MetalLBOperatorDeploymentName, deploy.Status.Replicas, len(pods.Items))
}
for _, pod := range pods.Items {
if pod.Status.Phase != corev1.PodRunning {
return fmt.Errorf("deployment %s pod %s is not running, expected status %s got %s", consts.MetalLBOperatorDeploymentName, pod.Name, corev1.PodRunning, pod.Status.Phase)
}
}
return nil
}, metallbutils.DeployTimeout, metallbutils.Interval).ShouldNot(HaveOccurred())
},
Entry("Native Mode", params.NativeMode),
Entry("FRR Mode", params.FRRMode),
Entry("FRR-K8s Mode", params.FRRK8sMode),
)
})
Context("MetalLB contains incorrect data", func() {
Context("MetalLB has incorrect name", func() {
var metallb *metallbv1beta1.MetalLB
BeforeEach(func() {
var err error
metallb, err = metallbutils.Get(OperatorNameSpace, UseMetallbResourcesFromFile)
Expect(err).ToNot(HaveOccurred())
metallb.SetName("incorrectname")
Expect(testclient.Client.Create(context.Background(), metallb)).Should(Succeed())
})
AfterEach(func() {
metallbutils.Delete(metallb)
})
It("should not be reconciled", func() {
By("checking MetalLB resource status")
Eventually(func() bool {
instance := &metallbv1beta1.MetalLB{}
err := testclient.Client.Get(context.TODO(), goclient.ObjectKey{Namespace: metallb.Namespace, Name: metallb.Name}, instance)
Expect(err).ToNot(HaveOccurred())
for _, condition := range instance.Status.Conditions {
if condition.Type == status.ConditionDegraded && condition.Status == metav1.ConditionTrue {
return true
}
}
return false
}, 30*time.Second, 5*time.Second).Should(BeTrue())
})
})
Context("Correct and incorrect MetalLB resources coexist", func() {
var correct_metallb *metallbv1beta1.MetalLB
var incorrect_metallb *metallbv1beta1.MetalLB
BeforeEach(func() {
var err error
correct_metallb, err = metallbutils.Get(OperatorNameSpace, UseMetallbResourcesFromFile)
Expect(err).ToNot(HaveOccurred())
Expect(testclient.Client.Create(context.Background(), correct_metallb)).Should(Succeed())
incorrect_metallb, err = metallbutils.Get(OperatorNameSpace, UseMetallbResourcesFromFile)
Expect(err).ToNot(HaveOccurred())
incorrect_metallb.SetName("incorrectname")
Expect(testclient.Client.Create(context.Background(), incorrect_metallb)).Should(Succeed())
})
AfterEach(func() {
metallbutils.Delete(incorrect_metallb)
metallbutils.DeleteAndCheck(correct_metallb)
})
It("should have correct statuses", func() {
By("checking MetalLB resource status")
Eventually(func() bool {
instance := &metallbv1beta1.MetalLB{}
err := testclient.Client.Get(context.TODO(), goclient.ObjectKey{Namespace: incorrect_metallb.Namespace, Name: incorrect_metallb.Name}, instance)
Expect(err).ToNot(HaveOccurred())
return metallbutils.CheckConditionStatus(instance) == status.ConditionDegraded
}, 30*time.Second, 5*time.Second).Should(BeTrue())
Eventually(func() bool {
instance := &metallbv1beta1.MetalLB{}
err := testclient.Client.Get(context.TODO(), goclient.ObjectKey{Namespace: correct_metallb.Namespace, Name: correct_metallb.Name}, instance)
Expect(err).ToNot(HaveOccurred())
return metallbutils.CheckConditionStatus(instance) == status.ConditionAvailable
}, metallb.DeployTimeout, 5*time.Second).Should(BeTrue())
// Delete incorrectly named resource
err := testclient.Client.Delete(context.Background(), incorrect_metallb)
Expect(err).ToNot(HaveOccurred())
Eventually(func() bool {
err := testclient.Client.Get(context.Background(), goclient.ObjectKey{Namespace: incorrect_metallb.Namespace, Name: incorrect_metallb.Name}, incorrect_metallb)
return errors.IsNotFound(err)
}, 1*time.Minute, 5*time.Second).Should(BeTrue(), "Failed to delete MetalLB custom resource")
// Correctly named resource status should not change
Eventually(func() bool {
instance := &metallbv1beta1.MetalLB{}
err := testclient.Client.Get(context.TODO(), goclient.ObjectKey{Namespace: correct_metallb.Namespace, Name: correct_metallb.Name}, instance)
Expect(err).ToNot(HaveOccurred())
return metallbutils.CheckConditionStatus(instance) == status.ConditionAvailable
}, 30*time.Second, 5*time.Second).Should(BeTrue())
})
})
})
Context("MetalLB configured extra config parameters", func() {
var correct_metallb *metallbv1beta1.MetalLB
var priorityClass *schv1.PriorityClass
priorityClassName := "high-priority"
BeforeEach(func() {
var err error
correct_metallb, err = metallbutils.Get(OperatorNameSpace, UseMetallbResourcesFromFile)
Expect(err).ToNot(HaveOccurred())
priorityClass = metallbutils.NewPriorityClass(priorityClassName, 10000)
Expect(testclient.Client.Create(context.Background(), priorityClass)).Should(Succeed())
})
AfterEach(func() {
metallbutils.DeleteAndCheck(correct_metallb)
metallbutils.DeletePriorityClass(priorityClass)
})
It("set with additional parameters", func() {
By("create and validate resources")
metallb := metallbutils.New(OperatorNameSpace, func(m *metallbv1beta1.MetalLB) {
m.Spec.SpeakerConfig = &metallbv1beta1.Config{
PriorityClassName: priorityClass.GetName(),
Annotations: map[string]string{"test": "e2e"},
Resources: &v1.ResourceRequirements{Limits: map[v1.ResourceName]resource.Quantity{v1.ResourceCPU: *resource.NewMilliQuantity(100, resource.DecimalSI)}},
}
m.Spec.ControllerConfig = &metallbv1beta1.Config{
PriorityClassName: priorityClass.GetName(),
Annotations: map[string]string{"test": "e2e"},
Resources: &v1.ResourceRequirements{Limits: map[v1.ResourceName]resource.Quantity{v1.ResourceCPU: *resource.NewMilliQuantity(100, resource.DecimalSI)}},
Affinity: &v1.Affinity{PodAffinity: &v1.PodAffinity{RequiredDuringSchedulingIgnoredDuringExecution: []v1.PodAffinityTerm{{LabelSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"component": "controller",
}},
TopologyKey: "kubernetes.io/hostname"}}}},
}
})
Expect(testclient.Client.Create(context.Background(), metallb)).Should(Succeed())
Eventually(func() error {
controller, err := testclient.Client.Deployments(metallb.Namespace).Get(context.Background(), consts.MetalLBDeploymentName, metav1.GetOptions{})
if err != nil {
return err
}
pods, err := testclient.Client.Pods(OperatorNameSpace).List(context.Background(), metav1.ListOptions{
LabelSelector: "component=controller"})
if err != nil {
return err
}
if len(pods.Items) != int(controller.Status.Replicas) {
return fmt.Errorf("deployment %s pods are not ready, expected %d replicas got %d pods", consts.MetalLBOperatorDeploymentName, controller.Status.Replicas, len(pods.Items))
}
var controllerContainerFound bool
for _, pod := range pods.Items {
if pod.Status.Phase != corev1.PodRunning {
return fmt.Errorf("deployment %s pod %s is not running, expected status %s got %s", consts.MetalLBOperatorDeploymentName, pod.Name, corev1.PodRunning, pod.Status.Phase)
}
for _, container := range pod.Spec.Containers {
if container.Name == "controller" {
if container.Resources.Limits.Cpu().MilliValue() != int64(100) {
return fmt.Errorf("controller CPU limit should be 100")
}
controllerContainerFound = true
}
}
}
if !controllerContainerFound {
return fmt.Errorf("controller container not found")
}
if controller.Spec.Template.Spec.PriorityClassName != priorityClassName {
return fmt.Errorf("controller PriorityClassName different than '%s'", priorityClassName)
}
if controller.Spec.Template.Annotations["test"] != "e2e" {
return fmt.Errorf("controller test annotation different than 'e2e'")
}
if controller.Spec.Template.Spec.Affinity.PodAffinity.RequiredDuringSchedulingIgnoredDuringExecution[0].LabelSelector.MatchLabels["component"] != "controller" {
return fmt.Errorf("controller LabelSelector different than 'controller'")
}
if controller.Spec.Template.Spec.Affinity.PodAffinity.RequiredDuringSchedulingIgnoredDuringExecution[0].TopologyKey != "kubernetes.io/hostname" {
return fmt.Errorf("controller TopologyKey different than 'kubernetes.io/hostname'")
}
return nil
}, metallbutils.DeployTimeout, metallbutils.Interval).ShouldNot(HaveOccurred())
Eventually(func() error {
daemonset, err := testclient.Client.DaemonSets(metallb.Namespace).Get(context.Background(), consts.MetalLBDaemonsetName, metav1.GetOptions{})
if err != nil {
return err
}
pods, err := testclient.Client.Pods(OperatorNameSpace).List(context.Background(), metav1.ListOptions{
LabelSelector: "component=speaker"})
if err != nil {
return err
}
if len(pods.Items) != int(daemonset.Status.DesiredNumberScheduled) {
return fmt.Errorf("daemonset %s pods are not ready, expected %d generations got %d pods", consts.MetalLBDaemonsetName, int(daemonset.Status.DesiredNumberScheduled), len(pods.Items))
}
var speakerContainerFound bool
for _, pod := range pods.Items {
if pod.Status.Phase != corev1.PodRunning {
return fmt.Errorf("daemonset %s pod %s is not running, expected status %s got %s", consts.MetalLBDaemonsetName, pod.Name, corev1.PodRunning, pod.Status.Phase)
}
for _, container := range pod.Spec.Containers {
if container.Name == "speaker" {
if container.Resources.Limits.Cpu().MilliValue() != int64(100) {
return fmt.Errorf("speaker CPU limit should be 100")
}
speakerContainerFound = true
}
}
}
if !speakerContainerFound {
return fmt.Errorf("speaker container not found")
}
if daemonset.Spec.Template.Spec.PriorityClassName != priorityClassName {
return fmt.Errorf("speaker PriorityClassName different than '%s'", priorityClassName)
}
if daemonset.Spec.Template.Annotations["test"] != "e2e" {
return fmt.Errorf("speaker test annotation different than 'e2e'")
}
return nil
}, metallbutils.DeployTimeout, metallbutils.Interval).ShouldNot(HaveOccurred())
metallbutils.DeleteAndCheck(metallb)
})
})
Context("Update MetalLB resources", func() {
var metallb *metallbv1beta1.MetalLB
var priorityClass *schv1.PriorityClass
priorityClassName := "high-priority"
BeforeEach(func() {
var err error
metallb, err = metallbutils.Get(OperatorNameSpace, UseMetallbResourcesFromFile)
Expect(err).ToNot(HaveOccurred())
Expect(testclient.Client.Create(context.Background(), metallb)).Should(Succeed())
priorityClass = metallbutils.NewPriorityClass(priorityClassName, 10000)
Expect(testclient.Client.Create(context.Background(), priorityClass)).Should(Succeed())
})
AfterEach(func() {
metallbutils.DeleteAndCheck(metallb)
metallbutils.DeletePriorityClass(priorityClass)
})
It("patch additional parameters", func() {
Eventually(func() error {
controller, err := testclient.Client.Deployments(metallb.Namespace).Get(context.Background(), consts.MetalLBDeploymentName, metav1.GetOptions{})
if err != nil {
return err
}
pods, err := testclient.Client.Pods(OperatorNameSpace).List(context.Background(), metav1.ListOptions{
LabelSelector: "component=controller"})
if err != nil {
return err
}
if len(pods.Items) != int(controller.Status.Replicas) {
return fmt.Errorf("deployment %s pods are not ready, expected %d replicas got %d pods", consts.MetalLBOperatorDeploymentName, controller.Status.Replicas, len(pods.Items))
}
var controllerContainerFound bool
for _, pod := range pods.Items {
if pod.Status.Phase != corev1.PodRunning {
return fmt.Errorf("deployment %s pod %s is not running, expected status %s got %s", consts.MetalLBOperatorDeploymentName, pod.Name, corev1.PodRunning, pod.Status.Phase)
}
for _, container := range pod.Spec.Containers {
if container.Name == "controller" {
if container.Resources.Limits.Cpu().MilliValue() != int64(0) {
return fmt.Errorf("controller CPU limit should be 0")
}
controllerContainerFound = true
}
}
}
if !controllerContainerFound {
return fmt.Errorf("controller container not found")
}
if controller.Spec.Template.Spec.PriorityClassName != "" {
return fmt.Errorf("controller PriorityClassName should not be set")
}
if controller.Spec.Template.Annotations["test"] != "" {
return fmt.Errorf("controller test annotation should not be set")
}
return nil
}, metallbutils.DeployTimeout, metallbutils.Interval).ShouldNot(HaveOccurred())
Eventually(func() error {
daemonset, err := testclient.Client.DaemonSets(metallb.Namespace).Get(context.Background(), consts.MetalLBDaemonsetName, metav1.GetOptions{})
if err != nil {
return err
}
pods, err := testclient.Client.Pods(OperatorNameSpace).List(context.Background(), metav1.ListOptions{
LabelSelector: "component=speaker"})
if err != nil {
return err
}
if len(pods.Items) != int(daemonset.Status.DesiredNumberScheduled) {
return fmt.Errorf("daemonset %s pods are not ready, expected %d generations got %d pods", consts.MetalLBDaemonsetName, int(daemonset.Status.DesiredNumberScheduled), len(pods.Items))
}
for _, pod := range pods.Items {
if pod.Status.Phase != corev1.PodRunning {
return fmt.Errorf("daemonset %s pod %s is not running, expected status %s got %s", consts.MetalLBDaemonsetName, pod.Name, corev1.PodRunning, pod.Status.Phase)
}
}
return nil
}, metallbutils.DeployTimeout, metallbutils.Interval).ShouldNot(HaveOccurred())
instance := &metallbv1beta1.MetalLB{}
err := testclient.Client.Get(context.TODO(), goclient.ObjectKey{Namespace: metallb.Namespace, Name: metallb.Name}, instance)
Expect(err).ToNot(HaveOccurred())
instance.Spec.ControllerConfig = &metallbv1beta1.Config{
PriorityClassName: priorityClass.GetName(),
Annotations: map[string]string{"test": "e2e"},
Resources: &v1.ResourceRequirements{Limits: map[v1.ResourceName]resource.Quantity{v1.ResourceCPU: *resource.NewMilliQuantity(100, resource.DecimalSI)}},
Affinity: &v1.Affinity{PodAffinity: &v1.PodAffinity{RequiredDuringSchedulingIgnoredDuringExecution: []v1.PodAffinityTerm{{LabelSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"component": "controller",
}},
TopologyKey: "kubernetes.io/hostname"}}}},
}
err = testclient.Client.Update(context.TODO(), instance)
Expect(err).ToNot(HaveOccurred())
By("checking MetalLB resource status")
Eventually(func() error {
controller, err := testclient.Client.Deployments(metallb.Namespace).Get(context.Background(), consts.MetalLBDeploymentName, metav1.GetOptions{})
if err != nil {
return err
}
pods, err := testclient.Client.Pods(OperatorNameSpace).List(context.Background(), metav1.ListOptions{
LabelSelector: "component=controller"})
if err != nil {
return err
}
if len(pods.Items) != int(controller.Status.Replicas) {
return fmt.Errorf("deployment %s pods are not ready, expected %d replicas got %d pods", consts.MetalLBOperatorDeploymentName, controller.Status.Replicas, len(pods.Items))
}
if controller.Spec.Template.Spec.PriorityClassName != priorityClassName {
return fmt.Errorf("controller PriorityClassName different than '%s'", priorityClassName)
}
if controller.Spec.Template.Annotations["test"] != "e2e" {
return fmt.Errorf("controller test annotation different than 'e2e'")
}
if controller.Spec.Template.Spec.Affinity.PodAffinity.RequiredDuringSchedulingIgnoredDuringExecution[0].LabelSelector.MatchLabels["component"] != "controller" {
return fmt.Errorf("controller LabelSelector different than 'controller'")
}
if controller.Spec.Template.Spec.Affinity.PodAffinity.RequiredDuringSchedulingIgnoredDuringExecution[0].TopologyKey != "kubernetes.io/hostname" {
return fmt.Errorf("controller TopologyKey different than 'kubernetes.io/hostname'")
}
for _, pod := range pods.Items {
for _, container := range pod.Spec.Containers {
if container.Name == "controller" {
if container.Resources.Limits.Cpu().MilliValue() != int64(100) {
return fmt.Errorf("controller CPU limit should be 100")
}
}
}
}
return nil
}, metallbutils.DeployTimeout, metallbutils.Interval).ShouldNot(HaveOccurred())
})
})
Context("Invalid MetalLB resources", func() {
var correct_metallb *metallbv1beta1.MetalLB
BeforeEach(func() {
var err error
correct_metallb, err = metallbutils.Get(OperatorNameSpace, UseMetallbResourcesFromFile)
Expect(err).ToNot(HaveOccurred())
})
AfterEach(func() {
metallbutils.DeleteAndCheck(correct_metallb)
})
It("validate create with incorrect toleration", func() {
metallb := metallbutils.New(OperatorNameSpace, func(m *metallbv1beta1.MetalLB) {
tolerations := []corev1.Toleration{{Effect: corev1.TaintEffectNoSchedule, Key: "group",
Operator: corev1.TolerationOpEqual, TolerationSeconds: &resource.MaxMilliValue,
Value: "infra"}}
m.Spec.ControllerTolerations = tolerations
})
Expect(testclient.Client.Create(context.Background(), metallb)).ShouldNot(Succeed())
})
It("validate update with incorrect toleration", func() {
Expect(testclient.Client.Create(context.Background(), correct_metallb)).Should(Succeed())
instance := &metallbv1beta1.MetalLB{}
By("checking MetalLB CR status is set")
Eventually(func() bool {
err := testclient.Client.Get(context.Background(), goclient.ObjectKey{Namespace: correct_metallb.Namespace, Name: correct_metallb.Name}, instance)
Expect(err).ToNot(HaveOccurred())
if instance.Status.Conditions == nil {
return false
}
for _, condition := range instance.Status.Conditions {
switch condition.Type {
case status.ConditionAvailable:
if condition.Status == metav1.ConditionFalse {
return false
}
case status.ConditionProgressing:
if condition.Status == metav1.ConditionTrue {
return false
}
case status.ConditionDegraded:
if condition.Status == metav1.ConditionTrue {
return false
}
case status.ConditionUpgradeable:
if condition.Status == metav1.ConditionFalse {
return false
}
}
}
return true
}, 5*time.Minute, 5*time.Second).Should(BeTrue())
instance.Spec.SpeakerTolerations = []corev1.Toleration{{Effect: corev1.TaintEffectNoSchedule, Key: "group",
Operator: corev1.TolerationOpEqual, TolerationSeconds: &resource.MaxMilliValue,
Value: "infra"}}
Expect(testclient.Client.Update(context.Background(), instance)).ShouldNot(Succeed())
})
It("validate incorrect affinity", func() {
metallb := metallbutils.New(OperatorNameSpace, func(m *metallbv1beta1.MetalLB) {
m.Spec.ControllerConfig = &metallbv1beta1.Config{
Affinity: &v1.Affinity{NodeAffinity: &v1.NodeAffinity{PreferredDuringSchedulingIgnoredDuringExecution: []v1.PreferredSchedulingTerm{
{Weight: 0, Preference: v1.NodeSelectorTerm{MatchExpressions: []v1.NodeSelectorRequirement{{Key: "zone",
Operator: v1.NodeSelectorOpIn, Values: []string{"east"}}}}},
}}},
}
})
Expect(testclient.Client.Create(context.Background(), metallb)).ShouldNot(Succeed())
metallb = metallbutils.New(OperatorNameSpace, func(m *metallbv1beta1.MetalLB) {
m.Spec.SpeakerConfig = &metallbv1beta1.Config{
Affinity: &v1.Affinity{NodeAffinity: &v1.NodeAffinity{PreferredDuringSchedulingIgnoredDuringExecution: []v1.PreferredSchedulingTerm{
{Weight: 101, Preference: v1.NodeSelectorTerm{MatchExpressions: []v1.NodeSelectorRequirement{{Key: "zone",
Operator: v1.NodeSelectorOpIn, Values: []string{"west"}}}}},
}}},
}
})
Expect(testclient.Client.Create(context.Background(), metallb)).ShouldNot(Succeed())
metallb = metallbutils.New(OperatorNameSpace, func(m *metallbv1beta1.MetalLB) {
m.Spec.ControllerConfig = &metallbv1beta1.Config{
Affinity: &v1.Affinity{NodeAffinity: &v1.NodeAffinity{PreferredDuringSchedulingIgnoredDuringExecution: []v1.PreferredSchedulingTerm{
{Weight: 10, Preference: v1.NodeSelectorTerm{MatchExpressions: []v1.NodeSelectorRequirement{{Key: "zone",
Operator: v1.NodeSelectorOpIn, Values: []string{"east"}}}}},
}}},
}
})
Expect(testclient.Client.Create(context.Background(), metallb)).Should(Succeed())
metallbutils.DeleteAndCheck(metallb)
metallb = metallbutils.New(OperatorNameSpace, func(m *metallbv1beta1.MetalLB) {
m.Spec.SpeakerConfig = &metallbv1beta1.Config{
Affinity: &v1.Affinity{NodeAffinity: &v1.NodeAffinity{PreferredDuringSchedulingIgnoredDuringExecution: []v1.PreferredSchedulingTerm{
{Weight: 100, Preference: v1.NodeSelectorTerm{MatchExpressions: []v1.NodeSelectorRequirement{{Key: "zone",
Operator: v1.NodeSelectorOpIn, Values: []string{"west"}}}}},
}}},
}
})
Expect(testclient.Client.Create(context.Background(), metallb)).Should(Succeed())
})
})
})
// Gomega transformation functions for v1.Container
func envGetter(c v1.Container) []v1.EnvVar { return c.Env }
func nameGetter(c v1.Container) string { return c.Name }