Skip to content

Commit a38fdcf

Browse files
authored
Merge pull request kubernetes#75975 from oomichi/golint-e2e-framework-pv_util
Fix golint failures of e2e/framework/pv_util.go
2 parents 42ff0d8 + 8563167 commit a38fdcf

File tree

1 file changed

+52
-42
lines changed

1 file changed

+52
-42
lines changed

test/e2e/framework/pv_util.go

Lines changed: 52 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
"fmt"
2121
"time"
2222

23-
. "github.com/onsi/ginkgo"
23+
"github.com/onsi/ginkgo"
2424
v1 "k8s.io/api/core/v1"
2525
apierrs "k8s.io/apimachinery/pkg/api/errors"
2626
"k8s.io/apimachinery/pkg/api/resource"
@@ -34,31 +34,35 @@ import (
3434
)
3535

3636
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.
3941
VolumeSelectorKey = "e2e-pv-pool"
4042
)
4143

4244
var (
43-
// Common selinux labels
45+
// SELinuxLabel is common selinux labels.
4446
SELinuxLabel = &v1.SELinuxOptions{
4547
Level: "s0:c0,c1"}
4648
)
4749

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
4953
// guaranteed to be unique. The value is {} (empty struct) since we're only interested
5054
// in the PV's name and if it is present. We must always Get the pv object before
5155
// referencing any of its values, eg its ClaimRef.
52-
type pvval struct{}
5356
type PVMap map[string]pvval
5457

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
5661
// value is {} (empty struct) since we're only interested in the PVC's name and if it is
5762
// present. We must always Get the pvc object before referencing any of its values, eg.
5863
// its VolumeName.
5964
// Note: It's unsafe to add keys to a map in a loop. Their insertion in the map is
6065
// unpredictable and can result in the same key being iterated over again.
61-
type pvcval struct{}
6266
type PVCMap map[types.NamespacedName]pvcval
6367

