Skip to content

Commit 0106751

Browse files
authored
fix(vmop): fix vd annotation and label restoring by vmop restore (#1753)
Signed-off-by: Valeriy Khorunzhin <[email protected]>
1 parent 407d6fb commit 0106751

File tree

3 files changed

+96
-34
lines changed

3 files changed

+96
-34
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
Copyright 2025 Flant JSC
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package vdsnapshot
18+
19+
import (
20+
"encoding/json"
21+
"fmt"
22+
23+
vsv1 "github.com/kubernetes-csi/external-snapshotter/client/v6/apis/volumesnapshot/v1"
24+
25+
"github.com/deckhouse/virtualization-controller/pkg/common/annotations"
26+
"github.com/deckhouse/virtualization/api/core/v1alpha2"
27+
)
28+
29+
// AddOriginalMetadata adds original annotations and labels from VolumeSnapshot to VirtualDisk,
30+
// without overwriting existing values
31+
func AddOriginalMetadata(vd *v1alpha2.VirtualDisk, vs *vsv1.VolumeSnapshot) error {
32+
var (
33+
labelsMap map[string]string
34+
annotationsMap map[string]string
35+
)
36+
37+
if vs != nil && vs.Annotations != nil {
38+
if vs.Annotations[annotations.AnnVirtualDiskOriginalLabels] != "" {
39+
err := json.Unmarshal([]byte(vs.Annotations[annotations.AnnVirtualDiskOriginalLabels]), &labelsMap)
40+
if err != nil {
41+
return fmt.Errorf("failed to unmarshal the original labels: %w", err)
42+
}
43+
}
44+
45+
if vd.Labels == nil {
46+
vd.Labels = make(map[string]string)
47+
}
48+
for key, value := range labelsMap {
49+
if _, exists := vd.Labels[key]; !exists {
50+
vd.Labels[key] = value
51+
}
52+
}
53+
54+
if vs.Annotations[annotations.AnnVirtualDiskOriginalAnnotations] != "" {
55+
err := json.Unmarshal([]byte(vs.Annotations[annotations.AnnVirtualDiskOriginalAnnotations]), &annotationsMap)
56+
if err != nil {
57+
return fmt.Errorf("failed to unmarshal the original annotations: %w", err)
58+
}
59+
}
60+
61+
if vd.Annotations == nil {
62+
vd.Annotations = make(map[string]string)
63+
}
64+
for key, value := range labelsMap {
65+
if _, exists := vd.Annotations[key]; !exists {
66+
vd.Annotations[key] = value
67+
}
68+
}
69+
}
70+
71+
return nil
72+
}

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@ import (
2121
"errors"
2222
"fmt"
2323

24+
vsv1 "github.com/kubernetes-csi/external-snapshotter/client/v6/apis/volumesnapshot/v1"
2425
corev1 "k8s.io/api/core/v1"
2526
apierrors "k8s.io/apimachinery/pkg/api/errors"
2627
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2728
"k8s.io/apimachinery/pkg/types"
2829
"sigs.k8s.io/controller-runtime/pkg/client"
2930

3031
"github.com/deckhouse/virtualization-controller/pkg/common/object"
32+
commonvdsnapshot "github.com/deckhouse/virtualization-controller/pkg/common/vdsnapshot"
3133
"github.com/deckhouse/virtualization-controller/pkg/controller/service/restorer/common"
3234
restorer "github.com/deckhouse/virtualization-controller/pkg/controller/service/restorer/restorers"
3335
"github.com/deckhouse/virtualization/api/core/v1alpha2"
@@ -363,6 +365,23 @@ func getVirtualDisks(ctx context.Context, client client.Client, vmSnapshot *v1al
363365
},
364366
}
365367

368+
if vdSnapshot.Status.VolumeSnapshotName != "" {
369+
vsKey := types.NamespacedName{
370+
Namespace: vdSnapshot.Namespace,
371+
Name: vdSnapshot.Status.VolumeSnapshotName,
372+
}
373+
374+
vs, err := object.FetchObject(ctx, vsKey, client, &vsv1.VolumeSnapshot{})
375+
if err != nil {
376+
return nil, fmt.Errorf("failed to fetch the volume snapshot %q: %w", vsKey.Name, err)
377+
}
378+
379+
err = commonvdsnapshot.AddOriginalMetadata(&vd, vs)
380+
if err != nil {
381+
return nil, fmt.Errorf("failed to add original metadata: %w", err)
382+
}
383+
}
384+
366385
vds = append(vds, &vd)
367386
}
368387

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

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package step
1818

