Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle delete events when caching Ready GameServers for Allocation #815

Merged
merged 2 commits into from
Jun 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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