Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
[skip ci]
  • Loading branch information
markmandel committed Mar 12, 2019
1 parent 7c86276 commit e55ee80
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
2 changes: 2 additions & 0 deletions pkg/gameserversets/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,8 @@ func computeReconciliationAction(gsSet *v1alpha1.GameServerSet, list []*v1alpha1
if deleteCount > 0 {
if gsSet.Spec.Scheduling == v1alpha1.Packed {
potentialDeletions = sortGameServersByLeastFullNodes(potentialDeletions, counts)
} else {
// TODO: sortGameServersByNewFirst
}

toDelete = append(toDelete, potentialDeletions[0:deleteCount]...)
Expand Down
16 changes: 15 additions & 1 deletion pkg/gameserversets/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func TestComputeReconciliationAction(t *testing.T) {
for _, tc := range cases {
t.Run(tc.desc, func(t *testing.T) {
gsSet := &v1alpha1.GameServerSet{}
gsSet.Spec.Scheduling = v1alpha1.Packed
gsSet.Spec.Scheduling = v1alpha1.Distributed

toAdd, toDelete, isPartial := computeReconciliationAction(gsSet, tc.list, map[string]gameservers.NodeCount{},
tc.targetReplicaCount, maxTestCreationsPerBatch, maxTestDeletionsPerBatch, maxTestPendingPerBatch)
Expand All @@ -171,6 +171,20 @@ func TestComputeReconciliationAction(t *testing.T) {
assert.Equal(t, tc.wantIsPartial, isPartial, "is partial reconciliation")
})
}

/*
TODO: make these tests work
t.Run("test packed scale down", func(t *testing.T) {
gsSet := &v1alpha1.GameServerSet{Spec: v1alpha1.GameServerSetSpec{Scheduling: v1alpha1.Packed}}
toAdd, toDelete, isPartial := computeReconciliationAction(gsSet, )
})
t.Run("test distributed scale down", func(t *testing.T) {
gsSet := &v1alpha1.GameServerSet{Spec: v1alpha1.GameServerSetSpec{Scheduling: v1alpha1.Packed}}
toAdd, toDelete, isPartial := computeReconciliationAction(gsSet, )
})*/
}

func TestComputeStatus(t *testing.T) {
Expand Down
12 changes: 12 additions & 0 deletions pkg/gameserversets/gameserversets.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ func sortGameServersByLeastFullNodes(list []*v1alpha1.GameServer, count map[stri
return list
}

// sortGameServersByNewFirst sorts by newest gameservers first, and returns them
func sortGameServersByNewFirst(list []*v1alpha1.GameServer) []*v1alpha1.GameServer {
sort.Slice(list, func(i, j int) bool {
a := list[i]
b := list[j]

return a.ObjectMeta.CreationTimestamp.Before(&b.ObjectMeta.CreationTimestamp)
})

return list
}

// ListGameServersByGameServerSetOwner lists the GameServers for a given GameServerSet
func ListGameServersByGameServerSetOwner(gameServerLister listerv1alpha1.GameServerLister,
gsSet *v1alpha1.GameServerSet) ([]*v1alpha1.GameServer, error) {
Expand Down
18 changes: 18 additions & 0 deletions pkg/gameserversets/gameserversets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package gameserversets
import (
"sort"
"testing"
"time"

"agones.dev/agones/pkg/apis/stable/v1alpha1"
"agones.dev/agones/pkg/gameservers"
Expand Down Expand Up @@ -49,6 +50,23 @@ func TestSortGameServersByLeastFullNodes(t *testing.T) {
assert.Equal(t, "g1", result[2].ObjectMeta.Name)
}

func TestSortGameServersByNewFirst(t *testing.T) {
now := metav1.Now()

list := []*v1alpha1.GameServer{
{ObjectMeta: metav1.ObjectMeta{Name: "g1", CreationTimestamp: metav1.Time{Time: now.Add(10 * time.Second)}}},
{ObjectMeta: metav1.ObjectMeta{Name: "g2", CreationTimestamp: now}},
{ObjectMeta: metav1.ObjectMeta{Name: "g3", CreationTimestamp: metav1.Time{Time: now.Add(30 * time.Second)}}},
}
l := len(list)

result := sortGameServersByNewFirst(list)
assert.Len(t, result, l)
assert.Equal(t, "g2", result[0].ObjectMeta.Name)
assert.Equal(t, "g1", result[1].ObjectMeta.Name)
assert.Equal(t, "g3", result[2].ObjectMeta.Name)
}

func TestListGameServersByGameServerSetOwner(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit e55ee80

Please sign in to comment.