Skip to content

Commit b4dc55a

Browse files
committed
Fix DataImportCron reconciliation when first default StorageClass is set
The default SC change condition only handled changes from one SC to another, but did not handle the first default SC being set. To get the DIC-created DV with the DIC-special RWO preference, it must be deleted (as DV is immutable) and recreated with the right settings from the new default StorageClass. Signed-off-by: Noam Assouline <nassouli@redhat.com>
1 parent 5591d89 commit b4dc55a

2 files changed

Lines changed: 72 additions & 5 deletions

File tree

pkg/controller/dataimportcron-controller.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -359,19 +359,25 @@ func (r *DataImportCronReconciler) update(ctx context.Context, dataImportCron *c
359359
return res, err
360360
}
361361
if desiredStorageClass != nil {
362-
if deleted, err := r.deleteOutdatedPendingPvc(ctx, pvc, desiredStorageClass.Name, dataImportCron.Name); deleted || err != nil {
362+
desiredSc := desiredStorageClass.Name
363+
if deleted, err := r.deleteOutdatedPendingPvc(ctx, pvc, desiredSc, dataImportCron.Name); deleted || err != nil {
363364
return res, err
364365
}
365366
currentSc, hasCurrent := dataImportCron.Annotations[AnnStorageClass]
366-
desiredSc := desiredStorageClass.Name
367-
if hasCurrent && currentSc != desiredSc {
368-
r.log.Info("Storage class changed, delete most recent source on the old sc as it's no longer the desired", "currentSc", currentSc, "desiredSc", desiredSc)
367+
var storageClassChanged bool
368+
if hasCurrent {
369+
storageClassChanged = currentSc != desiredSc
370+
} else {
371+
storageClassChanged = len(imports) > 0
372+
}
373+
if storageClassChanged {
374+
r.log.Info("Storage class changed, deleting imports on old SC", "currentSc", currentSc, "desiredSc", desiredSc)
369375
if err := r.handleStorageClassChange(ctx, dataImportCron, desiredSc); err != nil {
370376
return res, err
371377
}
372378
return reconcile.Result{RequeueAfter: time.Second}, nil
373379
}
374-
cc.AddAnnotation(dataImportCron, AnnStorageClass, desiredStorageClass.Name)
380+
cc.AddAnnotation(dataImportCron, AnnStorageClass, desiredSc)
375381
}
376382
format, err := r.getSourceFormat(ctx, desiredStorageClass)
377383
if err != nil {

pkg/controller/dataimportcron-controller_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1844,6 +1844,67 @@ var _ = Describe("All DataImportCron Tests", func() {
18441844
Expect(dv.Spec.Storage.AccessModes).To(BeEmpty())
18451845
})
18461846

1847+
It("Should delete old DV and requeue when first default StorageClass is set", func() {
1848+
reconciler = createDataImportCronReconciler()
1849+
cron = newDataImportCron(cronName)
1850+
err := reconciler.client.Create(context.TODO(), cron)
1851+
Expect(err).ToNot(HaveOccurred())
1852+
1853+
cc.AddAnnotation(cron, AnnSourceDesiredDigest, testDigest)
1854+
err = reconciler.client.Update(context.TODO(), cron)
1855+
Expect(err).ToNot(HaveOccurred())
1856+
dataSource = &cdiv1.DataSource{}
1857+
1858+
// Reconcile creates a DV without any SC
1859+
_, err = reconciler.Reconcile(context.TODO(), cronReq)
1860+
Expect(err).ToNot(HaveOccurred())
1861+
err = reconciler.client.Get(context.TODO(), cronKey, cron)
1862+
Expect(err).ToNot(HaveOccurred())
1863+
1864+
imports := cron.Status.CurrentImports
1865+
Expect(imports).ToNot(BeEmpty())
1866+
oldDvName := imports[0].DataVolumeName
1867+
1868+
oldDv := &cdiv1.DataVolume{}
1869+
err = reconciler.client.Get(context.TODO(), dvKey(oldDvName), oldDv)
1870+
Expect(err).ToNot(HaveOccurred())
1871+
Expect(cron.Annotations).ToNot(HaveKey(AnnStorageClass))
1872+
1873+
err = reconciler.client.Create(context.TODO(), sc)
1874+
Expect(err).ToNot(HaveOccurred())
1875+
sp := &cdiv1.StorageProfile{
1876+
ObjectMeta: metav1.ObjectMeta{Name: storageClassName},
1877+
}
1878+
err = reconciler.client.Create(context.TODO(), sp)
1879+
Expect(err).ToNot(HaveOccurred())
1880+
1881+
// Reconcile detects nil-to-non-nil SC change, deletes old DV, requeues
1882+
res, err := reconciler.Reconcile(context.TODO(), cronReq)
1883+
Expect(err).ToNot(HaveOccurred())
1884+
Expect(res.RequeueAfter).To(Equal(time.Second))
1885+
1886+
err = reconciler.client.Get(context.TODO(), dvKey(oldDvName), oldDv)
1887+
Expect(k8serrors.IsNotFound(err)).To(BeTrue())
1888+
1889+
err = reconciler.client.Get(context.TODO(), cronKey, cron)
1890+
Expect(err).ToNot(HaveOccurred())
1891+
Expect(cron.Annotations[AnnStorageClass]).To(Equal(storageClassName))
1892+
1893+
// Reconcile again creates a new DV
1894+
_, err = reconciler.Reconcile(context.TODO(), cronReq)
1895+
Expect(err).ToNot(HaveOccurred())
1896+
1897+
err = reconciler.client.Get(context.TODO(), cronKey, cron)
1898+
Expect(err).ToNot(HaveOccurred())
1899+
imports = cron.Status.CurrentImports
1900+
Expect(imports).ToNot(BeEmpty())
1901+
newDvName := imports[0].DataVolumeName
1902+
1903+
newDv := &cdiv1.DataVolume{}
1904+
err = reconciler.client.Get(context.TODO(), dvKey(newDvName), newDv)
1905+
Expect(err).ToNot(HaveOccurred())
1906+
})
1907+
18471908
})
18481909
})
18491910
})

0 commit comments

Comments
 (0)