1919
import (
2020
"context"
21-
"encoding/json"
2221
"fmt"
2322
"strings"
2423

@@ -35,6 +34,7 @@ import (
3534
"github.com/deckhouse/virtualization-controller/pkg/common/annotations"
3635
"github.com/deckhouse/virtualization-controller/pkg/common/object"
3736
"github.com/deckhouse/virtualization-controller/pkg/common/pointer"
37+
commonvdsnapshot "github.com/deckhouse/virtualization-controller/pkg/common/vdsnapshot"
3838
"github.com/deckhouse/virtualization-controller/pkg/controller/conditions"
3939
"github.com/deckhouse/virtualization-controller/pkg/controller/service"
4040
vdsupplements "github.com/deckhouse/virtualization-controller/pkg/controller/vd/internal/supplements"
@@ -137,41 +137,12 @@ func (s CreatePVCFromVDSnapshotStep) Take(ctx context.Context, vd *v1alpha2.Virt
137137
vd.Status.SourceUID = pointer.GetPointer(vdSnapshot.UID)
138138
vdsupplements.SetPVCName(vd, pvc.Name)
139139

140-
s.AddOriginalMetadata(vd, vs)
141-
return nil, nil
142-
}
143-
144-
// AddOriginalMetadata adds original annotations and labels from VolumeSnapshot to VirtualDisk,
145-
// without overwriting existing values
146-
func (s CreatePVCFromVDSnapshotStep) AddOriginalMetadata(vd *v1alpha2.VirtualDisk, vs *vsv1.VolumeSnapshot) {
147-
if vd.Annotations == nil {
148-
vd.Annotations = make(map[string]string)
149-
}
150-
if vd.Labels == nil {
151-
vd.Labels = make(map[string]string)
152-
}
153-
154-
if annotationsJSON := vs.Annotations[annotations.AnnVirtualDiskOriginalAnnotations]; annotationsJSON != "" {
155-
var originalAnnotations map[string]string
156-
if err := json.Unmarshal([]byte(annotationsJSON), &originalAnnotations); err == nil {
157-
for key, value := range originalAnnotations {
158-
if _, exists := vd.Annotations[key]; !exists {
159-
vd.Annotations[key] = value
160-
}
161-
}
162-
}
140+
err = commonvdsnapshot.AddOriginalMetadata(vd, vs)
141+
if err != nil {
142+
return nil, fmt.Errorf("failed to add original metadata: %w", err)
163143
}
164144

165-
if labelsJSON := vs.Annotations[annotations.AnnVirtualDiskOriginalLabels]; labelsJSON != "" {
166-
var originalLabels map[string]string
167-
if err := json.Unmarshal([]byte(labelsJSON), &originalLabels); err == nil {
168-
for key, value := range originalLabels {
169-
if _, exists := vd.Labels[key]; !exists {
170-
vd.Labels[key] = value
171-
}
172-
}
173-
}
174-
}
145+
return nil, nil
175146
}
176147

177148
func (s CreatePVCFromVDSnapshotStep) buildPVC(vd *v1alpha2.VirtualDisk, vs *vsv1.VolumeSnapshot) *corev1.PersistentVolumeClaim {

0 commit comments

Comments
 (0)