Skip to content

Commit

Permalink
Update test to handle consumer pod with population correctly
Browse files Browse the repository at this point in the history
Signed-off-by: Shelly Kagan <skagan@redhat.com>
  • Loading branch information
ShellyKa13 committed Apr 3, 2023
1 parent d74fdcf commit d09ffc2
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 6 deletions.
4 changes: 2 additions & 2 deletions pkg/controller/populators/populator-base.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (r *ReconcilerBase) handleStorageClass(pvc *corev1.PersistentVolumeClaim) (

if storageClass.VolumeBindingMode != nil && *storageClass.VolumeBindingMode == storagev1.VolumeBindingWaitForFirstConsumer {
waitForFirstConsumer = true
nodeName := pvc.Annotations[annSelectedNode]
nodeName := pvc.Annotations[AnnSelectedNode]
if nodeName == "" {
// Wait for the PVC to get a node name before continuing
return false, waitForFirstConsumer, nil
Expand All @@ -92,7 +92,7 @@ func (r *ReconcilerBase) createPVCPrime(pvc *corev1.PersistentVolumeClaim, sourc
annotations[annPopulationTargetPVC] = pvc.Name
annotations[cc.AnnImmediateBinding] = ""
if waitForFirstConsumer {
annotations[annSelectedNode] = pvc.Annotations[annSelectedNode]
annotations[AnnSelectedNode] = pvc.Annotations[AnnSelectedNode]
}
pvcPrimeName := PVCPrimeName(pvc)
pvcPrime := &corev1.PersistentVolumeClaim{
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/populators/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ const (
// createdPVCPrimeSucceesfully provides a const to indicate we created PVC prime for population
createdPVCPrimeSucceesfully = "CreatedPVCPrimeSucceesfully"

// annSelectedNode annotation is added to a PVC that has been triggered by scheduler to
// AnnSelectedNode annotation is added to a PVC that has been triggered by scheduler to
// be dynamically provisioned. Its value is the name of the selected node.
annSelectedNode = "volume.kubernetes.io/selected-node"
AnnSelectedNode = "volume.kubernetes.io/selected-node"
// annMigratedTo annotation is added to a PVC and PV that is supposed to be
// dynamically provisioned/deleted by by its corresponding CSI driver
// through the CSIMigration feature flags. When this annotation is set the
Expand Down
39 changes: 39 additions & 0 deletions tests/framework/pvc.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

cdiv1 "kubevirt.io/containerized-data-importer-api/pkg/apis/core/v1beta1"
controller "kubevirt.io/containerized-data-importer/pkg/controller/common"
"kubevirt.io/containerized-data-importer/pkg/controller/populators"
"kubevirt.io/containerized-data-importer/pkg/image"
"kubevirt.io/containerized-data-importer/pkg/util/naming"
"kubevirt.io/containerized-data-importer/tests/utils"
Expand All @@ -42,6 +43,18 @@ func (f *Framework) CreateBoundPVCFromDefinition(def *k8sv1.PersistentVolumeClai
return pvc
}

// CreateBoundPopulationPVCFromDefinition is a wrapper around utils.CreatePVCFromDefinition that also force binds pvc on
// on WaitForFirstConsumer storage class by executing f.ForceBindIfWaitForFirstConsumer(pvc)
func (f *Framework) CreateBoundPopulationPVCFromDefinition(def *k8sv1.PersistentVolumeClaim) *k8sv1.PersistentVolumeClaim {
pvc, err := utils.CreatePVCFromDefinition(f.K8sClient, f.Namespace.Name, def)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
pvc, err = utils.WaitForPVC(f.K8sClient, pvc.Namespace, pvc.Name)
gomega.Expect(err).ToNot(gomega.HaveOccurred())

f.ForceBindIfWaitForFirstConsumerPopulationPVC(pvc)
return pvc
}

// DeletePVC is a wrapper around utils.DeletePVC
func (f *Framework) DeletePVC(pvc *k8sv1.PersistentVolumeClaim) error {
return utils.DeletePVC(f.K8sClient, f.Namespace.Name, pvc.Name)
Expand Down Expand Up @@ -82,6 +95,13 @@ func (f *Framework) ForceBindIfWaitForFirstConsumer(targetPvc *k8sv1.PersistentV
}
}

// ForceBindIfWaitForFirstConsumerPopulationPVC creates a Pod with the passed in PVC mounted under /dev/pvc, which forces the PVC to be scheduled and bound.
func (f *Framework) ForceBindIfWaitForFirstConsumerPopulationPVC(targetPvc *k8sv1.PersistentVolumeClaim) {
if f.IsBindingModeWaitForFirstConsumer(targetPvc.Spec.StorageClassName) {
createConsumerPodForPouplationPVC(targetPvc, f)
}
}

func createConsumerPod(targetPvc *k8sv1.PersistentVolumeClaim, f *Framework) {
fmt.Fprintf(ginkgo.GinkgoWriter, "INFO: creating \"consumer-pod\" to force binding PVC: %s\n", targetPvc.Name)
namespace := targetPvc.Namespace
Expand All @@ -101,6 +121,25 @@ func createConsumerPod(targetPvc *k8sv1.PersistentVolumeClaim, f *Framework) {
utils.DeletePodNoGrace(f.K8sClient, executorPod, namespace)
}

func createConsumerPodForPouplationPVC(targetPvc *k8sv1.PersistentVolumeClaim, f *Framework) {
fmt.Fprintf(ginkgo.GinkgoWriter, "INFO: creating \"consumer-pod\" to get 'selected-node' annotation on PVC: %s\n", targetPvc.Name)
namespace := targetPvc.Namespace

err := utils.WaitForPersistentVolumeClaimPhase(f.K8sClient, targetPvc.Namespace, k8sv1.ClaimPending, targetPvc.Name)
gomega.Expect(err).ToNot(gomega.HaveOccurred())

podName := naming.GetResourceName("consumer-pod", targetPvc.Name)
executorPod, err := f.CreateNoopPodWithPVC(podName, namespace, targetPvc)
gomega.Expect(err).ToNot(gomega.HaveOccurred())

selectedNode, status, err := utils.WaitForPVCAnnotation(f.K8sClient, namespace, targetPvc, populators.AnnSelectedNode)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(status).To(gomega.BeTrue())
gomega.Expect(selectedNode).ToNot(gomega.BeEmpty())

utils.DeletePodNoGrace(f.K8sClient, executorPod, namespace)
}

// VerifyPVCIsEmpty verifies a passed in PVC is empty, returns true if the PVC is empty, false if it is not. Optionaly, specify node for the pod.
func VerifyPVCIsEmpty(f *Framework, pvc *k8sv1.PersistentVolumeClaim, node string) (bool, error) {
var err error
Expand Down
4 changes: 2 additions & 2 deletions tests/upload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ var _ = Describe("[rfe_id:138][crit:high][vendor:cnv-qe@redhat.com][level:compon
})
Context("standard", func() {
BeforeEach(func() {
pvc = f.CreateBoundPVCFromDefinition(utils.UploadPopulationPVCDefinition())
pvc = f.CreateBoundPopulationPVCFromDefinition(utils.UploadPopulationPVCDefinition())
err := createUploadPopulatorCR(cdiv1.DataVolumeKubeVirt)
Expect(err).ToNot(HaveOccurred())
})
Expand Down Expand Up @@ -516,7 +516,7 @@ var _ = Describe("[rfe_id:138][crit:high][vendor:cnv-qe@redhat.com][level:compon

Context("archive", func() {
BeforeEach(func() {
pvc = f.CreateBoundPVCFromDefinition(utils.UploadPopulationPVCDefinition())
pvc = f.CreateBoundPopulationPVCFromDefinition(utils.UploadPopulationPVCDefinition())
err := createUploadPopulatorCR(cdiv1.DataVolumeArchive)
Expect(err).ToNot(HaveOccurred())
})
Expand Down

0 comments on commit d09ffc2

Please sign in to comment.