runtime: small refactor to improve readability

Remove some confusing/duplicate code so it's more readable

Fixes: #3454

Signed-off-by: Feng Wang <feng.wang@databricks.com>
This commit is contained in:
Feng Wang 2022-03-04 09:20:12 -08:00
parent f905161bbb
commit e76519af83

View File

@ -599,30 +599,30 @@ func (c *Container) createBlockDevices(ctx context.Context) error {
} }
// iterate all mounts and create block device if it's block based. // iterate all mounts and create block device if it's block based.
for i, m := range c.mounts { for i := range c.mounts {
if len(m.BlockDeviceID) > 0 { if len(c.mounts[i].BlockDeviceID) > 0 {
// Non-empty m.BlockDeviceID indicates there's already one device // Non-empty m.BlockDeviceID indicates there's already one device
// associated with the mount,so no need to create a new device for it // associated with the mount,so no need to create a new device for it
// and we only create block device for bind mount // and we only create block device for bind mount
continue continue
} }
if m.Type != "bind" { if c.mounts[i].Type != "bind" {
// We only handle for bind-mounts // We only handle for bind-mounts
continue continue
} }
// Handle directly assigned volume. Update the mount info based on the mount info json. // Handle directly assigned volume. Update the mount info based on the mount info json.
mntInfo, e := volume.VolumeMountInfo(m.Source) mntInfo, e := volume.VolumeMountInfo(c.mounts[i].Source)
if e != nil && !os.IsNotExist(e) { if e != nil && !os.IsNotExist(e) {
c.Logger().WithError(e).WithField("mount-source", m.Source). c.Logger().WithError(e).WithField("mount-source", c.mounts[i].Source).
Error("failed to parse the mount info file for a direct assigned volume") Error("failed to parse the mount info file for a direct assigned volume")
continue continue
} }
if mntInfo != nil { if mntInfo != nil {
// Write out sandbox info file on the mount source to allow CSI to communicate with the runtime // Write out sandbox info file on the mount source to allow CSI to communicate with the runtime
if err := volume.RecordSandboxId(c.sandboxID, m.Source); err != nil { if err := volume.RecordSandboxId(c.sandboxID, c.mounts[i].Source); err != nil {
c.Logger().WithError(err).Error("error writing sandbox info") c.Logger().WithError(err).Error("error writing sandbox info")
} }
@ -638,15 +638,11 @@ func (c *Container) createBlockDevices(ctx context.Context) error {
c.mounts[i].Type = mntInfo.FsType c.mounts[i].Type = mntInfo.FsType
c.mounts[i].Options = mntInfo.Options c.mounts[i].Options = mntInfo.Options
c.mounts[i].ReadOnly = readonly c.mounts[i].ReadOnly = readonly
m.Source = mntInfo.Device
m.Type = mntInfo.FsType
m.Options = mntInfo.Options
m.ReadOnly = readonly
} }
var stat unix.Stat_t var stat unix.Stat_t
if err := unix.Stat(m.Source, &stat); err != nil { if err := unix.Stat(c.mounts[i].Source, &stat); err != nil {
return fmt.Errorf("stat %q failed: %v", m.Source, err) return fmt.Errorf("stat %q failed: %v", c.mounts[i].Source, err)
} }
var di *config.DeviceInfo var di *config.DeviceInfo
@ -656,17 +652,17 @@ func (c *Container) createBlockDevices(ctx context.Context) error {
// instead of passing this as a shared mount. // instead of passing this as a shared mount.
if stat.Mode&unix.S_IFBLK == unix.S_IFBLK { if stat.Mode&unix.S_IFBLK == unix.S_IFBLK {
di = &config.DeviceInfo{ di = &config.DeviceInfo{
HostPath: m.Source, HostPath: c.mounts[i].Source,
ContainerPath: m.Destination, ContainerPath: c.mounts[i].Destination,
DevType: "b", DevType: "b",
Major: int64(unix.Major(uint64(stat.Rdev))), Major: int64(unix.Major(uint64(stat.Rdev))),
Minor: int64(unix.Minor(uint64(stat.Rdev))), Minor: int64(unix.Minor(uint64(stat.Rdev))),
ReadOnly: m.ReadOnly, ReadOnly: c.mounts[i].ReadOnly,
} }
// Check whether source can be used as a pmem device // Check whether source can be used as a pmem device
} else if di, err = config.PmemDeviceInfo(m.Source, m.Destination); err != nil { } else if di, err = config.PmemDeviceInfo(c.mounts[i].Source, c.mounts[i].Destination); err != nil {
c.Logger().WithError(err). c.Logger().WithError(err).
WithField("mount-source", m.Source). WithField("mount-source", c.mounts[i].Source).
Debug("no loop device") Debug("no loop device")
} }
@ -675,7 +671,7 @@ func (c *Container) createBlockDevices(ctx context.Context) error {
if err != nil { if err != nil {
// Do not return an error, try to create // Do not return an error, try to create
// devices for other mounts // devices for other mounts
c.Logger().WithError(err).WithField("mount-source", m.Source). c.Logger().WithError(err).WithField("mount-source", c.mounts[i].Source).
Error("device manager failed to create new device") Error("device manager failed to create new device")
continue continue