Skip to content

Commit

Permalink
Check for associated memory dump before creating new pvc
Browse files Browse the repository at this point in the history
Signed-off-by: Shelly Kagan <[email protected]>
  • Loading branch information
ShellyKa13 committed Jul 14, 2022
1 parent e95cd11 commit 871629d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
17 changes: 16 additions & 1 deletion pkg/virtctl/vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,17 @@ func createPVCforMemoryDump(namespace, vmName, claimName string, virtClient kube
return nil
}

func checkNoAssociatedMemoryDump(namespace, vmName string, virtClient kubecli.KubevirtClient) error {
vm, err := virtClient.VirtualMachine(namespace).Get(vmName, &metav1.GetOptions{})
if err != nil {
return err
}
if vm.Status.MemoryDumpRequest != nil {
return fmt.Errorf("please remove current memory dump association before creating a new claim for a new memory dump")
}
return nil
}

func memoryDump(args []string, claimName, namespace string, virtClient kubecli.KubevirtClient) error {
vmName := args[1]
switch args[0] {
Expand All @@ -495,7 +506,11 @@ func memoryDump(args []string, claimName, namespace string, virtClient kubecli.K
if claimName == "" {
return fmt.Errorf("missing claim name")
}
err := createPVCforMemoryDump(namespace, vmName, claimName, virtClient)
err := checkNoAssociatedMemoryDump(namespace, vmName, virtClient)
if err != nil {
return err
}
err = createPVCforMemoryDump(namespace, vmName, claimName, virtClient)
if err != nil {
return err
}
Expand Down
33 changes: 33 additions & 0 deletions pkg/virtctl/vm/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,26 @@ var _ = Describe("VirtualMachine", func() {
})
}

expectGetVM := func(withAssociatedMemoryDump bool) {
vm := &v1.VirtualMachine{
Spec: v1.VirtualMachineSpec{},
Status: v1.VirtualMachineStatus{},
}
if withAssociatedMemoryDump {
vm.Status.MemoryDumpRequest = &v1.VirtualMachineMemoryDumpRequest{}
}
kubecli.MockKubevirtClientInstance.EXPECT().VirtualMachine(k8smetav1.NamespaceDefault).Return(vmInterface).Times(1)
vmInterface.EXPECT().Get(vmName, gomock.Any()).Return(vm, nil).Times(1)
}

expectGetVMNoAssociatedMemoryDump := func() {
expectGetVM(false)
}

expectGetVMWithAssociatedMemoryDump := func() {
expectGetVM(true)
}

expectGetVMI := func() {
quantity, _ := resource.ParseQuantity("256Mi")
vmi := &v1.VirtualMachineInstance{
Expand Down Expand Up @@ -717,7 +737,17 @@ var _ = Describe("VirtualMachine", func() {
Expect(res.Error()).To(ContainSubstring("missing claim name"))
})

It("should fail call memory dump subresource with create-claim with already associated memory dump pvc", func() {
expectGetVMWithAssociatedMemoryDump()
commandAndArgs := []string{"memory-dump", "get", "testvm", claimNameFlag, createClaimFlag}
cmd := clientcmd.NewVirtctlCommand(commandAndArgs...)
res := cmd.Execute()
Expect(res).NotTo(BeNil())
Expect(res.Error()).To(ContainSubstring("please remove current memory dump"))
})

It("should fail call memory dump subresource with create-claim and existing pvc", func() {
expectGetVMNoAssociatedMemoryDump()
expectPVCGet(pvcSpec())
commandAndArgs := []string{"memory-dump", "get", "testvm", claimNameFlag, createClaimFlag}
cmd := clientcmd.NewVirtctlCommand(commandAndArgs...)
Expand All @@ -727,6 +757,7 @@ var _ = Describe("VirtualMachine", func() {
})

It("should fail call memory dump subresource with create-claim no vmi", func() {
expectGetVMNoAssociatedMemoryDump()
kubecli.MockKubevirtClientInstance.EXPECT().VirtualMachineInstance(k8smetav1.NamespaceDefault).Return(vmiInterface).Times(1)
vmiInterface.EXPECT().Get(vmName, &k8smetav1.GetOptions{}).Return(nil, errors.NewNotFound(v1.Resource("virtualmachineinstance"), vmName))
commandAndArgs := []string{"memory-dump", "get", "testvm", claimNameFlag, createClaimFlag}
Expand All @@ -737,6 +768,7 @@ var _ = Describe("VirtualMachine", func() {
})

It("should fail call memory dump subresource with readonly access mode", func() {
expectGetVMNoAssociatedMemoryDump()
expectGetVMI()
commandAndArgs := []string{"memory-dump", "get", "testvm", claimNameFlag, createClaimFlag, "--access-mode=ReadOnlyMany"}
cmd := clientcmd.NewVirtctlCommand(commandAndArgs...)
Expand All @@ -746,6 +778,7 @@ var _ = Describe("VirtualMachine", func() {
})

DescribeTable("should create pvc for memory dump and call subresource", func(storageclass, accessMode string) {
expectGetVMNoAssociatedMemoryDump()
expectGetVMI()
expectPVCCreate(claimName, storageclass, accessMode)
expectVMEndpointMemoryDump("testvm", claimName)
Expand Down

0 comments on commit 871629d

Please sign in to comment.