Skip to content

Commit ba6141a

Browse files
authored
Merge pull request kubernetes#126405 from sttts/sttts-sync-informerfactory-start
Call non-blocking informerFactory.Start synchronously to avoid races
2 parents 250f7b5 + cd69335 commit ba6141a

File tree

20 files changed

+32
-20
lines changed

20 files changed

+32
-20
lines changed

pkg/controller/garbagecollector/garbagecollector_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ func setupGC(t *testing.T, config *restclient.Config) garbageCollector {
223223
t.Fatal(err)
224224
}
225225
stop := make(chan struct{})
226-
go sharedInformers.Start(stop)
226+
sharedInformers.Start(stop)
227227
return garbageCollector{gc, stop}
228228
}
229229

pkg/controller/validatingadmissionpolicystatus/controller_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ func TestTypeChecking(t *testing.T) {
113113
if err != nil {
114114
t.Fatalf("cannot create controller: %v", err)
115115
}
116-
go informerFactory.Start(ctx.Done())
116+
informerFactory.Start(ctx.Done())
117117
go controller.Run(ctx, 1)
118118
err = wait.PollUntilContextCancel(ctx, time.Second, false, func(ctx context.Context) (done bool, err error) {
119119
name := policy.Name

pkg/controller/volume/ephemeral/controller_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ func TestSyncHandler(t *testing.T) {
152152
ec, _ := c.(*ephemeralController)
153153

154154
// Ensure informers are up-to-date.
155-
go informerFactory.Start(ctx.Done())
155+
informerFactory.Start(ctx.Done())
156156
informerFactory.WaitForCacheSync(ctx.Done())
157157
cache.WaitForCacheSync(ctx.Done(), podInformer.Informer().HasSynced, pvcInformer.Informer().HasSynced)
158158

pkg/controlplane/controller/leaderelection/leaderelection_controller_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ import (
3232
"k8s.io/client-go/informers"
3333
"k8s.io/client-go/kubernetes/fake"
3434
"k8s.io/utils/ptr"
35-
36-
"k8s.io/client-go/tools/cache"
3735
)
3836

3937
func TestReconcileElectionStep(t *testing.T) {
@@ -339,7 +337,7 @@ func TestReconcileElectionStep(t *testing.T) {
339337
ctx := context.Background()
340338
client := fake.NewSimpleClientset()
341339
informerFactory := informers.NewSharedInformerFactory(client, 0)
342-
_ = informerFactory.Coordination().V1alpha1().LeaseCandidates().Lister()
340+
343341
controller, err := NewController(
344342
informerFactory.Coordination().V1().Leases(),
345343
informerFactory.Coordination().V1alpha1().LeaseCandidates(),
@@ -349,8 +347,7 @@ func TestReconcileElectionStep(t *testing.T) {
349347
if err != nil {
350348
t.Fatal(err)
351349
}
352-
go informerFactory.Start(ctx.Done())
353-
informerFactory.WaitForCacheSync(ctx.Done())
350+
354351
// Set up the fake client with the existing lease
355352
if tc.existingLease != nil {
356353
_, err = client.CoordinationV1().Leases(tc.existingLease.Namespace).Create(ctx, tc.existingLease, metav1.CreateOptions{})
@@ -366,7 +363,10 @@ func TestReconcileElectionStep(t *testing.T) {
366363
t.Fatal(err)
367364
}
368365
}
369-
cache.WaitForCacheSync(ctx.Done(), controller.leaseCandidateInformer.Informer().HasSynced)
366+
367+
informerFactory.Start(ctx.Done())
368+
informerFactory.WaitForCacheSync(ctx.Done())
369+
370370
requeue, err := controller.reconcileElectionStep(ctx, tc.leaseNN)
371371

372372
if (requeue != 0) != tc.expectedRequeue {
@@ -639,7 +639,7 @@ func TestController(t *testing.T) {
639639
t.Fatal(err)
640640
}
641641

642-
go informerFactory.Start(ctx.Done())
642+
informerFactory.Start(ctx.Done())
643643
go controller.Run(ctx, 1)
644644

645645
go func() {

pkg/controlplane/controller/leaderelection/leasecandidategc_controller_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func TestLeaseCandidateGCController(t *testing.T) {
115115
leaseCandidateInformer := informerFactory.Coordination().V1alpha1().LeaseCandidates()
116116
controller := NewLeaseCandidateGC(client, 10*time.Millisecond, leaseCandidateInformer)
117117

118-
go informerFactory.Start(ctx.Done())
118+
informerFactory.Start(ctx.Done())
119119
informerFactory.WaitForCacheSync(ctx.Done())
120120

121121
// Create lease candidates

pkg/proxy/config/api_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func TestNewServicesSourceApi_UpdatesAndMultipleServices(t *testing.T) {
6060

6161
serviceConfig := NewServiceConfig(ctx, sharedInformers.Core().V1().Services(), time.Minute)
6262
serviceConfig.RegisterEventHandler(handler)
63-
go sharedInformers.Start(stopCh)
63+
sharedInformers.Start(stopCh)
6464
go serviceConfig.Run(stopCh)
6565

6666
// Add the first service
@@ -141,7 +141,7 @@ func TestNewEndpointsSourceApi_UpdatesAndMultipleEndpoints(t *testing.T) {
141141

142142
endpointsliceConfig := NewEndpointSliceConfig(ctx, sharedInformers.Discovery().V1().EndpointSlices(), time.Minute)
143143
endpointsliceConfig.RegisterEventHandler(handler)
144-
go sharedInformers.Start(stopCh)
144+
sharedInformers.Start(stopCh)
145145
go endpointsliceConfig.Run(stopCh)
146146

147147
// Add the first endpoints

pkg/proxy/config/config_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ func TestNewServiceAddedAndNotified(t *testing.T) {
240240
config := NewServiceConfig(ctx, sharedInformers.Core().V1().Services(), time.Minute)
241241
handler := NewServiceHandlerMock()
242242
config.RegisterEventHandler(handler)
243-
go sharedInformers.Start(stopCh)
243+
sharedInformers.Start(stopCh)
244244
go config.Run(stopCh)
245245

246246
service := &v1.Service{
@@ -265,7 +265,7 @@ func TestServiceAddedRemovedSetAndNotified(t *testing.T) {
265265
config := NewServiceConfig(ctx, sharedInformers.Core().V1().Services(), time.Minute)
266266
handler := NewServiceHandlerMock()
267267
config.RegisterEventHandler(handler)
268-
go sharedInformers.Start(stopCh)
268+
sharedInformers.Start(stopCh)
269269
go config.Run(stopCh)
270270

271271
service1 := &v1.Service{
@@ -304,7 +304,7 @@ func TestNewServicesMultipleHandlersAddedAndNotified(t *testing.T) {
304304
handler2 := NewServiceHandlerMock()
305305
config.RegisterEventHandler(handler)
306306
config.RegisterEventHandler(handler2)
307-
go sharedInformers.Start(stopCh)
307+
sharedInformers.Start(stopCh)
308308
go config.Run(stopCh)
309309

310310
service1 := &v1.Service{
@@ -339,7 +339,7 @@ func TestNewEndpointsMultipleHandlersAddedAndNotified(t *testing.T) {
339339
handler2 := NewEndpointSliceHandlerMock()
340340
config.RegisterEventHandler(handler)
341341
config.RegisterEventHandler(handler2)
342-
go sharedInformers.Start(stopCh)
342+
sharedInformers.Start(stopCh)
343343
go config.Run(stopCh)
344344

345345
endpoints1 := &discoveryv1.EndpointSlice{
@@ -386,7 +386,7 @@ func TestNewEndpointsMultipleHandlersAddRemoveSetAndNotified(t *testing.T) {
386386
handler2 := NewEndpointSliceHandlerMock()
387387
config.RegisterEventHandler(handler)
388388
config.RegisterEventHandler(handler2)
389-
go sharedInformers.Start(stopCh)
389+
sharedInformers.Start(stopCh)
390390
go config.Run(stopCh)
391391

392392
endpoints1 := &discoveryv1.EndpointSlice{

staging/src/k8s.io/apiextensions-apiserver/examples/client-go/pkg/client/informers/externalversions/factory.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

staging/src/k8s.io/apiextensions-apiserver/pkg/client/informers/externalversions/factory.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

staging/src/k8s.io/client-go/informers/factory.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)