diff --git a/pkg/appliance/checks.go b/pkg/appliance/checks.go index 38febf6a..070de30d 100644 --- a/pkg/appliance/checks.go +++ b/pkg/appliance/checks.go @@ -305,7 +305,7 @@ var ( ErrControllerVersionMismatch = errors.New("all controllers need to be prepared with the same version when doing a major or minor version upgrade.") ) -func CheckNeedsMultiControllerUpgrade(stats *openapi.StatsAppliancesList, appliances []openapi.Appliance) ([]openapi.Appliance, error) { +func CheckNeedsMultiControllerUpgrade(stats *openapi.StatsAppliancesList, upgradeStatusMap map[string]UpgradeStatusResult, appliances []openapi.Appliance) ([]openapi.Appliance, error) { var ( errs *multierror.Error preparedControllers []openapi.Appliance @@ -339,10 +339,10 @@ func CheckNeedsMultiControllerUpgrade(stats *openapi.StatsAppliancesList, applia if res, _ := CompareVersionsAndBuildNumber(highestCurrentVersion, current); res > 0 { highestCurrentVersion = current } - us := as.GetUpgrade() - if us.GetStatus() == UpgradeStatusReady { + us := upgradeStatusMap[app.GetId()] + if us.Status == UpgradeStatusReady { preparedControllers = append(preparedControllers, app) - targetVersion, err := ParseVersionString(us.GetDetails()) + targetVersion, err := ParseVersionString(us.Details) if err != nil { errs = multierror.Append(errs, err) continue @@ -365,21 +365,19 @@ func CheckNeedsMultiControllerUpgrade(stats *openapi.StatsAppliancesList, applia // check if prepared controllers has mismatching version preparedClone := slices.Clone(preparedControllers) for i, a := range preparedClone { - for _, s := range stats.GetData() { - if a.GetId() != s.GetId() { - continue - } - - us := s.GetUpgrade() - targetVersion, err := ParseVersionString(us.GetDetails()) - if err != nil { - errs = multierror.Append(errs, err) - continue - } - if res, _ := CompareVersionsAndBuildNumber(highestPreparedVersion, targetVersion); res < 0 { - mismatchControllers = append(mismatchControllers, a) - preparedControllers = append(preparedControllers[:i], preparedControllers[i+1:]...) - } + us, ok := upgradeStatusMap[a.GetId()] + if !ok { + errs = multierror.Append(errs, ErrNoApplianceStats) + continue + } + targetVersion, err := ParseVersionString(us.Details) + if err != nil { + errs = multierror.Append(errs, err) + continue + } + if res, _ := CompareVersionsAndBuildNumber(highestPreparedVersion, targetVersion); res < 0 { + mismatchControllers = append(mismatchControllers, a) + preparedControllers = append(preparedControllers[:i], preparedControllers[i+1:]...) } } diff --git a/pkg/appliance/checks_test.go b/pkg/appliance/checks_test.go index 62a7f0be..69adc9e9 100644 --- a/pkg/appliance/checks_test.go +++ b/pkg/appliance/checks_test.go @@ -673,7 +673,16 @@ func TestCheckNeedsMultiControllerUpgrade(t *testing.T) { stats.Data = append(stats.Data, d.stat) argAppliances = append(argAppliances, d.appliance) } - got, err := CheckNeedsMultiControllerUpgrade(&stats, argAppliances) + upgradeStatusMap := map[string]UpgradeStatusResult{} + for _, s := range stats.GetData() { + us := s.GetUpgrade() + upgradeStatusMap[s.GetId()] = UpgradeStatusResult{ + Status: us.GetStatus(), + Details: us.GetDetails(), + Name: s.GetName(), + } + } + got, err := CheckNeedsMultiControllerUpgrade(&stats, upgradeStatusMap, argAppliances) if (err != nil) != tt.wantErr { t.Errorf("CheckNeedsMultiControllerUpgrade() error = %v, wantErr %v", err, tt.wantErr) return diff --git a/pkg/appliance/mock.go b/pkg/appliance/mock.go index 1153d551..b9a19353 100644 --- a/pkg/appliance/mock.go +++ b/pkg/appliance/mock.go @@ -228,7 +228,7 @@ func (cts *CollectiveTestStruct) GenerateStubs(appliances []openapi.Appliance, s continue } us := s.GetUpgrade() - if count <= 0 { + if us.GetStatus() != UpgradeStatusIdle && count <= 0 { us.SetStatus(UpgradeStatusReady) } else if count == 1 { us.SetStatus(UpgradeStatusIdle) @@ -406,7 +406,7 @@ func GenerateApplianceWithStats(activeFunctions []string, name, hostname, curren currentStatsData.SetOnline(online) currentStatsData.SetVolumeNumber(0) currentStatsData.SetUpgrade(openapi.StatsAppliancesListAllOfUpgrade{ - Status: &upgradeStatus, + Status: openapi.PtrString(upgradeStatus), Details: openapi.PtrString(targetVersion), }) diff --git a/pkg/appliance/upgrade.go b/pkg/appliance/upgrade.go index 74dc3150..ccf79501 100644 --- a/pkg/appliance/upgrade.go +++ b/pkg/appliance/upgrade.go @@ -48,6 +48,7 @@ type UpgradePlan struct { Skipping []SkipUpgrade BackupIds []string stats *openapi.StatsAppliancesList + upgradeStatusMap map[string]UpgradeStatusResult adminHostname string primary *openapi.Appliance allAppliances []openapi.Appliance @@ -55,9 +56,10 @@ type UpgradePlan struct { func NewUpgradePlan(appliances []openapi.Appliance, stats *openapi.StatsAppliancesList, upgradeStatusMap map[string]UpgradeStatusResult, adminHostname string, filter map[string]map[string]string, orderBy []string, descending bool) (*UpgradePlan, error) { plan := UpgradePlan{ - adminHostname: adminHostname, - stats: stats, - allAppliances: appliances, + adminHostname: adminHostname, + stats: stats, + upgradeStatusMap: upgradeStatusMap, + allAppliances: appliances, } primary, err := FindPrimaryController(appliances, plan.adminHostname, false) @@ -289,7 +291,7 @@ func (up *UpgradePlan) GetPrimaryController() *openapi.Appliance { func (up *UpgradePlan) Validate() error { // we check if all controllers need upgrade very early - if _, err := CheckNeedsMultiControllerUpgrade(up.stats, up.allAppliances); err != nil { + if _, err := CheckNeedsMultiControllerUpgrade(up.stats, up.upgradeStatusMap, up.allAppliances); err != nil { return err } return nil