Skip to content

Commit fc2dee7

Browse files
committed
fix(restore): fix original metadata restoration (#1827)
Fix original metadata restoration --------- Signed-off-by: Isteb4k <dmitry.rakitin@flant.com> (cherry picked from commit 6d8d3eb)
1 parent bb6ff22 commit fc2dee7

File tree

6 files changed

+83
-222
lines changed

6 files changed

+83
-222
lines changed

api/core/v1alpha2/events.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,6 @@ const (
105105
// ReasonVIStorageClassNotFound is event reason that VIStorageClass not found.
106106
ReasonVIStorageClassNotFound = "VirtualImageStorageClassNotFound"
107107

108-
// ReasonMetadataSyncStarted is event reason that Metadata sync is started.
109-
ReasonMetadataSyncStarted = "MetadataSyncStarted"
110108
// ReasonDataSourceSyncStarted is event reason that DataSource sync is started.
111109
ReasonDataSourceSyncStarted = "DataSourceImportStarted"
112110
// ReasonDataSourceSyncInProgress is event reason that DataSource sync is in progress.

images/virtualization-artifact/pkg/controller/service/restorer/snapshot_resources.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,18 @@ package restorer
1818

1919
import (
2020
"context"
21+
"encoding/json"
2122
"errors"
2223
"fmt"
2324

25+
vsv1 "github.com/kubernetes-csi/external-snapshotter/client/v6/apis/volumesnapshot/v1"
2426
corev1 "k8s.io/api/core/v1"
2527
apierrors "k8s.io/apimachinery/pkg/api/errors"
2628
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2729
"k8s.io/apimachinery/pkg/types"
2830
"sigs.k8s.io/controller-runtime/pkg/client"
2931

32+
"github.com/deckhouse/virtualization-controller/pkg/common/annotations"
3033
"github.com/deckhouse/virtualization-controller/pkg/common/object"
3134
"github.com/deckhouse/virtualization-controller/pkg/controller/service/restorer/common"
3235
restorer "github.com/deckhouse/virtualization-controller/pkg/controller/service/restorer/restorers"
@@ -363,6 +366,11 @@ func getVirtualDisks(ctx context.Context, client client.Client, vmSnapshot *v1al
363366
},
364367
}
365368

369+
err = AddOriginalMetadata(ctx, &vd, vdSnapshot, client)
370+
if err != nil {
371+
return nil, fmt.Errorf("add original metadata: %w", err)
372+
}
373+
366374
vds = append(vds, &vd)
367375
}
368376

@@ -372,3 +380,72 @@ func getVirtualDisks(ctx context.Context, client client.Client, vmSnapshot *v1al
372380
func (r *SnapshotResources) GetObjectHandlers() []ObjectHandler {
373381
return r.objectHandlers
374382
}
383+
384+
func AddOriginalMetadata(ctx context.Context, vd *v1alpha2.VirtualDisk, vdSnapshot *v1alpha2.VirtualDiskSnapshot, client client.Client) error {
385+
vsKey := types.NamespacedName{
386+
Namespace: vdSnapshot.Namespace,
387+
Name: vdSnapshot.Status.VolumeSnapshotName,
388+
}
389+
390+
vs, err := object.FetchObject(ctx, vsKey, client, &vsv1.VolumeSnapshot{})
391+
if err != nil {
392+
return fmt.Errorf("fetch the volume snapshot %q: %w", vsKey.Name, err)
393+
}
394+
395+
if vs == nil {
396+
return fmt.Errorf("the volume snapshot %q is nil, please report a bug", vsKey.Name)
397+
}
398+
399+
return errors.Join(
400+
setOriginalAnnotations(vd, vs),
401+
setOriginalLabels(vd, vs),
402+
)
403+
}
404+
405+
func setOriginalAnnotations(vd *v1alpha2.VirtualDisk, vs *vsv1.VolumeSnapshot) error {
406+
if vs == nil || vs.Annotations[annotations.AnnVirtualDiskOriginalAnnotations] == "" {
407+
return nil
408+
}
409+
410+
var annotationsMap map[string]string
411+
err := json.Unmarshal([]byte(vs.Annotations[annotations.AnnVirtualDiskOriginalAnnotations]), &annotationsMap)
412+
if err != nil {
413+
return fmt.Errorf("failed to unmarshal the original annotations: %w", err)
414+
}
415+
416+
if vd.Annotations == nil {
417+
vd.Annotations = make(map[string]string)
418+
}
419+
420+
for key, value := range annotationsMap {
421+
if _, exists := vd.Annotations[key]; !exists {
422+
vd.Annotations[key] = value
423+
}
424+
}
425+
426+
return nil
427+
}
428+
429+
func setOriginalLabels(vd *v1alpha2.VirtualDisk, vs *vsv1.VolumeSnapshot) error {
430+
if vs == nil || vs.Annotations[annotations.AnnVirtualDiskOriginalLabels] == "" {
431+
return nil
432+
}
433+
434+
var labelsMap map[string]string
435+
err := json.Unmarshal([]byte(vs.Annotations[annotations.AnnVirtualDiskOriginalLabels]), &labelsMap)
436+
if err != nil {
437+
return fmt.Errorf("failed to unmarshal the original annotations: %w", err)
438+
}
439+
440+
if vd.Labels == nil {
441+
vd.Labels = make(map[string]string)
442+
}
443+
444+
for key, value := range labelsMap {
445+
if _, exists := vd.Labels[key]; !exists {
446+
vd.Labels[key] = value
447+
}
448+
}
449+
450+
return nil
451+
}

images/virtualization-artifact/pkg/controller/vd/internal/source/object_ref_vdsnapshot.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ func (ds ObjectRefVirtualDiskSnapshot) Sync(ctx context.Context, vd *v1alpha2.Vi
6969
return steptaker.NewStepTakers[*v1alpha2.VirtualDisk](
7070
step.NewReadyStep(ds.diskService, pvc, cb),
7171
step.NewTerminatingStep(pvc),
72-
step.NewAddOriginalMetadataStep(ds.recorder, ds.client, cb),
7372
step.NewCreatePVCFromVDSnapshotStep(pvc, ds.recorder, ds.client, cb),
7473
step.NewWaitForPVCStep(pvc, ds.client, cb),
7574
).Run(ctx, vd)

images/virtualization-artifact/pkg/controller/vd/internal/source/object_ref_vdsnapshot_test.go

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package source
1818

1919
import (
2020
"context"
21-
"fmt"
2221
"log/slog"
2322
"testing"
2423

@@ -35,7 +34,6 @@ import (
3534
"sigs.k8s.io/controller-runtime/pkg/client/fake"
3635
"sigs.k8s.io/controller-runtime/pkg/client/interceptor"
3736

38-
"github.com/deckhouse/virtualization-controller/pkg/common/annotations"
3937
"github.com/deckhouse/virtualization-controller/pkg/controller/conditions"
4038
"github.com/deckhouse/virtualization-controller/pkg/controller/supplements"
4139
"github.com/deckhouse/virtualization-controller/pkg/eventrecord"
@@ -275,30 +273,6 @@ var _ = Describe("ObjectRef VirtualDiskSnapshot", func() {
275273
Expect(vd.Status.Target.PersistentVolumeClaim).NotTo(BeEmpty())
276274
})
277275
})
278-
279-
Context("Virtual disk has annotations and labels", func() {
280-
It("checks that the restored virtual disk has its original metadata", func() {
281-
key := "key"
282-
value := "value"
283-
originalMetadata := fmt.Sprintf("{\"%s\":\"%s\"}", key, value)
284-
285-
vs.Annotations = map[string]string{
286-
annotations.AnnVirtualDiskOriginalAnnotations: originalMetadata,
287-
annotations.AnnVirtualDiskOriginalLabels: originalMetadata,
288-
}
289-
290-
client := fake.NewClientBuilder().WithScheme(scheme).WithObjects(vdSnapshot, vs).Build()
291-
292-
syncer := NewObjectRefVirtualDiskSnapshot(recorder, svc, client)
293-
294-
res, err := syncer.Sync(ctx, vd)
295-
Expect(err).ToNot(HaveOccurred())
296-
Expect(res.IsZero()).To(BeTrue())
297-
298-
Expect(vd.Annotations).To(HaveKeyWithValue(key, value))
299-
Expect(vd.Labels).To(HaveKeyWithValue(key, value))
300-
})
301-
})
302276
})
303277

304278
func ExpectStats(vd *v1alpha2.VirtualDisk) {

images/virtualization-artifact/pkg/controller/vd/internal/source/step/add_original_metadata_step.go

Lines changed: 0 additions & 193 deletions
This file was deleted.

images/virtualization-artifact/pkg/controller/vmrestore/internal/life_cycle.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
"github.com/deckhouse/virtualization-controller/pkg/common/object"
3636
"github.com/deckhouse/virtualization-controller/pkg/controller/conditions"
3737
"github.com/deckhouse/virtualization-controller/pkg/controller/service"
38+
svcrestorer "github.com/deckhouse/virtualization-controller/pkg/controller/service/restorer"
3839
"github.com/deckhouse/virtualization-controller/pkg/controller/vmrestore/internal/restorer"
3940
"github.com/deckhouse/virtualization-controller/pkg/eventrecord"
4041
"github.com/deckhouse/virtualization/api/core/v1alpha2"
@@ -443,6 +444,11 @@ func (h LifeCycleHandler) getVirtualDisks(ctx context.Context, vmSnapshot *v1alp
443444
},
444445
}
445446

447+
err = svcrestorer.AddOriginalMetadata(ctx, &vd, vdSnapshot, h.client)
448+
if err != nil {
449+
return nil, fmt.Errorf("add original metadata: %w", err)
450+
}
451+
446452
vds = append(vds, &vd)
447453
}
448454

0 commit comments

Comments
 (0)