|
| 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 linked_clone |
| 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 | +// Keeping this method here as it populates the array used for cleanup |
| 33 | +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 { |
| 34 | + |
| 35 | + volumeMap := map[*corev1.PersistentVolumeClaim][]*corev1.PersistentVolume{} |
| 36 | + |
| 37 | + fmt.Println("Create PVC and verify PVC is bound") |
| 38 | + for i := 0; i < numOfPVc; i++ { |
| 39 | + ginkgo.By(fmt.Sprintf("Creating PVC in iteration: %v", |
| 40 | + i)) |
| 41 | + pvclaim, pv := CreateAndValidatePvc(ctx, client, namespace, storageclass) |
| 42 | + volumeMap[pvclaim] = pv |
| 43 | + |
| 44 | + fmt.Println("Create Pod and attach to PVC") |
| 45 | + if doCreatePod || doCreateDep { |
| 46 | + CreatePodForPvc(ctx, e2eTestConfig, client, namespace, []*corev1.PersistentVolumeClaim{pvclaim}, doCreatePod, doCreateDep) |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + return volumeMap |
| 51 | +} |
| 52 | + |
| 53 | +// This method can be used to create snapshot in parallel |
| 54 | +// Keeping this method here as it populates the array used for cleanup |
| 55 | +func CreateSnapshotInParallel(ctx context.Context, e2eTestConfig *config.E2eTestConfig, namespace string, volumeMap map[*corev1.PersistentVolumeClaim][]*corev1.PersistentVolume) chan *snapV1.VolumeSnapshot { |
| 56 | + |
| 57 | + var wg sync.WaitGroup |
| 58 | + snapshots := make(chan *snapV1.VolumeSnapshot, len(volumeMap)) |
| 59 | + |
| 60 | + for pvclaim, pvList := range volumeMap { |
| 61 | + wg.Add(1) |
| 62 | + go func() { |
| 63 | + defer wg.Done() |
| 64 | + snapshot, _ := CreateVolumeSnapshot(ctx, e2eTestConfig, namespace, pvclaim, pvList, constants.DiskSize) |
| 65 | + snapshots <- snapshot |
| 66 | + }() |
| 67 | + } |
| 68 | + |
| 69 | + wg.Wait() |
| 70 | + close(snapshots) |
| 71 | + fmt.Println("All threads completed") |
| 72 | + return snapshots |
| 73 | +} |
| 74 | + |
| 75 | +// Expected to create few linked clones before calling this method |
| 76 | +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) { |
| 77 | + var wg sync.WaitGroup |
| 78 | + |
| 79 | + lcPvcCreated := make(chan *corev1.PersistentVolumeClaim, iteration) |
| 80 | + lcPvCreated := make(chan []*corev1.PersistentVolume, iteration) |
| 81 | + |
| 82 | + for i := 0; i < iteration; i++ { |
| 83 | + fmt.Printf("Iteration %d\n", i) |
| 84 | + |
| 85 | + wg.Add(2) // We're launching 2 goroutines per iteration |
| 86 | + |
| 87 | + go func(id int) { |
| 88 | + defer wg.Done() |
| 89 | + linkdeClonePvc, lcPv := CreateAndValidateLinkedClone(ctx, client, namespace, storageclass, snapshot.Name) |
| 90 | + lcPvcCreated <- linkdeClonePvc |
| 91 | + lcPvCreated <- lcPv |
| 92 | + }(i) |
| 93 | + |
| 94 | + go func(id int) { |
| 95 | + defer wg.Done() |
| 96 | + fpv.DeletePersistentVolumeClaim(ctx, client, pvcList[i].Name, namespace) |
| 97 | + }(i) |
| 98 | + } |
| 99 | + |
| 100 | + wg.Wait() |
| 101 | + close(lcPvcCreated) |
| 102 | + close(lcPvCreated) |
| 103 | + |
| 104 | + return lcPvcCreated, lcPvCreated |
| 105 | +} |
0 commit comments