Skip to content

Commit 970ec95

Browse files
committed
Helpers for linked-clone
1 parent a68cb53 commit 970ec95

File tree

2 files changed

+485
-0
lines changed

2 files changed

+485
-0
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
Copyright 2025 The Kubernetes Authors.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
package helpers
15+
16+
import (
17+
"context"
18+
"fmt"
19+
"sync"
20+
21+
snapV1 "github.com/kubernetes-csi/external-snapshotter/client/v8/apis/volumesnapshot/v1"
22+
"github.com/onsi/ginkgo/v2"
23+
corev1 "k8s.io/api/core/v1"
24+
v1 "k8s.io/api/storage/v1"
25+
clientset "k8s.io/client-go/kubernetes"
26+
fpv "k8s.io/kubernetes/test/e2e/framework/pv"
27+
"sigs.k8s.io/vsphere-csi-driver/v3/tests/e2e/config"
28+
"sigs.k8s.io/vsphere-csi-driver/v3/tests/e2e/constants"
29+
)
30+
31+
// This function can be used to create given number of PVC/pod
32+
func CreateMultiplePvcPod(ctx context.Context, e2eTestConfig *config.E2eTestConfig, client clientset.Interface, namespace string, storageclass *v1.StorageClass, doCreatePod bool, doCreateDep bool, numOfPVc int) map[*corev1.PersistentVolumeClaim][]*corev1.PersistentVolume {
33+
34+
volumeMap := map[*corev1.PersistentVolumeClaim][]*corev1.PersistentVolume{}
35+
36+
fmt.Println("Create PVC and verify PVC is bound")
37+
for i := 0; i < numOfPVc; i++ {
38+
ginkgo.By(fmt.Sprintf("Creating PVC in iteration: %v",
39+
i))
40+
pvclaim, pv := CreateAndValidatePvc(ctx, client, namespace, storageclass)
41+
volumeMap[pvclaim] = pv
42+
43+
fmt.Println("Create Pod and attach to PVC")
44+
if doCreatePod || doCreateDep {
45+
CreatePodForPvc(ctx, e2eTestConfig, client, namespace, []*corev1.PersistentVolumeClaim{pvclaim}, doCreatePod, doCreateDep)
46+
}
47+
}
48+
49+
return volumeMap
50+
}
51+
52+
// This method can be used to create snapshot in parallel
53+
func CreateSnapshotInParallel(ctx context.Context, e2eTestConfig *config.E2eTestConfig, namespace string, volumeMap map[*corev1.PersistentVolumeClaim][]*corev1.PersistentVolume) chan *snapV1.VolumeSnapshot {
54+
55+
var wg sync.WaitGroup
56+
snapshots := make(chan *snapV1.VolumeSnapshot, len(volumeMap))
57+
58+
for pvclaim, pvList := range volumeMap {
59+
wg.Add(1)
60+
go func() {
61+
defer wg.Done()
62+
snapshot, _ := CreateVolumeSnapshot(ctx, e2eTestConfig, namespace, pvclaim, pvList, constants.DiskSize)
63+
snapshots <- snapshot
64+
}()
65+
}
66+
67+
wg.Wait()
68+
close(snapshots)
69+
fmt.Println("All threads completed")
70+
return snapshots
71+
}
72+
73+
// Expected to create few linked clones before calling this method
74+
func CreateDeleteLinkedClonesInParallel(ctx context.Context, client clientset.Interface, namespace string, storageclass *v1.StorageClass, snapshot *snapV1.VolumeSnapshot, pvcList []*corev1.PersistentVolumeClaim, iteration int) (chan *corev1.PersistentVolumeClaim, chan []*corev1.PersistentVolume) {
75+
var wg sync.WaitGroup
76+
77+
lcPvcCreated := make(chan *corev1.PersistentVolumeClaim, iteration)
78+
lcPvCreated := make(chan []*corev1.PersistentVolume, iteration)
79+
80+
for i := 0; i < iteration; i++ {
81+
fmt.Printf("Iteration %d\n", i)
82+
83+
wg.Add(2) // We're launching 2 goroutines per iteration
84+
85+
go func(id int) {
86+
defer wg.Done()
87+
linkdeClonePvc, lcPv := CreateAndValidateLinkedClone(ctx, client, namespace, storageclass, snapshot.Name)
88+
lcPvcCreated <- linkdeClonePvc
89+
lcPvCreated <- lcPv
90+
}(i)
91+
92+
go func(id int) {
93+
defer wg.Done()
94+
fpv.DeletePersistentVolumeClaim(ctx, client, pvcList[i].Name, namespace)
95+
}(i)
96+
}
97+
98+
wg.Wait()
99+
close(lcPvcCreated)
100+
close(lcPvCreated)
101+
102+
return lcPvcCreated, lcPvCreated
103+
}

0 commit comments

Comments
 (0)