Skip to content

Commit

Permalink
a bit more updates and UTs
Browse files Browse the repository at this point in the history
Signed-off-by: Shelly Kagan <skagan@redhat.com>
  • Loading branch information
ShellyKa13 committed Jun 7, 2023
1 parent 7776b9d commit ce5395d
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 2 deletions.
56 changes: 56 additions & 0 deletions pkg/controller/populators/import-populator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,16 @@ import (
"net/url"
"strconv"
"strings"
"time"

. "github.com/onsi/ginkgo"
"github.com/onsi/ginkgo/extensions/table"
. "github.com/onsi/gomega"

snapshotv1 "github.com/kubernetes-csi/external-snapshotter/client/v6/apis/volumesnapshot/v1"
corev1 "k8s.io/api/core/v1"
extv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
Expand Down Expand Up @@ -258,6 +261,8 @@ var _ = Describe("Import populator tests", func() {
pvcPrime, err := reconciler.getPVCPrime(targetPvc)
Expect(err).ToNot(HaveOccurred())
Expect(pvcPrime).ToNot(BeNil())
// make sure we didnt inflate size
Expect(pvcPrime.Spec.Resources.Requests[corev1.ResourceStorage]).To(Equal(resource.MustParse("1G")))
Expect(pvcPrime.GetAnnotations()).ToNot(BeNil())
Expect(pvcPrime.GetAnnotations()[AnnImmediateBinding]).To(Equal(""))
Expect(pvcPrime.GetAnnotations()[AnnUploadRequest]).To(Equal(""))
Expand Down Expand Up @@ -302,6 +307,57 @@ var _ = Describe("Import populator tests", func() {
Expect(err).To(Not(HaveOccurred()))
Expect(result).To(Not(BeNil()))
})

table.DescribeTable("should update target pvc with desired annotations from pvc prime", func(podPhase string) {
targetPvc := CreatePvcInStorageClass(targetPvcName, metav1.NamespaceDefault, &sc.Name, nil, nil, corev1.ClaimPending)
targetPvc.Spec.DataSourceRef = dataSourceRef
volumeImportSource := getVolumeImportSource(true, metav1.NamespaceDefault)
pvcPrime := getPVCPrime(targetPvc, nil)
for _, ann := range desiredAnnotations {
AddAnnotation(pvcPrime, ann, "somevalue")
}
AddAnnotation(pvcPrime, AnnPodPhase, podPhase)
AddAnnotation(pvcPrime, "undesiredAnn", "somevalue")

pv := &corev1.PersistentVolume{
ObjectMeta: metav1.ObjectMeta{
Name: "pv",
},
Spec: corev1.PersistentVolumeSpec{
ClaimRef: &corev1.ObjectReference{
Namespace: pvcPrime.Namespace,
Name: pvcPrime.Name,
},
},
}
pvcPrime.Spec.VolumeName = pv.Name

By("Reconcile")
reconciler = createImportPopulatorReconciler(targetPvc, pvcPrime, pv, volumeImportSource, sc)
result, err := reconciler.Reconcile(context.TODO(), reconcile.Request{NamespacedName: types.NamespacedName{Name: targetPvcName, Namespace: metav1.NamespaceDefault}})
Expect(err).To(Not(HaveOccurred()))
Expect(result).ToNot(BeNil())
if podPhase == string(corev1.PodRunning) {
Expect(result.RequeueAfter).To(Equal(2 * time.Second))
} else {
Expect(result.RequeueAfter).To(Equal(0 * time.Second))
}

updatedPVC := &corev1.PersistentVolumeClaim{}
err = reconciler.client.Get(context.TODO(), types.NamespacedName{Name: targetPvcName, Namespace: metav1.NamespaceDefault}, updatedPVC)
Expect(err).ToNot(HaveOccurred())
Expect(updatedPVC.GetAnnotations()).ToNot(BeNil())
for _, ann := range desiredAnnotations {
_, ok := updatedPVC.Annotations[ann]
Expect(ok).To(BeTrue())
}
_, ok := updatedPVC.Annotations["undesiredAnn"]
Expect(ok).To(BeFalse())
},
table.Entry("with pod running phase", string(corev1.PodRunning)),
table.Entry("with pod succeded phase", string(corev1.PodFailed)),
table.Entry("with pod succeded phase", string(corev1.PodSucceeded)),
)
})

var _ = Describe("Import populator progress report", func() {
Expand Down
4 changes: 3 additions & 1 deletion pkg/controller/populators/populator-base.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,9 @@ func (r *ReconcilerBase) createPVCPrime(pvc *corev1.PersistentVolumeClaim, sourc

type updatePVCAnnotationsFunc func(pvc, pvcPrime *corev1.PersistentVolumeClaim)

var desiredAnnotations = []string{cc.AnnPodPhase, cc.AnnPodReady, cc.AnnPodRestarts, cc.AnnPreallocationRequested, cc.AnnPreallocationApplied, cc.AnnRunningCondition, cc.AnnRunningConditionMessage, cc.AnnRunningConditionReason, cc.AnnBoundCondition, cc.AnnBoundConditionMessage, cc.AnnBoundConditionReason}
var desiredAnnotations = []string{cc.AnnPodPhase, cc.AnnPodReady, cc.AnnPodRestarts,
cc.AnnPreallocationRequested, cc.AnnPreallocationApplied,
cc.AnnRunningCondition, cc.AnnRunningConditionMessage, cc.AnnRunningConditionReason}

func (r *ReconcilerBase) updatePVCWithPVCPrimeAnnotations(pvc, pvcPrime *corev1.PersistentVolumeClaim, updateFunc updatePVCAnnotationsFunc) error {
pvcCopy := pvc.DeepCopy()
Expand Down
46 changes: 46 additions & 0 deletions pkg/controller/populators/upload-populator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,52 @@ var _ = Describe("Datavolume controller reconcile loop", func() {
Expect(pvcPrime).ToNot(BeNil())
Expect(pvcPrime.Annotations[cc.AnnSelectedNode]).To(Equal("node01"))
})

DescribeTable("should update target pvc with desired annotations from pvc prime", func(podPhase string) {
pvc := newUploadPopulatorPVC("test-pvc")
cc.AddAnnotation(pvc, AnnPVCPrimeName, PVCPrimeName(pvc))
uploadPV := uploadPV(pvc)

volumeUploadSourceCR := newUploadPopulatorCR("", false)
scName := "test-sc"
sc := cc.CreateStorageClassWithProvisioner(scName, map[string]string{cc.AnnDefaultStorageClass: "true"}, map[string]string{}, "csi-plugin")
r := createUploadPopulatorReconciler(pvc, volumeUploadSourceCR, sc, uploadPV)

_, err := r.Reconcile(context.TODO(), reconcile.Request{NamespacedName: types.NamespacedName{Name: "test-pvc", Namespace: metav1.NamespaceDefault}})
Expect(err).ToNot(HaveOccurred())
pvcPrime, err := r.getPVCPrime(pvc)
Expect(err).ToNot(HaveOccurred())

pvcPrime.Spec.VolumeName = "test-pv"
pvcPrime.UID = pvcPrimeUID
for _, ann := range desiredAnnotations {
cc.AddAnnotation(pvcPrime, ann, "somevalue")
}
cc.AddAnnotation(pvcPrime, cc.AnnPodPhase, podPhase)
cc.AddAnnotation(pvcPrime, "undesiredAnn", "somevalue")
err = r.client.Update(context.TODO(), pvcPrime)
Expect(err).ToNot(HaveOccurred())

By("Reconcile")
result, err := r.Reconcile(context.TODO(), reconcile.Request{NamespacedName: types.NamespacedName{Name: "test-pvc", Namespace: metav1.NamespaceDefault}})
Expect(err).To(Not(HaveOccurred()))
Expect(result).ToNot(BeNil())

updatedPVC := &corev1.PersistentVolumeClaim{}
err = r.client.Get(context.TODO(), types.NamespacedName{Name: "test-pvc", Namespace: metav1.NamespaceDefault}, updatedPVC)
Expect(err).ToNot(HaveOccurred())
Expect(updatedPVC.GetAnnotations()).ToNot(BeNil())
for _, ann := range desiredAnnotations {
_, ok := updatedPVC.Annotations[ann]
Expect(ok).To(BeTrue())
}
_, ok := updatedPVC.Annotations["undesiredAnn"]
Expect(ok).To(BeFalse())
},
Entry("with pod running phase", string(corev1.PodRunning)),
Entry("with pod succeded phase", string(corev1.PodFailed)),
Entry("with pod succeded phase", string(corev1.PodSucceeded)),
)
})

func newUploadPopulatorPVC(name string) *corev1.PersistentVolumeClaim {
Expand Down
4 changes: 3 additions & 1 deletion tests/framework/pvc.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,10 @@ func (f *Framework) ForceBindPvcIfDvIsWaitForFirstConsumer(dv *cdiv1.DataVolume)
pvc, err := utils.WaitForPVC(f.K8sClient, dv.Namespace, dv.Name)
gomega.Expect(err).ToNot(gomega.HaveOccurred(), "PVC should exist")
if f.IsBindingModeWaitForFirstConsumer(pvc.Spec.StorageClassName) {
// check if pvc is a population pvc but not from pvc or snapshot
if pvc.Spec.DataSourceRef != nil &&
dv.Spec.Source.PVC == nil && dv.Spec.Source.Snapshot == nil {
(dv.Spec.Source == nil || dv.Spec.Source.PVC == nil) &&
(dv.Spec.Source == nil || dv.Spec.Source.Snapshot == nil) {
err = utils.WaitForDataVolumePhase(f, dv.Namespace, cdiv1.PendingPopulation, dv.Name)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
createConsumerPodForPopulationPVC(pvc, f)
Expand Down

0 comments on commit ce5395d

Please sign in to comment.