Skip to content

Commit

Permalink
f - implement random sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
brianberzins committed Apr 27, 2022
1 parent b7eb80a commit b10af6d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
7 changes: 7 additions & 0 deletions reaper/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
v1 "k8s.io/api/core/v1"
"math/rand"
"os"
"strconv"
"strings"
Expand Down Expand Up @@ -171,7 +172,13 @@ func podSortingStrategy() (func([]v1.Pod), error) {
if !present {
return func(pods []v1.Pod) {}, nil
}

//rand.Shuffle(len(a), func(i, j int) { a[i], a[j] = a[j], a[i] })
switch sortingStrategy {
case "random":
return func(pods []v1.Pod) {
rand.Shuffle(len(pods), func(i, j int) { pods[i], pods[j] = pods[j], pods[i] })
}, nil
default:
return nil, errors.New("unknown pod sorting strategy")
}
Expand Down
12 changes: 12 additions & 0 deletions reaper/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"math/rand"
"os"
"testing"
"time"
Expand Down Expand Up @@ -285,6 +286,17 @@ func TestOptions(t *testing.T) {
_, err := podSortingStrategy()
assert.Error(t, err)
})
t.Run("random", func(t *testing.T) {
os.Clearenv()
os.Setenv(envPodSortingStrategy, "random")
sorter, err := podSortingStrategy()
assert.NotNil(t, sorter)
assert.NoError(t, err)
subject := testPodList()
rand.Seed(2) // magic seed to force switch
sorter(subject)
assert.NotEqual(t, testPodList(), subject)
})
})
}

Expand Down

0 comments on commit b10af6d

Please sign in to comment.