|
| 1 | +/* |
| 2 | +Copyright 2025 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package e2e |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + |
| 22 | + snapV1 "github.com/kubernetes-csi/external-snapshotter/client/v8/apis/volumesnapshot/v1" |
| 23 | + snapclient "github.com/kubernetes-csi/external-snapshotter/client/v8/clientset/versioned" |
| 24 | + "github.com/onsi/ginkgo/v2" |
| 25 | + "github.com/onsi/gomega" |
| 26 | + vmopv1 "github.com/vmware-tanzu/vm-operator/api/v1alpha1" |
| 27 | + vmopv2 "github.com/vmware-tanzu/vm-operator/api/v1alpha2" |
| 28 | + vmopv3 "github.com/vmware-tanzu/vm-operator/api/v1alpha3" |
| 29 | + vmopv4 "github.com/vmware-tanzu/vm-operator/api/v1alpha4" |
| 30 | + v1 "k8s.io/api/core/v1" |
| 31 | + "k8s.io/apimachinery/pkg/api/resource" |
| 32 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 33 | + "k8s.io/apimachinery/pkg/runtime" |
| 34 | + clientset "k8s.io/client-go/kubernetes" |
| 35 | + _ "k8s.io/client-go/plugin/pkg/client/auth/oidc" |
| 36 | + "k8s.io/kubernetes/test/e2e/framework" |
| 37 | + fpod "k8s.io/kubernetes/test/e2e/framework/pod" |
| 38 | + fpv "k8s.io/kubernetes/test/e2e/framework/pv" |
| 39 | + fss "k8s.io/kubernetes/test/e2e/framework/statefulset" |
| 40 | + admissionapi "k8s.io/pod-security-admission/api" |
| 41 | + ctlrclient "sigs.k8s.io/controller-runtime/pkg/client" |
| 42 | + |
| 43 | + cnsoperatorv1alpha1 "sigs.k8s.io/vsphere-csi-driver/v3/pkg/apis/cnsoperator" |
| 44 | + k8s "sigs.k8s.io/vsphere-csi-driver/v3/pkg/kubernetes" |
| 45 | +) |
| 46 | + |
| 47 | +var _ = ginkgo.Describe("WCP-predata", |
| 48 | + ginkgo.Label(p0, wcp, core), func() { |
| 49 | + |
| 50 | + f := framework.NewDefaultFramework("supervisor-predata") |
| 51 | + f.NamespacePodSecurityEnforceLevel = admissionapi.LevelPrivileged |
| 52 | + |
| 53 | + var ( |
| 54 | + client clientset.Interface |
| 55 | + namespace string |
| 56 | + storagePolicyName string |
| 57 | + scParameters map[string]string |
| 58 | + ) |
| 59 | + |
| 60 | + ginkgo.BeforeEach(func() { |
| 61 | + client = f.ClientSet |
| 62 | + namespace = getNamespaceToRunTests(f) |
| 63 | + bootstrap() |
| 64 | + |
| 65 | + // Skip if not supervisor cluster |
| 66 | + if !supervisorCluster { |
| 67 | + ginkgo.Skip("Test is only for Supervisor Cluster") |
| 68 | + } |
| 69 | + |
| 70 | + scParameters = make(map[string]string) |
| 71 | + storagePolicyName = GetAndExpectStringEnvVar(envStoragePolicyNameForSharedDatastores) |
| 72 | + profileID := e2eVSphere.GetSpbmPolicyID(storagePolicyName) |
| 73 | + scParameters[scParamStoragePolicyID] = profileID |
| 74 | + }) |
| 75 | + |
| 76 | + /* |
| 77 | + Supervisor Cluster Pre-Data Setup |
| 78 | +
|
| 79 | + Creates resources WITHOUT cleanup: |
| 80 | + - 5 standalone PVCs with Pods |
| 81 | + - 1 StatefulSet with 5 replicas |
| 82 | + - 3 VolumeSnapshots |
| 83 | + - 2 VMService VMs |
| 84 | +
|
| 85 | + Total: 12 PVCs (5 standalone + 5 StatefulSet + 2 VM PVCs) |
| 86 | + */ |
| 87 | + ginkgo.It("Create 5 PVCs, 1 StatefulSet (5 replicas), 3 Snapshots, and 2 VMs without cleanup", |
| 88 | + func() { |
| 89 | + ctx, cancel := context.WithCancel(context.Background()) |
| 90 | + defer cancel() |
| 91 | + |
| 92 | + framework.Logf("=== Supervisor Pre-Data Setup Started ===") |
| 93 | + framework.Logf("Namespace: %s", namespace) |
| 94 | + framework.Logf("Storage Policy: %s", storagePolicyName) |
| 95 | + |
| 96 | + // Get storage class |
| 97 | + storageclass, err := client.StorageV1().StorageClasses().Get(ctx, storagePolicyName, metav1.GetOptions{}) |
| 98 | + gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 99 | + |
| 100 | + // Initialize clients |
| 101 | + var snapc *snapclient.Clientset |
| 102 | + restConfig = getRestConfigClient() |
| 103 | + snapc, err = snapclient.NewForConfig(restConfig) |
| 104 | + gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 105 | + |
| 106 | + cnsopC, err := k8s.NewClientForGroup(ctx, restConfig, cnsoperatorv1alpha1.GroupName) |
| 107 | + gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 108 | + |
| 109 | + // ============================================== |
| 110 | + // Section 1: Create 5 Standalone PVCs with Pods |
| 111 | + // ============================================== |
| 112 | + ginkgo.By("Step 1: Creating 5 standalone PVCs with Pods") |
| 113 | + |
| 114 | + standalonePVCs := make([]*v1.PersistentVolumeClaim, 5) |
| 115 | + standalonePVs := make([]*v1.PersistentVolume, 5) |
| 116 | + standalonePods := make([]*v1.Pod, 5) |
| 117 | + |
| 118 | + for i := 0; i < 5; i++ { |
| 119 | + pvc, err := createPVC(ctx, client, namespace, nil, diskSize, storageclass, v1.ReadWriteOnce) |
| 120 | + gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 121 | + standalonePVCs[i] = pvc |
| 122 | + framework.Logf("Created PVC: %s", pvc.Name) |
| 123 | + } |
| 124 | + |
| 125 | + // Wait for PVCs to be bound |
| 126 | + standalonePVs, err = fpv.WaitForPVClaimBoundPhase(ctx, client, standalonePVCs, |
| 127 | + framework.ClaimProvisionTimeout) |
| 128 | + gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 129 | + framework.Logf("All 5 standalone PVCs are bound") |
| 130 | + |
| 131 | + // Verify CNS metadata |
| 132 | + for i := 0; i < 5; i++ { |
| 133 | + volHandle := standalonePVs[i].Spec.CSI.VolumeHandle |
| 134 | + err = waitAndVerifyCnsVolumeMetadata(ctx, volHandle, standalonePVCs[i], standalonePVs[i], nil) |
| 135 | + gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 136 | + } |
| 137 | + |
| 138 | + // Create pods |
| 139 | + for i := 0; i < 5; i++ { |
| 140 | + pod, err := createPod(ctx, client, namespace, nil, |
| 141 | + []*v1.PersistentVolumeClaim{standalonePVCs[i]}, false, "") |
| 142 | + gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 143 | + standalonePods[i] = pod |
| 144 | + framework.Logf("Created Pod: %s", pod.Name) |
| 145 | + } |
| 146 | + |
| 147 | + // Wait for pods to be running |
| 148 | + for i := 0; i < 5; i++ { |
| 149 | + err = fpod.WaitForPodRunningInNamespace(ctx, client, standalonePods[i]) |
| 150 | + gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 151 | + } |
| 152 | + framework.Logf("All 5 standalone pods are running") |
| 153 | + |
| 154 | + // ============================================== |
| 155 | + // Section 2: Create StatefulSet with 5 replicas |
| 156 | + // ============================================== |
| 157 | + ginkgo.By("Step 2: Creating StatefulSet with 5 replicas") |
| 158 | + |
| 159 | + service := CreateService(namespace, client) |
| 160 | + framework.Logf("Service created: %s", service.Name) |
| 161 | + |
| 162 | + statefulset := GetStatefulSetFromManifest(namespace) |
| 163 | + replicas := int32(5) |
| 164 | + statefulset.Spec.Replicas = &replicas |
| 165 | + statefulset.Spec.VolumeClaimTemplates[0].Spec.StorageClassName = &storagePolicyName |
| 166 | + statefulset.Spec.VolumeClaimTemplates[0].Spec.Resources.Requests[v1.ResourceStorage] = |
| 167 | + resource.MustParse(diskSize) |
| 168 | + |
| 169 | + CreateStatefulSet(namespace, statefulset, client) |
| 170 | + framework.Logf("StatefulSet created: %s with 5 replicas", statefulset.Name) |
| 171 | + |
| 172 | + // Wait for StatefulSet to be ready |
| 173 | + fss.WaitForStatusReadyReplicas(ctx, client, statefulset, replicas) |
| 174 | + gomega.Expect(fss.CheckMount(ctx, client, statefulset, mountPath)).NotTo(gomega.HaveOccurred()) |
| 175 | + |
| 176 | + ssPodsAfterCreation, err := fss.GetPodList(ctx, client, statefulset) |
| 177 | + gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 178 | + gomega.Expect(len(ssPodsAfterCreation.Items)).To(gomega.Equal(int(replicas))) |
| 179 | + framework.Logf("StatefulSet is ready with 5 replicas") |
| 180 | + |
| 181 | + // ============================================== |
| 182 | + // Section 3: Create 3 VolumeSnapshots |
| 183 | + // ============================================== |
| 184 | + |
| 185 | + vcVersion = getVCversion(ctx, vcAddress) |
| 186 | + isVC90 := isVersionGreaterOrEqual(vcVersion, quotaSupportedVCVersion) |
| 187 | + |
| 188 | + if isVC90 { |
| 189 | + ginkgo.By("Step 3: Creating 3 VolumeSnapshots") |
| 190 | + |
| 191 | + volumeSnapshotClass, err := createVolumeSnapshotClass(ctx, snapc, deletionPolicy) |
| 192 | + gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 193 | + framework.Logf("VolumeSnapshotClass created: %s", volumeSnapshotClass.Name) |
| 194 | + |
| 195 | + volumeSnapshots := make([]*snapV1.VolumeSnapshot, 3) |
| 196 | + for i := 0; i < 3; i++ { |
| 197 | + snapshot, err := snapc.SnapshotV1().VolumeSnapshots(namespace).Create(ctx, |
| 198 | + getVolumeSnapshotSpec(namespace, volumeSnapshotClass.Name, standalonePVCs[i].Name), |
| 199 | + metav1.CreateOptions{}) |
| 200 | + gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 201 | + volumeSnapshots[i] = snapshot |
| 202 | + framework.Logf("Created VolumeSnapshot: %s from PVC: %s", |
| 203 | + snapshot.Name, standalonePVCs[i].Name) |
| 204 | + } |
| 205 | + |
| 206 | + // Wait for snapshots to be ready |
| 207 | + for i := 0; i < 3; i++ { |
| 208 | + snapshot, err := waitForVolumeSnapshotReadyToUse(*snapc, ctx, namespace, volumeSnapshots[i].Name) |
| 209 | + gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 210 | + gomega.Expect(snapshot.Status.RestoreSize.Cmp(resource.MustParse(diskSize))).To(gomega.BeZero()) |
| 211 | + framework.Logf("VolumeSnapshot %s is ready", volumeSnapshots[i].Name) |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + // ============================================== |
| 216 | + // Section 4: Create 2 VMService VMs |
| 217 | + // ============================================== |
| 218 | + ginkgo.By("Step 4: Creating 2 VMService VMs") |
| 219 | + |
| 220 | + vmImageName := GetAndExpectStringEnvVar(envVmsvcVmImageName) |
| 221 | + vmopScheme := runtime.NewScheme() |
| 222 | + gomega.Expect(vmopv1.AddToScheme(vmopScheme)).Should(gomega.Succeed()) |
| 223 | + gomega.Expect(vmopv2.AddToScheme(vmopScheme)).Should(gomega.Succeed()) |
| 224 | + gomega.Expect(vmopv3.AddToScheme(vmopScheme)).Should(gomega.Succeed()) |
| 225 | + gomega.Expect(vmopv4.AddToScheme(vmopScheme)).Should(gomega.Succeed()) |
| 226 | + vmopC, err := ctlrclient.New(f.ClientConfig(), ctlrclient.Options{Scheme: vmopScheme}) |
| 227 | + gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 228 | + vmi := waitNGetVmiForImageName(ctx, vmopC, vmImageName) |
| 229 | + gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 230 | + framework.Logf("Found VirtualMachineImage: %s", vmi) |
| 231 | + |
| 232 | + secretName := createBootstrapSecretForVmsvcVms(ctx, client, namespace) |
| 233 | + framework.Logf("Created bootstrap secret: %s", secretName) |
| 234 | + |
| 235 | + vmClass := vmClassBestEffortSmall |
| 236 | + |
| 237 | + vmPVCs := make([]*v1.PersistentVolumeClaim, 2) |
| 238 | + vms := make([]*vmopv1.VirtualMachine, 2) |
| 239 | + |
| 240 | + for i := 0; i < 2; i++ { |
| 241 | + // Create PVC for VM |
| 242 | + vmPvc, err := createPVC(ctx, client, namespace, nil, "10Gi", storageclass, v1.ReadWriteOnce) |
| 243 | + gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 244 | + vmPVCs[i] = vmPvc |
| 245 | + framework.Logf("Created VM PVC: %s", vmPvc.Name) |
| 246 | + |
| 247 | + // Wait for PVC to be bound |
| 248 | + _, err = fpv.WaitForPVClaimBoundPhase(ctx, client, |
| 249 | + []*v1.PersistentVolumeClaim{vmPVCs[i]}, framework.ClaimProvisionTimeout) |
| 250 | + gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 251 | + |
| 252 | + // Create VM |
| 253 | + vm := createVmServiceVmWithPvcs(ctx, vmopC, namespace, vmClass, |
| 254 | + []*v1.PersistentVolumeClaim{vmPVCs[i]}, vmi, storagePolicyName, secretName) |
| 255 | + vms[i] = vm |
| 256 | + framework.Logf("Created VMService VM: %s", vm.Name) |
| 257 | + |
| 258 | + // Verify PVC attached to VM |
| 259 | + err = waitNverifyPvcsAreAttachedToVmsvcVm(ctx, vmopC, cnsopC, vms[i], |
| 260 | + []*v1.PersistentVolumeClaim{vmPVCs[i]}) |
| 261 | + gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 262 | + framework.Logf("Verified PVC attached to VM: %s", vms[i].Name) |
| 263 | + } |
| 264 | + |
| 265 | + // ============================================== |
| 266 | + // Final Summary |
| 267 | + // ============================================== |
| 268 | + ginkgo.By("Step 5: Verifying all resources") |
| 269 | + |
| 270 | + framework.Logf("=== Supervisor Pre-Data Setup Complete ===") |
| 271 | + framework.Logf("Namespace: %s", namespace) |
| 272 | + framework.Logf("Storage Policy: %s", storagePolicyName) |
| 273 | + framework.Logf("") |
| 274 | + framework.Logf("Resources Created:") |
| 275 | + framework.Logf(" - Standalone PVCs: 5 (5 x 2Gi = 10Gi)") |
| 276 | + framework.Logf(" - Standalone Pods: 5") |
| 277 | + framework.Logf(" - StatefulSet: 1 with 5 replicas (5 x 2Gi = 10Gi)") |
| 278 | + framework.Logf(" - VolumeSnapshots: 3 (~6Gi)") |
| 279 | + framework.Logf(" - VMService VMs: 2 (2 x 10Gi = 20Gi)") |
| 280 | + framework.Logf(" - VM PVCs: 2") |
| 281 | + framework.Logf("") |
| 282 | + framework.Logf("Total Resources:") |
| 283 | + framework.Logf(" - PVCs: 12 (5 standalone + 5 StatefulSet + 2 VM)") |
| 284 | + framework.Logf(" - Pods: 10 (5 standalone + 5 StatefulSet)") |
| 285 | + framework.Logf(" - VMs: 2") |
| 286 | + framework.Logf(" - Snapshots: 3") |
| 287 | + framework.Logf(" - Total Storage: ~46Gi") |
| 288 | + framework.Logf("==========================================") |
| 289 | + |
| 290 | + ginkgo.By("*** RESOURCES NOT CLEANED UP - Will persist after test ***") |
| 291 | + }) |
| 292 | + }) |
0 commit comments