Skip to content

Commit 07bdc15

Browse files
committed
refactor volume resize related changes
1 parent 2b74d90 commit 07bdc15

7 files changed

Lines changed: 61 additions & 30 deletions

File tree

api/machine.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,12 @@ type EmptyDiskSpec struct {
8484
}
8585

8686
type VolumeConnection struct {
87-
Driver string ` json:"driver,omitempty"`
88-
Handle string ` json:"handle,omitempty"`
89-
Attributes map[string]string ` json:"attributes,omitempty"`
90-
SecretData map[string][]byte ` json:"secret_data,omitempty"`
91-
EncryptionData map[string][]byte ` json:"encryption_data,omitempty"`
92-
EffectiveStorageBytes int64 ` json:"effective_storage_bytes,omitempty"`
87+
Driver string `json:"driver,omitempty"`
88+
Handle string `json:"handle,omitempty"`
89+
Attributes map[string]string `json:"attributes,omitempty"`
90+
SecretData map[string][]byte `json:"secret_data,omitempty"`
91+
EncryptionData map[string][]byte `json:"encryption_data,omitempty"`
92+
EffectiveStorageBytes int64 `json:"effective_storage_bytes,omitempty"`
9393
}
9494

9595
type VolumeState string

internal/controllers/machine_controller_volumes.go

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import (
2626
"libvirt.org/go/libvirtxml"
2727
)
2828

29+
var ErrNotAssignedDomain = errors.New("not assigned to domain")
30+
2931
func (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

85105
func (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,

internal/plugins/volume/ceph/ceph.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,9 @@ func (p *plugin) getVolumeData(spec *api.VolumeSpec) (vData *volumeData, err err
174174
if connection.Handle == "" {
175175
return nil, fmt.Errorf("volume connection does not specify handle")
176176
}
177+
if connection.EffectiveStorageBytes <= 0 {
178+
return nil, fmt.Errorf("volume connection does not specify effective storage bytes")
179+
}
177180
vData.handle = connection.Handle
178181

179182
vData.monitors, vData.image, err = readVolumeAttributes(connection.Attributes)
@@ -193,9 +196,8 @@ func (p *plugin) getVolumeData(spec *api.VolumeSpec) (vData *volumeData, err err
193196
}
194197
}
195198

196-
if effectiveStorageBytes := spec.Connection.EffectiveStorageBytes; effectiveStorageBytes != 0 {
197-
vData.effectiveStorageBytes = effectiveStorageBytes
198-
}
199+
vData.effectiveStorageBytes = spec.Connection.EffectiveStorageBytes
200+
199201
return vData, nil
200202
}
201203

internal/server/machine_volume_attach_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
libvirtutils "github.com/ironcore-dev/libvirt-provider/internal/libvirt/utils"
1313
. "github.com/onsi/ginkgo/v2"
1414
. "github.com/onsi/gomega"
15+
"k8s.io/apimachinery/pkg/api/resource"
1516
"libvirt.org/go/libvirtxml"
1617
)
1718

@@ -115,6 +116,7 @@ var _ = Describe("AttachVolume", func() {
115116
"userID": []byte(cephUsername),
116117
"userKey": []byte(cephUserkey),
117118
},
119+
EffectiveStorageBytes: resource.NewQuantity(1*1024*1024*1024, resource.BinarySI).Value(),
118120
},
119121
},
120122
})

internal/server/machine_volume_detach_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
libvirtutils "github.com/ironcore-dev/libvirt-provider/internal/libvirt/utils"
1313
. "github.com/onsi/ginkgo/v2"
1414
. "github.com/onsi/gomega"
15+
"k8s.io/apimachinery/pkg/api/resource"
1516
"libvirt.org/go/libvirtxml"
1617
)
1718

@@ -60,6 +61,7 @@ var _ = Describe("DetachVolume", func() {
6061
"userID": []byte(cephUsername),
6162
"userKey": []byte(cephUserkey),
6263
},
64+
EffectiveStorageBytes: resource.NewQuantity(1*1024*1024*1024, resource.BinarySI).Value(),
6365
},
6466
},
6567
},

internal/server/machine_volume_update.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,19 @@ func (s *Server) UpdateVolume(ctx context.Context, req *iri.UpdateVolumeRequest)
2020
return nil, convertInternalErrorToGRPC(ErrInvalidRequest)
2121
}
2222

23-
iriVolume := req.Volume
2423
apiMachine, err := s.machineStore.Get(ctx, req.MachineId)
2524
if err != nil {
2625
return nil, convertInternalErrorToGRPC(fmt.Errorf("failed to get machine '%s': %w", req.MachineId, err))
2726
}
2827

2928
apiVolumeIndex := apiMachineVolumeIndex(apiMachine, req.Volume.Name)
30-
if apiVolumeIndex < 0 {
29+
if apiVolumeIndex == -1 {
3130
return nil, convertInternalErrorToGRPC(fmt.Errorf("volume '%s' not found in machine '%s': %w", req.Volume.Name, req.MachineId, ErrVolumeNotFound))
3231
}
3332

3433
apiBaseVolume := apiMachine.Spec.Volumes[apiVolumeIndex]
35-
apiBaseVolume.Device = iriVolume.Device
36-
if volumeConnection := iriVolume.Connection; volumeConnection != nil {
34+
apiBaseVolume.Device = req.Volume.Device
35+
if volumeConnection := req.Volume.Connection; volumeConnection != nil {
3736
apiBaseVolume.Connection = &api.VolumeConnection{
3837
Driver: volumeConnection.Driver,
3938
Handle: volumeConnection.Handle,

internal/server/machine_volume_update_test.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,6 @@ var _ = Describe("UpdateVolume", func() {
119119
}).Should(Equal(2))
120120
Expect(disks[0].Serial).To(HavePrefix("oda"))
121121

122-
// wait to complete machine reconciliation
123-
time.Sleep(20 * time.Second)
124-
125122
By("updating machine volume")
126123
updateVolumeResp, err := machineClient.UpdateVolume(ctx, &iri.UpdateVolumeRequest{
127124
MachineId: createResp.Machine.Metadata.Id,
@@ -146,9 +143,6 @@ var _ = Describe("UpdateVolume", func() {
146143
Expect(err).NotTo(HaveOccurred())
147144
Expect(updateVolumeResp).NotTo(BeNil())
148145

149-
// wait to complete machine reconciliation
150-
time.Sleep(20 * time.Second)
151-
152146
By("ensuring volume has been resized and updated in machine spec field")
153147
Eventually(func(g Gomega) *iri.Volume {
154148
listResp, err := machineClient.ListMachines(ctx, &iri.ListMachinesRequest{

0 commit comments

Comments
 (0)