From 19c8814a70e3357c5f26a861e26693906af16d2a Mon Sep 17 00:00:00 2001 From: Mark Mandel Date: Fri, 7 Jun 2019 14:08:06 -0700 Subject: [PATCH] Handle delete events when caching Ready GameServers for Allocation (#815) --- pkg/gameserverallocations/controller.go | 28 +++++++++++++++----- pkg/gameserverallocations/controller_test.go | 27 +++++++++++++++++++ 2 files changed, 48 insertions(+), 7 deletions(-) diff --git a/pkg/gameserverallocations/controller.go b/pkg/gameserverallocations/controller.go index 5bf1824af9..f0ba2b1cc0 100644 --- a/pkg/gameserverallocations/controller.go +++ b/pkg/gameserverallocations/controller.go @@ -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 diff --git a/pkg/gameserverallocations/controller_test.go b/pkg/gameserverallocations/controller_test.go index 73a9989263..ccbb348dfc 100644 --- a/pkg/gameserverallocations/controller_test.go +++ b/pkg/gameserverallocations/controller_test.go @@ -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) {