Skip to content

Commit

Permalink
Handle delete events when caching Ready GameServers for Allocation (#815
Browse files Browse the repository at this point in the history
)
  • Loading branch information
markmandel authored Jun 7, 2019
1 parent bcb0c85 commit 19c8814
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 7 deletions.
28 changes: 21 additions & 7 deletions pkg/gameserverallocations/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,16 +146,30 @@ func NewController(apiServer *apiserver.APIServer,
// only interested in if the old / new state was/is Ready
oldGs := oldObj.(*stablev1alpha1.GameServer)
newGs := newObj.(*stablev1alpha1.GameServer)
if oldGs.Status.State == stablev1alpha1.GameServerStateReady || newGs.Status.State == stablev1alpha1.GameServerStateReady {
if key, ok := c.getKey(newGs); ok {
if newGs.Status.State == stablev1alpha1.GameServerStateReady {
c.readyGameServers.Store(key, newGs)
} else {
c.readyGameServers.Delete(key)
}
key, ok := c.getKey(newGs)
if !ok {
return
}
if newGs.IsBeingDeleted() {
c.readyGameServers.Delete(key)
} else if oldGs.Status.State == stablev1alpha1.GameServerStateReady || newGs.Status.State == stablev1alpha1.GameServerStateReady {
if newGs.Status.State == stablev1alpha1.GameServerStateReady {
c.readyGameServers.Store(key, newGs)
} else {
c.readyGameServers.Delete(key)
}
}
},
DeleteFunc: func(obj interface{}) {
gs, ok := obj.(*stablev1alpha1.GameServer)
if !ok {
return
}
var key string
if key, ok = c.getKey(gs); ok {
c.readyGameServers.Delete(key)
}
},
})

return c
Expand Down
27 changes: 27 additions & 0 deletions pkg/gameserverallocations/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,33 @@ func TestControllerRunCacheSync(t *testing.T) {
watch.Modify(gs.DeepCopy())

assertCacheEntries(0)

// add back in ready gameserver
gs.Status.State = stablev1alpha1.GameServerStateReady
watch.Modify(gs.DeepCopy())
assertCacheEntries(0)
gs.Status.State = stablev1alpha1.GameServerStateReady
watch.Modify(gs.DeepCopy())
assertCacheEntries(1)

// update with deletion timestamp
n := metav1.Now()
deletedCopy := gs.DeepCopy()
deletedCopy.ObjectMeta.DeletionTimestamp = &n
watch.Modify(deletedCopy)
assertCacheEntries(0)

// add back in ready gameserver
gs.Status.State = stablev1alpha1.GameServerStateReady
watch.Modify(gs.DeepCopy())
assertCacheEntries(0)
gs.Status.State = stablev1alpha1.GameServerStateReady
watch.Modify(gs.DeepCopy())
assertCacheEntries(1)

// now actually delete it
watch.Delete(gs.DeepCopy())
assertCacheEntries(0)
}

func TestGetRandomlySelectedGS(t *testing.T) {
Expand Down

0 comments on commit 19c8814

Please sign in to comment.