Skip to content

Commit

Permalink
Merge pull request #586 from appgate:index-out-of-range-issue
Browse files Browse the repository at this point in the history
Fix: panic on `upgrade complete`
  • Loading branch information
kajes committed Sep 9, 2024
2 parents 7378c1a + 3bd1bf1 commit 0b30995
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 28 deletions.
13 changes: 10 additions & 3 deletions pkg/appliance/checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ func CheckNeedsMultiControllerUpgrade(stats *openapi.StatsAppliancesList, upgrad
// Now we need to check if the unprepared controllers
// are already running the max prepared version
unpreparedClone := slices.Clone(unpreparedControllers)
for i, a := range unpreparedClone {
for _, a := range unpreparedClone {
for _, s := range stats.GetData() {
if a.GetId() != s.GetId() {
continue
Expand All @@ -397,17 +397,24 @@ func CheckNeedsMultiControllerUpgrade(stats *openapi.StatsAppliancesList, upgrad
}
if res, _ := CompareVersionsAndBuildNumber(highestPreparedVersion, currentVersion); res == 0 {
alreadySameVersion = append(alreadySameVersion, a)
unpreparedControllers = append(unpreparedControllers[:i], unpreparedControllers[i+1:]...)
}
}
}

// if something went wrong during version identification
// just return the errors
if errs != nil {
return nil, errs.ErrorOrNil()
}

// If unprepared controllers are already upgraded, remove them from unpreparedController
if len(alreadySameVersion) > 0 {
unpreparedControllers = util.Filter(unpreparedControllers, func(a openapi.Appliance) bool {
return util.InSliceFunc(a, alreadySameVersion, func(i openapi.Appliance, c openapi.Appliance) bool {
return i.GetId() != c.GetId()
})
})
}

// If this is true, no need to check anymore
// we ignore offline controllers at this point
if (totalControllers - len(offlineControllers)) == (len(preparedControllers) + len(alreadySameVersion)) {
Expand Down
13 changes: 13 additions & 0 deletions pkg/appliance/checks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -596,9 +596,13 @@ func TestCheckNeedsMultiControllerUpgrade(t *testing.T) {
c4, s4, _ := GenerateApplianceWithStats([]string{FunctionController}, "controller4", "", "6.2.0", "6.3.0", statusHealthy, UpgradeStatusReady, true, "default", "default")
c5, s5, _ := GenerateApplianceWithStats([]string{FunctionController}, "controller5", "", "6.2.0", "6.3.0", statusHealthy, UpgradeStatusReady, true, "default", "default")
c6, s6, _ := GenerateApplianceWithStats([]string{FunctionController}, "controller6", "", "6.2.0", "6.3.0", statusHealthy, UpgradeStatusReady, true, "default", "default")
c12, s12, _ := GenerateApplianceWithStats([]string{FunctionController}, "controller12", "", "6.3.0", "6.3.1", statusHealthy, UpgradeStatusReady, true, "default", "default")

// unprepared max version
c7, s7, _ := GenerateApplianceWithStats([]string{FunctionController}, "controller7", "", "6.3.0", "", statusHealthy, UpgradeStatusIdle, true, "default", "default")
c9, s9, _ := GenerateApplianceWithStats([]string{FunctionController}, "controller9", "", "6.3.1", "", statusHealthy, UpgradeStatusIdle, true, "default", "default")
c10, s10, _ := GenerateApplianceWithStats([]string{FunctionController}, "controller10", "", "6.3.1", "", statusHealthy, UpgradeStatusIdle, true, "default", "default")
c11, s11, _ := GenerateApplianceWithStats([]string{FunctionController}, "controller11", "", "6.3.1", "", statusHealthy, UpgradeStatusIdle, true, "default", "default")

// offline
c8, s8, _ := GenerateApplianceWithStats([]string{FunctionController}, "controller8", "", "6.2.0", "", statusHealthy, UpgradeStatusIdle, false, "default", "default")
Expand Down Expand Up @@ -660,6 +664,15 @@ func TestCheckNeedsMultiControllerUpgrade(t *testing.T) {
{c8, s8},
},
},
{
name: "only one left to upgrade",
in: []inData{
{c12, s12},
{c9, s9},
{c10, s10},
{c11, s11},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
41 changes: 41 additions & 0 deletions pkg/util/generics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package util

import "errors"

func Find[A any](s []A, f func(A) bool) (A, error) {
var r A
for _, v := range s {
if f(v) {
return v, nil
}
}
return r, errors.New("no match in slice")
}

func Filter[A any](s []A, f func(A) bool) []A {
filtered := []A{}
for _, i := range s {
if f(i) {
filtered = append(filtered, i)
}
}
return filtered
}

func InSlice[C comparable](n C, h []C) bool {
for _, i := range h {
if i == n {
return true
}
}
return false
}

func InSliceFunc[A any](n A, h []A, f func(i, p A) bool) bool {
for _, item := range h {
if f(item, n) {
return true
}
}
return false
}
25 changes: 0 additions & 25 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"net/url"
"os"
"regexp"
"sort"
"strings"
"time"

Expand Down Expand Up @@ -35,20 +34,6 @@ func AppendIfMissing[V comparable](slice []V, i V) []V {
return append(slice, i)
}

func InSlice(needle string, haystack []string) bool {
stack := make([]string, len(haystack))
copy(stack, haystack)
sort.Strings(stack)
i := sort.Search(
len(stack),
func(i int) bool { return stack[i] >= needle },
)
if i < len(stack) && stack[i] == needle {
return true
}
return false
}

// SearchSlice will search a slice of strings and return all matching results.
// The search can either be case sensitive or not.
func SearchSlice(needle string, haystack []string, caseInsensitive bool) []string {
Expand Down Expand Up @@ -204,13 +189,3 @@ func AddSocketLogHook(path string) error {
log.AddHook(hook)
return nil
}

func Find[T any](s []T, f func(T) bool) (T, error) {
var r T
for _, v := range s {
if f(v) {
return v, nil
}
}
return r, errors.New("no match in slice")
}

0 comments on commit 0b30995

Please sign in to comment.