runtime: device/persist: drop persist dependency from device pkgs

Rather than have device package depend on persist, let's define the
(almost duplicate) structures within device itself, and have the Kata
Container's persist pkg import these.

This'll help avoid unecessary dependencies within our core packages.

Signed-off-by: Eric Ernst <eric_ernst@apple.com>
This commit is contained in:
Eric Ernst 2022-06-16 00:24:04 -07:00
parent f9e96c6506
commit f97d9b45c8
11 changed files with 37 additions and 43 deletions

View File

@ -10,7 +10,6 @@ import (
"context" "context"
"github.com/kata-containers/kata-containers/src/runtime/pkg/device/config" "github.com/kata-containers/kata-containers/src/runtime/pkg/device/config"
persistapi "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/persist/api"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
@ -78,10 +77,10 @@ type Device interface {
Dereference() uint Dereference() uint
// Save converts Device to DeviceState // Save converts Device to DeviceState
Save() persistapi.DeviceState Save() config.DeviceState
// Load loads DeviceState and converts it to specific device // Load loads DeviceState and converts it to specific device
Load(persistapi.DeviceState) Load(config.DeviceState)
} }
// DeviceManager can be used to create a new device, this can be used as single // DeviceManager can be used to create a new device, this can be used as single
@ -94,5 +93,5 @@ type DeviceManager interface {
IsDeviceAttached(string) bool IsDeviceAttached(string) bool
GetDeviceByID(string) Device GetDeviceByID(string) Device
GetAllDevices() []Device GetAllDevices() []Device
LoadDevices([]persistapi.DeviceState) LoadDevices([]config.DeviceState)
} }

View File

