@@ -20,7 +20,7 @@ import (
20
20
"fmt"
21
21
"time"
22
22
23
- . "github.com/onsi/ginkgo"
23
+ "github.com/onsi/ginkgo"
24
24
v1 "k8s.io/api/core/v1"
25
25
apierrs "k8s.io/apimachinery/pkg/api/errors"
26
26
"k8s.io/apimachinery/pkg/api/resource"
@@ -34,31 +34,35 @@ import (
34
34
)
35
35
36
36
const (
37
- PDRetryTimeout = 5 * time .Minute
38
- PDRetryPollTime = 5 * time .Second
37
+ pdRetryTimeout = 5 * time .Minute
38
+ pdRetryPollTime = 5 * time .Second
39
+
40
+ // VolumeSelectorKey is the key for volume selector.
39
41
VolumeSelectorKey = "e2e-pv-pool"
40
42
)
41
43
42
44
var (
43
- // Common selinux labels
45
+ // SELinuxLabel is common selinux labels.
44
46
SELinuxLabel = & v1.SELinuxOptions {
45
47
Level : "s0:c0,c1" }
46
48
)
47
49
48
- // Map of all PVs used in the multi pv-pvc tests. The key is the PV's name, which is
50
+ type pvval struct {}
51
+
52
+ // PVMap is a map of all PVs used in the multi pv-pvc tests. The key is the PV's name, which is
49
53
// guaranteed to be unique. The value is {} (empty struct) since we're only interested
50
54
// in the PV's name and if it is present. We must always Get the pv object before
51
55
// referencing any of its values, eg its ClaimRef.
52
- type pvval struct {}
53
56
type PVMap map [string ]pvval
54
57
55
- // Map of all PVCs used in the multi pv-pvc tests. The key is "namespace/pvc.Name". The
58
+ type pvcval struct {}
59
+
60
+ // PVCMap is a map of all PVCs used in the multi pv-pvc tests. The key is "namespace/pvc.Name". The
56
61
// value is {} (empty struct) since we're only interested in the PVC's name and if it is
57
62
// present. We must always Get the pvc object before referencing any of its values, eg.
58
63
// its VolumeName.
59
64
// Note: It's unsafe to add keys to a map in a loop. Their insertion in the map is
60
65
// unpredictable and can result in the same key being iterated over again.
61
- type pvcval struct {}
62
66
type PVCMap map [types.NamespacedName ]pvcval
63
67
64
68
// PersistentVolumeConfig is consumed by MakePersistentVolume() to generate a PV object
@@ -84,7 +88,6 @@ type PersistentVolumeConfig struct {
84
88
// PersistentVolumeClaimConfig is consumed by MakePersistentVolumeClaim() to generate a PVC object.
85
89
// AccessModes defaults to all modes (RWO, RWX, ROX) if left empty
86
90
// (+optional) Annotations defines the PVC's annotations
87
-
88
91
type PersistentVolumeClaimConfig struct {
89
92
AccessModes []v1.PersistentVolumeAccessMode
90
93
Annotations map [string ]string
@@ -101,7 +104,7 @@ type NodeSelection struct {
101
104
Affinity * v1.Affinity
102
105
}
103
106
104
- // Clean up a pv and pvc in a single pv/pvc test case.
107
+ // PVPVCCleanup cleans up a pv and pvc in a single pv/pvc test case.
105
108
// Note: delete errors are appended to []error so that we can attempt to delete both the pvc and pv.
106
109
func PVPVCCleanup (c clientset.Interface , ns string , pv * v1.PersistentVolume , pvc * v1.PersistentVolumeClaim ) []error {
107
110
var errs []error
@@ -125,7 +128,7 @@ func PVPVCCleanup(c clientset.Interface, ns string, pv *v1.PersistentVolume, pvc
125
128
return errs
126
129
}
127
130
128
- // Clean up pvs and pvcs in multi-pv-pvc test cases. Entries found in the pv and claim maps are
131
+ // PVPVCMapCleanup Cleans up pvs and pvcs in multi-pv-pvc test cases. Entries found in the pv and claim maps are
129
132
// deleted as long as the Delete api call succeeds.
130
133
// Note: delete errors are appended to []error so that as many pvcs and pvs as possible are deleted.
131
134
func PVPVCMapCleanup (c clientset.Interface , ns string , pvols PVMap , claims PVCMap ) []error {
@@ -151,7 +154,7 @@ func PVPVCMapCleanup(c clientset.Interface, ns string, pvols PVMap, claims PVCMa
151
154
return errs
152
155
}
153
156
154
- // Delete the PV.
157
+ // DeletePersistentVolume deletes the PV.
155
158
func DeletePersistentVolume (c clientset.Interface , pvName string ) error {
156
159
if c != nil && len (pvName ) > 0 {
157
160
Logf ("Deleting PersistentVolume %q" , pvName )
@@ -163,7 +166,7 @@ func DeletePersistentVolume(c clientset.Interface, pvName string) error {
163
166
return nil
164
167
}
165
168
166
- // Delete the Claim
169
+ // DeletePersistentVolumeClaim deletes the Claim.
167
170
func DeletePersistentVolumeClaim (c clientset.Interface , pvcName string , ns string ) error {
168
171
if c != nil && len (pvcName ) > 0 {
169
172
Logf ("Deleting PersistentVolumeClaim %q" , pvcName )
@@ -175,7 +178,7 @@ func DeletePersistentVolumeClaim(c clientset.Interface, pvcName string, ns strin
175
178
return nil
176
179
}
177
180
178
- // Delete the PVC and wait for the PV to enter its expected phase. Validate that the PV
181
+ // DeletePVCandValidatePV deletes the PVC and waits for the PV to enter its expected phase. Validate that the PV
179
182
// has been reclaimed (assumption here about reclaimPolicy). Caller tells this func which
180
183
// phase value to expect for the pv bound to the to-be-deleted claim.
181
184
func DeletePVCandValidatePV (c clientset.Interface , ns string , pvc * v1.PersistentVolumeClaim , pv * v1.PersistentVolume , expectPVPhase v1.PersistentVolumePhase ) error {
@@ -216,7 +219,7 @@ func DeletePVCandValidatePV(c clientset.Interface, ns string, pvc *v1.Persistent
216
219
return nil
217
220
}
218
221
219
- // Wraps deletePVCandValidatePV() by calling the function in a loop over the PV map. Only bound PVs
222
+ // DeletePVCandValidatePVGroup wraps deletePVCandValidatePV() by calling the function in a loop over the PV map. Only bound PVs
220
223
// are deleted. Validates that the claim was deleted and the PV is in the expected Phase (Released,
221
224
// Available, Bound).
222
225
// Note: if there are more claims than pvs then some of the remaining claims may bind to just made
@@ -269,12 +272,12 @@ func createPV(c clientset.Interface, pv *v1.PersistentVolume) (*v1.PersistentVol
269
272
return pv , nil
270
273
}
271
274
272
- // create the PV resource. Fails test on error.
275
+ // CreatePV creates the PV resource. Fails test on error.
273
276
func CreatePV (c clientset.Interface , pv * v1.PersistentVolume ) (* v1.PersistentVolume , error ) {
274
277
return createPV (c , pv )
275
278
}
276
279
277
- // create the PVC resource. Fails test on error.
280
+ // CreatePVC creates the PVC resource. Fails test on error.
278
281
func CreatePVC (c clientset.Interface , ns string , pvc * v1.PersistentVolumeClaim ) (* v1.PersistentVolumeClaim , error ) {
279
282
pvc , err := c .CoreV1 ().PersistentVolumeClaims (ns ).Create (pvc )
280
283
if err != nil {
@@ -283,7 +286,7 @@ func CreatePVC(c clientset.Interface, ns string, pvc *v1.PersistentVolumeClaim)
283
286
return pvc , nil
284
287
}
285
288
286
- // Create a PVC followed by the PV based on the passed in nfs-server ip and
289
+ // CreatePVCPV creates a PVC followed by the PV based on the passed in nfs-server ip and
287
290
// namespace. If the "preBind" bool is true then pre-bind the PV to the PVC
288
291
// via the PV's ClaimRef. Return the pv and pvc to reflect the created objects.
289
292
// Note: in the pre-bind case the real PVC name, which is generated, is not
@@ -300,7 +303,7 @@ func CreatePVCPV(c clientset.Interface, pvConfig PersistentVolumeConfig, pvcConf
300
303
// make the pv spec
301
304
pv := MakePersistentVolume (pvConfig )
302
305
303
- By (fmt .Sprintf ("Creating a PVC followed by a%s PV" , preBindMsg ))
306
+ ginkgo . By (fmt .Sprintf ("Creating a PVC followed by a%s PV" , preBindMsg ))
304
307
pvc , err := CreatePVC (c , ns , pvc )
305
308
if err != nil {
306
309
return nil , nil , err
@@ -317,7 +320,7 @@ func CreatePVCPV(c clientset.Interface, pvConfig PersistentVolumeConfig, pvcConf
317
320
return pv , pvc , nil
318
321
}
319
322
320
- // Create a PV followed by the PVC based on the passed in nfs-server ip and
323
+ // CreatePVPVC creates a PV followed by the PVC based on the passed in nfs-server ip and
321
324
// namespace. If the "preBind" bool is true then pre-bind the PVC to the PV
322
325
// via the PVC's VolumeName. Return the pv and pvc to reflect the created
323
326
// objects.
@@ -351,7 +354,7 @@ func CreatePVPVC(c clientset.Interface, pvConfig PersistentVolumeConfig, pvcConf
351
354
return pv , pvc , nil
352
355
}
353
356
354
- // Create the desired number of PVs and PVCs and return them in separate maps. If the
357
+ // CreatePVsPVCs creates the desired number of PVs and PVCs and returns them in separate maps. If the
355
358
// number of PVs != the number of PVCs then the min of those two counts is the number of
356
359
// PVs expected to bind. If a Create error occurs, the returned maps may contain pv and pvc
357
360
// entries for the resources that were successfully created. In other words, when the caller
@@ -399,7 +402,7 @@ func CreatePVsPVCs(numpvs, numpvcs int, c clientset.Interface, ns string, pvConf
399
402
return pvMap , pvcMap , nil
400
403
}
401
404
402
- // Wait for the pv and pvc to bind to each other.
405
+ // WaitOnPVandPVC waits for the pv and pvc to bind to each other.
403
406
func WaitOnPVandPVC (c clientset.Interface , ns string , pv * v1.PersistentVolume , pvc * v1.PersistentVolumeClaim ) error {
404
407
// Wait for newly created PVC to bind to the PV
405
408
Logf ("Waiting for PV %v to bind to PVC %v" , pv .Name , pvc .Name )
@@ -442,7 +445,7 @@ func WaitOnPVandPVC(c clientset.Interface, ns string, pv *v1.PersistentVolume, p
442
445
return nil
443
446
}
444
447
445
- // Search for bound PVs and PVCs by examining pvols for non-nil claimRefs.
448
+ // WaitAndVerifyBinds searches for bound PVs and PVCs by examining pvols for non-nil claimRefs.
446
449
// NOTE: Each iteration waits for a maximum of 3 minutes per PV and, if the PV is bound,
447
450
// up to 3 minutes for the PVC. When the number of PVs != number of PVCs, this can lead
448
451
// to situations where the maximum wait times are reached several times in succession,
@@ -495,15 +498,15 @@ func WaitAndVerifyBinds(c clientset.Interface, ns string, pvols PVMap, claims PV
495
498
496
499
// Test the pod's exit code to be zero.
497
500
func testPodSuccessOrFail (c clientset.Interface , ns string , pod * v1.Pod ) error {
498
- By ("Pod should terminate with exitcode 0 (success)" )
501
+ ginkgo . By ("Pod should terminate with exitcode 0 (success)" )
499
502
if err := WaitForPodSuccessInNamespace (c , pod .Name , ns ); err != nil {
500
503
return fmt .Errorf ("pod %q failed to reach Success: %v" , pod .Name , err )
501
504
}
502
505
Logf ("Pod %v succeeded " , pod .Name )
503
506
return nil
504
507
}
505
508
506
- // Deletes the passed-in pod and waits for the pod to be terminated. Resilient to the pod
509
+ // DeletePodWithWait deletes the passed-in pod and waits for the pod to be terminated. Resilient to the pod
507
510
// not existing.
508
511
func DeletePodWithWait (f * Framework , c clientset.Interface , pod * v1.Pod ) error {
509
512
if pod == nil {
@@ -512,7 +515,7 @@ func DeletePodWithWait(f *Framework, c clientset.Interface, pod *v1.Pod) error {
512
515
return DeletePodWithWaitByName (f , c , pod .GetName (), pod .GetNamespace ())
513
516
}
514
517
515
- // Deletes the named and namespaced pod and waits for the pod to be terminated. Resilient to the pod
518
+ // DeletePodWithWaitByName deletes the named and namespaced pod and waits for the pod to be terminated. Resilient to the pod
516
519
// not existing.
517
520
func DeletePodWithWaitByName (f * Framework , c clientset.Interface , podName , podNamespace string ) error {
518
521
Logf ("Deleting pod %q in namespace %q" , podName , podNamespace )
@@ -531,7 +534,7 @@ func DeletePodWithWaitByName(f *Framework, c clientset.Interface, podName, podNa
531
534
return nil
532
535
}
533
536
534
- // Create the test pod, wait for (hopefully) success, and then delete the pod.
537
+ // CreateWaitAndDeletePod creates the test pod, wait for (hopefully) success, and then delete the pod.
535
538
// Note: need named return value so that the err assignment in the defer sets the returned error.
536
539
// Has been shown to be necessary using Go 1.7.
537
540
func CreateWaitAndDeletePod (f * Framework , c clientset.Interface , ns string , pvc * v1.PersistentVolumeClaim ) (err error ) {
@@ -560,7 +563,7 @@ func makePvcKey(ns, name string) types.NamespacedName {
560
563
return types.NamespacedName {Namespace : ns , Name : name }
561
564
}
562
565
563
- // Returns a PV definition based on the nfs server IP. If the PVC is not nil
566
+ // MakePersistentVolume returns a PV definition based on the nfs server IP. If the PVC is not nil
564
567
// then the PV is defined with a ClaimRef which includes the PVC's namespace.
565
568
// If the PVC is nil then the PV is not defined with a ClaimRef. If no reclaimPolicy
566
569
// is assigned, assumes "Retain". Specs are expected to match the test's PVC.
@@ -607,7 +610,7 @@ func MakePersistentVolume(pvConfig PersistentVolumeConfig) *v1.PersistentVolume
607
610
}
608
611
}
609
612
610
- // Returns a PVC definition based on the namespace.
613
+ // MakePersistentVolumeClaim returns a PVC definition based on the namespace.
611
614
// Note: if this PVC is intended to be pre-bound to a PV, whose name is not
612
615
// known until the PV is instantiated, then the func CreatePVPVC will add
613
616
// pvc.Spec.VolumeName to this claim.
@@ -641,7 +644,7 @@ func MakePersistentVolumeClaim(cfg PersistentVolumeClaimConfig, ns string) *v1.P
641
644
642
645
func createPDWithRetry (zone string ) (string , error ) {
643
646
var err error
644
- for start := time .Now (); time .Since (start ) < PDRetryTimeout ; time .Sleep (PDRetryPollTime ) {
647
+ for start := time .Now (); time .Since (start ) < pdRetryTimeout ; time .Sleep (pdRetryPollTime ) {
645
648
newDiskName , err := createPD (zone )
646
649
if err != nil {
647
650
Logf ("Couldn't create a new PD, sleeping 5 seconds: %v" , err )
@@ -653,20 +656,23 @@ func createPDWithRetry(zone string) (string, error) {
653
656
return "" , err
654
657
}
655
658
659
+ // CreatePDWithRetry creates PD with retry.
656
660
func CreatePDWithRetry () (string , error ) {
657
661
return createPDWithRetry ("" )
658
662
}
659
663
664
+ // CreatePDWithRetryAndZone creates PD on zone with retry.
660
665
func CreatePDWithRetryAndZone (zone string ) (string , error ) {
661
666
return createPDWithRetry (zone )
662
667
}
663
668
669
+ // DeletePDWithRetry deletes PD with retry.
664
670
func DeletePDWithRetry (diskName string ) error {
665
671
var err error
666
- for start := time .Now (); time .Since (start ) < PDRetryTimeout ; time .Sleep (PDRetryPollTime ) {
672
+ for start := time .Now (); time .Since (start ) < pdRetryTimeout ; time .Sleep (pdRetryPollTime ) {
667
673
err = deletePD (diskName )
668
674
if err != nil {
669
- Logf ("Couldn't delete PD %q, sleeping %v: %v" , diskName , PDRetryPollTime , err )
675
+ Logf ("Couldn't delete PD %q, sleeping %v: %v" , diskName , pdRetryPollTime , err )
670
676
continue
671
677
}
672
678
Logf ("Successfully deleted PD %q." , diskName )
@@ -686,13 +692,13 @@ func deletePD(pdName string) error {
686
692
return TestContext .CloudConfig .Provider .DeletePD (pdName )
687
693
}
688
694
689
- // Returns a pod definition based on the namespace. The pod references the PVC's
695
+ // MakeWritePod returns a pod definition based on the namespace. The pod references the PVC's
690
696
// name.
691
697
func MakeWritePod (ns string , pvc * v1.PersistentVolumeClaim ) * v1.Pod {
692
698
return MakePod (ns , nil , []* v1.PersistentVolumeClaim {pvc }, true , "touch /mnt/volume1/SUCCESS && (id -G | grep -E '\\ b777\\ b')" )
693
699
}
694
700
695
- // Returns a pod definition based on the namespace. The pod references the PVC's
701
+ // MakePod returns a pod definition based on the namespace. The pod references the PVC's
696
702
// name. A slice of BASH commands can be supplied as args to be run by the pod
697
703
func MakePod (ns string , nodeSelector map [string ]string , pvclaims []* v1.PersistentVolumeClaim , isPrivileged bool , command string ) * v1.Pod {
698
704
if len (command ) == 0 {
@@ -737,8 +743,8 @@ func MakePod(ns string, nodeSelector map[string]string, pvclaims []*v1.Persisten
737
743
return podSpec
738
744
}
739
745
740
- // Returns a pod definition based on the namespace using nginx image
741
- func MakeNginxPod (ns string , nodeSelector map [string ]string , pvclaims []* v1.PersistentVolumeClaim ) * v1.Pod {
746
+ // makeNginxPod returns a pod definition based on the namespace using nginx image
747
+ func makeNginxPod (ns string , nodeSelector map [string ]string , pvclaims []* v1.PersistentVolumeClaim ) * v1.Pod {
742
748
podSpec := & v1.Pod {
743
749
TypeMeta : metav1.TypeMeta {
744
750
Kind : "Pod" ,
@@ -778,7 +784,7 @@ func MakeNginxPod(ns string, nodeSelector map[string]string, pvclaims []*v1.Pers
778
784
return podSpec
779
785
}
780
786
781
- // Returns a pod definition based on the namespace. The pod references the PVC's
787
+ // MakeSecPod returns a pod definition based on the namespace. The pod references the PVC's
782
788
// name. A slice of BASH commands can be supplied as args to be run by the pod.
783
789
// SELinux testing requires to pass HostIPC and HostPID as booleansi arguments.
784
790
func MakeSecPod (ns string , pvclaims []* v1.PersistentVolumeClaim , isPrivileged bool , command string , hostIPC bool , hostPID bool , seLinuxLabel * v1.SELinuxOptions , fsGroup * int64 ) * v1.Pod {
@@ -860,8 +866,9 @@ func CreatePod(client clientset.Interface, namespace string, nodeSelector map[st
860
866
return pod , nil
861
867
}
862
868
869
+ // CreateNginxPod creates an enginx pod.
863
870
func CreateNginxPod (client clientset.Interface , namespace string , nodeSelector map [string ]string , pvclaims []* v1.PersistentVolumeClaim ) (* v1.Pod , error ) {
864
- pod := MakeNginxPod (namespace , nodeSelector , pvclaims )
871
+ pod := makeNginxPod (namespace , nodeSelector , pvclaims )
865
872
pod , err := client .CoreV1 ().Pods (namespace ).Create (pod )
866
873
if err != nil {
867
874
return nil , fmt .Errorf ("pod Create API error: %v" , err )
@@ -879,12 +886,12 @@ func CreateNginxPod(client clientset.Interface, namespace string, nodeSelector m
879
886
return pod , nil
880
887
}
881
888
882
- // create security pod with given claims
889
+ // CreateSecPod creates security pod with given claims
883
890
func CreateSecPod (client clientset.Interface , namespace string , pvclaims []* v1.PersistentVolumeClaim , isPrivileged bool , command string , hostIPC bool , hostPID bool , seLinuxLabel * v1.SELinuxOptions , fsGroup * int64 , timeout time.Duration ) (* v1.Pod , error ) {
884
891
return CreateSecPodWithNodeSelection (client , namespace , pvclaims , isPrivileged , command , hostIPC , hostPID , seLinuxLabel , fsGroup , NodeSelection {}, timeout )
885
892
}
886
893
887
- // create security pod with given claims
894
+ // CreateSecPodWithNodeSelection creates security pod with given claims
888
895
func CreateSecPodWithNodeSelection (client clientset.Interface , namespace string , pvclaims []* v1.PersistentVolumeClaim , isPrivileged bool , command string , hostIPC bool , hostPID bool , seLinuxLabel * v1.SELinuxOptions , fsGroup * int64 , node NodeSelection , timeout time.Duration ) (* v1.Pod , error ) {
889
896
pod := MakeSecPod (namespace , pvclaims , isPrivileged , command , hostIPC , hostPID , seLinuxLabel , fsGroup )
890
897
// Setting node
@@ -948,7 +955,7 @@ func SetAntiAffinity(nodeSelection *NodeSelection, nodeName string) {
948
955
SetNodeAffinityRequirement (nodeSelection , v1 .NodeSelectorOpNotIn , nodeName )
949
956
}
950
957
951
- // Define and create a pod with a mounted PV. Pod runs infinite loop until killed.
958
+ // CreateClientPod defines and creates a pod with a mounted PV. Pod runs infinite loop until killed.
952
959
func CreateClientPod (c clientset.Interface , ns string , pvc * v1.PersistentVolumeClaim ) (* v1.Pod , error ) {
953
960
return CreatePod (c , ns , nil , []* v1.PersistentVolumeClaim {pvc }, true , "" )
954
961
}
@@ -973,7 +980,7 @@ func CreateUnschedulablePod(client clientset.Interface, namespace string, nodeSe
973
980
return pod , nil
974
981
}
975
982
976
- // wait until all pvcs phase set to bound
983
+ // WaitForPVClaimBoundPhase waits until all pvcs phase set to bound
977
984
func WaitForPVClaimBoundPhase (client clientset.Interface , pvclaims []* v1.PersistentVolumeClaim , timeout time.Duration ) ([]* v1.PersistentVolume , error ) {
978
985
persistentvolumes := make ([]* v1.PersistentVolume , len (pvclaims ))
979
986
@@ -996,6 +1003,7 @@ func WaitForPVClaimBoundPhase(client clientset.Interface, pvclaims []*v1.Persist
996
1003
return persistentvolumes , nil
997
1004
}
998
1005
1006
+ // CreatePVSource creates a PV source.
999
1007
func CreatePVSource (zone string ) (* v1.PersistentVolumeSource , error ) {
1000
1008
diskName , err := CreatePDWithRetryAndZone (zone )
1001
1009
if err != nil {
@@ -1004,10 +1012,12 @@ func CreatePVSource(zone string) (*v1.PersistentVolumeSource, error) {
1004
1012
return TestContext .CloudConfig .Provider .CreatePVSource (zone , diskName )
1005
1013
}
1006
1014
1015
+ // DeletePVSource deletes a PV source.
1007
1016
func DeletePVSource (pvSource * v1.PersistentVolumeSource ) error {
1008
1017
return TestContext .CloudConfig .Provider .DeletePVSource (pvSource )
1009
1018
}
1010
1019
1020
+ // GetBoundPV returns a PV details.
1011
1021
func GetBoundPV (client clientset.Interface , pvc * v1.PersistentVolumeClaim ) (* v1.PersistentVolume , error ) {
1012
1022
// Get new copy of the claim
1013
1023
claim , err := client .CoreV1 ().PersistentVolumeClaims (pvc .Namespace ).Get (pvc .Name , metav1.GetOptions {})
0 commit comments