-
Notifications
You must be signed in to change notification settings - Fork 282
/
Copy pathimport-controller_test.go
1412 lines (1295 loc) · 59.7 KB
/
import-controller_test.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
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright 2020 The CDI 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 controller
import (
"context"
"encoding/json"
"fmt"
"reflect"
"strconv"
"strings"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
kvalidation "k8s.io/apimachinery/pkg/util/validation"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/tools/record"
bootstrapapi "k8s.io/cluster-bootstrap/token/api"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
cdiv1 "kubevirt.io/containerized-data-importer-api/pkg/apis/core/v1beta1"
"kubevirt.io/containerized-data-importer/pkg/common"
cc "kubevirt.io/containerized-data-importer/pkg/controller/common"
featuregates "kubevirt.io/containerized-data-importer/pkg/feature-gates"
"kubevirt.io/containerized-data-importer/pkg/util/naming"
sdkapi "kubevirt.io/controller-lifecycle-operator-sdk/api"
)
const (
testEndPoint = "http://test.somewhere.tt.blah"
testImage = "test/image"
testPullPolicy = "Always"
testRestartPolicy = corev1.RestartPolicyOnFailure
)
var (
testStorageClass = "test-sc"
importLog = logf.Log.WithName("import-controller-test")
)
var _ = Describe("Test PVC annotations status", func() {
It("Should return complete if annotation is set", func() {
testPvc := cc.CreatePvc("testPvc1", "default", map[string]string{cc.AnnPodPhase: string(corev1.PodSucceeded)}, nil)
Expect(cc.IsPVCComplete(testPvc)).To(BeTrue())
})
It("Should NOT return complete if annotation is not succeeded", func() {
testPvc := cc.CreatePvc("testPvc1", "default", map[string]string{cc.AnnPodPhase: string(corev1.PodPending)}, nil)
Expect(cc.IsPVCComplete(testPvc)).To(BeFalse())
})
It("Should NOT return complete if annotation is missing", func() {
testPvc := cc.CreatePvc("testPvc1", "default", map[string]string{}, nil)
Expect(cc.IsPVCComplete(testPvc)).To(BeFalse())
})
It("Should be interesting if NOT complete, and endpoint and source is set", func() {
r := createImportReconciler()
testPvc := cc.CreatePvc("testPvc1", "default", map[string]string{cc.AnnPodPhase: string(corev1.PodPending), cc.AnnEndpoint: testEndPoint, cc.AnnSource: cc.SourceHTTP}, nil)
Expect(r.shouldReconcilePVC(testPvc, importLog)).To(BeTrue())
})
It("Should NOT be interesting if complete, and endpoint and source is set", func() {
r := createImportReconciler()
testPvc := cc.CreatePvc("testPvc1", "default", map[string]string{cc.AnnPodPhase: string(corev1.PodSucceeded), cc.AnnEndpoint: testEndPoint, cc.AnnSource: cc.SourceHTTP}, nil)
Expect(r.shouldReconcilePVC(testPvc, importLog)).To(BeFalse())
})
It("Should be interesting if NOT complete, and endpoint missing and source is set", func() {
r := createImportReconciler()
testPvc := cc.CreatePvc("testPvc1", "default", map[string]string{cc.AnnPodPhase: string(corev1.PodRunning), cc.AnnSource: cc.SourceHTTP}, nil)
Expect(r.shouldReconcilePVC(testPvc, importLog)).To(BeTrue())
})
It("Should be interesting if NOT complete, and endpoint set and source is missing", func() {
r := createImportReconciler()
testPvc := cc.CreatePvc("testPvc1", "default", map[string]string{cc.AnnPodPhase: string(corev1.PodPending), cc.AnnEndpoint: testEndPoint}, nil)
Expect(r.shouldReconcilePVC(testPvc, importLog)).To(BeTrue())
})
It("Should NOT be interesting if NOT BOUND, and endpoint and source is set, and honorWaitForFirstConsumerEnabled", func() {
r := createImportReconciler()
r.featureGates = &FakeFeatureGates{honorWaitForFirstConsumerEnabled: true}
testPvc := createPendingPvc("testPvc1", "default", map[string]string{cc.AnnPodPhase: string(corev1.PodPending), cc.AnnEndpoint: testEndPoint, cc.AnnSource: cc.SourceHTTP}, nil)
Expect(r.shouldReconcilePVC(testPvc, importLog)).To(BeFalse())
})
It("Should be interesting if NOT BOUND, and endpoint and source is set, and honorWaitForFirstConsumerEnabled and isImmediateBindingRequested is requested", func() {
r := createImportReconciler()
r.featureGates = &FakeFeatureGates{honorWaitForFirstConsumerEnabled: true}
testPvc := createPendingPvc("testPvc1", "default", map[string]string{
cc.AnnPodPhase: string(corev1.PodPending),
cc.AnnEndpoint: testEndPoint,
cc.AnnSource: cc.SourceHTTP,
cc.AnnImmediateBinding: "true",
}, nil)
Expect(r.shouldReconcilePVC(testPvc, importLog)).To(BeTrue())
})
It("Should be interesting if NOT BOUND, and endpoint and source is set, and honorWaitForFirstConsumerEnabled is false and isImmediateBindingRequested is requested", func() {
r := createImportReconciler()
r.featureGates = &FakeFeatureGates{honorWaitForFirstConsumerEnabled: false}
testPvc := createPendingPvc("testPvc1", "default", map[string]string{
cc.AnnPodPhase: string(corev1.PodPending),
cc.AnnEndpoint: testEndPoint,
cc.AnnSource: cc.SourceHTTP,
cc.AnnImmediateBinding: "true",
}, nil)
Expect(r.shouldReconcilePVC(testPvc, importLog)).To(BeTrue())
})
It("Should be interesting if complete, and endpoint and source is set, and multistage import not done", func() {
r := createImportReconciler()
testPvc := cc.CreatePvc("testPvc1", "default", map[string]string{
cc.AnnPodPhase: string(corev1.PodSucceeded),
cc.AnnEndpoint: testEndPoint, cc.AnnSource: cc.SourceHTTP,
cc.AnnCurrentCheckpoint: "test-check",
}, nil)
Expect(r.shouldReconcilePVC(testPvc, importLog)).To(BeTrue())
})
It("Should NOT be interesting if complete, and endpoint and source is set, and multistage import done", func() {
r := createImportReconciler()
testPvc := cc.CreatePvc("testPvc1", "default", map[string]string{
cc.AnnPodPhase: string(corev1.PodSucceeded),
cc.AnnEndpoint: testEndPoint, cc.AnnSource: cc.SourceHTTP,
cc.AnnCurrentCheckpoint: "test-check",
cc.AnnMultiStageImportDone: "true",
}, nil)
Expect(r.shouldReconcilePVC(testPvc, importLog)).To(BeFalse())
})
})
var _ = Describe("ImportConfig Controller reconcile loop", func() {
var (
reconciler *ImportReconciler
)
AfterEach(func() {
if reconciler != nil {
close(reconciler.recorder.(*record.FakeRecorder).Events)
reconciler = nil
}
})
It("Should return success if a PVC with no annotations is passed, due to it being ignored", func() {
reconciler = createImportReconciler(cc.CreatePvc("testPvc1", "default", map[string]string{}, nil))
_, err := reconciler.Reconcile(context.TODO(), reconcile.Request{NamespacedName: types.NamespacedName{Name: "testPvc1", Namespace: "default"}})
Expect(err).ToNot(HaveOccurred())
})
It("Should return success if no PVC can be found, due to it not existing", func() {
reconciler = createImportReconciler()
_, err := reconciler.Reconcile(context.TODO(), reconcile.Request{})
Expect(err).ToNot(HaveOccurred())
})
It("Should return success if no PVC can be found due to not existing in passed namespace", func() {
reconciler = createImportReconciler(cc.CreatePvc("testPvc1", "default", map[string]string{cc.AnnEndpoint: testEndPoint}, nil))
_, err := reconciler.Reconcile(context.TODO(), reconcile.Request{NamespacedName: types.NamespacedName{Name: "testPvc1", Namespace: "invalid"}})
Expect(err).ToNot(HaveOccurred())
})
It("Should succeed and be marked complete, if creating a block PVC with source none", func() {
pvc := createBlockPvc("testPvc1", "block", map[string]string{cc.AnnSource: cc.SourceNone}, nil)
pod := cc.CreateImporterTestPod(pvc, "testPvc1", nil)
pod.Status = corev1.PodStatus{
Phase: corev1.PodSucceeded,
ContainerStatuses: []v1.ContainerStatus{
{
State: v1.ContainerState{
Terminated: &v1.ContainerStateTerminated{
Message: "Import Completed",
Reason: "Reason",
},
},
},
},
}
reconciler = createImportReconciler(pvc, pod)
_, err := reconciler.Reconcile(context.TODO(), reconcile.Request{NamespacedName: types.NamespacedName{Name: "testPvc1", Namespace: "block"}})
Expect(err).ToNot(HaveOccurred())
resultPvc := &corev1.PersistentVolumeClaim{}
err = reconciler.client.Get(context.TODO(), types.NamespacedName{Name: "testPvc1", Namespace: "block"}, resultPvc)
Expect(err).ToNot(HaveOccurred())
Expect(resultPvc.GetAnnotations()[cc.AnnPodPhase]).To(BeEquivalentTo(corev1.PodSucceeded))
})
It("should do nothing and not error, if a PVC that is completed is passed", func() {
orgPvc := cc.CreatePvc("testPvc1", "default", map[string]string{cc.AnnEndpoint: testEndPoint, cc.AnnPodPhase: string(corev1.PodSucceeded)}, nil)
orgPvc.TypeMeta.APIVersion = "v1"
orgPvc.TypeMeta.Kind = "PersistentVolumeClaim"
reconciler = createImportReconciler(orgPvc)
_, err := reconciler.Reconcile(context.TODO(), reconcile.Request{NamespacedName: types.NamespacedName{Name: "testPvc1", Namespace: "default"}})
Expect(err).ToNot(HaveOccurred())
resPvc := &corev1.PersistentVolumeClaim{}
err = reconciler.client.Get(context.TODO(), types.NamespacedName{Namespace: orgPvc.Namespace, Name: orgPvc.Name}, resPvc)
Expect(err).ToNot(HaveOccurred())
Expect(reflect.DeepEqual(orgPvc, resPvc)).To(BeTrue())
})
It("Should init PVC with a POD name if a PVC with all needed annotations is passed", func() {
pvc := cc.CreatePvc("testPvc1", "default", map[string]string{cc.AnnEndpoint: testEndPoint}, nil)
pvc.Status.Phase = v1.ClaimBound
reconciler = createImportReconciler(pvc)
_, err := reconciler.Reconcile(context.TODO(), reconcile.Request{NamespacedName: types.NamespacedName{Name: "testPvc1", Namespace: "default"}})
Expect(err).ToNot(HaveOccurred())
resultPvc := &corev1.PersistentVolumeClaim{}
err = reconciler.client.Get(context.TODO(), types.NamespacedName{Name: "testPvc1", Namespace: "default"}, resultPvc)
Expect(err).ToNot(HaveOccurred())
Expect(resultPvc.GetAnnotations()[cc.AnnImportPod]).ToNot(BeEmpty())
})
It("Should requeue and not create a pod if target pvc in use", func() {
pvc := cc.CreatePvc("testPvc1", "default", map[string]string{cc.AnnEndpoint: testEndPoint, cc.AnnImportPod: "importer-testPvc1"}, nil)
pvc.Status.Phase = v1.ClaimBound
pod := podUsingPVC(pvc, false)
reconciler := createImportReconciler(pvc, pod)
result, err := reconciler.Reconcile(context.TODO(), reconcile.Request{NamespacedName: types.NamespacedName{Name: "testPvc1", Namespace: "default"}})
Expect(err).ToNot(HaveOccurred())
Expect(result.Requeue).To(BeTrue())
podList := &corev1.PodList{}
err = reconciler.client.List(context.TODO(), podList, &client.ListOptions{})
Expect(err).ToNot(HaveOccurred())
Expect(podList.Items).To(HaveLen(1))
By("Checking events recorded")
close(reconciler.recorder.(*record.FakeRecorder).Events)
found := false
for event := range reconciler.recorder.(*record.FakeRecorder).Events {
By(fmt.Sprintf("Event: %v", event))
if strings.Contains(event, "ImportTargetInUse") {
found = true
}
}
Expect(found).To(BeTrue())
})
It("Should create a POD if target pvc no longer in use (pod using the PVC failed)", func() {
pvc := cc.CreatePvc("testPvc1", "default", map[string]string{cc.AnnEndpoint: testEndPoint, cc.AnnImportPod: "importer-testPvc1"}, nil)
pvc.Status.Phase = v1.ClaimBound
podFinishedUsingPvc := podUsingPVC(pvc, false)
podFinishedUsingPvc.Status.Phase = v1.PodFailed
reconciler := createImportReconciler(pvc, podFinishedUsingPvc)
_, err := reconciler.Reconcile(context.TODO(), reconcile.Request{NamespacedName: types.NamespacedName{Name: "testPvc1", Namespace: "default"}})
Expect(err).ToNot(HaveOccurred())
pod := &corev1.Pod{}
err = reconciler.client.Get(context.TODO(), types.NamespacedName{Name: "importer-testPvc1", Namespace: "default"}, pod)
Expect(err).ToNot(HaveOccurred())
foundEndPoint := false
for _, envVar := range pod.Spec.Containers[0].Env {
if envVar.Name == common.ImporterEndpoint {
foundEndPoint = true
Expect(envVar.Value).To(Equal(testEndPoint))
}
}
Expect(foundEndPoint).To(BeTrue())
})
It("Should create a POD with node placement", func() {
pvc := cc.CreatePvc("testPvc1", "default", map[string]string{cc.AnnEndpoint: testEndPoint, cc.AnnImportPod: "importer-testPvc1"}, nil)
pvc.Status.Phase = v1.ClaimBound
reconciler = createImportReconciler(pvc)
workloads := updateCdiWithTestNodePlacement(reconciler.client)
_, err := reconciler.Reconcile(context.TODO(), reconcile.Request{NamespacedName: types.NamespacedName{Name: "testPvc1", Namespace: "default"}})
Expect(err).ToNot(HaveOccurred())
pod := &corev1.Pod{}
err = reconciler.client.Get(context.TODO(), types.NamespacedName{Name: "importer-testPvc1", Namespace: "default"}, pod)
Expect(err).ToNot(HaveOccurred())
Expect(pod.Spec.Affinity).To(Equal(workloads.Affinity))
Expect(pod.Spec.NodeSelector).To(Equal(workloads.NodeSelector))
Expect(pod.Spec.Tolerations).To(Equal(workloads.Tolerations))
})
It("Should create a POD if a PVC with all needed annotations is passed", func() {
pvc := cc.CreatePvc("testPvc1", "default", map[string]string{cc.AnnEndpoint: testEndPoint, cc.AnnImportPod: "importer-testPvc1", cc.AnnPodNetwork: "net1"}, nil)
pvc.Status.Phase = v1.ClaimBound
reconciler = createImportReconciler(pvc)
_, err := reconciler.Reconcile(context.TODO(), reconcile.Request{NamespacedName: types.NamespacedName{Name: "testPvc1", Namespace: "default"}})
Expect(err).ToNot(HaveOccurred())
pod := &corev1.Pod{}
err = reconciler.client.Get(context.TODO(), types.NamespacedName{Name: "importer-testPvc1", Namespace: "default"}, pod)
Expect(err).ToNot(HaveOccurred())
Expect(pod.Labels[common.AppKubernetesPartOfLabel]).To(Equal("testing"))
foundEndPoint := false
for _, envVar := range pod.Spec.Containers[0].Env {
if envVar.Name == common.ImporterEndpoint {
foundEndPoint = true
Expect(envVar.Value).To(Equal(testEndPoint))
}
}
Expect(foundEndPoint).To(BeTrue())
By("Verifying the pod is annotated correctly")
Expect(pod.GetAnnotations()[cc.AnnPodNetwork]).To(Equal("net1"))
Expect(pod.GetAnnotations()[cc.AnnPodSidecarInjectionIstio]).To(Equal(cc.AnnPodSidecarInjectionIstioDefault))
Expect(pod.GetAnnotations()[cc.AnnPodSidecarInjectionLinkerd]).To(Equal(cc.AnnPodSidecarInjectionLinkerdDefault))
})
It("Should not pass non-approved PVC annotation to created POD", func() {
pvc := cc.CreatePvc("testPvc1", "default", map[string]string{cc.AnnEndpoint: testEndPoint, cc.AnnImportPod: "importer-testPvc1", "annot1": "value1"}, nil)
pvc.Status.Phase = v1.ClaimBound
reconciler = createImportReconciler(pvc)
_, err := reconciler.Reconcile(context.TODO(), reconcile.Request{NamespacedName: types.NamespacedName{Name: "testPvc1", Namespace: "default"}})
Expect(err).ToNot(HaveOccurred())
pod := &corev1.Pod{}
err = reconciler.client.Get(context.TODO(), types.NamespacedName{Name: "importer-testPvc1", Namespace: "default"}, pod)
Expect(err).ToNot(HaveOccurred())
foundEndPoint := false
for _, envVar := range pod.Spec.Containers[0].Env {
if envVar.Name == common.ImporterEndpoint {
foundEndPoint = true
Expect(envVar.Value).To(Equal(testEndPoint))
}
}
Expect(foundEndPoint).To(BeTrue())
By("Verifying the pod is not annotated with annot")
Expect(pod.GetAnnotations()["annot1"]).ToNot(Equal("value1"))
})
It("Should error if a POD with the same name exists, but is not owned by the PVC, if a PVC with all needed annotations is passed", func() {
pod := &corev1.Pod{
TypeMeta: metav1.TypeMeta{
Kind: "Pod",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "importer-testPvc1",
Namespace: "default",
},
}
reconciler = createImportReconciler(cc.CreatePvc("testPvc1", "default", map[string]string{cc.AnnEndpoint: testEndPoint}, nil), pod)
_, err := reconciler.Reconcile(context.TODO(), reconcile.Request{NamespacedName: types.NamespacedName{Name: "testPvc1", Namespace: "default"}})
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("Pod is not owned by PVC"))
})
})
var _ = Describe("Update PVC from POD", func() {
var (
reconciler *ImportReconciler
)
AfterEach(func() {
if reconciler != nil {
close(reconciler.recorder.(*record.FakeRecorder).Events)
reconciler = nil
}
})
It("Should update the PVC status to succeeded, if pod is succeeded and then delete the pod", func() {
pvc := cc.CreatePvc("testPvc1", "default", map[string]string{cc.AnnEndpoint: testEndPoint, cc.AnnPodPhase: string(corev1.PodPending)}, nil)
pod := cc.CreateImporterTestPod(pvc, "testPvc1", nil)
pod.Status = corev1.PodStatus{
Phase: corev1.PodSucceeded,
ContainerStatuses: []v1.ContainerStatus{
{
State: v1.ContainerState{
Terminated: &v1.ContainerStateTerminated{
Message: "Import Completed",
Reason: "Reason",
},
},
},
},
}
reconciler = createImportReconciler(pvc, pod)
resPod := &corev1.Pod{}
err := reconciler.client.Get(context.TODO(), types.NamespacedName{Name: "importer-testPvc1", Namespace: "default"}, resPod)
Expect(err).ToNot(HaveOccurred())
err = reconciler.updatePvcFromPod(pvc, pod, reconciler.log)
Expect(err).ToNot(HaveOccurred())
By("Checking import successful event recorded")
event := <-reconciler.recorder.(*record.FakeRecorder).Events
Expect(event).To(ContainSubstring("Import Successful"))
By("Checking pvc phase has been updated")
resPvc := &corev1.PersistentVolumeClaim{}
err = reconciler.client.Get(context.TODO(), types.NamespacedName{Name: "testPvc1", Namespace: "default"}, resPvc)
Expect(err).ToNot(HaveOccurred())
Expect(resPvc.GetAnnotations()[cc.AnnPodPhase]).To(BeEquivalentTo(corev1.PodSucceeded))
By("Checking pod has been deleted")
resPod = &corev1.Pod{}
err = reconciler.client.Get(context.TODO(), types.NamespacedName{Name: "importer-testPvc1", Namespace: "default"}, resPod)
Expect(err).To(HaveOccurred())
Expect(errors.IsNotFound(err)).To(BeTrue())
Expect(resPvc.GetAnnotations()[cc.AnnRunningCondition]).To(Equal("false"))
Expect(resPvc.GetAnnotations()[cc.AnnRunningConditionMessage]).To(Equal("Import Completed"))
Expect(resPvc.GetAnnotations()[cc.AnnRunningConditionReason]).To(Equal("Reason"))
})
DescribeTable("Should handle termination messages", func(termMsg, conditionMessage string) {
pvc := cc.CreatePvc("testPvc1", "default", map[string]string{}, nil)
pod := cc.CreateImporterTestPod(pvc, "testPvc1", nil)
pod.Status = corev1.PodStatus{
Phase: corev1.PodSucceeded,
ContainerStatuses: []v1.ContainerStatus{
{
State: v1.ContainerState{
Terminated: &v1.ContainerStateTerminated{
Message: termMsg,
},
},
},
},
}
reconciler = createImportReconciler(pvc, pod)
err := reconciler.updatePvcFromPod(pvc, pod, reconciler.log)
Expect(err).ToNot(HaveOccurred())
resPvc := &corev1.PersistentVolumeClaim{}
err = reconciler.client.Get(context.TODO(), types.NamespacedName{Name: "testPvc1", Namespace: "default"}, resPvc)
Expect(err).ToNot(HaveOccurred())
Expect(resPvc.GetAnnotations()).To(HaveKeyWithValue(cc.AnnPodPhase, string(corev1.PodSucceeded)))
Expect(resPvc.GetAnnotations()).To(HaveKeyWithValue(cc.AnnRunningCondition, "false"))
Expect(resPvc.GetAnnotations()).To(HaveKeyWithValue(cc.AnnRunningConditionMessage, conditionMessage))
},
Entry("Message which can be unmarshalled", `{"preAllocationApplied": true, "message": "Import Complete"}`, "Import Complete"),
Entry("Message which cannot be unmarshalled", "somemessage", "somemessage"),
)
DescribeTable("Update the PVC labels from termination message if pod is succeeded", func(phase v1.PodPhase, updated bool) {
const testKeyExisting = "test"
const testValueExisting = "existing"
termMsg := common.TerminationMessage{
Labels: map[string]string{
"instancetype.kubevirt.io/default-instancetype": "u1.small",
"instancetype.kubevirt.io/default-preference": "fedora",
testKeyExisting: "somethingelse",
},
}
termMsgBytes, err := json.Marshal(termMsg)
Expect(err).ToNot(HaveOccurred())
// The existing key should not be overwritten
pvc := cc.CreatePvc("testPvc1", "default", map[string]string{}, map[string]string{testKeyExisting: testValueExisting})
pod := cc.CreateImporterTestPod(pvc, "testPvc1", nil)
pod.Status = corev1.PodStatus{
Phase: phase,
ContainerStatuses: []v1.ContainerStatus{
{
State: v1.ContainerState{
Terminated: &v1.ContainerStateTerminated{
Message: string(termMsgBytes),
},
},
},
},
}
reconciler = createImportReconciler(pvc, pod)
err = reconciler.updatePvcFromPod(pvc, pod, reconciler.log)
Expect(err).ToNot(HaveOccurred())
resPvc := &corev1.PersistentVolumeClaim{}
err = reconciler.client.Get(context.TODO(), types.NamespacedName{Name: "testPvc1", Namespace: "default"}, resPvc)
Expect(err).ToNot(HaveOccurred())
for k, v := range termMsg.Labels {
if k == testKeyExisting {
Expect(resPvc.GetLabels()).To(HaveKeyWithValue(testKeyExisting, testValueExisting))
continue
}
if updated {
Expect(resPvc.GetLabels()).To(HaveKeyWithValue(k, v))
} else {
Expect(resPvc.GetLabels()).ToNot(HaveKey(k))
}
}
},
Entry("should", v1.PodSucceeded, true),
Entry("should not", v1.PodFailed, false),
)
It("Should update the PVC status to running, if pod is running", func() {
pvc := cc.CreatePvc("testPvc1", "default", map[string]string{cc.AnnEndpoint: testEndPoint, cc.AnnPodPhase: string(corev1.PodPending)}, nil)
pod := cc.CreateImporterTestPod(pvc, "testPvc1", nil)
pod.Status = corev1.PodStatus{
Phase: corev1.PodRunning,
ContainerStatuses: []v1.ContainerStatus{
{
State: v1.ContainerState{
Running: &v1.ContainerStateRunning{},
},
},
},
}
reconciler = createImportReconciler(pvc, pod)
resPod := &corev1.Pod{}
err := reconciler.client.Get(context.TODO(), types.NamespacedName{Name: "importer-testPvc1", Namespace: "default"}, resPod)
Expect(err).ToNot(HaveOccurred())
err = reconciler.updatePvcFromPod(pvc, pod, reconciler.log)
Expect(err).ToNot(HaveOccurred())
By("Checking pvc phase has been updated")
resPvc := &corev1.PersistentVolumeClaim{}
err = reconciler.client.Get(context.TODO(), types.NamespacedName{Name: "testPvc1", Namespace: "default"}, resPvc)
Expect(err).ToNot(HaveOccurred())
Expect(resPvc.GetAnnotations()[cc.AnnPodPhase]).To(BeEquivalentTo(corev1.PodRunning))
Expect(resPvc.GetAnnotations()[cc.AnnImportPod]).To(Equal(pod.Name))
By("Checking pod has NOT been deleted")
resPod = &corev1.Pod{}
err = reconciler.client.Get(context.TODO(), types.NamespacedName{Name: "importer-testPvc1", Namespace: "default"}, resPod)
Expect(err).ToNot(HaveOccurred())
By("Making sure the label has been added")
Expect(resPvc.GetLabels()[common.CDILabelKey]).To(Equal(common.CDILabelValue))
Expect(resPvc.GetAnnotations()[cc.AnnRunningCondition]).To(Equal("true"))
Expect(resPvc.GetAnnotations()[cc.AnnRunningConditionMessage]).To(Equal(""))
Expect(resPvc.GetAnnotations()[cc.AnnRunningConditionReason]).To(Equal("Pod is running"))
})
It("Should create scratch PVC, if pod is pending and PVC is marked with scratch", func() {
scratchPvc := &corev1.PersistentVolumeClaim{}
scratchPvc.Name = "testPvc1-scratch"
pvc := cc.CreatePvcInStorageClass("testPvc1", "default", &testStorageClass, map[string]string{cc.AnnEndpoint: testEndPoint, cc.AnnPodPhase: string(corev1.PodPending), cc.AnnRequiresScratch: "true"}, nil, corev1.ClaimBound)
pod := cc.CreateImporterTestPod(pvc, "testPvc1", scratchPvc)
pod.Status = corev1.PodStatus{
Phase: corev1.PodPending,
ContainerStatuses: []v1.ContainerStatus{
{
State: v1.ContainerState{
Waiting: &v1.ContainerStateWaiting{
Message: "Pending",
},
},
},
},
}
reconciler = createImportReconciler(pvc, pod)
err := reconciler.updatePvcFromPod(pvc, pod, reconciler.log)
Expect(err).ToNot(HaveOccurred())
By("Checking scratch PVC has been created")
// Once all controllers are converted, we will use the runtime lib client instead of client-go and retrieval needs to change here.
err = reconciler.client.Get(context.TODO(), types.NamespacedName{Name: "testPvc1-scratch", Namespace: "default"}, scratchPvc)
Expect(err).ToNot(HaveOccurred())
// Since fsOverhead is 0, the scratch space size should be 1Mi aligned close to 1G
requestSize := scratchPvc.Spec.Resources.Requests[corev1.ResourceStorage]
Expect(requestSize.Value()).To(Equal(int64(999292928)))
Expect(scratchPvc.Labels[common.AppKubernetesPartOfLabel]).To(Equal("testing"))
resPvc := &corev1.PersistentVolumeClaim{}
err = reconciler.client.Get(context.TODO(), types.NamespacedName{Name: "testPvc1", Namespace: "default"}, resPvc)
Expect(err).ToNot(HaveOccurred())
Expect(resPvc.GetAnnotations()[cc.AnnImportPod]).To(Equal(pod.Name))
Expect(resPvc.GetAnnotations()[cc.AnnRunningCondition]).To(Equal("false"))
Expect(resPvc.GetAnnotations()[cc.AnnRunningConditionMessage]).To(Equal("Pending"))
Expect(resPvc.GetAnnotations()[cc.AnnRunningConditionReason]).To(BeEmpty())
Expect(resPvc.GetAnnotations()[cc.AnnBoundCondition]).To(Equal("false"))
Expect(resPvc.GetAnnotations()[cc.AnnBoundConditionMessage]).To(Equal("Creating scratch space"))
Expect(resPvc.GetAnnotations()[cc.AnnBoundConditionReason]).To(Equal(creatingScratch))
})
// TODO: Update me to stay in progress if we were in progress already, its a pod failure and it will get restarted.
It("Should update phase on PVC, if pod exited with error state that is NOT scratchspace exit", func() {
pvc := cc.CreatePvcInStorageClass("testPvc1", "default", &testStorageClass, map[string]string{cc.AnnEndpoint: testEndPoint, cc.AnnPodPhase: string(corev1.PodRunning)}, nil, corev1.ClaimBound)
pod := cc.CreateImporterTestPod(pvc, "testPvc1", nil)
pod.Status = corev1.PodStatus{
Phase: corev1.PodFailed,
ContainerStatuses: []corev1.ContainerStatus{
{
RestartCount: 2,
State: v1.ContainerState{
Terminated: &corev1.ContainerStateTerminated{
ExitCode: 1,
Message: "I went poof",
Reason: "Explosion",
},
},
LastTerminationState: corev1.ContainerState{
Terminated: &corev1.ContainerStateTerminated{
ExitCode: 1,
Message: "I went poof",
Reason: "Explosion",
},
},
},
},
}
reconciler = createImportReconciler(pvc, pod)
err := reconciler.updatePvcFromPod(pvc, pod, reconciler.log)
Expect(err).ToNot(HaveOccurred())
By("Checking pvc phase has been updated")
resPvc := &corev1.PersistentVolumeClaim{}
err = reconciler.client.Get(context.TODO(), types.NamespacedName{Name: "testPvc1", Namespace: "default"}, resPvc)
Expect(err).ToNot(HaveOccurred())
Expect(resPvc.GetAnnotations()[cc.AnnPodPhase]).To(BeEquivalentTo(corev1.PodFailed))
Expect(resPvc.GetAnnotations()[cc.AnnImportPod]).To(Equal(pod.Name))
Expect(resPvc.GetAnnotations()[cc.AnnPodRestarts]).To(Equal("2"))
By("Checking error event recorded")
event := <-reconciler.recorder.(*record.FakeRecorder).Events
Expect(event).To(ContainSubstring("I went poof"))
Expect(resPvc.GetAnnotations()[cc.AnnRunningCondition]).To(Equal("false"))
Expect(resPvc.GetAnnotations()[cc.AnnRunningConditionMessage]).To(Equal("I went poof"))
Expect(resPvc.GetAnnotations()[cc.AnnRunningConditionReason]).To(Equal("Explosion"))
})
It("Should NOT update phase on PVC, if pod exited with termination message stating scratch space is required", func() {
pvc := cc.CreatePvcInStorageClass("testPvc1", "default", &testStorageClass, map[string]string{cc.AnnEndpoint: testEndPoint, cc.AnnPodPhase: string(corev1.PodRunning)}, nil, corev1.ClaimBound)
scratchPvc := &corev1.PersistentVolumeClaim{}
scratchPvc.Name = "testPvc1-scratch"
pod := cc.CreateImporterTestPod(pvc, "testPvc1", scratchPvc)
pod.Status = corev1.PodStatus{
Phase: corev1.PodPending,
ContainerStatuses: []corev1.ContainerStatus{
{
State: v1.ContainerState{
Terminated: &corev1.ContainerStateTerminated{
ExitCode: 0,
Message: `{"scratchSpaceRequired": true}`,
},
},
},
},
}
reconciler = createImportReconciler(pvc, pod)
err := reconciler.updatePvcFromPod(pvc, pod, reconciler.log)
Expect(err).ToNot(HaveOccurred())
By("Checking pvc phase has been updated")
resPvc := &corev1.PersistentVolumeClaim{}
err = reconciler.client.Get(context.TODO(), types.NamespacedName{Name: "testPvc1", Namespace: "default"}, resPvc)
Expect(err).ToNot(HaveOccurred())
By("Verifying that the phase hasn't changed")
Expect(resPvc.GetAnnotations()[cc.AnnPodPhase]).To(BeEquivalentTo(corev1.PodRunning))
Expect(resPvc.GetAnnotations()[cc.AnnImportPod]).To(Equal(pod.Name))
Expect(resPvc.GetAnnotations()[cc.AnnPodRestarts]).To(Equal("0"))
// No scratch space because the pod is not in pending.
Expect(resPvc.GetAnnotations()[cc.AnnBoundCondition]).To(Equal("false"))
Expect(resPvc.GetAnnotations()[cc.AnnBoundConditionMessage]).To(Equal("Creating scratch space"))
Expect(resPvc.GetAnnotations()[cc.AnnBoundConditionReason]).To(Equal(creatingScratch))
})
It("Should mark PVC as waiting for VDDK configmap, if not already present", func() {
pvc := cc.CreatePvcInStorageClass("testPvc1", "default", &testStorageClass, map[string]string{cc.AnnEndpoint: testEndPoint, cc.AnnImportPod: "testpod", cc.AnnSource: cc.SourceVDDK}, nil, corev1.ClaimPending)
reconciler = createImportReconciler(pvc)
err := reconciler.createImporterPod(pvc)
By("Checking importer pod creation returned an error")
Expect(err).To(HaveOccurred())
By("Checking pvc annotations have been updated")
resPvc := &corev1.PersistentVolumeClaim{}
err = reconciler.client.Get(context.TODO(), types.NamespacedName{Name: "testPvc1", Namespace: "default"}, resPvc)
Expect(err).ToNot(HaveOccurred())
Expect(resPvc.GetAnnotations()[cc.AnnBoundCondition]).To(Equal("false"))
Expect(resPvc.GetAnnotations()[cc.AnnBoundConditionMessage]).To(Equal(fmt.Sprintf("waiting for v2v-vmware configmap or %s annotation for VDDK image", cc.AnnVddkInitImageURL)))
Expect(resPvc.GetAnnotations()[cc.AnnBoundConditionReason]).To(Equal(common.AwaitingVDDK))
By("Checking again after creating configmap")
configmap := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: common.VddkConfigMap,
Namespace: "cdi",
},
Data: map[string]string{
common.VddkConfigDataKey: "test",
},
}
Expect(reconciler.client.Create(context.TODO(), configmap)).To(Succeed())
Expect(reconciler.createImporterPod(pvc)).To(Succeed())
})
It("Should not mark PVC as waiting for VDDK configmap, if already present", func() {
configmap := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: common.VddkConfigMap,
Namespace: "cdi",
},
Data: map[string]string{
common.VddkConfigDataKey: "test",
},
}
pvc := cc.CreatePvcInStorageClass("testPvc1", "default", &testStorageClass, map[string]string{cc.AnnEndpoint: testEndPoint, cc.AnnImportPod: "testpod", cc.AnnSource: cc.SourceVDDK}, nil, corev1.ClaimBound)
reconciler = createImportReconciler(configmap, pvc)
err := reconciler.createImporterPod(pvc)
Expect(err).ToNot(HaveOccurred())
})
It("Should not mark PVC as waiting for VDDK configmap, if image URL annotation is present", func() {
pvc := cc.CreatePvcInStorageClass("testPvc1", "default", &testStorageClass, map[string]string{cc.AnnEndpoint: testEndPoint, cc.AnnImportPod: "testpod", cc.AnnSource: cc.SourceVDDK, cc.AnnVddkInitImageURL: "test://image"}, nil, corev1.ClaimBound)
reconciler = createImportReconciler(pvc)
err := reconciler.createImporterPod(pvc)
Expect(err).ToNot(HaveOccurred())
resPvc := &corev1.PersistentVolumeClaim{}
err = reconciler.client.Get(context.TODO(), types.NamespacedName{Name: "testPvc1", Namespace: "default"}, resPvc)
Expect(err).ToNot(HaveOccurred())
Expect(resPvc.GetAnnotations()[cc.AnnVddkInitImageURL]).To(Equal("test://image"))
Expect(common.AwaitingVDDK).ToNot(Equal(resPvc.GetAnnotations()[cc.AnnBoundConditionReason]))
})
It("Should copy VDDK connection information to annotations on PVC", func() {
pvc := cc.CreatePvcInStorageClass("testPvc1", "default", &testStorageClass, map[string]string{cc.AnnEndpoint: testEndPoint, cc.AnnPodPhase: string(corev1.PodRunning), cc.AnnSource: cc.SourceVDDK}, nil, corev1.ClaimBound)
scratchPvc := &corev1.PersistentVolumeClaim{}
scratchPvc.Name = "testPvc1-scratch"
pod := cc.CreateImporterTestPod(pvc, "testPvc1", scratchPvc)
pod.Status = corev1.PodStatus{
Phase: corev1.PodSucceeded,
ContainerStatuses: []corev1.ContainerStatus{
{
LastTerminationState: corev1.ContainerState{
Terminated: &corev1.ContainerStateTerminated{
ExitCode: 0,
Message: "",
},
},
State: v1.ContainerState{
Terminated: &corev1.ContainerStateTerminated{
ExitCode: 0,
Message: `{"vddkInfo": {"Version": "1.0.0", "Host": "esx15.test.lan"}}`,
Reason: "Completed",
},
},
},
},
}
reconciler = createImportReconciler(pvc, pod)
err := reconciler.updatePvcFromPod(pvc, pod, reconciler.log)
Expect(err).ToNot(HaveOccurred())
resPvc := &corev1.PersistentVolumeClaim{}
err = reconciler.client.Get(context.TODO(), types.NamespacedName{Name: "testPvc1", Namespace: "default"}, resPvc)
Expect(err).ToNot(HaveOccurred())
Expect(resPvc.GetAnnotations()[cc.AnnVddkHostConnection]).To(Equal("esx15.test.lan"))
Expect(resPvc.GetAnnotations()[cc.AnnVddkVersion]).To(Equal("1.0.0"))
})
It("Should delete pod for scratch space even if retainAfterCompletion is set", func() {
annotations := map[string]string{
cc.AnnEndpoint: testEndPoint,
cc.AnnImportPod: "testpod",
// gets added by controller
// cc.AnnRequiresScratch: "true",
cc.AnnSource: cc.SourceVDDK,
cc.AnnPodRetainAfterCompletion: "true",
}
pvc := cc.CreatePvcInStorageClass("testPvc1", "default", &testStorageClass, annotations, nil, corev1.ClaimPending)
pod := cc.CreateImporterTestPod(pvc, "testPvc1", nil)
pod.Status = corev1.PodStatus{
Phase: corev1.PodSucceeded,
ContainerStatuses: []corev1.ContainerStatus{
{
State: corev1.ContainerState{
Terminated: &corev1.ContainerStateTerminated{
ExitCode: 0,
Message: `{"scratchSpaceRequired": true}`,
},
},
},
},
}
reconciler = createImportReconciler(pvc, pod)
initPod := &corev1.Pod{}
err := reconciler.client.Get(context.TODO(), types.NamespacedName{Name: "importer-testPvc1", Namespace: "default"}, initPod)
Expect(err).ToNot(HaveOccurred())
Expect(initPod).ToNot(BeNil())
err = reconciler.updatePvcFromPod(pvc, pod, reconciler.log)
Expect(err).ToNot(HaveOccurred())
resPod := &corev1.Pod{}
err = reconciler.client.Get(context.TODO(), types.NamespacedName{Name: "importer-testPvc1", Namespace: "default"}, resPod)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("\"importer-testPvc1\" not found"))
})
It("Should delete pod in favor of recreating with cache=trynone in case of OOMKilled", func() {
annotations := map[string]string{
cc.AnnEndpoint: testEndPoint,
cc.AnnSource: cc.SourceRegistry,
cc.AnnRegistryImportMethod: string(cdiv1.RegistryPullNode),
}
pvc := cc.CreatePvcInStorageClass("testPvc1", "default", &testStorageClass, annotations, nil, corev1.ClaimPending)
reconciler = createImportReconciler(pvc)
_, err := reconciler.Reconcile(context.TODO(), reconcile.Request{NamespacedName: types.NamespacedName{Name: "testPvc1", Namespace: "default"}})
Expect(err).ToNot(HaveOccurred())
// First reconcile decides pods name, second creates it
_, err = reconciler.Reconcile(context.TODO(), reconcile.Request{NamespacedName: types.NamespacedName{Name: "testPvc1", Namespace: "default"}})
Expect(err).ToNot(HaveOccurred())
// Simulate OOMKilled on pod
resPod := &corev1.Pod{}
err = reconciler.client.Get(context.TODO(), types.NamespacedName{Name: "importer-testPvc1", Namespace: "default"}, resPod)
Expect(err).ToNot(HaveOccurred())
resPod.Status = corev1.PodStatus{
Phase: corev1.PodRunning,
ContainerStatuses: []corev1.ContainerStatus{
{
State: v1.ContainerState{
Terminated: &corev1.ContainerStateTerminated{
ExitCode: 137,
// This is an API
// https://github.com/kubernetes/kubernetes/blob/e38531e9a2359c2ba1505cb04d62d6810edc616e/staging/src/k8s.io/cri-api/pkg/apis/runtime/v1/api.pb.go#L5822-L5823
Reason: cc.OOMKilledReason,
},
},
},
},
}
err = reconciler.client.Status().Update(context.TODO(), resPod)
Expect(err).ToNot(HaveOccurred())
// Reconcile picks OOMKilled and deletes pod
_, err = reconciler.Reconcile(context.TODO(), reconcile.Request{NamespacedName: types.NamespacedName{Name: "testPvc1", Namespace: "default"}})
Expect(err).ToNot(HaveOccurred())
err = reconciler.client.Get(context.TODO(), types.NamespacedName{Name: "importer-testPvc1", Namespace: "default"}, resPod)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("\"importer-testPvc1\" not found"))
// Next reconcile recreates pod with cache=trynone
_, err = reconciler.Reconcile(context.TODO(), reconcile.Request{NamespacedName: types.NamespacedName{Name: "testPvc1", Namespace: "default"}})
Expect(err).ToNot(HaveOccurred())
err = reconciler.client.Get(context.TODO(), types.NamespacedName{Name: "importer-testPvc1", Namespace: "default"}, resPod)
Expect(err).ToNot(HaveOccurred())
Expect(resPod.Spec.Containers[0].Env).To(ContainElement(
corev1.EnvVar{
Name: common.CacheMode,
Value: common.CacheModeTryNone,
},
))
})
})
var _ = Describe("Create Importer Pod", func() {
var scratchPvcName = "scratchPvc"
DescribeTable("should", func(pvc *corev1.PersistentVolumeClaim, scratchPvcName *string) {
reconciler := createImportReconciler(pvc)
podEnvVar := &importPodEnvVar{
ep: "",
httpProxy: "",
httpsProxy: "",
secretName: "",
source: "",
contentType: "",
imageSize: "1G",
certConfigMap: "",
diskID: "",
filesystemOverhead: "0.055",
insecureTLS: false,
}
podArgs := &importerPodArgs{
image: testImage,
verbose: "5",
pullPolicy: testPullPolicy,
podEnvVar: podEnvVar,
pvc: pvc,
scratchPvcName: scratchPvcName,
priorityClassName: pvc.Annotations[cc.AnnPriorityClassName],
}
pod, err := createImporterPod(context.TODO(), reconciler.log, reconciler.client, podArgs, map[string]string{})
Expect(err).ToNot(HaveOccurred())
By("Verifying PVC owns pod")
Expect(pod.GetOwnerReferences()).To(HaveLen(1))
Expect(pod.GetOwnerReferences()[0].UID).To(Equal(pvc.GetUID()))
By("Verifying volume mode is correct")
if cc.GetVolumeMode(pvc) == corev1.PersistentVolumeBlock {
Expect(pod.Spec.Containers[0].VolumeDevices[0].Name).To(Equal(cc.DataVolName))
Expect(pod.Spec.Containers[0].VolumeDevices[0].DevicePath).To(Equal(common.WriteBlockPath))
if scratchPvcName != nil {
By("Verifying scratch space is set if available")
Expect(pod.Spec.Containers[0].VolumeMounts).To(HaveLen(1))
Expect(pod.Spec.Containers[0].VolumeMounts[0].Name).To(Equal(cc.ScratchVolName))
Expect(pod.Spec.Containers[0].VolumeMounts[0].MountPath).To(Equal(common.ScratchDataDir))
}
} else {
Expect(pod.Spec.Containers[0].VolumeMounts[0].Name).To(Equal(cc.DataVolName))
Expect(pod.Spec.Containers[0].VolumeMounts[0].MountPath).To(Equal(common.ImporterDataDir))
if scratchPvcName != nil {
By("Verifying scratch space is set if available")
Expect(pod.Spec.Containers[0].VolumeMounts).To(HaveLen(2))
Expect(pod.Spec.Containers[0].VolumeMounts[1].Name).To(Equal(cc.ScratchVolName))
Expect(pod.Spec.Containers[0].VolumeMounts[1].MountPath).To(Equal(common.ScratchDataDir))
}
}
By("Verifying container spec is correct")
Expect(pod.Spec.Containers[0].Image).To(Equal(testImage))
Expect(pod.Spec.Containers[0].ImagePullPolicy).To(BeEquivalentTo(testPullPolicy))
Expect(pod.Spec.Containers[0].Args[0]).To(Equal("-v=5"))
Expect(pod.Spec.PriorityClassName).To(Equal(pvc.Annotations[cc.AnnPriorityClassName]))
},
Entry("should create pod with file system volume mode", cc.CreatePvc("testPvc1", "default", map[string]string{cc.AnnEndpoint: testEndPoint, cc.AnnPodPhase: string(corev1.PodPending), cc.AnnImportPod: "podName", cc.AnnPriorityClassName: "p0"}, nil), nil),
Entry("should create pod with block volume mode", createBlockPvc("testBlockPvc1", "default", map[string]string{cc.AnnEndpoint: testEndPoint, cc.AnnPodPhase: string(corev1.PodPending), cc.AnnImportPod: "podName", cc.AnnPriorityClassName: "p0"}, nil), nil),
Entry("should create pod with file system volume mode and scratchspace", cc.CreatePvc("testPvc1", "default", map[string]string{cc.AnnEndpoint: testEndPoint, cc.AnnPodPhase: string(corev1.PodPending), cc.AnnImportPod: "podName", cc.AnnPriorityClassName: "p0"}, nil), &scratchPvcName),
Entry("should create pod with block volume mode and scratchspace", createBlockPvc("testBlockPvc1", "default", map[string]string{cc.AnnEndpoint: testEndPoint, cc.AnnPodPhase: string(corev1.PodPending), cc.AnnImportPod: "podName", cc.AnnPriorityClassName: "p0"}, nil), &scratchPvcName),
)
DescribeTable("should append current checkpoint name to importer pod", func(pvcName, checkpointID string) {
pvc := cc.CreatePvc(pvcName, "default", map[string]string{cc.AnnCurrentCheckpoint: checkpointID, cc.AnnEndpoint: testEndPoint}, nil)
pvc.Status.Phase = v1.ClaimBound
suffix := fmt.Sprintf("%s-checkpoint-%s", pvcName, checkpointID)
expectedName := fmt.Sprintf("importer-%s", suffix)
if len(expectedName) > kvalidation.DNS1123SubdomainMaxLength {
expectedName = naming.GetResourceName("importer", suffix)
}
reconciler := createImportReconciler(pvc)
_, err := reconciler.Reconcile(context.TODO(), reconcile.Request{NamespacedName: types.NamespacedName{Name: pvcName, Namespace: "default"}})
Expect(err).ToNot(HaveOccurred())
// First reconcile sets cc.AnnImportPod
resPvc := &corev1.PersistentVolumeClaim{}
err = reconciler.client.Get(context.TODO(), types.NamespacedName{Name: pvcName, Namespace: "default"}, resPvc)
Expect(err).ToNot(HaveOccurred())
Expect(resPvc.Annotations[cc.AnnImportPod]).To(Equal(expectedName))
// Second reconcile creates pod
_, err = reconciler.Reconcile(context.TODO(), reconcile.Request{NamespacedName: types.NamespacedName{Name: pvcName, Namespace: "default"}})
Expect(err).ToNot(HaveOccurred())
resPod := &corev1.Pod{}
err = reconciler.client.Get(context.TODO(), types.NamespacedName{Name: expectedName, Namespace: "default"}, resPod)
Expect(err).ToNot(HaveOccurred())
},
Entry("with short PVC and checkpoint names", "testPvc1", "snap1"),
Entry("with long checkpoint name", "testPvc1", strings.Repeat("repeating-checkpoint-id-", 10)),
Entry("with long PVC name", strings.Repeat("test-pvc-", 20), "snap1"),
Entry("with long PVC and checkpoint names", strings.Repeat("test-pvc-", 20), strings.Repeat("repeating-checkpoint-id-", 10)),
)
DescribeTable("should use the correct restart policy", func(restartPolicy string, expectedPolicy corev1.RestartPolicy) {
pvc := cc.CreatePvc(pvcName, "default", map[string]string{cc.AnnEndpoint: testEndPoint, cc.AnnImportPod: "podName"}, nil)
reconciler := createImportReconciler(pvc)
podEnvVar := &importPodEnvVar{
ep: "",
httpProxy: "",
httpsProxy: "",
secretName: "",
source: "",
contentType: "",
imageSize: "1G",
certConfigMap: "",
diskID: "",
filesystemOverhead: "0.055",
insecureTLS: false,
}
podArgs := &importerPodArgs{
image: testImage,
verbose: "5",
pullPolicy: testPullPolicy,
restartPolicy: restartPolicy,
podEnvVar: podEnvVar,
pvc: pvc,
scratchPvcName: &scratchPvcName,
priorityClassName: pvc.Annotations[cc.AnnPriorityClassName],
}
pod, err := createImporterPod(context.TODO(), reconciler.log, reconciler.client, podArgs, map[string]string{})
Expect(err).ToNot(HaveOccurred())
Expect(pod.Spec.RestartPolicy).To(BeEquivalentTo(expectedPolicy))
},
Entry("with an on failure restart policy", string(corev1.RestartPolicyOnFailure), corev1.RestartPolicyOnFailure),
Entry("with a never restart policy", string(corev1.RestartPolicyNever), corev1.RestartPolicyNever),
Entry("without a specified restart policy", "", corev1.RestartPolicy(common.DefaultRestartPolicy)),
)
It("should mount extra VDDK arguments ConfigMap when annotation is set", func() {
pvcName := "testPvc1"
podName := "testpod"
extraArgs := "testing-123"
annotations := map[string]string{
cc.AnnEndpoint: testEndPoint,
cc.AnnImportPod: podName,
cc.AnnSource: cc.SourceVDDK,
cc.AnnVddkInitImageURL: "testing-vddk",
cc.AnnVddkExtraArgs: extraArgs,
}
pvc := cc.CreatePvcInStorageClass(pvcName, "default", &testStorageClass, annotations, nil, corev1.ClaimBound)
reconciler := createImportReconciler(pvc)
_, err := reconciler.Reconcile(context.TODO(), reconcile.Request{NamespacedName: types.NamespacedName{Name: pvcName, Namespace: "default"}})
Expect(err).ToNot(HaveOccurred())
pod := &corev1.Pod{}
err = reconciler.client.Get(context.TODO(), types.NamespacedName{Name: podName, Namespace: "default"}, pod)
Expect(err).ToNot(HaveOccurred())
found := false // Look for vddk-args mount
for _, volume := range pod.Spec.Volumes {
if volume.ConfigMap != nil && volume.ConfigMap.Name == extraArgs {
found = true
}