@ -4,7 +4,7 @@
// SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: Apache-2.0
// //
package persistapi package config
import vcTypes "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/types" import vcTypes "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/types"
@ -12,7 +12,7 @@ import vcTypes "github.com/kata-containers/kata-containers/src/runtime/virtconta
// BlockDrive represents a block storage drive which may be used in case the storage // BlockDrive represents a block storage drive which may be used in case the storage
// driver has an underlying block storage device. // driver has an underlying block storage device.
type BlockDrive struct { type BlockDriveState struct {
// File is the path to the disk-image/device which will be used with this drive // File is the path to the disk-image/device which will be used with this drive
File string File string
@ -50,7 +50,7 @@ type BlockDrive struct {
} }
// VFIODev represents a VFIO drive used for hotplugging // VFIODev represents a VFIO drive used for hotplugging
type VFIODev struct { type VFIODevState struct {
// ID is used to identify this drive in the hypervisor options. // ID is used to identify this drive in the hypervisor options.
ID string ID string
@ -65,7 +65,7 @@ type VFIODev struct {
} }
// VhostUserDeviceAttrs represents data shared by most vhost-user devices // VhostUserDeviceAttrs represents data shared by most vhost-user devices
type VhostUserDeviceAttrs struct { type VhostUserDeviceAttrsState struct {
DevID string DevID string
SocketPath string SocketPath string
Type string Type string
@ -89,11 +89,11 @@ type DeviceState struct {
// for example, for BlockDevice, we can set DriverOptions["block-driver"]="virtio-blk" // for example, for BlockDevice, we can set DriverOptions["block-driver"]="virtio-blk"
DriverOptions map[string]string DriverOptions map[string]string
// VhostUserDeviceAttrs is specific for vhost-user device driver // VhostUserDeviceAttrsState is specific for vhost-user device driver
VhostUserDev *VhostUserDeviceAttrs `json:",omitempty"` VhostUserDev *VhostUserDeviceAttrsState `json:",omitempty"`
// BlockDrive is specific for block device driver // BlockDrive is specific for block device driver
BlockDrive *BlockDrive `json:",omitempty"` BlockDrive *BlockDriveState `json:",omitempty"`
ID string ID string
@ -108,8 +108,8 @@ type DeviceState struct {
// More info in mknod(1). // More info in mknod(1).
DevType string DevType string
// VFIODev is specific VFIO device driver // VFIODevState is specific VFIO device driver
VFIODevs []*VFIODev `json:",omitempty"` VFIODevs []*VFIODevState `json:",omitempty"`
RefCount uint RefCount uint
AttachCount uint AttachCount uint

View File

@ -12,7 +12,6 @@ import (
"github.com/kata-containers/kata-containers/src/runtime/pkg/device/api" "github.com/kata-containers/kata-containers/src/runtime/pkg/device/api"
"github.com/kata-containers/kata-containers/src/runtime/pkg/device/config" "github.com/kata-containers/kata-containers/src/runtime/pkg/device/config"
persistapi "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/persist/api"
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/utils" "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/utils"
) )
@ -159,13 +158,13 @@ func (device *BlockDevice) GetDeviceInfo() interface{} {
} }
// Save converts Device to DeviceState // Save converts Device to DeviceState
func (device *BlockDevice) Save() persistapi.DeviceState { func (device *BlockDevice) Save() config.DeviceState {
ds := device.GenericDevice.Save() ds := device.GenericDevice.Save()
ds.Type = string(device.DeviceType()) ds.Type = string(device.DeviceType())
drive := device.BlockDrive drive := device.BlockDrive
if drive != nil { if drive != nil {
ds.BlockDrive = &persistapi.BlockDrive{ ds.BlockDrive = &config.BlockDriveState{
File: drive.File, File: drive.File,
Format: drive.Format, Format: drive.Format,
ID: drive.ID, ID: drive.ID,
@ -183,7 +182,7 @@ func (device *BlockDevice) Save() persistapi.DeviceState {
} }
// Load loads DeviceState and converts it to specific device // Load loads DeviceState and converts it to specific device
func (device *BlockDevice) Load(ds persistapi.DeviceState) { func (device *BlockDevice) Load(ds config.DeviceState) {
device.GenericDevice = &GenericDevice{} device.GenericDevice = &GenericDevice{}
device.GenericDevice.Load(ds) device.GenericDevice.Load(ds)

View File

@ -12,7 +12,6 @@ import (
"github.com/kata-containers/kata-containers/src/runtime/pkg/device/api" "github.com/kata-containers/kata-containers/src/runtime/pkg/device/api"
"github.com/kata-containers/kata-containers/src/runtime/pkg/device/config" "github.com/kata-containers/kata-containers/src/runtime/pkg/device/config"
persistapi "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/persist/api"
) )
// GenericDevice refers to a device that is neither a VFIO device, block device or VhostUserDevice. // GenericDevice refers to a device that is neither a VFIO device, block device or VhostUserDevice.
@ -128,8 +127,8 @@ func (device *GenericDevice) bumpAttachCount(attach bool) (skip bool, err error)
} }
// Save converts Device to DeviceState // Save converts Device to DeviceState
func (device *GenericDevice) Save() persistapi.DeviceState { func (device *GenericDevice) Save() config.DeviceState {
dss := persistapi.DeviceState{ dss := config.DeviceState{
ID: device.ID, ID: device.ID,
Type: string(device.DeviceType()), Type: string(device.DeviceType()),
RefCount: device.RefCount, RefCount: device.RefCount,
@ -148,7 +147,7 @@ func (device *GenericDevice) Save() persistapi.DeviceState {
} }
// Load loads DeviceState and converts it to specific device // Load loads DeviceState and converts it to specific device
func (device *GenericDevice) Load(ds persistapi.DeviceState) { func (device *GenericDevice) Load(ds config.DeviceState) {
device.ID = ds.ID device.ID = ds.ID
device.RefCount = ds.RefCount device.RefCount = ds.RefCount
device.AttachCount = ds.AttachCount device.AttachCount = ds.AttachCount

View File

@ -18,7 +18,6 @@ import (
"github.com/kata-containers/kata-containers/src/runtime/pkg/device/api" "github.com/kata-containers/kata-containers/src/runtime/pkg/device/api"
"github.com/kata-containers/kata-containers/src/runtime/pkg/device/config" "github.com/kata-containers/kata-containers/src/runtime/pkg/device/config"
persistapi "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/persist/api"
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/utils" "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/utils"
) )
@ -174,14 +173,14 @@ func (device *VFIODevice) GetDeviceInfo() interface{} {
} }
// Save converts Device to DeviceState // Save converts Device to DeviceState
func (device *VFIODevice) Save() persistapi.DeviceState { func (device *VFIODevice) Save() config.DeviceState {
ds := device.GenericDevice.Save() ds := device.GenericDevice.Save()
ds.Type = string(device.DeviceType()) ds.Type = string(device.DeviceType())
devs := device.VfioDevs devs := device.VfioDevs
for _, dev := range devs { for _, dev := range devs {
if dev != nil { if dev != nil {
ds.VFIODevs = append(ds.VFIODevs, &persistapi.VFIODev{ ds.VFIODevs = append(ds.VFIODevs, &config.VFIODevState{
ID: dev.ID, ID: dev.ID,
Type: uint32(dev.Type), Type: uint32(dev.Type),
BDF: dev.BDF, BDF: dev.BDF,
@ -193,7 +192,7 @@ func (device *VFIODevice) Save() persistapi.DeviceState {
} }
// Load loads DeviceState and converts it to specific device // Load loads DeviceState and converts it to specific device
func (device *VFIODevice) Load(ds persistapi.DeviceState) { func (device *VFIODevice) Load(ds config.DeviceState) {
device.GenericDevice = &GenericDevice{} device.GenericDevice = &GenericDevice{}
device.GenericDevice.Load(ds) device.GenericDevice.Load(ds)

View File

@ -11,7 +11,6 @@ import (
"github.com/kata-containers/kata-containers/src/runtime/pkg/device/api" "github.com/kata-containers/kata-containers/src/runtime/pkg/device/api"
"github.com/kata-containers/kata-containers/src/runtime/pkg/device/config" "github.com/kata-containers/kata-containers/src/runtime/pkg/device/config"
persistapi "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/persist/api"
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/utils" "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/utils"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
@ -156,13 +155,13 @@ func (device *VhostUserBlkDevice) GetDeviceInfo() interface{} {
} }
// Save converts Device to DeviceState // Save converts Device to DeviceState
func (device *VhostUserBlkDevice) Save() persistapi.DeviceState { func (device *VhostUserBlkDevice) Save() config.DeviceState {
ds := device.GenericDevice.Save() ds := device.GenericDevice.Save()
ds.Type = string(device.DeviceType()) ds.Type = string(device.DeviceType())
vAttr := device.VhostUserDeviceAttrs vAttr := device.VhostUserDeviceAttrs
if vAttr != nil { if vAttr != nil {
ds.VhostUserDev = &persistapi.VhostUserDeviceAttrs{ ds.VhostUserDev = &config.VhostUserDeviceAttrsState{
DevID: vAttr.DevID, DevID: vAttr.DevID,
SocketPath: vAttr.SocketPath, SocketPath: vAttr.SocketPath,
Type: string(vAttr.Type), Type: string(vAttr.Type),
@ -174,7 +173,7 @@ func (device *VhostUserBlkDevice) Save() persistapi.DeviceState {
} }
// Load loads DeviceState and converts it to specific device // Load loads DeviceState and converts it to specific device
func (device *VhostUserBlkDevice) Load(ds persistapi.DeviceState) { func (device *VhostUserBlkDevice) Load(ds config.DeviceState) {
device.GenericDevice = &GenericDevice{} device.GenericDevice = &GenericDevice{}
device.GenericDevice.Load(ds) device.GenericDevice.Load(ds)

View File

@ -12,7 +12,6 @@ import (
"github.com/kata-containers/kata-containers/src/runtime/pkg/device/api" "github.com/kata-containers/kata-containers/src/runtime/pkg/device/api"
"github.com/kata-containers/kata-containers/src/runtime/pkg/device/config" "github.com/kata-containers/kata-containers/src/runtime/pkg/device/config"
persistapi "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/persist/api"
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/utils" "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/utils"
) )
@ -75,10 +74,10 @@ func (device *VhostUserNetDevice) GetDeviceInfo() interface{} {
} }
// Save converts Device to DeviceState // Save converts Device to DeviceState
func (device *VhostUserNetDevice) Save() persistapi.DeviceState { func (device *VhostUserNetDevice) Save() config.DeviceState {
ds := device.GenericDevice.Save() ds := device.GenericDevice.Save()
ds.Type = string(device.DeviceType()) ds.Type = string(device.DeviceType())
ds.VhostUserDev = &persistapi.VhostUserDeviceAttrs{ ds.VhostUserDev = &config.VhostUserDeviceAttrsState{
DevID: device.DevID, DevID: device.DevID,
SocketPath: device.SocketPath, SocketPath: device.SocketPath,
Type: string(device.Type), Type: string(device.Type),
@ -88,7 +87,7 @@ func (device *VhostUserNetDevice) Save() persistapi.DeviceState {
} }
// Load loads DeviceState and converts it to specific device // Load loads DeviceState and converts it to specific device
func (device *VhostUserNetDevice) Load(ds persistapi.DeviceState) { func (device *VhostUserNetDevice) Load(ds config.DeviceState) {
device.GenericDevice = &GenericDevice{} device.GenericDevice = &GenericDevice{}
device.GenericDevice.Load(ds) device.GenericDevice.Load(ds)

View File

@ -12,7 +12,6 @@ import (
"github.com/kata-containers/kata-containers/src/runtime/pkg/device/api" "github.com/kata-containers/kata-containers/src/runtime/pkg/device/api"
"github.com/kata-containers/kata-containers/src/runtime/pkg/device/config" "github.com/kata-containers/kata-containers/src/runtime/pkg/device/config"
persistapi "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/persist/api"
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/utils" "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/utils"
) )
@ -75,10 +74,10 @@ func (device *VhostUserSCSIDevice) GetDeviceInfo() interface{} {
} }
// Save converts Device to DeviceState // Save converts Device to DeviceState
func (device *VhostUserSCSIDevice) Save() persistapi.DeviceState { func (device *VhostUserSCSIDevice) Save() config.DeviceState {
ds := device.GenericDevice.Save() ds := device.GenericDevice.Save()
ds.Type = string(device.DeviceType()) ds.Type = string(device.DeviceType())
ds.VhostUserDev = &persistapi.VhostUserDeviceAttrs{ ds.VhostUserDev = &config.VhostUserDeviceAttrsState{
DevID: device.DevID, DevID: device.DevID,
SocketPath: device.SocketPath, SocketPath: device.SocketPath,
Type: string(device.Type), Type: string(device.Type),
@ -88,7 +87,7 @@ func (device *VhostUserSCSIDevice) Save() persistapi.DeviceState {
} }
// Load loads DeviceState and converts it to specific device // Load loads DeviceState and converts it to specific device
func (device *VhostUserSCSIDevice) Load(ds persistapi.DeviceState) { func (device *VhostUserSCSIDevice) Load(ds config.DeviceState) {
device.GenericDevice = &GenericDevice{} device.GenericDevice = &GenericDevice{}
device.GenericDevice.Load(ds) device.GenericDevice.Load(ds)

View File

@ -17,7 +17,6 @@ import (
"github.com/kata-containers/kata-containers/src/runtime/pkg/device/api" "github.com/kata-containers/kata-containers/src/runtime/pkg/device/api"
"github.com/kata-containers/kata-containers/src/runtime/pkg/device/config" "github.com/kata-containers/kata-containers/src/runtime/pkg/device/config"
"github.com/kata-containers/kata-containers/src/runtime/pkg/device/drivers" "github.com/kata-containers/kata-containers/src/runtime/pkg/device/drivers"
persistapi "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/persist/api"
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/utils" "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/utils"
) )
@ -242,7 +241,7 @@ func (dm *deviceManager) IsDeviceAttached(id string) bool {
} }
// LoadDevices load devices from persist state // LoadDevices load devices from persist state
func (dm *deviceManager) LoadDevices(devStates []persistapi.DeviceState) { func (dm *deviceManager) LoadDevices(devStates []config.DeviceState) {
dm.Lock() dm.Lock()
defer dm.Unlock() defer dm.Unlock()

View File

@ -8,8 +8,9 @@ package virtcontainers
import ( import (
"errors" "errors"
hv "github.com/kata-containers/kata-containers/src/runtime/pkg/hypervisors"
"github.com/kata-containers/kata-containers/src/runtime/pkg/device/api" "github.com/kata-containers/kata-containers/src/runtime/pkg/device/api"
devconfig "github.com/kata-containers/kata-containers/src/runtime/pkg/device/config"
hv "github.com/kata-containers/kata-containers/src/runtime/pkg/hypervisors"
exp "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/experimental" exp "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/experimental"
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/persist" "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/persist"
persistapi "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/persist/api" persistapi "github.com/kata-containers/kata-containers/src/runtime/virtcontainers/persist/api"
@ -65,7 +66,7 @@ func (s *Sandbox) dumpHypervisor(ss *persistapi.SandboxState) {
ss.HypervisorState.BlockIndexMap = s.state.BlockIndexMap ss.HypervisorState.BlockIndexMap = s.state.BlockIndexMap
} }
func deviceToDeviceState(devices []api.Device) (dss []persistapi.DeviceState) { func deviceToDeviceState(devices []api.Device) (dss []devconfig.DeviceState) {
for _, dev := range devices { for _, dev := range devices {
dss = append(dss, dev.Save()) dss = append(dss, dev.Save())
} }
@ -323,7 +324,7 @@ func (s *Sandbox) loadAgent(as persistapi.AgentState) {
} }
} }
func (s *Sandbox) loadDevices(devStates []persistapi.DeviceState) { func (s *Sandbox) loadDevices(devStates []devconfig.DeviceState) {
s.devManager.LoadDevices(devStates) s.devManager.LoadDevices(devStates)
} }

View File

@ -7,6 +7,7 @@
package persistapi package persistapi
import ( import (
dev "github.com/kata-containers/kata-containers/src/runtime/pkg/device/config"
hv "github.com/kata-containers/kata-containers/src/runtime/pkg/hypervisors" hv "github.com/kata-containers/kata-containers/src/runtime/pkg/hypervisors"
) )
@ -26,7 +27,7 @@ type SandboxState struct {
CgroupPaths map[string]string CgroupPaths map[string]string
// Devices plugged to sandbox(hypervisor) // Devices plugged to sandbox(hypervisor)
Devices []DeviceState Devices []dev.DeviceState
// State is sandbox running status // State is sandbox running status
State string State string