Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(vd): remove pv protection #309

Merged
merged 1 commit into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crds/virtualdisk.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ spec:
type: array
description: |
A list of `VirtualMachines` that use the disk
example: [{ name: "VM100" }, { name: "VM200" }]
example: [{ name: "VM100" }]
items:
type: object
properties:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,6 @@ func (s DiskService) CleanUp(ctx context.Context, sup *supplements.Generator) (b
if err != nil {
return false, err
}
pv, err := s.GetPersistentVolume(ctx, pvc)
if err != nil {
return false, err
}

var resourcesHaveDeleted bool

Expand All @@ -123,20 +119,6 @@ func (s DiskService) CleanUp(ctx context.Context, sup *supplements.Generator) (b
}
}

if pv != nil {
resourcesHaveDeleted = true

err = s.protection.RemoveProtection(ctx, pv)
if err != nil {
return false, err
}

err = s.client.Delete(ctx, pv)
if err != nil && !k8serrors.IsNotFound(err) {
return false, err
}
}

return resourcesHaveDeleted || subResourcesHaveDeleted, nil
}

Expand All @@ -159,7 +141,9 @@ func (s DiskService) CleanUpSupplements(ctx context.Context, sup *supplements.Ge
if err != nil && !k8serrors.IsNotFound(err) {
return false, err
}
pvc, err := s.GetPersistentVolumeClaim(ctx, sup)

var pvc *corev1.PersistentVolumeClaim
pvc, err = s.GetPersistentVolumeClaim(ctx, sup)
if err != nil {
return false, err
}
Expand All @@ -177,13 +161,13 @@ func (s DiskService) CleanUpSupplements(ctx context.Context, sup *supplements.Ge
return hasDeleted, supplements.CleanupForDataVolume(ctx, s.client, sup, s.dvcrSettings)
}

func (s DiskService) Protect(ctx context.Context, owner client.Object, dv *cdiv1.DataVolume, pvc *corev1.PersistentVolumeClaim, pv *corev1.PersistentVolume) error {
func (s DiskService) Protect(ctx context.Context, owner client.Object, dv *cdiv1.DataVolume, pvc *corev1.PersistentVolumeClaim) error {
err := s.protection.AddOwnerRef(ctx, owner, pvc)
if err != nil {
return fmt.Errorf("failed to add owner ref for pvc: %w", err)
}

err = s.protection.AddProtection(ctx, dv, pvc, pv)
err = s.protection.AddProtection(ctx, dv, pvc)
if err != nil {
return fmt.Errorf("failed to add protection for disk's supplements: %w", err)
}
Expand Down Expand Up @@ -259,14 +243,6 @@ func (s DiskService) GetPersistentVolumeClaim(ctx context.Context, sup *suppleme
return helper.FetchObject(ctx, sup.PersistentVolumeClaim(), s.client, &corev1.PersistentVolumeClaim{})
}

func (s DiskService) GetPersistentVolume(ctx context.Context, pvc *corev1.PersistentVolumeClaim) (*corev1.PersistentVolume, error) {
if pvc == nil {
return nil, nil
}

return helper.FetchObject(ctx, types.NamespacedName{Name: pvc.Spec.VolumeName}, s.client, &corev1.PersistentVolume{})
}

func (s DiskService) CheckImportProcess(ctx context.Context, dv *cdiv1.DataVolume, pvc *corev1.PersistentVolumeClaim, storageClassName *string) error {
var err error

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,15 @@ func (ds BlankDataSource) Sync(ctx context.Context, vd *virtv2.VirtualDisk) (boo
if err != nil {
return false, err
}
pv, err := ds.diskService.GetPersistentVolume(ctx, pvc)
if err != nil {
return false, err
}

switch {
case isDiskProvisioningFinished(condition):
log.Info("Disk provisioning finished: clean up")

setPhaseConditionForFinishedDisk(pv, pvc, &condition, &vd.Status.Phase, supgen)
setPhaseConditionForFinishedDisk(pvc, &condition, &vd.Status.Phase, supgen)

// Protect Ready Disk and underlying PVC and PV.
err = ds.diskService.Protect(ctx, vd, nil, pvc, pv)
err = ds.diskService.Protect(ctx, vd, nil, pvc)
if err != nil {
return false, err
}
Expand All @@ -87,7 +83,7 @@ func (ds BlankDataSource) Sync(ctx context.Context, vd *virtv2.VirtualDisk) (boo
}

return CleanUpSupplements(ctx, vd, ds)
case common.AnyTerminating(dv, pvc, pv):
case common.AnyTerminating(dv, pvc):
log.Info("Waiting for supplements to be terminated")
case dv == nil:
log.Info("Start import to PVC")
Expand Down Expand Up @@ -140,7 +136,7 @@ func (ds BlankDataSource) Sync(ctx context.Context, vd *virtv2.VirtualDisk) (boo
vd.Status.Capacity = ds.diskService.GetCapacity(pvc)
vd.Status.Target.PersistentVolumeClaim = dv.Status.ClaimName

err = ds.diskService.Protect(ctx, vd, dv, pvc, pv)
err = ds.diskService.Protect(ctx, vd, dv, pvc)
if err != nil {
return false, err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,15 @@ func (ds HTTPDataSource) Sync(ctx context.Context, vd *virtv2.VirtualDisk) (bool
if err != nil {
return false, err
}
pv, err := ds.diskService.GetPersistentVolume(ctx, pvc)
if err != nil {
return false, err
}

switch {
case isDiskProvisioningFinished(condition):
log.Info("Disk provisioning finished: clean up")

setPhaseConditionForFinishedDisk(pv, pvc, &condition, &vd.Status.Phase, supgen)
setPhaseConditionForFinishedDisk(pvc, &condition, &vd.Status.Phase, supgen)

// Protect Ready Disk and underlying PVC and PV.
err = ds.diskService.Protect(ctx, vd, nil, pvc, pv)
err = ds.diskService.Protect(ctx, vd, nil, pvc)
if err != nil {
return false, err
}
Expand All @@ -109,7 +105,7 @@ func (ds HTTPDataSource) Sync(ctx context.Context, vd *virtv2.VirtualDisk) (bool
}

return CleanUpSupplements(ctx, vd, ds)
case common.AnyTerminating(pod, dv, pvc, pv):
case common.AnyTerminating(pod, dv, pvc):
log.Info("Waiting for supplements to be terminated")
case pod == nil:
log.Info("Start import to DVCR")
Expand Down Expand Up @@ -231,7 +227,7 @@ func (ds HTTPDataSource) Sync(ctx context.Context, vd *virtv2.VirtualDisk) (bool
vd.Status.Capacity = ds.diskService.GetCapacity(pvc)
vd.Status.Target.PersistentVolumeClaim = dv.Status.ClaimName

err = ds.diskService.Protect(ctx, vd, dv, pvc, pv)
err = ds.diskService.Protect(ctx, vd, dv, pvc)
if err != nil {
return false, err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,15 @@ func (ds ObjectRefDataSource) Sync(ctx context.Context, vd *virtv2.VirtualDisk)
if err != nil {
return false, err
}
pv, err := ds.diskService.GetPersistentVolume(ctx, pvc)
if err != nil {
return false, err
}

switch {
case isDiskProvisioningFinished(condition):
log.Info("Disk provisioning finished: clean up")

setPhaseConditionForFinishedDisk(pv, pvc, &condition, &vd.Status.Phase, supgen)
setPhaseConditionForFinishedDisk(pvc, &condition, &vd.Status.Phase, supgen)

// Protect Ready Disk and underlying PVC and PV.
err = ds.diskService.Protect(ctx, vd, nil, pvc, pv)
err = ds.diskService.Protect(ctx, vd, nil, pvc)
if err != nil {
return false, err
}
Expand All @@ -96,7 +92,7 @@ func (ds ObjectRefDataSource) Sync(ctx context.Context, vd *virtv2.VirtualDisk)
}

return CleanUpSupplements(ctx, vd, ds)
case common.AnyTerminating(dv, pvc, pv):
case common.AnyTerminating(dv, pvc):
log.Info("Waiting for supplements to be terminated")
case dv == nil:
log.Info("Start import to PVC")
Expand Down Expand Up @@ -171,7 +167,7 @@ func (ds ObjectRefDataSource) Sync(ctx context.Context, vd *virtv2.VirtualDisk)
vd.Status.Capacity = ds.diskService.GetCapacity(pvc)
vd.Status.Target.PersistentVolumeClaim = dv.Status.ClaimName

err = ds.diskService.Protect(ctx, vd, dv, pvc, pv)
err = ds.diskService.Protect(ctx, vd, dv, pvc)
if err != nil {
return false, err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,15 @@ func (ds RegistryDataSource) Sync(ctx context.Context, vd *virtv2.VirtualDisk) (
if err != nil {
return false, err
}
pv, err := ds.diskService.GetPersistentVolume(ctx, pvc)
if err != nil {
return false, err
}

switch {
case isDiskProvisioningFinished(condition):
log.Info("Disk provisioning finished: clean up")

setPhaseConditionForFinishedDisk(pv, pvc, &condition, &vd.Status.Phase, supgen)
setPhaseConditionForFinishedDisk(pvc, &condition, &vd.Status.Phase, supgen)

// Protect Ready Disk and underlying PVC and PV.
err = ds.diskService.Protect(ctx, vd, nil, pvc, pv)
err = ds.diskService.Protect(ctx, vd, nil, pvc)
if err != nil {
return false, err
}
Expand Down Expand Up @@ -235,7 +231,7 @@ func (ds RegistryDataSource) Sync(ctx context.Context, vd *virtv2.VirtualDisk) (
vd.Status.Capacity = ds.diskService.GetCapacity(pvc)
vd.Status.Target.PersistentVolumeClaim = dv.Status.ClaimName

err = ds.diskService.Protect(ctx, vd, dv, pvc, pv)
err = ds.diskService.Protect(ctx, vd, dv, pvc)
if err != nil {
return false, err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ func isDiskProvisioningFinished(c metav1.Condition) bool {
}

func setPhaseConditionForFinishedDisk(
pv *corev1.PersistentVolume,
pvc *corev1.PersistentVolumeClaim,
condition *metav1.Condition,
phase *virtv2.DiskPhase,
Expand All @@ -110,7 +109,7 @@ func setPhaseConditionForFinishedDisk(
condition.Status = metav1.ConditionFalse
condition.Reason = vdcondition.Lost
condition.Message = fmt.Sprintf("PVC %s not found.", supgen.PersistentVolumeClaim().String())
case pv == nil:
case pvc.Status.Phase == corev1.ClaimLost:
*phase = virtv2.DiskLost
condition.Status = metav1.ConditionFalse
condition.Reason = vdcondition.Lost
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,6 @@ func (ds UploadDataSource) Sync(ctx context.Context, vd *virtv2.VirtualDisk) (bo
if err != nil {
return false, err
}
pv, err := ds.diskService.GetPersistentVolume(ctx, pvc)
if err != nil {
return false, err
}

if vd.Status.UploadCommand == "" {
if ing != nil && ing.Annotations[common.AnnUploadURL] != "" {
Expand All @@ -103,10 +99,10 @@ func (ds UploadDataSource) Sync(ctx context.Context, vd *virtv2.VirtualDisk) (bo
case isDiskProvisioningFinished(condition):
log.Info("Disk provisioning finished: clean up")

setPhaseConditionForFinishedDisk(pv, pvc, &condition, &vd.Status.Phase, supgen)
setPhaseConditionForFinishedDisk(pvc, &condition, &vd.Status.Phase, supgen)

// Protect Ready Disk and underlying PVC and PV.
err = ds.diskService.Protect(ctx, vd, nil, pvc, pv)
err = ds.diskService.Protect(ctx, vd, nil, pvc)
if err != nil {
return false, err
}
Expand Down Expand Up @@ -258,7 +254,7 @@ func (ds UploadDataSource) Sync(ctx context.Context, vd *virtv2.VirtualDisk) (bo
vd.Status.Capacity = ds.diskService.GetCapacity(pvc)
vd.Status.Target.PersistentVolumeClaim = dv.Status.ClaimName

err = ds.diskService.Protect(ctx, vd, dv, pvc, pv)
err = ds.diskService.Protect(ctx, vd, dv, pvc)
if err != nil {
return false, err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func (r *Reconciler) SetupController(_ context.Context, mgr manager.Manager, ctr
return true
}

return oldPVC.Status.Phase != newPVC.Status.Phase && newPVC.Status.Phase == corev1.ClaimBound
return oldPVC.Status.Phase != newPVC.Status.Phase
},
},
); err != nil {
Expand Down
Loading