mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-04-30 04:34:27 +00:00
clh: Consolidate the code path for device unplug
In cloud-hypervisor, it provides a single unified way of unplugging devices, e.g. the `/vm.RemoveDevice` HTTP API. Taking advantage of this API, we can simplify our implementation of `hotplugRemoveDevice` in `clh.go`, where we can consolidate similar code paths for different device unplug (e.g. no need to implement `hotplugRemoveBlockDevice` and `hotplugRemoveVfioDevice` separately). We will only need to retrieve the right `deviceID` based on the type of devices, and use the single unified HTTP API for device unplug. Fixes: #1076 Signed-off-by: Bo Chen <chen.bo@intel.com>
This commit is contained in:
parent
43ec107d94
commit
93d7962510
@ -487,55 +487,35 @@ func (clh *cloudHypervisor) hotplugAddDevice(devInfo interface{}, devType device
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (clh *cloudHypervisor) hotplugRemoveBlockDevice(drive *config.BlockDrive) error {
|
|
||||||
cl := clh.client()
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), clhHotPlugAPITimeout*time.Second)
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
driveID := clhDriveIndexToID(drive.Index)
|
|
||||||
|
|
||||||
if drive.Pmem {
|
|
||||||
return fmt.Errorf("pmem device hotplug remove not supported")
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err := cl.VmRemoveDevicePut(ctx, chclient.VmRemoveDevice{Id: driveID})
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
err = fmt.Errorf("failed to hotplug remove block device %+v %s", drive, openAPIClientError(err))
|
|
||||||
}
|
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (clh *cloudHypervisor) hotplugRemoveVfioDevice(device *config.VFIODev) error {
|
|
||||||
cl := clh.client()
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), clhHotPlugAPITimeout*time.Second)
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
_, err := cl.VmRemoveDevicePut(ctx, chclient.VmRemoveDevice{Id: device.ID})
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
err = fmt.Errorf("failed to hotplug remove vfio device %+v %s", device, openAPIClientError(err))
|
|
||||||
}
|
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (clh *cloudHypervisor) hotplugRemoveDevice(devInfo interface{}, devType deviceType) (interface{}, error) {
|
func (clh *cloudHypervisor) hotplugRemoveDevice(devInfo interface{}, devType deviceType) (interface{}, error) {
|
||||||
span, _ := clh.trace("hotplugRemoveDevice")
|
span, _ := clh.trace("hotplugRemoveDevice")
|
||||||
defer span.Finish()
|
defer span.Finish()
|
||||||
|
|
||||||
|
var deviceID string
|
||||||
|
|
||||||
switch devType {
|
switch devType {
|
||||||
case blockDev:
|
case blockDev:
|
||||||
return nil, clh.hotplugRemoveBlockDevice(devInfo.(*config.BlockDrive))
|
deviceID = clhDriveIndexToID(devInfo.(*config.BlockDrive).Index)
|
||||||
case vfioDev:
|
case vfioDev:
|
||||||
return nil, clh.hotplugRemoveVfioDevice(devInfo.(*config.VFIODev))
|
deviceID = devInfo.(*config.VFIODev).ID
|
||||||
default:
|
default:
|
||||||
clh.Logger().WithFields(log.Fields{"devInfo": devInfo,
|
clh.Logger().WithFields(log.Fields{"devInfo": devInfo,
|
||||||
"deviceType": devType}).Error("hotplugRemoveDevice: unsupported device")
|
"deviceType": devType}).Error("hotplugRemoveDevice: unsupported device")
|
||||||
return nil, fmt.Errorf("Could not hot remove device: unsupported device: %v, type: %v",
|
return nil, fmt.Errorf("Could not hot remove device: unsupported device: %v, type: %v",
|
||||||
devInfo, devType)
|
devInfo, devType)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cl := clh.client()
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), clhHotPlugAPITimeout*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
_, err := cl.VmRemoveDevicePut(ctx, chclient.VmRemoveDevice{Id: deviceID})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
err = fmt.Errorf("failed to hotplug remove (unplug) device %+v: %s", devInfo, openAPIClientError(err))
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (clh *cloudHypervisor) hypervisorConfig() HypervisorConfig {
|
func (clh *cloudHypervisor) hypervisorConfig() HypervisorConfig {
|
||||||
|
@ -390,7 +390,7 @@ func TestCloudHypervisorHotplugAddBlockDevice(t *testing.T) {
|
|||||||
assert.Error(err, "Hotplug block device not using 'virtio-blk' expected error")
|
assert.Error(err, "Hotplug block device not using 'virtio-blk' expected error")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCloudHypervisorHotplugRemoveBlockDevice(t *testing.T) {
|
func TestCloudHypervisorHotplugRemoveDevice(t *testing.T) {
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
|
|
||||||
clhConfig, err := newClhConfig()
|
clhConfig, err := newClhConfig()
|
||||||
@ -400,10 +400,12 @@ func TestCloudHypervisorHotplugRemoveBlockDevice(t *testing.T) {
|
|||||||
clh.config = clhConfig
|
clh.config = clhConfig
|
||||||
clh.APIClient = &clhClientMock{}
|
clh.APIClient = &clhClientMock{}
|
||||||
|
|
||||||
clh.config.BlockDeviceDriver = config.VirtioBlock
|
_, err = clh.hotplugRemoveDevice(&config.BlockDrive{}, blockDev)
|
||||||
err = clh.hotplugRemoveBlockDevice(&config.BlockDrive{Pmem: false})
|
assert.NoError(err, "Hotplug remove block device expected no error")
|
||||||
assert.NoError(err, "Hotplug remove disk block device expected no error")
|
|
||||||
|
|
||||||
err = clh.hotplugRemoveBlockDevice(&config.BlockDrive{Pmem: true})
|
_, err = clh.hotplugRemoveDevice(&config.VFIODev{}, vfioDev)
|
||||||
|
assert.NoError(err, "Hotplug remove vfio block device expected no error")
|
||||||
|
|
||||||
|
_, err = clh.hotplugRemoveDevice(nil, netDev)
|
||||||
assert.Error(err, "Hotplug remove pmem block device expected error")
|
assert.Error(err, "Hotplug remove pmem block device expected error")
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user