Skip to content

Commit

Permalink
Transferring annotations in smart clone and passing dv instead of client
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 22, 2023
1 parent 67bd1f8 commit c02fa12
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 24 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 := getDataVolume(r.client, req.NamespacedName)
dv, err := r.getDataVolume(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 getDataVolume(c client.Client, key types.NamespacedName) (*cdiv1.DataVolume, error) {
func (r *ReconcilerBase) getDataVolume(key types.NamespacedName) (*cdiv1.DataVolume, error) {
dv := &cdiv1.DataVolume{}
if err := c.Get(context.TODO(), key, dv); err != nil {
if err := r.client.Get(context.TODO(), key, dv); err != nil {
if k8serrors.IsNotFound(err) {
return nil, nil
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/controller/datavolume/import-controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,8 @@ var _ = Describe("All DataVolume Tests", func() {
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"}
dv.SetLabels(make(map[string]string))
dv.GetLabels()["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 Down
37 changes: 18 additions & 19 deletions pkg/controller/datavolume/smart-clone-controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,8 @@ func (r *SmartCloneReconciler) reconcileSnapshot(log logr.Logger, snapshot *snap
if err != nil {
return reconcile.Result{}, err
}
newPvc, err := newPvcFromSnapshot(r.client, snapshot.Name, snapshot, targetPvcSpec)

newPvc, err := newPvcFromSnapshot(dataVolume, snapshot.Name, snapshot, targetPvcSpec)
if err != nil {
return reconcile.Result{}, err
}
Expand Down Expand Up @@ -352,7 +353,7 @@ func (r *SmartCloneReconciler) getTargetPVC(dataVolume *cdiv1.DataVolume) (*core
return pvc, nil
}

func newPvcFromSnapshot(c client.Client, name string, snapshot *snapshotv1.VolumeSnapshot, targetPvcSpec *corev1.PersistentVolumeClaimSpec) (*corev1.PersistentVolumeClaim, error) {
func newPvcFromSnapshot(dv *cdiv1.DataVolume, name string, snapshot *snapshotv1.VolumeSnapshot, targetPvcSpec *corev1.PersistentVolumeClaimSpec) (*corev1.PersistentVolumeClaim, error) {
targetPvcSpecCopy := targetPvcSpec.DeepCopy()
restoreSize := snapshot.Status.RestoreSize
if restoreSize == nil {
Expand All @@ -369,15 +370,19 @@ func newPvcFromSnapshot(c client.Client, name string, snapshot *snapshotv1.Volum
common.CDILabelKey: common.CDILabelValue,
common.CDIComponentLabel: common.SmartClonerCDILabel,
}
for k, v := range dv.Labels {
labels[k] = v
}

dv, err := getDataVolume(c, client.ObjectKeyFromObject(snapshot))
if err != nil {
return nil, err
annotations := map[string]string{
AnnSmartCloneRequest: "true",
cc.AnnRunningCondition: string(corev1.ConditionFalse),
cc.AnnRunningConditionMessage: cc.CloneComplete,
cc.AnnRunningConditionReason: "Completed",
annSmartCloneSnapshot: key,
}
if dv != nil {
for k, v := range dv.Labels {
labels[k] = v
}
for k, v := range dv.ObjectMeta.Annotations {
annotations[k] = v
}

if util.ResolveVolumeMode(targetPvcSpecCopy.VolumeMode) == corev1.PersistentVolumeFilesystem {
Expand All @@ -386,16 +391,10 @@ func newPvcFromSnapshot(c client.Client, name string, snapshot *snapshotv1.Volum

target := &corev1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: snapshot.Namespace,
Labels: labels,
Annotations: map[string]string{
AnnSmartCloneRequest: "true",
cc.AnnRunningCondition: string(corev1.ConditionFalse),
cc.AnnRunningConditionMessage: cc.CloneComplete,
cc.AnnRunningConditionReason: "Completed",
annSmartCloneSnapshot: key,
},
Name: name,
Namespace: snapshot.Namespace,
Labels: labels,
Annotations: annotations,
},
Spec: corev1.PersistentVolumeClaimSpec{
DataSource: &corev1.TypedLocalObjectReference{
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(r.client, pvcName, snapshot, targetPvcSpec)
newPvc, err := newPvcFromSnapshot(dv, pvcName, snapshot, targetPvcSpec)
if err != nil {
return nil, err
}
Expand Down
26 changes: 26 additions & 0 deletions tests/datavolume_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1135,6 +1135,32 @@ var _ = Describe("[vendor:cnv-qe@redhat.com][level:component]DataVolume tests",
table.Entry("[test_id:8045]for clone DataVolume", createCloneDataVolume, fillCommand),
)

table.DescribeTable("Should pass all DV labels and annotations to the PVC", func(dvFunc func(string, string, string) *cdiv1.DataVolume, url string) {
dataVolume := dvFunc("pass-all-labels-dv", "1Gi", url)

By(fmt.Sprintf("creating new datavolume %s", dataVolume.Name))
dataVolume, err := utils.CreateDataVolumeFromDefinition(f.CdiClient, f.Namespace.Name, dataVolume)
Expect(err).ToNot(HaveOccurred())

dataVolume.Labels = map[string]string{"test-label-1": "test-label-1", "test-label-2": "test-label-2"}
dataVolume.Annotations = map[string]string{"test-annotation-1": "test-annotation-1", "test-annotation-2": "test-annotation-2"}

// verify PVC was created
By("verifying pvc was created and is Bound")
pvc, err := utils.WaitForPVC(f.K8sClient, dataVolume.Namespace, dataVolume.Name)
Expect(err).ToNot(HaveOccurred())

// All labels and annotations passed
Expect(pvc.Labels["test-label-1"]).To(Equal("test-label-1"))
Expect(pvc.Labels["test-label-2"]).To(Equal("test-label-2"))
Expect(pvc.Annotations["test-annotation-1"]).To(Equal("test-annotation-1"))
Expect(pvc.Annotations["test-annotation-2"]).To(Equal("test-annotation-2"))
},
table.Entry("for import DataVolume", utils.NewDataVolumeWithHTTPImport, tinyCoreIsoURL()),
table.Entry("for upload DataVolume", createUploadDataVolume, tinyCoreIsoURL()),
table.Entry("for clone DataVolume", createCloneDataVolume, fillCommand),
)

It("Should handle a pre populated DV", func() {
By(fmt.Sprintf("initializing dataVolume marked as prePopulated %s", dataVolumeName))
dataVolume := utils.NewDataVolumeWithHTTPImport(dataVolumeName, "1Gi", cirrosURL())
Expand Down

0 comments on commit c02fa12

Please sign in to comment.