6468
// PersistentVolumeConfig is consumed by MakePersistentVolume() to generate a PV object
@@ -84,7 +88,6 @@ type PersistentVolumeConfig struct {
8488
// PersistentVolumeClaimConfig is consumed by MakePersistentVolumeClaim() to generate a PVC object.
8589
// AccessModes defaults to all modes (RWO, RWX, ROX) if left empty
8690
// (+optional) Annotations defines the PVC's annotations
87-
8891
type PersistentVolumeClaimConfig struct {
8992
AccessModes []v1.PersistentVolumeAccessMode
9093
Annotations map[string]string
@@ -101,7 +104,7 @@ type NodeSelection struct {
101104
Affinity *v1.Affinity
102105
}
103106

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.
105108
// Note: delete errors are appended to []error so that we can attempt to delete both the pvc and pv.
106109
func PVPVCCleanup(c clientset.Interface, ns string, pv *v1.PersistentVolume, pvc *v1.PersistentVolumeClaim) []error {
107110
var errs []error
@@ -125,7 +128,7 @@ func PVPVCCleanup(c clientset.Interface, ns string, pv *v1.PersistentVolume, pvc
125128
return errs
126129
}
127130

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
129132
// deleted as long as the Delete api call succeeds.
130133
// Note: delete errors are appended to []error so that as many pvcs and pvs as possible are deleted.
131134
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
151154
return errs
152155
}
153156

154-
// Delete the PV.
157+
// DeletePersistentVolume deletes the PV.
155158
func DeletePersistentVolume(c clientset.Interface, pvName string) error {
156159
if c != nil && len(pvName) > 0 {
157160
Logf("Deleting PersistentVolume %q", pvName)
@@ -163,7 +166,7 @@ func DeletePersistentVolume(c clientset.Interface, pvName string) error {
163166
return nil
164167
}
165168

166-
// Delete the Claim
169+
// DeletePersistentVolumeClaim deletes the Claim.
167170
func DeletePersistentVolumeClaim(c clientset.Interface, pvcName string, ns string) error {
168171
if c != nil && len(pvcName) > 0 {
169172
Logf("Deleting PersistentVolumeClaim %q", pvcName)
@@ -175,7 +178,7 @@ func DeletePersistentVolumeClaim(c clientset.Interface, pvcName string, ns strin
175178
return nil
176179
}
177180

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
179182
// has been reclaimed (assumption here about reclaimPolicy). Caller tells this func which
180183
// phase value to expect for the pv bound to the to-be-deleted claim.
181184
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
216219
return nil
217220
}
218221

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
220223
// are deleted. Validates that the claim was deleted and the PV is in the expected Phase (Released,
221224
// Available, Bound).
222225
// 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
269272
return pv, nil
270273
}
271274

272-
// create the PV resource. Fails test on error.
275+
// CreatePV creates the PV resource. Fails test on error.
273276
func CreatePV(c clientset.Interface, pv *v1.PersistentVolume) (*v1.PersistentVolume, error) {
274277
return createPV(c, pv)
275278
}
276279

277-
// create the PVC resource. Fails test on error.
280+
// CreatePVC creates the PVC resource. Fails test on error.
278281
func CreatePVC(c clientset.Interface, ns string, pvc *v1.PersistentVolumeClaim) (*v1.PersistentVolumeClaim, error) {
279282
pvc, err := c.CoreV1().PersistentVolumeClaims(ns).Create(pvc)
280283
if err != nil {
@@ -283,7 +286,7 @@ func CreatePVC(c clientset.Interface, ns string, pvc *v1.PersistentVolumeClaim)
283286
return pvc, nil
284287
}
285288

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
287290
// namespace. If the "preBind" bool is true then pre-bind the PV to the PVC
288291
// via the PV's ClaimRef. Return the pv and pvc to reflect the created objects.
289292
// 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
300303
// make the pv spec
301304
pv := MakePersistentVolume(pvConfig)
302305

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))
304307
pvc, err := CreatePVC(c, ns, pvc)
305308
if err != nil {
306309
return nil, nil, err
@@ -317,7 +320,7 @@ func CreatePVCPV(c clientset.Interface, pvConfig PersistentVolumeConfig, pvcConf
317320
return pv, pvc, nil
318321
}
319322

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
321324
// namespace. If the "preBind" bool is true then pre-bind the PVC to the PV
322325
// via the PVC's VolumeName. Return the pv and pvc to reflect the created
323326
// objects.
@@ -351,7 +354,7 @@ func CreatePVPVC(c clientset.Interface, pvConfig PersistentVolumeConfig, pvcConf
351354
return pv, pvc, nil
352355
}
353356

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
355358
// number of PVs != the number of PVCs then the min of those two counts is the number of
356359
// PVs expected to bind. If a Create error occurs, the returned maps may contain pv and pvc
357360
// 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
399402
return pvMap, pvcMap, nil
400403
}
401404

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.
403406
func WaitOnPVandPVC(c clientset.Interface, ns string, pv *v1.PersistentVolume, pvc *v1.PersistentVolumeClaim) error {
404407
// Wait for newly created PVC to bind to the PV
405408
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
442445
return nil
443446
}
444447

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.
446449
// NOTE: Each iteration waits for a maximum of 3 minutes per PV and, if the PV is bound,
447450
// up to 3 minutes for the PVC. When the number of PVs != number of PVCs, this can lead
448451
// 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
495498

496499
// Test the pod's exit code to be zero.
497500
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)")
499502
if err := WaitForPodSuccessInNamespace(c, pod.Name, ns); err != nil {
500503
return fmt.Errorf("pod %q failed to reach Success: %v", pod.Name, err)
501504
}
502505
Logf("Pod %v succeeded ", pod.Name)
503506
return nil
504507
}
505508

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
507510
// not existing.
508511
func DeletePodWithWait(f *Framework, c clientset.Interface, pod *v1.Pod) error {
509512
if pod == nil {
@@ -512,7 +515,7 @@ func DeletePodWithWait(f *Framework, c clientset.Interface, pod *v1.Pod) error {
512515
return DeletePodWithWaitByName(f, c, pod.GetName(), pod.GetNamespace())
513516
}
514517

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
516519
// not existing.
517520
func DeletePodWithWaitByName(f *Framework, c clientset.Interface, podName, podNamespace string) error {
518521
Logf("Deleting pod %q in namespace %q", podName, podNamespace)
@@ -531,7 +534,7 @@ func DeletePodWithWaitByName(f *Framework, c clientset.Interface, podName, podNa
531534
return nil
532535
}
533536

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.
535538
// Note: need named return value so that the err assignment in the defer sets the returned error.
536539
// Has been shown to be necessary using Go 1.7.
537540
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 {
560563
return types.NamespacedName{Namespace: ns, Name: name}
561564
}
562565

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
564567
// then the PV is defined with a ClaimRef which includes the PVC's namespace.
565568
// If the PVC is nil then the PV is not defined with a ClaimRef. If no reclaimPolicy
566569
// is assigned, assumes "Retain". Specs are expected to match the test's PVC.
@@ -607,7 +610,7 @@ func MakePersistentVolume(pvConfig PersistentVolumeConfig) *v1.PersistentVolume
607610
}
608611
}
609612

610-
// Returns a PVC definition based on the namespace.
613+
// MakePersistentVolumeClaim returns a PVC definition based on the namespace.
611614
// Note: if this PVC is intended to be pre-bound to a PV, whose name is not
612615
// known until the PV is instantiated, then the func CreatePVPVC will add
613616
// pvc.Spec.VolumeName to this claim.
@@ -641,7 +644,7 @@ func MakePersistentVolumeClaim(cfg PersistentVolumeClaimConfig, ns string) *v1.P
641644

642645
func createPDWithRetry(zone string) (string, error) {
643646
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) {
645648
newDiskName, err := createPD(zone)
646649
if err != nil {
647650
Logf("Couldn't create a new PD, sleeping 5 seconds: %v", err)
@@ -653,20 +656,23 @@ func createPDWithRetry(zone string) (string, error) {
653656
return "", err
654657
}
655658

659+
// CreatePDWithRetry creates PD with retry.
656660
func CreatePDWithRetry() (string, error) {
657661
return createPDWithRetry("")
658662
}
659663

664+
// CreatePDWithRetryAndZone creates PD on zone with retry.
660665
func CreatePDWithRetryAndZone(zone string) (string, error) {
661666
return createPDWithRetry(zone)
662667
}
663668

669+
// DeletePDWithRetry deletes PD with retry.
664670
func DeletePDWithRetry(diskName string) error {
665671
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) {
667673
err = deletePD(diskName)
668674
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)
670676
continue
671677
}
672678
Logf("Successfully deleted PD %q.", diskName)
@@ -686,13 +692,13 @@ func deletePD(pdName string) error {
686692
return TestContext.CloudConfig.Provider.DeletePD(pdName)
687693
}
688694

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
690696
// name.
691697
func MakeWritePod(ns string, pvc *v1.PersistentVolumeClaim) *v1.Pod {
692698
return MakePod(ns, nil, []*v1.PersistentVolumeClaim{pvc}, true, "touch /mnt/volume1/SUCCESS && (id -G | grep -E '\\b777\\b')")
693699
}
694700

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
696702
// name. A slice of BASH commands can be supplied as args to be run by the pod
697703
func MakePod(ns string, nodeSelector map[string]string, pvclaims []*v1.PersistentVolumeClaim, isPrivileged bool, command string) *v1.Pod {
698704
if len(command) == 0 {
@@ -737,8 +743,8 @@ func MakePod(ns string, nodeSelector map[string]string, pvclaims []*v1.Persisten
737743
return podSpec
738744
}
739745

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 {
742748
podSpec := &v1.Pod{
743749
TypeMeta: metav1.TypeMeta{
744750
Kind: "Pod",
@@ -778,7 +784,7 @@ func MakeNginxPod(ns string, nodeSelector map[string]string, pvclaims []*v1.Pers
778784
return podSpec
779785
}
780786

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
782788
// name. A slice of BASH commands can be supplied as args to be run by the pod.
783789
// SELinux testing requires to pass HostIPC and HostPID as booleansi arguments.
784790
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
860866
return pod, nil
861867
}
862868

869+
// CreateNginxPod creates an enginx pod.
863870
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)
865872
pod, err := client.CoreV1().Pods(namespace).Create(pod)
866873
if err != nil {
867874
return nil, fmt.Errorf("pod Create API error: %v", err)
@@ -879,12 +886,12 @@ func CreateNginxPod(client clientset.Interface, namespace string, nodeSelector m
879886
return pod, nil
880887
}
881888

882-
// create security pod with given claims
889+
// CreateSecPod creates security pod with given claims
883890
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) {
884891
return CreateSecPodWithNodeSelection(client, namespace, pvclaims, isPrivileged, command, hostIPC, hostPID, seLinuxLabel, fsGroup, NodeSelection{}, timeout)
885892
}
886893

887-
// create security pod with given claims
894+
// CreateSecPodWithNodeSelection creates security pod with given claims
888895
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) {
889896
pod := MakeSecPod(namespace, pvclaims, isPrivileged, command, hostIPC, hostPID, seLinuxLabel, fsGroup)
890897
// Setting node
@@ -948,7 +955,7 @@ func SetAntiAffinity(nodeSelection *NodeSelection, nodeName string) {
948955
SetNodeAffinityRequirement(nodeSelection, v1.NodeSelectorOpNotIn, nodeName)
949956
}
950957

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.
952959
func CreateClientPod(c clientset.Interface, ns string, pvc *v1.PersistentVolumeClaim) (*v1.Pod, error) {
953960
return CreatePod(c, ns, nil, []*v1.PersistentVolumeClaim{pvc}, true, "")
954961
}
@@ -973,7 +980,7 @@ func CreateUnschedulablePod(client clientset.Interface, namespace string, nodeSe
973980
return pod, nil
974981
}
975982

976-
// wait until all pvcs phase set to bound
983+
// WaitForPVClaimBoundPhase waits until all pvcs phase set to bound
977984
func WaitForPVClaimBoundPhase(client clientset.Interface, pvclaims []*v1.PersistentVolumeClaim, timeout time.Duration) ([]*v1.PersistentVolume, error) {
978985
persistentvolumes := make([]*v1.PersistentVolume, len(pvclaims))
979986

@@ -996,6 +1003,7 @@ func WaitForPVClaimBoundPhase(client clientset.Interface, pvclaims []*v1.Persist
9961003
return persistentvolumes, nil
9971004
}
9981005

1006+
// CreatePVSource creates a PV source.
9991007
func CreatePVSource(zone string) (*v1.PersistentVolumeSource, error) {
10001008
diskName, err := CreatePDWithRetryAndZone(zone)
10011009
if err != nil {
@@ -1004,10 +1012,12 @@ func CreatePVSource(zone string) (*v1.PersistentVolumeSource, error) {
10041012
return TestContext.CloudConfig.Provider.CreatePVSource(zone, diskName)
10051013
}
10061014

1015+
// DeletePVSource deletes a PV source.
10071016
func DeletePVSource(pvSource *v1.PersistentVolumeSource) error {
10081017
return TestContext.CloudConfig.Provider.DeletePVSource(pvSource)
10091018
}
10101019

1020+
// GetBoundPV returns a PV details.
10111021
func GetBoundPV(client clientset.Interface, pvc *v1.PersistentVolumeClaim) (*v1.PersistentVolume, error) {
10121022
// Get new copy of the claim
10131023
claim, err := client.CoreV1().PersistentVolumeClaims(pvc.Namespace).Get(pvc.Name, metav1.GetOptions{})

0 commit comments

Comments
 (0)