@@ -26,6 +26,8 @@ import (
2626 "libvirt.org/go/libvirtxml"
2727)
2828
29+ var ErrNotAssignedDomain = errors .New ("not assigned to domain" )
30+
2931func (r * MachineReconciler ) deleteVolumes (ctx context.Context , log logr.Logger , machine * api.Machine ) error {
3032 mounter := r .machineVolumeMounter (machine )
3133 var errs []error
@@ -66,20 +68,38 @@ func (r *MachineReconciler) machineVolumeMounter(machine *api.Machine) VolumeMou
6668 }
6769}
6870
69- func getVolumeStatus (machine * api.Machine , volumeID string ) * api.VolumeStatus {
70- for _ , volumeStatus := range machine .Status .VolumeStatus {
71- if volumeID == volumeStatus .Handle {
72- return & volumeStatus
71+ func (r * MachineReconciler ) getLastVolumeSize (machineID , deviceName string ) (int64 , error ) {
72+ domain , err := r .host .Libvirt ().DomainLookupByUUID (libvirtutils .UUIDStringToBytes (machineID ))
73+ if err != nil {
74+ if ! libvirt .IsNotFound (err ) {
75+ return 0 , fmt .Errorf ("error getting blockInfo from domain for machine %s device %s: %w" , machineID , deviceName , err )
7376 }
77+ return 0 , nil
7478 }
75- return nil
79+
80+ virtDeviceName := computeVirtioDiskTargetDeviceName (deviceName )
81+ _ , capacity , _ , err := r .host .Libvirt ().DomainGetBlockInfo (domain , virtDeviceName , 0 )
82+
83+ if err != nil {
84+ if IsNotAssignedDomainErr (err ) {
85+ return 0 , nil
86+ }
87+ return 0 , fmt .Errorf ("error getting blockInfo from domain for machine %s device %s: %w" , machineID , deviceName , err )
88+ }
89+
90+ return int64 (capacity ), nil
7691}
7792
78- func getLastVolumeSize (machine * api.Machine , volumeID string ) int64 {
79- if status := getVolumeStatus (machine , volumeID ); status != nil && status .Size != 0 {
80- return status .Size
93+ func IsNotAssignedDomainErr (err error ) bool {
94+ var lvErr libvirt.Error
95+ if ! errors .As (err , & lvErr ) {
96+ return false
8197 }
82- return 0
98+ if lvErr .Code == uint32 (libvirt .ErrInvalidArg ) && strings .Contains (err .Error (), ErrNotAssignedDomain .Error ()) {
99+ return true
100+ }
101+
102+ return false
83103}
84104
85105func (r * MachineReconciler ) attachDetachVolumes (ctx context.Context , log logr.Logger , machine * api.Machine , attacher VolumeAttacher ) ([]api.VolumeStatus , error ) {
@@ -635,6 +655,18 @@ func (r *MachineReconciler) applyVolume(
635655 return "" , 0 , fmt .Errorf ("error applying volume mount: %w" , err )
636656 }
637657
658+ lastVolumeSize , err := r .getLastVolumeSize (machine .ID , desiredVolume .Device )
659+ if err != nil {
660+ return volumeID , 0 , fmt .Errorf ("error getting last volume size: %w" , err )
661+ }
662+
663+ var volumeSize int64
664+ if providerVolume != nil {
665+ volumeSize = providerVolume .EffectiveStorageBytesSize
666+ } else {
667+ volumeSize = lastVolumeSize
668+ }
669+
638670 log .V (1 ).Info ("Ensuring volume is attached" )
639671 if err := attacher .AttachVolume (& AttachVolume {
640672 Name : desiredVolume .Name ,
@@ -645,8 +677,8 @@ func (r *MachineReconciler) applyVolume(
645677 }
646678
647679 //TODO do epsilon comparison
648- if lastVolumeSize := getLastVolumeSize ( machine , volumeID ); lastVolumeSize != 0 && providerVolume . EffectiveStorageBytesSize != lastVolumeSize {
649- log .V (1 ).Info ("Resize volume" , "volumeID" , volumeID , "lastSize" , lastVolumeSize , "volumeSize" , providerVolume . EffectiveStorageBytesSize )
680+ if lastVolumeSize != 0 && volumeSize != lastVolumeSize {
681+ log .V (1 ).Info ("Resize volume" , "volumeID" , volumeID , "lastSize" , lastVolumeSize , "volumeSize" , volumeSize )
650682 if err := attacher .ResizeVolume (& AttachVolume {
651683 Name : desiredVolume .Name ,
652684 Device : desiredVolume .Device ,
0 commit comments