virtcontainers: kata_agent: Factorize appending devices

This commit factorizes the code appending devices to the device list
provided to the Kata agent, in order to reduce the complexity of the
function createContainer().

Fixes #56

Signed-off-by: Sebastien Boeuf <sebastien.boeuf@intel.com>
This commit is contained in:
Sebastien Boeuf
2018-03-09 10:22:55 -08:00
parent c2e9801a97
commit 8152e15a61
2 changed files with 65 additions and 17 deletions

View File

@@ -579,6 +579,25 @@ func constraintGRPCSpec(grpcSpec *grpc.Spec) {
}
}
func (k *kataAgent) appendDevices(deviceList []*grpc.Device, devices []Device) []*grpc.Device {
for _, device := range devices {
d, ok := device.(*BlockDevice)
if !ok {
continue
}
kataDevice := &grpc.Device{
Type: kataBlkDevType,
VmPath: d.VirtPath,
ContainerPath: d.DeviceInfo.ContainerPath,
}
deviceList = append(deviceList, kataDevice)
}
return deviceList
}
func (k *kataAgent) createContainer(pod *Pod, c *Container) (*Process, error) {
ociSpecJSON, ok := c.config.Annotations[vcAnnotations.ConfigJSONKey]
if !ok {
@@ -673,22 +692,8 @@ func (k *kataAgent) createContainer(pod *Pod, c *Container) (*Process, error) {
// irrelevant information to the agent.
constraintGRPCSpec(grpcSpec)
// Append container mounts for block devices passed with --device.
for _, device := range c.devices {
d, ok := device.(*BlockDevice)
if !ok {
continue
}
kataDevice := &grpc.Device{
Type: kataBlkDevType,
VmPath: d.VirtPath,
ContainerPath: d.DeviceInfo.ContainerPath,
}
ctrDevices = append(ctrDevices, kataDevice)
}
// Append container devices for block devices passed with --device.
ctrDevices = k.appendDevices(ctrDevices, c.devices)
req := &grpc.CreateContainerRequest{
ContainerId: c.id,

View File

@@ -33,7 +33,11 @@ import (
"google.golang.org/grpc"
)
var testKataProxyURLTempl = "unix://%s/kata-proxy-test.sock"
var (
testKataProxyURLTempl = "unix://%s/kata-proxy-test.sock"
testBlockDeviceVirtPath = "testBlockDeviceVirtPath"
testBlockDeviceCtrPath = "testBlockDeviceCtrPath"
)
func proxyHandlerDiscard(c net.Conn) {
buf := make([]byte, 1024)
@@ -336,3 +340,42 @@ func TestGenerateInterfacesAndRoutes(t *testing.T) {
"Routes returned didn't match: got %+v, expecting %+v", resRoutes, expectedRoutes)
}
func TestAppendDevicesEmptyContainerDeviceList(t *testing.T) {
k := kataAgent{}
devList := []*pb.Device{}
expected := []*pb.Device{}
ctrDevices := []Device{}
updatedDevList := k.appendDevices(devList, ctrDevices)
assert.True(t, reflect.DeepEqual(updatedDevList, expected),
"Device lists didn't match: got %+v, expecting %+v",
updatedDevList, expected)
}
func TestAppendDevices(t *testing.T) {
k := kataAgent{}
devList := []*pb.Device{}
expected := []*pb.Device{
{
Type: kataBlkDevType,
VmPath: testBlockDeviceVirtPath,
ContainerPath: testBlockDeviceCtrPath,
},
}
ctrDevices := []Device{
&BlockDevice{
VirtPath: testBlockDeviceVirtPath,
DeviceInfo: DeviceInfo{
ContainerPath: testBlockDeviceCtrPath,
},
},
}
updatedDevList := k.appendDevices(devList, ctrDevices)
assert.True(t, reflect.DeepEqual(updatedDevList, expected),
"Device lists didn't match: got %+v, expecting %+v",
updatedDevList, expected)
}