Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions pkg/util/kube/vmsnapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,18 @@ func CalculateReservedForSnapshotPerStorageClass(

// Add the VM's memory to the total reserved capacity.
if vmSnapshot.Spec.Memory {
// TODO(lubron): Fetch the requested memory size from the VM.status.hardware.memory.reservation.
vmClass := &vmopv1.VirtualMachineClass{}
if err := k8sClient.Get(ctx, ctrlclient.ObjectKey{Namespace: vmSnapshot.Namespace, Name: vm.Spec.ClassName}, vmClass); err != nil {
return nil, fmt.Errorf("failed to get VMClass %s: %w", vm.Spec.ClassName, err)
if vm.Status.Hardware == nil || vm.Status.Hardware.Memory == nil {
return nil, fmt.Errorf("failed to calculate reserved storage capacity for snapshot since VM does not have memory usage set")
}

if _, ok := requestedMap[vm.Spec.StorageClass]; !ok {
requestedMap[vm.Spec.StorageClass] = resource.NewQuantity(0, resource.BinarySI)
}

logger.V(4).Info("adding memory size to the total reserved capacity", "memory", vmClass.Spec.Hardware.Memory, "storageClass", vm.Spec.StorageClass)
requestedMap[vm.Spec.StorageClass].Add(vmClass.Spec.Hardware.Memory)
logger.V(4).Info("adding VM's memory size to the total reserved capacity",
"memory", vm.Status.Hardware.Memory.Total,
"storageClass", vm.Spec.StorageClass)
requestedMap[vm.Spec.StorageClass].Add(*vm.Status.Hardware.Memory.Total)
}

volumePVCMap := make(map[string]*vmopv1.PersistentVolumeClaimVolumeSource)
Expand Down
22 changes: 9 additions & 13 deletions pkg/util/kube/vmsnapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,18 @@ var _ = Describe("CalculateReservedForSnapshot", func() {
}

vmClass = builder.DummyVirtualMachineClass("vm-class")
vmClass.Spec.Hardware.Memory = size10GB
vmClass.Namespace = namespace
vm = builder.DummyBasicVirtualMachine("dummy-vm", namespace)
vm.Spec.ClassName = vmClass.Name

vm.Status = vmopv1.VirtualMachineStatus{
Hardware: &vmopv1.VirtualMachineHardwareStatus{
Memory: &vmopv1.VirtualMachineMemoryAllocationStatus{
Total: &size10GB,
},
},
}

vmSnapshot = builder.DummyVirtualMachineSnapshot(namespace, "my-snapshot", vm.Name)
storageClass = builder.DummyStorageClass()
vm.Spec.StorageClass = storageClass.Name
Expand Down Expand Up @@ -159,18 +167,6 @@ var _ = Describe("CalculateReservedForSnapshot", func() {
})
})

When("VMClass is not found", func() {
BeforeEach(func() {
vmSnapshot.Spec.Memory = true
vm.Status.PowerState = vmopv1.VirtualMachinePowerStateOn
vm.Spec.ClassName = "unknown-vm-class"
})
It("should return error", func() {
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("failed to get VMClass"))
})
})

When("There is a VM with a storage class, also a FCD with same storage class", func() {
var pvc1 *corev1.PersistentVolumeClaim
BeforeEach(func() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2080,17 +2080,30 @@ func testVMSnapshotRequestedCapacityHandlerHandleCreate() {
vmSnapshot.Spec.Memory = true
})

When("virtual machine class is not found", func() {
When("VM does not have status.hardware set", func() {
BeforeEach(func() {
vm.Spec.ClassName = "non-existent-vm-class"
withObjects = []ctrlclient.Object{vm}
})

It("should write StatusInternalServerError code to the response object", func() {
Expect(resp.Allowed).To(BeFalse())
Expect(int(resp.Result.Code)).To(Equal(http.StatusInternalServerError))
Expect(resp.RequestedCapacities).To(HaveLen(0))
Expect(resp.Result.Message).To(ContainSubstring("failed to calculate reserved storage capacity for snapshot since VM does not have memory usage set"))
})
})

When("VM does not have status.hardware.memory set", func() {
BeforeEach(func() {
vm.Status.Hardware = &vmopv1.VirtualMachineHardwareStatus{}
withObjects = []ctrlclient.Object{vmSnapshot, vm}
})

It("should write StatusInternalServerError code to the response object", func() {
Expect(resp.Allowed).To(BeFalse())
Expect(int(resp.Result.Code)).To(Equal(http.StatusInternalServerError))
Expect(resp.RequestedCapacities).To(HaveLen(0))
Expect(resp.Result.Message).To(ContainSubstring("failed to get VMClass"))
Expect(resp.Result.Message).To(ContainSubstring("failed to calculate reserved storage capacity for snapshot since VM does not have memory usage set"))
})
})
})
Expand Down
Loading