Skip to content

Commit

Permalink
feat(dataimportcron-controller): Copy labels to VolumeSnapshots
Browse files Browse the repository at this point in the history
When using VolumeSnapshots copy the labels found on the source PVC to
the created or an existing VolumeSnapshot.

Signed-off-by: Felix Matouschek <[email protected]>
  • Loading branch information
0xFelix committed Aug 14, 2024
1 parent 049ac9f commit 53051e8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
10 changes: 5 additions & 5 deletions pkg/controller/dataimportcron-controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -695,15 +695,14 @@ func (r *DataImportCronReconciler) handleSnapshot(ctx context.Context, dataImpor
if err != nil {
return err
}
labels := map[string]string{
common.CDILabelKey: common.CDILabelValue,
common.CDIComponentLabel: "",
}
desiredSnapshot := &snapshotv1.VolumeSnapshot{
ObjectMeta: metav1.ObjectMeta{
Name: pvc.Name,
Namespace: dataImportCron.Namespace,
Labels: labels,
Labels: map[string]string{
common.CDILabelKey: common.CDILabelValue,
common.CDIComponentLabel: "",
},
},
Spec: snapshotv1.VolumeSnapshotSpec{
Source: snapshotv1.VolumeSnapshotSource{
Expand All @@ -713,6 +712,7 @@ func (r *DataImportCronReconciler) handleSnapshot(ctx context.Context, dataImpor
},
}
r.setDataImportCronResourceLabels(dataImportCron, desiredSnapshot)
cc.CopyAllowedLabels(pvc.GetLabels(), desiredSnapshot)

currentSnapshot := &snapshotv1.VolumeSnapshot{}
if err := r.client.Get(ctx, client.ObjectKeyFromObject(desiredSnapshot), currentSnapshot); err != nil {
Expand Down
8 changes: 6 additions & 2 deletions pkg/controller/dataimportcron-controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1042,7 +1042,7 @@ var _ = Describe("All DataImportCron Tests", func() {
Expect(dv.Annotations[cc.AnnImmediateBinding]).To(Equal("true"))
})

It("Should create snapshot, and update DataImportCron and DataSource once its ready to use", func() {
It("Should create snapshot, and update DataImportCron once its ready to use", func() {
cron = newDataImportCron(cronName)
dataSource = nil
retentionPolicy := cdiv1.DataImportCronRetainNone
Expand Down Expand Up @@ -1071,7 +1071,9 @@ var _ = Describe("All DataImportCron Tests", func() {
Expect(*dv.Spec.Source.Registry.URL).To(Equal(testRegistryURL + "@" + testDigest))
Expect(dv.Annotations[cc.AnnImmediateBinding]).To(Equal("true"))

pvc := cc.CreatePvc(dv.Name, dv.Namespace, nil, nil)
pvc := cc.CreatePvc(dv.Name, dv.Namespace, nil, map[string]string{
testKubevirtIoKey: testKubevirtIoValue,
})
pvc.Spec.VolumeMode = ptr.To[corev1.PersistentVolumeMode]("dummy")
err = reconciler.client.Create(context.TODO(), pvc)
Expect(err).ToNot(HaveOccurred())
Expand Down Expand Up @@ -1119,6 +1121,8 @@ var _ = Describe("All DataImportCron Tests", func() {
Expect(err).ToNot(HaveOccurred())
Expect(*snap.Status.ReadyToUse).To(BeTrue())
Expect(*snap.Spec.Source.PersistentVolumeClaimName).To(Equal(dvName))
// Expect labels to be copied from the source PVC
Expect(snap.Labels).To(HaveKeyWithValue(testKubevirtIoKey, testKubevirtIoValue))

err = reconciler.client.Delete(context.TODO(), cron)
Expect(err).ToNot(HaveOccurred())
Expand Down
18 changes: 10 additions & 8 deletions tests/dataimportcron_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ const (
importsToKeep = 1
emptySchedule = ""
errorDigest = "sha256:12345678900987654321"
testKubevirtIoKey = "test.kubevirt.io/test"
testKubevirtIoValue = "testvalue"
)

var _ = Describe("DataImportCron", Serial, func() {
Expand Down Expand Up @@ -154,6 +156,7 @@ var _ = Describe("DataImportCron", Serial, func() {
},
}
snapshot = utils.WaitSnapshotReady(f.CrClient, snapshot)
Expect(snapshot.Labels).To(HaveKeyWithValue(testKubevirtIoKey, testKubevirtIoValue))
deleted, err := utils.WaitPVCDeleted(f.K8sClient, name, ns, 30*time.Second)
if err != nil {
// work around https://github.com/kubernetes-csi/external-snapshotter/issues/957
Expand Down Expand Up @@ -325,16 +328,15 @@ var _ = Describe("DataImportCron", Serial, func() {

By("Verify DataSource was updated")
var dataSource *cdiv1.DataSource
Eventually(func() bool {
Eventually(func(g Gomega) {
dataSource, err = f.CdiClient.CdiV1beta1().DataSources(ns).Get(context.TODO(), cron.Spec.ManagedDataSource, metav1.GetOptions{})
if errors.IsNotFound(err) {
return false
}
Expect(err).ToNot(HaveOccurred())
g.Expect(err).ToNot(HaveOccurred())
readyCond := controller.FindDataSourceConditionByType(dataSource, cdiv1.DataSourceReady)
return readyCond != nil && readyCond.Status == corev1.ConditionTrue &&
getDataSourceName(format, dataSource) == currentImportDv
}, dataImportCronTimeout, pollingInterval).Should(BeTrue(), "DataSource was not updated")
g.Expect(readyCond).ToNot(BeNil())
g.Expect(readyCond.Status).To(Equal(corev1.ConditionTrue))
g.Expect(getDataSourceName(format, dataSource)).To(Equal(currentImportDv))
g.Expect(dataSource.Labels).To(HaveKeyWithValue(testKubevirtIoKey, testKubevirtIoValue))
}, dataImportCronTimeout, pollingInterval).Should(Succeed(), "DataSource was not updated")

By("Verify cron was updated")
Expect(cron.Status.LastImportedPVC).ToNot(BeNil())
Expand Down

0 comments on commit 53051e8

Please sign in to comment.