Skip to content

Commit

Permalink
scanner: simplify the complex Method
Browse files Browse the repository at this point in the history
Signed-off-by: Vicente Cheng <vicente.cheng@suse.com>
  • Loading branch information
Vicente-Cheng committed May 9, 2024
1 parent f13eaf8 commit afaec3d
Showing 1 changed file with 37 additions and 26 deletions.
63 changes: 37 additions & 26 deletions pkg/controller/blockdevice/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,40 @@ func (s *Scanner) collectAllDevices() []*deviceWithAutoProvision {
return allDevices
}

func (s *Scanner) handleExistingDev(oldBd *diskv1.BlockDevice, newBd *diskv1.BlockDevice, autoProvisioned bool) {
if isDevPathChanged(oldBd, newBd) {
logrus.Debugf("Enqueue block device %s for device path change", newBd.Name)
s.Blockdevices.Enqueue(s.Namespace, newBd.Name)
} else if isDevAlreadyProvisioned(newBd) {
logrus.Debugf("Skip the provisioned device: %s", newBd.Name)
} else if s.NeedsAutoProvision(oldBd, autoProvisioned) {
logrus.Debugf("Enqueue block device %s for auto-provisioning", newBd.Name)
s.Blockdevices.Enqueue(s.Namespace, newBd.Name)
} else {
logrus.Debugf("Skip updating device %s", newBd.Name)
}
}

func (s *Scanner) deactivateBlockDevices(oldBds map[string]*diskv1.BlockDevice) error {
for _, oldBd := range oldBds {
if oldBd.Status.State == diskv1.BlockDeviceInactive {
logrus.Debugf("The device %s is already inactive, continue.", oldBd.Name)
continue
}
logrus.Debugf("Change the device %s to inactive.", oldBd.Name)
newBd := oldBd.DeepCopy()
newBd.Status.State = diskv1.BlockDeviceInactive
if !reflect.DeepEqual(oldBd, newBd) {
logrus.Debugf("Update block device %s for new formatting and mount state", oldBd.Name)
if _, err := s.Blockdevices.Update(newBd); err != nil {
logrus.Errorf("Update device %s status error", oldBd.Name)
return err
}
}
}
return nil
}

// scanBlockDevicesOnNode scans block devices on the node, and it will either create or update them.
func (s *Scanner) scanBlockDevicesOnNode() error {
logrus.Debugf("Scan block devices of node: %s", s.NodeName)
Expand All @@ -144,17 +178,7 @@ func (s *Scanner) scanBlockDevicesOnNode() error {
autoProvisioned := device.AutoProvisioned
logrus.Debugf("Processing block device %s with wwn: %s", bd.Name, bd.Status.DeviceStatus.Details.WWN)
if oldBd, ok := oldBds[bd.Name]; ok {
if isDevPathChanged(oldBd, bd) {
logrus.Debugf("Enqueue block device %s for device path change", bd.Name)
s.Blockdevices.Enqueue(s.Namespace, bd.Name)
} else if isDevAlreadyProvisioned(bd) {
logrus.Debugf("Skip the provisioned device: %s", bd.Name)
} else if s.NeedsAutoProvision(oldBd, autoProvisioned) {
logrus.Debugf("Enqueue block device %s for auto-provisioning", bd.Name)
s.Blockdevices.Enqueue(s.Namespace, bd.Name)
} else {
logrus.Debugf("Skip updating device %s", bd.Name)
}
s.handleExistingDev(oldBd, bd, autoProvisioned)
// only first time to update the cache
if !CacheDiskTags.Initialized() && oldBd.Spec.Tags != nil && len(oldBd.Spec.Tags) > 0 {
CacheDiskTags.UpdateDiskTags(oldBd.Name, oldBd.Spec.Tags)
Expand Down Expand Up @@ -185,21 +209,8 @@ func (s *Scanner) scanBlockDevicesOnNode() error {

// We do not remove the block device that maybe just temporily not available.
// Set it to inactive and give the chance to recover.
for _, oldBd := range oldBds {
if oldBd.Status.State == diskv1.BlockDeviceInactive {
logrus.Debugf("The device %s is already inactive, continue.", oldBd.Name)
continue
}
logrus.Debugf("Change the device %s to inactive.", oldBd.Name)
newBd := oldBd.DeepCopy()
newBd.Status.State = diskv1.BlockDeviceInactive
if !reflect.DeepEqual(oldBd, newBd) {
logrus.Debugf("Update block device %s for new formatting and mount state", oldBd.Name)
if _, err := s.Blockdevices.Update(newBd); err != nil {
logrus.Errorf("Update device %s status error", oldBd.Name)
return err
}
}
if err := s.deactivateBlockDevices(oldBds); err != nil {
return err
}
return nil
}
Expand Down

0 comments on commit afaec3d

Please sign in to comment.