Skip to content

Commit

Permalink
Ensure volume is removed before returning success (#90)
Browse files Browse the repository at this point in the history
When removing a volume from a VM, ensure that the volume is
actually removed from the VM before returning nil from the
ControllerUnpublishVolume function.

Added EnsureVolumeRemoved function to virtClient

Signed-off-by: Alexander Wels <awels@redhat.com>
  • Loading branch information
awels authored Jul 6, 2023
1 parent 6d78160 commit fa92820
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
18 changes: 18 additions & 0 deletions pkg/kubevirt/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Client interface {
AddVolumeToVM(namespace string, vmName string, hotPlugRequest *kubevirtv1.AddVolumeOptions) error
RemoveVolumeFromVM(namespace string, vmName string, hotPlugRequest *kubevirtv1.RemoveVolumeOptions) error
EnsureVolumeAvailable(namespace, vmName, volumeName string, timeout time.Duration) error
EnsureVolumeRemoved(namespace, vmName, volumeName string, timeout time.Duration) error
}

type client struct {
Expand Down Expand Up @@ -81,6 +82,23 @@ func (c *client) EnsureVolumeAvailable(namespace, vmName, volumeName string, tim
})
}

// EnsureVolumeAvailable checks to make sure the volume is available in the node before returning, checks for 2 minutes
func (c *client) EnsureVolumeRemoved(namespace, vmName, volumeName string, timeout time.Duration) error {
return wait.PollImmediate(time.Second, timeout, func() (done bool, err error) {
vmi, err := c.virtClient.VirtualMachineInstance(namespace).Get(context.TODO(), vmName, &metav1.GetOptions{})
if err != nil {
return false, err
}
for _, volume := range vmi.Status.VolumeStatus {
if volume.Name == volumeName {
return false, nil
}
}
// Have not found the hotplugged volume
return true, nil
})
}

// ListVirtualMachines fetches a list of VMIs from the passed in namespace
func (c *client) ListVirtualMachines(namespace string) ([]kubevirtv1.VirtualMachineInstance, error) {
list, err := c.virtClient.VirtualMachineInstance(namespace).List(context.TODO(), &metav1.ListOptions{})
Expand Down
6 changes: 6 additions & 0 deletions pkg/service/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,12 @@ func (c *ControllerService) ControllerUnpublishVolume(ctx context.Context, req *
return nil, err
}
}
err = c.virtClient.EnsureVolumeRemoved(c.infraClusterNamespace, vmName, dvName, time.Minute*2)
if err != nil {
klog.Errorf("volume %s failed to be removed in time from VM %s, %v", dvName, vmName, err)
return nil, err
}

return &csi.ControllerUnpublishVolumeResponse{}, nil
}

Expand Down
4 changes: 4 additions & 0 deletions pkg/service/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,3 +363,7 @@ func (c *ControllerClientMock) RemoveVolumeFromVM(namespace string, vmName strin
func (c *ControllerClientMock) EnsureVolumeAvailable(namespace, vmName, volumeName string, timeout time.Duration) error {
return nil
}

func (c *ControllerClientMock) EnsureVolumeRemoved(namespace, vmName, volumeName string, timeout time.Duration) error {
return nil
}
6 changes: 5 additions & 1 deletion sanity/sanity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
Expand Down Expand Up @@ -166,6 +166,10 @@ func (k *fakeKubeVirtClient) EnsureVolumeAvailable(namespace, vmName, volumeName
return nil
}

func (k *fakeKubeVirtClient) EnsureVolumeRemoved(namespace, vmName, volumeName string, timeout time.Duration) error {
return nil
}

func (k *fakeKubeVirtClient) List() ([]byte, error) {
ds := make([]device, 0)
for _, value := range k.hotpluggedMap {
Expand Down

0 comments on commit fa92820

Please sign in to comment.