Skip to content

Commit

Permalink
Rebase and add utests
Browse files Browse the repository at this point in the history
Signed-off-by: Ido Aharon <iaharon@redhat.com>
  • Loading branch information
ido106 committed Feb 21, 2023
1 parent b8fc14a commit ea577b8
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 10 deletions.
6 changes: 3 additions & 3 deletions pkg/controller/datavolume/controller-base.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ func (r ReconcilerBase) syncCommon(log logr.Logger, req reconcile.Request, clean

func (r ReconcilerBase) sync(log logr.Logger, req reconcile.Request, cleanup, prepare dataVolumeSyncResultFunc) (*dataVolumeSyncResult, error) {
syncRes := &dataVolumeSyncResult{}
dv, err := r.getDataVolume(req.NamespacedName)
dv, err := getDataVolume(r.client, req.NamespacedName)
if dv == nil || err != nil {
syncRes.result = &reconcile.Result{}
return syncRes, err
Expand Down Expand Up @@ -365,9 +365,9 @@ func (r *ReconcilerBase) getPVC(dv *cdiv1.DataVolume) (*corev1.PersistentVolumeC
return pvc, nil
}

func (r *ReconcilerBase) getDataVolume(key types.NamespacedName) (*cdiv1.DataVolume, error) {
func getDataVolume(c client.Client, key types.NamespacedName) (*cdiv1.DataVolume, error) {
dv := &cdiv1.DataVolume{}
if err := r.client.Get(context.TODO(), key, dv); err != nil {
if err := c.Get(context.TODO(), key, dv); err != nil {
if k8serrors.IsNotFound(err) {
return nil, nil
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/controller/datavolume/import-controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -464,14 +464,15 @@ var _ = Describe("All DataVolume Tests", func() {
Expect(pvc.Spec.Resources.Requests.Storage().Value()).To(Equal(expectedSize.Value()))
})

It("Should pass annotation from DV to created a PVC on a DV", func() {
It("Should pass annotations and labels from DV to created PVC", func() {
dv := NewImportDataVolume("test-dv")
dv.SetAnnotations(make(map[string]string))
dv.GetAnnotations()["test-ann-1"] = "test-value-1"
dv.GetAnnotations()["test-ann-2"] = "test-value-2"
dv.GetAnnotations()[AnnSource] = "invalid phase should not copy"
dv.GetAnnotations()[AnnPodNetwork] = "data-network"
dv.GetAnnotations()[AnnPodSidecarInjection] = "false"
dv.Labels = map[string]string{"test": "test-label"}
reconciler = createImportReconciler(dv)
_, err := reconciler.Reconcile(context.TODO(), reconcile.Request{NamespacedName: types.NamespacedName{Name: "test-dv", Namespace: metav1.NamespaceDefault}})
Expect(err).ToNot(HaveOccurred())
Expand All @@ -486,6 +487,7 @@ var _ = Describe("All DataVolume Tests", func() {
Expect(pvc.GetAnnotations()[AnnPodNetwork]).To(Equal("data-network"))
Expect(pvc.GetAnnotations()[AnnPodSidecarInjection]).To(Equal("false"))
Expect(pvc.GetAnnotations()[AnnPriorityClassName]).To(Equal("p0"))
Expect(pvc.Labels["test"]).To(Equal("test-label"))
})

It("Should pass annotation from DV with S3 source to created a PVC on a DV", func() {
Expand Down
15 changes: 13 additions & 2 deletions pkg/controller/datavolume/smart-clone-controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ func (r *SmartCloneReconciler) reconcileSnapshot(log logr.Logger, snapshot *snap
if err != nil {
return reconcile.Result{}, err
}
newPvc, err := newPvcFromSnapshot(snapshot.Name, snapshot, targetPvcSpec)
newPvc, err := newPvcFromSnapshot(r.client, snapshot.Name, snapshot, targetPvcSpec)
if err != nil {
return reconcile.Result{}, err
}
Expand Down Expand Up @@ -352,7 +352,7 @@ func (r *SmartCloneReconciler) getTargetPVC(dataVolume *cdiv1.DataVolume) (*core
return pvc, nil
}

func newPvcFromSnapshot(name string, snapshot *snapshotv1.VolumeSnapshot, targetPvcSpec *corev1.PersistentVolumeClaimSpec) (*corev1.PersistentVolumeClaim, error) {
func newPvcFromSnapshot(c client.Client, name string, snapshot *snapshotv1.VolumeSnapshot, targetPvcSpec *corev1.PersistentVolumeClaimSpec) (*corev1.PersistentVolumeClaim, error) {
targetPvcSpecCopy := targetPvcSpec.DeepCopy()
restoreSize := snapshot.Status.RestoreSize
if restoreSize == nil {
Expand All @@ -369,6 +369,17 @@ func newPvcFromSnapshot(name string, snapshot *snapshotv1.VolumeSnapshot, target
common.CDILabelKey: common.CDILabelValue,
common.CDIComponentLabel: common.SmartClonerCDILabel,
}

dv, err := getDataVolume(c, client.ObjectKeyFromObject(snapshot))
if err != nil {
return nil, err
}
if dv != nil {
for k, v := range dv.Labels {
labels[k] = v
}
}

if util.ResolveVolumeMode(targetPvcSpecCopy.VolumeMode) == corev1.PersistentVolumeFilesystem {
labels[common.KubePersistentVolumeFillingUpSuppressLabelKey] = common.KubePersistentVolumeFillingUpSuppressLabelValue
}
Expand Down
9 changes: 6 additions & 3 deletions pkg/controller/datavolume/smart-clone-controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,10 @@ var _ = Describe("All smart clone tests", func() {
It("Should create PVC if snapshot ready", func() {
dv := newCloneDataVolume("test-dv")
q, _ := resource.ParseQuantity("500Mi")
// Set annotation on DV which we can verify on PVC later
dv.GetAnnotations()["test"] = "test-value"
// Set annotation and label on DV which we can verify on PVC later
dv.Annotations["test"] = "test-value"
dv.Labels = map[string]string{"test": "test-label"}

snapshot := createSnapshotVolume(dv.Name, dv.Namespace, nil)
snapshot.Spec.Source = snapshotv1.VolumeSnapshotSource{
PersistentVolumeClaimName: &[]string{"source"}[0],
Expand All @@ -234,8 +236,9 @@ var _ = Describe("All smart clone tests", func() {
Expect(err).ToNot(HaveOccurred())
Expect(pvc.Labels[common.AppKubernetesVersionLabel]).To(Equal("v0.0.0-tests"))
Expect(pvc.Labels[common.KubePersistentVolumeFillingUpSuppressLabelKey]).To(Equal(common.KubePersistentVolumeFillingUpSuppressLabelValue))
Expect(pvc.Labels["test"]).To(Equal("test-label"))
// Verify PVC's annotation
Expect(pvc.GetAnnotations()["test"]).To(Equal("test-value"))
Expect(pvc.Annotations["test"]).To(Equal("test-value"))
event := <-reconciler.recorder.(*record.FakeRecorder).Events
Expect(event).To(ContainSubstring("Creating PVC for smart-clone is in progress"))
})
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/datavolume/snapshot-clone-controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ func (r *SnapshotCloneReconciler) getStorageClassCorrespondingToSnapClass(driver
}

func (r *SnapshotCloneReconciler) makePvcFromSnapshot(pvcName string, dv *cdiv1.DataVolume, snapshot *snapshotv1.VolumeSnapshot, targetPvcSpec *corev1.PersistentVolumeClaimSpec) (*corev1.PersistentVolumeClaim, error) {
newPvc, err := newPvcFromSnapshot(pvcName, snapshot, targetPvcSpec)
newPvc, err := newPvcFromSnapshot(r.client, pvcName, snapshot, targetPvcSpec)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit ea577b8

Please sign in to comment.