k8s.io/kubernetes@v1.29.3/test/e2e/storage/vsphere/pvc_label_selector.go (about)

     1  /*
     2  Copyright 2017 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 vsphere
    18  
    19  import (
    20  	"context"
    21  	"time"
    22  
    23  	"github.com/onsi/ginkgo/v2"
    24  	v1 "k8s.io/api/core/v1"
    25  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    26  	clientset "k8s.io/client-go/kubernetes"
    27  	"k8s.io/kubernetes/test/e2e/feature"
    28  	"k8s.io/kubernetes/test/e2e/framework"
    29  	e2enode "k8s.io/kubernetes/test/e2e/framework/node"
    30  	e2epv "k8s.io/kubernetes/test/e2e/framework/pv"
    31  	e2eskipper "k8s.io/kubernetes/test/e2e/framework/skipper"
    32  	"k8s.io/kubernetes/test/e2e/storage/utils"
    33  	admissionapi "k8s.io/pod-security-admission/api"
    34  )
    35  
    36  /*
    37  This is a function test for Selector-Label Volume Binding Feature
    38  Test verifies volume with the matching label is bounded with the PVC.
    39  
    40  Test Steps
    41  ----------
    42  1. Create VMDK.
    43  2. Create pv with label volume-type:ssd, volume path set to vmdk created in previous step, and PersistentVolumeReclaimPolicy is set to Delete.
    44  3. Create PVC (pvcVvol) with label selector to match with volume-type:vvol
    45  4. Create PVC (pvcSsd) with label selector to match with volume-type:ssd
    46  5. Wait and verify pvSsd is bound with PV.
    47  6. Verify Status of pvcVvol is still pending.
    48  7. Delete pvcSsd.
    49  8. verify associated pv is also deleted.
    50  9. delete pvcVvol
    51  */
    52  var _ = utils.SIGDescribe("PersistentVolumes", feature.Vsphere, feature.LabelSelector, func() {
    53  	f := framework.NewDefaultFramework("pvclabelselector")
    54  	f.NamespacePodSecurityLevel = admissionapi.LevelPrivileged
    55  	var (
    56  		c          clientset.Interface
    57  		ns         string
    58  		pvSsd      *v1.PersistentVolume
    59  		pvcSsd     *v1.PersistentVolumeClaim
    60  		pvcVvol    *v1.PersistentVolumeClaim
    61  		volumePath string
    62  		ssdlabels  map[string]string
    63  		vvollabels map[string]string
    64  		err        error
    65  		nodeInfo   *NodeInfo
    66  	)
    67  	ginkgo.BeforeEach(func(ctx context.Context) {
    68  		e2eskipper.SkipUnlessProviderIs("vsphere")
    69  		c = f.ClientSet
    70  		ns = f.Namespace.Name
    71  		Bootstrap(f)
    72  		nodeInfo = GetReadySchedulableRandomNodeInfo(ctx, c)
    73  		framework.ExpectNoError(e2enode.WaitForAllNodesSchedulable(ctx, c, f.Timeouts.NodeSchedulable))
    74  		ssdlabels = make(map[string]string)
    75  		ssdlabels["volume-type"] = "ssd"
    76  		vvollabels = make(map[string]string)
    77  		vvollabels["volume-type"] = "vvol"
    78  
    79  	})
    80  
    81  	f.Describe("Selector-Label Volume Binding:vsphere", feature.Vsphere, func() {
    82  		ginkgo.AfterEach(func(ctx context.Context) {
    83  			ginkgo.By("Running clean up actions")
    84  			if framework.ProviderIs("vsphere") {
    85  				testCleanupVSpherePVClabelselector(ctx, c, ns, nodeInfo, volumePath, pvSsd, pvcSsd, pvcVvol)
    86  			}
    87  		})
    88  		ginkgo.It("should bind volume with claim for given label", func(ctx context.Context) {
    89  			volumePath, pvSsd, pvcSsd, pvcVvol, err = testSetupVSpherePVClabelselector(ctx, c, nodeInfo, ns, ssdlabels, vvollabels)
    90  			framework.ExpectNoError(err)
    91  
    92  			ginkgo.By("wait for the pvcSsd to bind with pvSsd")
    93  			framework.ExpectNoError(e2epv.WaitOnPVandPVC(ctx, c, f.Timeouts, ns, pvSsd, pvcSsd))
    94  
    95  			ginkgo.By("Verify status of pvcVvol is pending")
    96  			err = e2epv.WaitForPersistentVolumeClaimPhase(ctx, v1.ClaimPending, c, ns, pvcVvol.Name, 3*time.Second, 300*time.Second)
    97  			framework.ExpectNoError(err)
    98  
    99  			ginkgo.By("delete pvcSsd")
   100  			framework.ExpectNoError(e2epv.DeletePersistentVolumeClaim(ctx, c, pvcSsd.Name, ns), "Failed to delete PVC ", pvcSsd.Name)
   101  
   102  			ginkgo.By("verify pvSsd is deleted")
   103  			err = e2epv.WaitForPersistentVolumeDeleted(ctx, c, pvSsd.Name, 3*time.Second, 300*time.Second)
   104  			framework.ExpectNoError(err)
   105  			volumePath = ""
   106  
   107  			ginkgo.By("delete pvcVvol")
   108  			framework.ExpectNoError(e2epv.DeletePersistentVolumeClaim(ctx, c, pvcVvol.Name, ns), "Failed to delete PVC ", pvcVvol.Name)
   109  		})
   110  	})
   111  })
   112  
   113  func testSetupVSpherePVClabelselector(ctx context.Context, c clientset.Interface, nodeInfo *NodeInfo, ns string, ssdlabels map[string]string, vvollabels map[string]string) (volumePath string, pvSsd *v1.PersistentVolume, pvcSsd *v1.PersistentVolumeClaim, pvcVvol *v1.PersistentVolumeClaim, err error) {
   114  	ginkgo.By("creating vmdk")
   115  	volumePath, err = nodeInfo.VSphere.CreateVolume(&VolumeOptions{}, nodeInfo.DataCenterRef)
   116  	if err != nil {
   117  		return
   118  	}
   119  
   120  	ginkgo.By("creating the pv with label volume-type:ssd")
   121  	pvSsd = getVSpherePersistentVolumeSpec(volumePath, v1.PersistentVolumeReclaimDelete, ssdlabels)
   122  	pvSsd, err = c.CoreV1().PersistentVolumes().Create(ctx, pvSsd, metav1.CreateOptions{})
   123  	if err != nil {
   124  		return
   125  	}
   126  
   127  	ginkgo.By("creating pvc with label selector to match with volume-type:vvol")
   128  	pvcVvol = getVSpherePersistentVolumeClaimSpec(ns, vvollabels)
   129  	pvcVvol, err = c.CoreV1().PersistentVolumeClaims(ns).Create(ctx, pvcVvol, metav1.CreateOptions{})
   130  	if err != nil {
   131  		return
   132  	}
   133  
   134  	ginkgo.By("creating pvc with label selector to match with volume-type:ssd")
   135  	pvcSsd = getVSpherePersistentVolumeClaimSpec(ns, ssdlabels)
   136  	pvcSsd, err = c.CoreV1().PersistentVolumeClaims(ns).Create(ctx, pvcSsd, metav1.CreateOptions{})
   137  	return
   138  }
   139  
   140  func testCleanupVSpherePVClabelselector(ctx context.Context, c clientset.Interface, ns string, nodeInfo *NodeInfo, volumePath string, pvSsd *v1.PersistentVolume, pvcSsd *v1.PersistentVolumeClaim, pvcVvol *v1.PersistentVolumeClaim) {
   141  	ginkgo.By("running testCleanupVSpherePVClabelselector")
   142  	if len(volumePath) > 0 {
   143  		nodeInfo.VSphere.DeleteVolume(volumePath, nodeInfo.DataCenterRef)
   144  	}
   145  	if pvcSsd != nil {
   146  		framework.ExpectNoError(e2epv.DeletePersistentVolumeClaim(ctx, c, pvcSsd.Name, ns), "Failed to delete PVC ", pvcSsd.Name)
   147  	}
   148  	if pvcVvol != nil {
   149  		framework.ExpectNoError(e2epv.DeletePersistentVolumeClaim(ctx, c, pvcVvol.Name, ns), "Failed to delete PVC ", pvcVvol.Name)
   150  	}
   151  	if pvSsd != nil {
   152  		framework.ExpectNoError(e2epv.DeletePersistentVolume(ctx, c, pvSsd.Name), "Failed to delete PV ", pvSsd.Name)
   153  	}
   154  }