Skip to content
This repository was archived by the owner on May 12, 2021. It is now read-only.

Commit 44b58e4

Browse files
committed
clh: Add support to unplug block devices
This patch enables kata+clh to unplug block devices, which is required to pass cri-o integration tests. Fixes: #2832 Signed-off-by: Bo Chen <[email protected]>
1 parent 03fb9c5 commit 44b58e4

File tree

2 files changed

+61
-6
lines changed

2 files changed

+61
-6
lines changed

virtcontainers/clh.go

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ type clhClient interface {
8989
VmAddDevicePut(ctx context.Context, vmAddDevice chclient.VmAddDevice) (chclient.PciDeviceInfo, *http.Response, error)
9090
// Add a new disk device to the VM
9191
VmAddDiskPut(ctx context.Context, diskConfig chclient.DiskConfig) (chclient.PciDeviceInfo, *http.Response, error)
92+
// Remove a device from the VM
93+
VmRemoveDevicePut(ctx context.Context, vmRemoveDevice chclient.VmRemoveDevice) (*http.Response, error)
9294
}
9395

9496
type CloudHypervisorVersion struct {
@@ -478,9 +480,39 @@ func (clh *cloudHypervisor) hotplugAddDevice(devInfo interface{}, devType device
478480

479481
}
480482

483+
func (clh *cloudHypervisor) hotplugRemoveBlockDevice(drive *config.BlockDrive) error {
484+
cl := clh.client()
485+
ctx, cancel := context.WithTimeout(context.Background(), clhHotPlugAPITimeout*time.Second)
486+
defer cancel()
487+
488+
driveID := clhDriveIndexToID(drive.Index)
489+
490+
if drive.Pmem {
491+
return fmt.Errorf("pmem device hotplug remove not supported")
492+
}
493+
494+
_, err := cl.VmRemoveDevicePut(ctx, chclient.VmRemoveDevice{Id: driveID})
495+
496+
if err != nil {
497+
err = fmt.Errorf("failed to hotplug remove block device %+v %s", drive, openAPIClientError(err))
498+
}
499+
500+
return err
501+
}
502+
481503
func (clh *cloudHypervisor) hotplugRemoveDevice(devInfo interface{}, devType deviceType) (interface{}, error) {
482-
clh.Logger().WithField("function", "hotplugRemoveDevice").Warn("hotplug remove device not supported")
483-
return nil, nil
504+
span, _ := clh.trace("hotplugRemoveDevice")
505+
defer span.Finish()
506+
507+
switch devType {
508+
case blockDev:
509+
return nil, clh.hotplugRemoveBlockDevice(devInfo.(*config.BlockDrive))
510+
default:
511+
clh.Logger().WithFields(log.Fields{"devInfo": devInfo,
512+
"deviceType": devType}).Error("hotplugRemoveDevice: unsupported device")
513+
return nil, fmt.Errorf("Could not hot remove device: unsupported device: %v, type: %v",
514+
devInfo, devType)
515+
}
484516
}
485517

486518
func (clh *cloudHypervisor) hypervisorConfig() HypervisorConfig {

virtcontainers/clh_test.go

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ func (c *clhClientMock) VmAddDiskPut(ctx context.Context, diskConfig chclient.Di
104104
return chclient.PciDeviceInfo{}, nil, nil
105105
}
106106

107+
//nolint:golint
108+
func (c *clhClientMock) VmRemoveDevicePut(ctx context.Context, vmRemoveDevice chclient.VmRemoveDevice) (*http.Response, error) {
109+
return nil, nil
110+
}
111+
107112
func TestCloudHypervisorAddVSock(t *testing.T) {
108113
assert := assert.New(t)
109114
clh := cloudHypervisor{}
@@ -363,7 +368,7 @@ func TestCheckVersion(t *testing.T) {
363368
}
364369
}
365370

366-
func TestCloudHypervisorHotplugBlockDevice(t *testing.T) {
371+
func TestCloudHypervisorHotplugAddBlockDevice(t *testing.T) {
367372
assert := assert.New(t)
368373

369374
clhConfig, err := newClhConfig()
@@ -374,13 +379,31 @@ func TestCloudHypervisorHotplugBlockDevice(t *testing.T) {
374379
clh.APIClient = &clhClientMock{}
375380

376381
clh.config.BlockDeviceDriver = config.VirtioBlock
377-
err = clh.hotplugBlockDevice(&config.BlockDrive{Pmem: false})
382+
err = clh.hotplugAddBlockDevice(&config.BlockDrive{Pmem: false})
378383
assert.NoError(err, "Hotplug disk block device expected no error")
379384

380-
err = clh.hotplugBlockDevice(&config.BlockDrive{Pmem: true})
385+
err = clh.hotplugAddBlockDevice(&config.BlockDrive{Pmem: true})
381386
assert.Error(err, "Hotplug pmem block device expected error")
382387

383388
clh.config.BlockDeviceDriver = config.VirtioSCSI
384-
err = clh.hotplugBlockDevice(&config.BlockDrive{Pmem: false})
389+
err = clh.hotplugAddBlockDevice(&config.BlockDrive{Pmem: false})
385390
assert.Error(err, "Hotplug block device not using 'virtio-blk' expected error")
386391
}
392+
393+
func TestCloudHypervisorHotplugRemoveBlockDevice(t *testing.T) {
394+
assert := assert.New(t)
395+
396+
clhConfig, err := newClhConfig()
397+
assert.NoError(err)
398+
399+
clh := &cloudHypervisor{}
400+
clh.config = clhConfig
401+
clh.APIClient = &clhClientMock{}
402+
403+
clh.config.BlockDeviceDriver = config.VirtioBlock
404+
err = clh.hotplugRemoveBlockDevice(&config.BlockDrive{Pmem: false})
405+
assert.NoError(err, "Hotplug remove disk block device expected no error")
406+
407+
err = clh.hotplugRemoveBlockDevice(&config.BlockDrive{Pmem: true})
408+
assert.Error(err, "Hotplug remove pmem block device expected error")
409+
}

0 commit comments

Comments
 (0)