Refactor methods to reduce their Cognitive Complexity
Refactored pkg/virt-controller/watch/migration.go.
Method refactored: "execute", "sync" and "findRunningMigrations".
Was detected by SonarCloud code scan as a code smell issue.
@@ -178,15 +178,14 @@ func (c *MigrationController) execute(key string) error {
}
if !vmiExists {
+ var err error
+
if migration.DeletionTimestamp == nil {
logger.V(3).Infof("Deleting migration for deleted vmi %s/%s", migration.Namespace, migration.Spec.VMIName)
- err := c.clientset.VirtualMachineInstanceMigration(migration.Namespace).Delete(migration.Name, &v1.DeleteOptions{})
- if err != nil {
- return err
- }
+ err = c.clientset.VirtualMachineInstanceMigration(migration.Namespace).Delete(migration.Name, &v1.DeleteOptions{})
}
// nothing to process for a migration that's being deleted
- return nil
+ return err
}
vmi = vmiObj.(*virtv1.VirtualMachineInstance)
@@ -459,14 +458,10 @@ func (c *MigrationController) sync(key string, migration *virtv1.VirtualMachineI
return nil
}
- if vmi == nil || vmi.DeletionTimestamp != nil {
- // nothing to do with a deleted vmi
- return nil
- } else if vmi.Status.MigrationState != nil &&
- vmi.Status.MigrationState.MigrationUID == migration.UID &&
- vmi.Status.MigrationState.EndTimestamp != nil {
+ vmiDeleted := vmi == nil || vmi.DeletionTimestamp != nil
+ migrationDone := vmi.Status.MigrationState != nil && vmi.Status.MigrationState.MigrationUID == migration.UID && vmi.Status.MigrationState.EndTimestamp != nil
- // nothing to do here, the migration is done
+ if vmiDeleted || migrationDone {
return nil
}
@@ -864,8 +859,8 @@ func (c *MigrationController) findRunningMigrations() ([]*virtv1.VirtualMachineI
for _, migration := range notFinishedMigrations {
if migration.IsRunning() {
runningMigrations = append(runningMigrations, migration)
- } else {
-
+ continue
+ }
vmi, exists, err := c.vmiInformer.GetStore().GetByKey(migration.Namespace + "/" + migration.Spec.VMIName)
if err != nil {
return nil, err
@@ -881,6 +876,5 @@ func (c *MigrationController) findRunningMigrations() ([]*virtv1.VirtualMachineI
runningMigrations = append(runningMigrations, migration)
}
}
- }
return runningMigrations, nil
}
