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

Prevent race conditions by syncing cache on new Allocation elements #780

Merged
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
6 changes: 5 additions & 1 deletion pkg/gameserverallocations/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ type Controller struct {
gameServerGetter getterv1alpha1.GameServersGetter
gameServerLister listerv1alpha1.GameServerLister
allocationPolicyLister multiclusterlisterv1alpha1.GameServerAllocationPolicyLister
allocationPolicySynced cache.InformerSynced
secretLister corev1lister.SecretLister
secretSynced cache.InformerSynced
stop <-chan struct{}
workerqueue *workerqueue.WorkerQueue
recorder record.EventRecorder
Expand Down Expand Up @@ -124,7 +126,9 @@ func NewController(apiServer *apiserver.APIServer,
gameServerGetter: agonesClient.StableV1alpha1(),
gameServerLister: agonesInformer.GameServers().Lister(),
allocationPolicyLister: agonesInformerFactory.Multicluster().V1alpha1().GameServerAllocationPolicies().Lister(),
allocationPolicySynced: agonesInformerFactory.Multicluster().V1alpha1().GameServerAllocationPolicies().Informer().HasSynced,
secretLister: kubeInformerFactory.Core().V1().Secrets().Lister(),
secretSynced: kubeInformerFactory.Core().V1().Secrets().Informer().HasSynced,
}
c.baseLogger = runtime.NewLoggerWithType(c)
c.workerqueue = workerqueue.NewWorkerQueue(c.syncGameServers, c.baseLogger, logfields.GameServerKey, stable.GroupName+".GameServerUpdateController")
Expand Down Expand Up @@ -177,7 +181,7 @@ func (c *Controller) registerAPIResource(api *apiserver.APIServer) {
func (c *Controller) Run(_ int, stop <-chan struct{}) error {
c.stop = stop
c.baseLogger.Info("Wait for cache sync")
if !cache.WaitForCacheSync(stop, c.gameServerSynced) {
if !cache.WaitForCacheSync(stop, c.gameServerSynced, c.secretSynced, c.allocationPolicySynced) {
return errors.New("failed to wait for caches to sync")
}

Expand Down
20 changes: 10 additions & 10 deletions pkg/gameserverallocations/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func TestControllerAllocationHandler(t *testing.T) {
return true, gs, nil
})

stop, cancel := agtesting.StartInformers(m)
stop, cancel := agtesting.StartInformers(m, c.gameServerSynced)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the controller has the WaitForCacheSync, is it really needed to call StartInformers that does the same thing (e.g. calling WaitForCacheSync)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The WaitforcacheSync is called in the controller .Run() command - so just instantiating the controller doesn't fire the WaitforcacheSync.

And in tests (I've found anyway), you often don't want everything that happens inside the Run() command to fire.

This was following the patterns of the sample controller

defer cancel()

// This call initializes the cache
Expand Down Expand Up @@ -254,7 +254,7 @@ func TestControllerAllocatePriority(t *testing.T) {
return true, gs, nil
})

stop, cancel := agtesting.StartInformers(m)
stop, cancel := agtesting.StartInformers(m, c.gameServerSynced)
defer cancel()

// This call initializes the cache
Expand Down Expand Up @@ -646,7 +646,7 @@ func TestControllerRunCacheSync(t *testing.T) {

m.AgonesClient.AddWatchReactor("gameservers", k8stesting.DefaultWatchReactor(watch, nil))

stop, cancel := agtesting.StartInformers(m)
stop, cancel := agtesting.StartInformers(m, c.gameServerSynced)
defer cancel()

assertCacheEntries := func(expected int) {
Expand Down Expand Up @@ -759,7 +759,7 @@ func TestMultiClusterAllocationFromLocal(t *testing.T) {
}, nil
})

stop, cancel := agtesting.StartInformers(m)
stop, cancel := agtesting.StartInformers(m, c.allocationPolicySynced, c.gameServerSynced)
defer cancel()

// This call initializes the cache
Expand Down Expand Up @@ -805,7 +805,7 @@ func TestMultiClusterAllocationFromLocal(t *testing.T) {
}, nil
})

stop, cancel := agtesting.StartInformers(m)
stop, cancel := agtesting.StartInformers(m, c.allocationPolicySynced, c.gameServerSynced)
defer cancel()

// This call initializes the cache
Expand Down Expand Up @@ -893,7 +893,7 @@ func TestMultiClusterAllocationFromRemote(t *testing.T) {
return true, getTestSecret(secretName, server.TLS.Certificates[0].Certificate[0]), nil
})

stop, cancel := agtesting.StartInformers(m)
stop, cancel := agtesting.StartInformers(m, c.allocationPolicySynced, c.secretSynced, c.gameServerSynced)
defer cancel()

// This call initializes the cache
Expand Down Expand Up @@ -968,7 +968,7 @@ func TestMultiClusterAllocationFromRemote(t *testing.T) {
return true, getTestSecret(secretName, server.TLS.Certificates[0].Certificate[0]), nil
})

stop, cancel := agtesting.StartInformers(m)
stop, cancel := agtesting.StartInformers(m, c.allocationPolicySynced, c.secretSynced, c.gameServerSynced)
defer cancel()

// This call initializes the cache
Expand Down Expand Up @@ -1023,7 +1023,7 @@ func TestCreateRestClientError(t *testing.T) {
}}}, nil
})

_, cancel := agtesting.StartInformers(m)
_, cancel := agtesting.StartInformers(m, c.secretSynced)
defer cancel()

_, err := c.createRemoteClusterRestClient(defaultNs, "secret-name")
Expand All @@ -1048,7 +1048,7 @@ func TestCreateRestClientError(t *testing.T) {
}}}, nil
})

_, cancel := agtesting.StartInformers(m)
_, cancel := agtesting.StartInformers(m, c.secretSynced)
defer cancel()

_, err := c.createRemoteClusterRestClient(defaultNs, "secret-name")
Expand All @@ -1063,7 +1063,7 @@ func TestCreateRestClientError(t *testing.T) {
return true, getTestSecret("secret-name", []byte("XXX")), nil
})

_, cancel := agtesting.StartInformers(m)
_, cancel := agtesting.StartInformers(m, c.secretSynced)
defer cancel()

_, err := c.createRemoteClusterRestClient(defaultNs, "secret-name")
Expand Down