Skip to content

Commit 7dda12c

Browse files
danilrwxIsteb4k
authored andcommitted
feat(vd): add quota override label for PVC migration (#2148)
* feat(vd): add quota override label for PVC migration Add resource-quota-overrides.deckhouse.io/ignore label to target PVC during migration to prevent double counting. --------- Signed-off-by: Daniil Antoshin <daniil.antoshin@flant.com> (cherry picked from commit 4acea76)
1 parent e86d1dd commit 7dda12c

2 files changed

Lines changed: 26 additions & 1 deletion

File tree

images/virtualization-artifact/pkg/common/object/object.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,3 +182,15 @@ func RemoveAnnotation(ctx context.Context, cl client.Client, obj client.Object,
182182
}
183183
return cl.Patch(ctx, obj, client.RawPatch(types.JSONPatchType, bytes))
184184
}
185+
186+
func RemoveLabel(ctx context.Context, cl client.Client, obj client.Object, labelKey string) error {
187+
if _, exist := obj.GetLabels()[labelKey]; !exist {
188+
return nil
189+
}
190+
jsonOp := patch.WithRemove(fmt.Sprintf("/metadata/labels/%s", patch.EscapeJSONPointer(labelKey)))
191+
bytes, err := patch.NewJSONPatch(jsonOp).Bytes()
192+
if err != nil {
193+
return err
194+
}
195+
return cl.Patch(ctx, obj, client.RawPatch(types.JSONPatchType, bytes))
196+
}

images/virtualization-artifact/pkg/controller/vd/internal/migration.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
"sigs.k8s.io/controller-runtime/pkg/client"
3535
"sigs.k8s.io/controller-runtime/pkg/reconcile"
3636

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

596597
log.Info("Complete migration. Delete source PersistentVolumeClaim", slog.String("pvc.name", vd.Status.MigrationState.SourcePVC), slog.String("pvc.namespace", vd.Namespace))
598+
597599
err = h.deleteSourcePersistentVolumeClaim(ctx, vd)
598600
if err != nil {
599601
return err
600602
}
601603
log.Debug("Source PersistentVolumeClaim was deleted", slog.String("pvc.name", vd.Status.MigrationState.SourcePVC), slog.String("pvc.namespace", vd.Namespace))
602604

605+
// Remove quota override label from target PVC.
606+
if err := object.RemoveLabel(ctx, h.client, targetPVC, annotations.QuotaExcludeLabel); err != nil && !k8serrors.IsNotFound(err) {
607+
return fmt.Errorf("remove quota override label from target PVC: %w", err)
608+
}
609+
603610
if sc := vd.Spec.PersistentVolumeClaim.StorageClass; sc != nil && *sc != "" {
604611
vd.Status.StorageClassName = *sc
605612
}
@@ -655,6 +662,9 @@ func (h MigrationHandler) createTargetPersistentVolumeClaim(ctx context.Context,
655662
OwnerReferences: []metav1.OwnerReference{
656663
service.MakeControllerOwnerReference(vd),
657664
},
665+
Labels: map[string]string{
666+
annotations.QuotaExcludeLabel: annotations.QuotaExcludeValue,
667+
},
658668
},
659669
Spec: ptr.Deref(
660670
pvcspec.CreateSpec(&sc.Name, size, accessMode, volumeMode),
@@ -685,9 +695,12 @@ func (h MigrationHandler) deleteTargetPersistentVolumeClaim(ctx context.Context,
685695

686696
func (h MigrationHandler) deleteSourcePersistentVolumeClaim(ctx context.Context, vd *v1alpha2.VirtualDisk) error {
687697
pvc, err := h.getSourcePersistentVolumeClaim(ctx, vd)
688-
if pvc == nil || err != nil {
698+
if err != nil && !k8serrors.IsNotFound(err) {
689699
return err
690700
}
701+
if pvc == nil {
702+
return nil
703+
}
691704

692705
return deletePersistentVolumeClaim(ctx, pvc, h.client)
693706
}

0 commit comments

Comments
 (0)