Skip to content

Commit 68dce91

Browse files
author
Valeriy Khorunzhin
committed
add
Signed-off-by: Valeriy Khorunzhin <valeriy.khorunzhin@flant.com>
1 parent d541cf8 commit 68dce91

6 files changed

Lines changed: 156 additions & 4 deletions

File tree

api/core/v1alpha2/virtual_machine_snapshot.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,15 @@ type VirtualMachineSnapshotSpec struct {
7272
VolumeSnapshotClasses []VolumeSnapshotClassName `json:"volumeSnapshotClasses,omitempty"`
7373
}
7474

75+
type ResourceRef struct {
76+
// Kind of resource
77+
Kind string `json:"kind,omitempty"`
78+
// Api version of resource
79+
ApiVersion string `json:"apiVersion,omitempty"`
80+
// Resource Name
81+
Name string `json:"name,omitempty"`
82+
}
83+
7584
type VirtualMachineSnapshotStatus struct {
7685
Phase VirtualMachineSnapshotPhase `json:"phase"`
7786
// Whether a virtual machine snapshot is consistent.
@@ -80,6 +89,8 @@ type VirtualMachineSnapshotStatus struct {
8089
VirtualMachineSnapshotSecretName string `json:"virtualMachineSnapshotSecretName,omitempty"`
8190
// List of VirtualDiskSnapshot names for the snapshots taken from the virtual disks of the associated virtual machine.
8291
VirtualDiskSnapshotNames []string `json:"virtualDiskSnapshotNames,omitempty"`
92+
// Snapshotted resource list
93+
Resources []ResourceRef `json:"resources,omitempty"`
8394
// The latest detailed observations of the VirtualMachineSnapshot resource.
8495
Conditions []metav1.Condition `json:"conditions,omitempty"`
8596
// Resource generation last processed by the controller.

api/core/v1alpha2/zz_generated.deepcopy.go

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/pkg/apiserver/api/generated/openapi/zz_generated.openapi.go

Lines changed: 49 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crds/virtualmachinesnapshots.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,21 @@ spec:
195195
- Failed
196196
- Terminating
197197
type: string
198+
resources:
199+
description: Snapshotted resource list
200+
items:
201+
properties:
202+
apiVersion:
203+
description: Api version of resource
204+
type: string
205+
kind:
206+
description: Kind of resource
207+
type: string
208+
name:
209+
description: Resource Name
210+
type: string
211+
type: object
212+
type: array
198213
virtualDiskSnapshotNames:
199214
description:
200215
List of VirtualDiskSnapshot names for the snapshots taken

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

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@ import (
2424

2525
corev1 "k8s.io/api/core/v1"
2626
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27+
"k8s.io/apimachinery/pkg/types"
2728
"k8s.io/utils/ptr"
29+
"sigs.k8s.io/controller-runtime/pkg/client"
2830
"sigs.k8s.io/controller-runtime/pkg/reconcile"
2931

32+
"github.com/deckhouse/virtualization-controller/pkg/common/object"
3033
"github.com/deckhouse/virtualization-controller/pkg/controller/conditions"
3134
"github.com/deckhouse/virtualization-controller/pkg/controller/service"
3235
"github.com/deckhouse/virtualization-controller/pkg/eventrecord"
@@ -42,13 +45,15 @@ type LifeCycleHandler struct {
4245
recorder eventrecord.EventRecorderLogger
4346
snapshotter Snapshotter
4447
storer Storer
48+
client client.Client
4549
}
4650

47-
func NewLifeCycleHandler(recorder eventrecord.EventRecorderLogger, snapshotter Snapshotter, storer Storer) *LifeCycleHandler {
51+
func NewLifeCycleHandler(recorder eventrecord.EventRecorderLogger, snapshotter Snapshotter, storer Storer, client client.Client) *LifeCycleHandler {
4852
return &LifeCycleHandler{
4953
recorder: recorder,
5054
snapshotter: snapshotter,
5155
storer: storer,
56+
client: client,
5257
}
5358
}
5459

@@ -353,7 +358,14 @@ func (h LifeCycleHandler) Handle(ctx context.Context, vmSnapshot *virtv2.Virtual
353358
return reconcile.Result{}, err
354359
}
355360

356-
// 9. Move to Ready phase.
361+
// 9. Fill status resources.
362+
err = h.fillStatusResources(ctx, vmSnapshot, vm)
363+
if err != nil {
364+
h.setPhaseConditionToFailed(cb, vmSnapshot, err)
365+
return reconcile.Result{}, err
366+
}
367+
368+
// 10. Move to Ready phase.
357369
log.Debug("The virtual disk snapshots are taken: the virtual machine snapshot is Ready now", "unfrozen", unfrozen)
358370

359371
vmSnapshot.Status.Phase = virtv2.VirtualMachineSnapshotPhaseReady
@@ -649,3 +661,48 @@ func getVDName(vdSnapshotName string, vmSnapshot *virtv2.VirtualMachineSnapshot)
649661
func getVDSnapshotName(vdName string, vmSnapshot *virtv2.VirtualMachineSnapshot) string {
650662
return fmt.Sprintf("%s-%s", vdName, vmSnapshot.UID)
651663
}
664+
665+
func (h LifeCycleHandler) fillStatusResources(ctx context.Context, vmSnapshot *virtv2.VirtualMachineSnapshot, vm *virtv2.VirtualMachine) error {
666+
vmSnapshot.Status.Resources = []virtv2.ResourceRef{}
667+
668+
vmSnapshot.Status.Resources = append(vmSnapshot.Status.Resources, virtv2.ResourceRef{
669+
Kind: virtv2.VirtualMachineKind,
670+
Name: vm.Name,
671+
})
672+
673+
for _, bdr := range vm.Status.BlockDeviceRefs {
674+
if bdr.Kind != virtv2.DiskDevice {
675+
continue
676+
}
677+
678+
vd, err := object.FetchObject(ctx, types.NamespacedName{Name: bdr.Name, Namespace: vm.Namespace}, h.client, &virtv2.VirtualDisk{})
679+
if err != nil {
680+
return err
681+
}
682+
if vd == nil {
683+
continue
684+
}
685+
vmSnapshot.Status.Resources = append(vmSnapshot.Status.Resources, virtv2.ResourceRef{
686+
Kind: vd.Kind,
687+
ApiVersion: vd.APIVersion,
688+
Name: vd.Name,
689+
})
690+
691+
if bdr.VirtualMachineBlockDeviceAttachmentName != "" {
692+
vmbda, err := object.FetchObject(ctx, types.NamespacedName{Name: bdr.VirtualMachineBlockDeviceAttachmentName, Namespace: vm.Namespace}, h.client, &virtv2.VirtualMachineBlockDeviceAttachment{})
693+
if err != nil {
694+
return err
695+
}
696+
if vmbda == nil {
697+
continue
698+
}
699+
vmSnapshot.Status.Resources = append(vmSnapshot.Status.Resources, virtv2.ResourceRef{
700+
Kind: vmbda.Kind,
701+
ApiVersion: vmbda.APIVersion,
702+
Name: vmbda.Name,
703+
})
704+
}
705+
}
706+
707+
return nil
708+
}

images/virtualization-artifact/pkg/controller/vmsnapshot/vmsnapshot_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func NewController(
5050
reconciler := NewReconciler(
5151
mgr.GetClient(),
5252
internal.NewVirtualMachineReadyHandler(snapshotter),
53-
internal.NewLifeCycleHandler(recorder, snapshotter, restorer.NewSecretRestorer(mgr.GetClient())),
53+
internal.NewLifeCycleHandler(recorder, snapshotter, restorer.NewSecretRestorer(mgr.GetClient()), mgr.GetClient()),
5454
)
5555

5656
vmSnapshotController, err := controller.New(ControllerName, mgr, controller.Options{

0 commit comments

Comments
 (0)