Skip to content

Commit 5eedf1e

Browse files
committed
fix(restore): fix original metadata restoration
--------- Signed-off-by: Isteb4k <dmitry.rakitin@flant.com>
1 parent da9ce73 commit 5eedf1e

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

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/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)