mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-08-16 23:17:42 +00:00
virtcontainers: Fix container.go cyclomatic complexity
With the Stores conversion, the newContainer() cyclomatic complexity went over 15. We fix that by extracting the block devices creation routine out of newContainer. Signed-off-by: Samuel Ortiz <sameo@linux.intel.com>
This commit is contained in:
parent
f8e7e308c3
commit
7b0376f3d3
@ -584,6 +584,42 @@ func filterDevices(sandbox *Sandbox, c *Container, devices []ContainerDevice) (r
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Container) createBlockDevices() error {
|
||||||
|
// iterate all mounts and create block device if it's block based.
|
||||||
|
for i, m := range c.mounts {
|
||||||
|
if len(m.BlockDeviceID) > 0 || m.Type != "bind" {
|
||||||
|
// Non-empty m.BlockDeviceID indicates there's already one device
|
||||||
|
// associated with the mount,so no need to create a new device for it
|
||||||
|
// and we only create block device for bind mount
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
var stat unix.Stat_t
|
||||||
|
if err := unix.Stat(m.Source, &stat); err != nil {
|
||||||
|
return fmt.Errorf("stat %q failed: %v", m.Source, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if mount is a block device file. If it is, the block device will be attached to the host
|
||||||
|
// instead of passing this as a shared mount.
|
||||||
|
if c.checkBlockDeviceSupport() && stat.Mode&unix.S_IFBLK == unix.S_IFBLK {
|
||||||
|
b, err := c.sandbox.devManager.NewDevice(config.DeviceInfo{
|
||||||
|
HostPath: m.Source,
|
||||||
|
ContainerPath: m.Destination,
|
||||||
|
DevType: "b",
|
||||||
|
Major: int64(unix.Major(stat.Rdev)),
|
||||||
|
Minor: int64(unix.Minor(stat.Rdev)),
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("device manager failed to create new device for %q: %v", m.Source, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
c.mounts[i].BlockDeviceID = b.DeviceID()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// newContainer creates a Container structure from a sandbox and a container configuration.
|
// newContainer creates a Container structure from a sandbox and a container configuration.
|
||||||
func newContainer(sandbox *Sandbox, contConfig ContainerConfig) (*Container, error) {
|
func newContainer(sandbox *Sandbox, contConfig ContainerConfig) (*Container, error) {
|
||||||
span, _ := sandbox.trace("newContainer")
|
span, _ := sandbox.trace("newContainer")
|
||||||
@ -630,37 +666,9 @@ func newContainer(sandbox *Sandbox, contConfig ContainerConfig) (*Container, err
|
|||||||
// restore mounts from disk
|
// restore mounts from disk
|
||||||
c.mounts = mounts
|
c.mounts = mounts
|
||||||
} else {
|
} else {
|
||||||
// for newly created container:
|
// Create block devices for newly created container
|
||||||
// iterate all mounts and create block device if it's block based.
|
if err := c.createBlockDevices(); err != nil {
|
||||||
for i, m := range c.mounts {
|
return nil, err
|
||||||
if len(m.BlockDeviceID) > 0 || m.Type != "bind" {
|
|
||||||
// Non-empty m.BlockDeviceID indicates there's already one device
|
|
||||||
// associated with the mount,so no need to create a new device for it
|
|
||||||
// and we only create block device for bind mount
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
var stat unix.Stat_t
|
|
||||||
if err := unix.Stat(m.Source, &stat); err != nil {
|
|
||||||
return nil, fmt.Errorf("stat %q failed: %v", m.Source, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if mount is a block device file. If it is, the block device will be attached to the host
|
|
||||||
// instead of passing this as a shared mount.
|
|
||||||
if c.checkBlockDeviceSupport() && stat.Mode&unix.S_IFBLK == unix.S_IFBLK {
|
|
||||||
b, err := c.sandbox.devManager.NewDevice(config.DeviceInfo{
|
|
||||||
HostPath: m.Source,
|
|
||||||
ContainerPath: m.Destination,
|
|
||||||
DevType: "b",
|
|
||||||
Major: int64(unix.Major(stat.Rdev)),
|
|
||||||
Minor: int64(unix.Minor(stat.Rdev)),
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("device manager failed to create new device for %q: %v", m.Source, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
c.mounts[i].BlockDeviceID = b.DeviceID()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user