Skip to content
12 changes: 12 additions & 0 deletions images/virtualization-artifact/pkg/common/object/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,15 @@ func RemoveAnnotation(ctx context.Context, cl client.Client, obj client.Object,
}
return cl.Patch(ctx, obj, client.RawPatch(types.JSONPatchType, bytes))
}

func RemoveLabel(ctx context.Context, cl client.Client, obj client.Object, labelKey string) error {
if _, exist := obj.GetLabels()[labelKey]; !exist {
return nil
}
jsonOp := patch.WithRemove(fmt.Sprintf("/metadata/labels/%s", patch.EscapeJSONPointer(labelKey)))
bytes, err := patch.NewJSONPatch(jsonOp).Bytes()
if err != nil {
return err
}
return cl.Patch(ctx, obj, client.RawPatch(types.JSONPatchType, bytes))
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

"github.com/deckhouse/virtualization-controller/pkg/common/annotations"
"github.com/deckhouse/virtualization-controller/pkg/common/object"
pvcspec "github.com/deckhouse/virtualization-controller/pkg/common/pvc"
commonvd "github.com/deckhouse/virtualization-controller/pkg/common/vd"
Expand Down Expand Up @@ -594,12 +595,18 @@ func (h MigrationHandler) handleComplete(ctx context.Context, vd *v1alpha2.Virtu
}

log.Info("Complete migration. Delete source PersistentVolumeClaim", slog.String("pvc.name", vd.Status.MigrationState.SourcePVC), slog.String("pvc.namespace", vd.Namespace))

err = h.deleteSourcePersistentVolumeClaim(ctx, vd)
if err != nil {
return err
}
log.Debug("Source PersistentVolumeClaim was deleted", slog.String("pvc.name", vd.Status.MigrationState.SourcePVC), slog.String("pvc.namespace", vd.Namespace))

// Remove quota override label from target PVC.
if err := object.RemoveLabel(ctx, h.client, targetPVC, annotations.QuotaExcludeLabel); err != nil && !k8serrors.IsNotFound(err) {
return fmt.Errorf("remove quota override label from target PVC: %w", err)
}

if sc := vd.Spec.PersistentVolumeClaim.StorageClass; sc != nil && *sc != "" {
vd.Status.StorageClassName = *sc
}
Expand Down Expand Up @@ -655,6 +662,9 @@ func (h MigrationHandler) createTargetPersistentVolumeClaim(ctx context.Context,
OwnerReferences: []metav1.OwnerReference{
service.MakeControllerOwnerReference(vd),
},
Labels: map[string]string{
annotations.QuotaExcludeLabel: annotations.QuotaExcludeValue,
},
},
Spec: ptr.Deref(
pvcspec.CreateSpec(&sc.Name, size, accessMode, volumeMode),
Expand Down Expand Up @@ -685,9 +695,12 @@ func (h MigrationHandler) deleteTargetPersistentVolumeClaim(ctx context.Context,

func (h MigrationHandler) deleteSourcePersistentVolumeClaim(ctx context.Context, vd *v1alpha2.VirtualDisk) error {
pvc, err := h.getSourcePersistentVolumeClaim(ctx, vd)
if pvc == nil || err != nil {
if err != nil && !k8serrors.IsNotFound(err) {
return err
}
if pvc == nil {
return nil
}

return deletePersistentVolumeClaim(ctx, pvc, h.client)
}
Expand Down
Loading