Skip to content

Commit bb350f7

Browse files
authored
Merge pull request kubernetes#125661 from mjudeikis/mjudeikis/poststarthookctx.stopch.cleanup
Clean deprecated context.StopCh
2 parents 1bb62cd + b8ae868 commit bb350f7

File tree

7 files changed

+39
-61
lines changed

7 files changed

+39
-61
lines changed

pkg/controlplane/apiserver/server.go

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2727
"k8s.io/apimachinery/pkg/util/runtime"
2828
"k8s.io/apimachinery/pkg/util/uuid"
29-
"k8s.io/apimachinery/pkg/util/wait"
3029
apiserverfeatures "k8s.io/apiserver/pkg/features"
3130
peerreconcilers "k8s.io/apiserver/pkg/reconcilers"
3231
genericregistry "k8s.io/apiserver/pkg/registry/generic"
@@ -136,7 +135,7 @@ func (c completedConfig) New(name string, delegationTarget genericapiserver.Dele
136135
}
137136
if len(c.SystemNamespaces) > 0 {
138137
s.GenericAPIServer.AddPostStartHookOrDie("start-system-namespaces-controller", func(hookContext genericapiserver.PostStartHookContext) error {
139-
go systemnamespaces.NewController(c.SystemNamespaces, client, s.VersionedInformers.Core().V1().Namespaces()).Run(hookContext.StopCh)
138+
go systemnamespaces.NewController(c.SystemNamespaces, client, s.VersionedInformers.Core().V1().Namespaces()).Run(hookContext.Done())
140139
return nil
141140
})
142141
}
@@ -156,7 +155,7 @@ func (c completedConfig) New(name string, delegationTarget genericapiserver.Dele
156155
client)
157156
s.GenericAPIServer.AddPostStartHookOrDie("peer-endpoint-reconciler-controller",
158157
func(hookContext genericapiserver.PostStartHookContext) error {
159-
peerEndpointCtrl.Start(hookContext.StopCh)
158+
peerEndpointCtrl.Start(hookContext.Done())
160159
return nil
161160
})
162161
s.GenericAPIServer.AddPreShutdownHookOrDie("peer-endpoint-reconciler-controller",
@@ -166,51 +165,42 @@ func (c completedConfig) New(name string, delegationTarget genericapiserver.Dele
166165
})
167166
if c.Extra.PeerProxy != nil {
168167
s.GenericAPIServer.AddPostStartHookOrDie("unknown-version-proxy-filter", func(context genericapiserver.PostStartHookContext) error {
169-
err := c.Extra.PeerProxy.WaitForCacheSync(context.StopCh)
168+
err := c.Extra.PeerProxy.WaitForCacheSync(context.Done())
170169
return err
171170
})
172171
}
173172
}
174173

175174
s.GenericAPIServer.AddPostStartHookOrDie("start-cluster-authentication-info-controller", func(hookContext genericapiserver.PostStartHookContext) error {
176175
controller := clusterauthenticationtrust.NewClusterAuthenticationTrustController(s.ClusterAuthenticationInfo, client)
177-
178-
// generate a context from stopCh. This is to avoid modifying files which are relying on apiserver
179-
// TODO: See if we can pass ctx to the current method
180-
ctx := wait.ContextForChannel(hookContext.StopCh)
181-
182176
// prime values and start listeners
183177
if s.ClusterAuthenticationInfo.ClientCA != nil {
184178
s.ClusterAuthenticationInfo.ClientCA.AddListener(controller)
185179
if controller, ok := s.ClusterAuthenticationInfo.ClientCA.(dynamiccertificates.ControllerRunner); ok {
186180
// runonce to be sure that we have a value.
187-
if err := controller.RunOnce(ctx); err != nil {
181+
if err := controller.RunOnce(hookContext); err != nil {
188182
runtime.HandleError(err)
189183
}
190-
go controller.Run(ctx, 1)
184+
go controller.Run(hookContext, 1)
191185
}
192186
}
193187
if s.ClusterAuthenticationInfo.RequestHeaderCA != nil {
194188
s.ClusterAuthenticationInfo.RequestHeaderCA.AddListener(controller)
195189
if controller, ok := s.ClusterAuthenticationInfo.RequestHeaderCA.(dynamiccertificates.ControllerRunner); ok {
196190
// runonce to be sure that we have a value.
197-
if err := controller.RunOnce(ctx); err != nil {
191+
if err := controller.RunOnce(hookContext); err != nil {
198192
runtime.HandleError(err)
199193
}
200-
go controller.Run(ctx, 1)
194+
go controller.Run(hookContext, 1)
201195
}
202196
}
203197

204-
go controller.Run(ctx, 1)
198+
go controller.Run(hookContext, 1)
205199
return nil
206200
})
207201

208202
if utilfeature.DefaultFeatureGate.Enabled(apiserverfeatures.APIServerIdentity) {
209203
s.GenericAPIServer.AddPostStartHookOrDie("start-kube-apiserver-identity-lease-controller", func(hookContext genericapiserver.PostStartHookContext) error {
210-
// generate a context from stopCh. This is to avoid modifying files which are relying on apiserver
211-
// TODO: See if we can pass ctx to the current method
212-
ctx := wait.ContextForChannel(hookContext.StopCh)
213-
214204
leaseName := s.GenericAPIServer.APIServerID
215205
holderIdentity := s.GenericAPIServer.APIServerID + "_" + string(uuid.NewUUID())
216206

@@ -227,7 +217,7 @@ func (c completedConfig) New(name string, delegationTarget genericapiserver.Dele
227217
metav1.NamespaceSystem,
228218
// TODO: receive identity label value as a parameter when post start hook is moved to generic apiserver.
229219
labelAPIServerHeartbeatFunc(name, peeraddress))
230-
go controller.Run(ctx)
220+
go controller.Run(hookContext)
231221
return nil
232222
})
233223
// TODO: move this into generic apiserver and make the lease identity value configurable
@@ -237,7 +227,7 @@ func (c completedConfig) New(name string, delegationTarget genericapiserver.Dele
237227
IdentityLeaseGCPeriod,
238228
metav1.NamespaceSystem,
239229
IdentityLeaseComponentLabelKey+"="+name,
240-
).Run(hookContext.StopCh)
230+
).Run(hookContext.Done())
241231
return nil
242232
})
243233
}
@@ -247,7 +237,7 @@ func (c completedConfig) New(name string, delegationTarget genericapiserver.Dele
247237
}
248238

249239
s.GenericAPIServer.AddPostStartHookOrDie("start-legacy-token-tracking-controller", func(hookContext genericapiserver.PostStartHookContext) error {
250-
go legacytokentracking.NewController(client).Run(hookContext.StopCh)
240+
go legacytokentracking.NewController(client).Run(hookContext.Done())
251241
return nil
252242
})
253243

pkg/controlplane/instance.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ func (c CompletedConfig) New(delegationTarget genericapiserver.DelegationTarget)
349349
KubernetesServiceNodePort: c.Extra.KubernetesServiceNodePort,
350350
}, client, c.ControlPlane.Extra.VersionedInformers.Core().V1().Services())
351351
s.ControlPlane.GenericAPIServer.AddPostStartHookOrDie("bootstrap-controller", func(hookContext genericapiserver.PostStartHookContext) error {
352-
kubernetesServiceCtrl.Start(hookContext.StopCh)
352+
kubernetesServiceCtrl.Start(hookContext.Done())
353353
return nil
354354
})
355355
s.ControlPlane.GenericAPIServer.AddPreShutdownHookOrDie("stop-kubernetes-service-controller", func() error {

pkg/registry/core/rest/storage_core.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ func (p *legacyProvider) PostStartHook() (string, genericapiserver.PostStartHook
514514
runner.Start()
515515
go func() {
516516
defer runner.Stop()
517-
<-context.StopCh
517+
<-context.Done()
518518
}()
519519

520520
// For backward compatibility, we ensure that if we never are able

pkg/registry/flowcontrol/rest/storage_flowcontrol.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ func (bce *bootstrapConfigurationEnsurer) ensureAPFBootstrapConfiguration(hookCo
145145
err = func() error {
146146
// get a derived context that gets cancelled after 5m or
147147
// when the StopCh gets closed, whichever happens first.
148-
ctx, cancel := contextFromChannelAndMaxWaitDuration(hookContext.StopCh, 5*time.Minute)
148+
ctx, cancel := contextFromChannelAndMaxWaitDuration(hookContext.Done(), 5*time.Minute)
149149
defer cancel()
150150

151151
if !cache.WaitForCacheSync(ctx.Done(), bce.informersSynced...) {
@@ -174,16 +174,15 @@ func (bce *bootstrapConfigurationEnsurer) ensureAPFBootstrapConfiguration(hookCo
174174
// we have successfully initialized the bootstrap configuration, now we
175175
// spin up a goroutine which reconciles the bootstrap configuration periodically.
176176
go func() {
177-
ctx := wait.ContextForChannel(hookContext.StopCh)
178177
wait.PollImmediateUntil(
179178
time.Minute,
180179
func() (bool, error) {
181-
if err := ensure(ctx, clientset, bce.fsLister, bce.plcLister); err != nil {
180+
if err := ensure(hookContext, clientset, bce.fsLister, bce.plcLister); err != nil {
182181
klog.ErrorS(err, "APF bootstrap ensurer ran into error, will retry later")
183182
}
184183
// always auto update both suggested and mandatory configuration
185184
return false, nil
186-
}, hookContext.StopCh)
185+
}, hookContext.Done())
187186
klog.Info("APF bootstrap ensurer is exiting")
188187
}()
189188

staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/apiserver.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ func (c completedConfig) New(delegationTarget genericapiserver.DelegationTarget)
220220
)
221221

222222
s.GenericAPIServer.AddPostStartHookOrDie("start-apiextensions-informers", func(context genericapiserver.PostStartHookContext) error {
223-
s.Informers.Start(context.StopCh)
223+
s.Informers.Start(context.Done())
224224
return nil
225225
})
226226
s.GenericAPIServer.AddPostStartHookOrDie("start-apiextensions-controllers", func(context genericapiserver.PostStartHookContext) error {
@@ -231,20 +231,20 @@ func (c completedConfig) New(delegationTarget genericapiserver.DelegationTarget)
231231
if s.GenericAPIServer.StaticOpenAPISpec != nil {
232232
if s.GenericAPIServer.OpenAPIVersionedService != nil {
233233
openapiController := openapicontroller.NewController(s.Informers.Apiextensions().V1().CustomResourceDefinitions())
234-
go openapiController.Run(s.GenericAPIServer.StaticOpenAPISpec, s.GenericAPIServer.OpenAPIVersionedService, context.StopCh)
234+
go openapiController.Run(s.GenericAPIServer.StaticOpenAPISpec, s.GenericAPIServer.OpenAPIVersionedService, context.Done())
235235
}
236236

237237
if s.GenericAPIServer.OpenAPIV3VersionedService != nil {
238238
openapiv3Controller := openapiv3controller.NewController(s.Informers.Apiextensions().V1().CustomResourceDefinitions())
239-
go openapiv3Controller.Run(s.GenericAPIServer.OpenAPIV3VersionedService, context.StopCh)
239+
go openapiv3Controller.Run(s.GenericAPIServer.OpenAPIV3VersionedService, context.Done())
240240
}
241241
}
242242

243-
go namingController.Run(context.StopCh)
244-
go establishingController.Run(context.StopCh)
245-
go nonStructuralSchemaController.Run(5, context.StopCh)
246-
go apiApprovalController.Run(5, context.StopCh)
247-
go finalizingController.Run(5, context.StopCh)
243+
go namingController.Run(context.Done())
244+
go establishingController.Run(context.Done())
245+
go nonStructuralSchemaController.Run(5, context.Done())
246+
go apiApprovalController.Run(5, context.Done())
247+
go finalizingController.Run(5, context.Done())
248248

249249
discoverySyncedCh := make(chan struct{})
250250
go discoveryController.Run(context.StopCh, discoverySyncedCh)
@@ -265,7 +265,7 @@ func (c completedConfig) New(delegationTarget genericapiserver.DelegationTarget)
265265
return true, nil
266266
}
267267
return false, nil
268-
}, context.StopCh)
268+
}, context.Done())
269269
})
270270

271271
return s, nil

staging/src/k8s.io/kube-aggregator/pkg/apiserver/apiserver.go

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -297,25 +297,14 @@ func (c completedConfig) NewWithDelegate(delegationTarget genericapiserver.Deleg
297297
}
298298
// We are passing the context to ProxyCerts.RunOnce as it needs to implement RunOnce(ctx) however the
299299
// context is not used at all. So passing a empty context shouldn't be a problem
300-
ctx := context.TODO()
301-
if err := aggregatorProxyCerts.RunOnce(ctx); err != nil {
300+
if err := aggregatorProxyCerts.RunOnce(context.Background()); err != nil {
302301
return nil, err
303302
}
304303
aggregatorProxyCerts.AddListener(apiserviceRegistrationController)
305304
s.proxyCurrentCertKeyContent = aggregatorProxyCerts.CurrentCertKeyContent
306305

307306
s.GenericAPIServer.AddPostStartHookOrDie("aggregator-reload-proxy-client-cert", func(postStartHookContext genericapiserver.PostStartHookContext) error {
308-
// generate a context from stopCh. This is to avoid modifying files which are relying on apiserver
309-
// TODO: See if we can pass ctx to the current method
310-
ctx, cancel := context.WithCancel(context.Background())
311-
go func() {
312-
select {
313-
case <-postStartHookContext.StopCh:
314-
cancel() // stopCh closed, so cancel our context
315-
case <-ctx.Done():
316-
}
317-
}()
318-
go aggregatorProxyCerts.Run(ctx, 1)
307+
go aggregatorProxyCerts.Run(postStartHookContext, 1)
319308
return nil
320309
})
321310
}
@@ -373,9 +362,9 @@ func (c completedConfig) NewWithDelegate(delegationTarget genericapiserver.Deleg
373362
}
374363

375364
s.GenericAPIServer.AddPostStartHookOrDie("apiservice-registration-controller", func(context genericapiserver.PostStartHookContext) error {
376-
go apiserviceRegistrationController.Run(context.StopCh, apiServiceRegistrationControllerInitiated)
365+
go apiserviceRegistrationController.Run(context.Done(), apiServiceRegistrationControllerInitiated)
377366
select {
378-
case <-context.StopCh:
367+
case <-context.Done():
379368
case <-apiServiceRegistrationControllerInitiated:
380369
}
381370

@@ -394,7 +383,7 @@ func (c completedConfig) NewWithDelegate(delegationTarget genericapiserver.Deleg
394383
// Discovery aggregation depends on the apiservice registration controller
395384
// having the full list of APIServices already synced
396385
select {
397-
case <-context.StopCh:
386+
case <-context.Done():
398387
return nil
399388
// Context cancelled, should abort/clean goroutines
400389
case <-apiServiceRegistrationControllerInitiated:
@@ -405,10 +394,10 @@ func (c completedConfig) NewWithDelegate(delegationTarget genericapiserver.Deleg
405394
// When discovery is ready, all APIServices will be present, with APIServices
406395
// that have not successfully synced discovery to be present but marked as Stale.
407396
discoverySyncedCh := make(chan struct{})
408-
go s.discoveryAggregationController.Run(context.StopCh, discoverySyncedCh)
397+
go s.discoveryAggregationController.Run(context.Done(), discoverySyncedCh)
409398

410399
select {
411-
case <-context.StopCh:
400+
case <-context.Done():
412401
return nil
413402
// Context cancelled, should abort/clean goroutines
414403
case <-discoverySyncedCh:
@@ -440,7 +429,7 @@ func (c completedConfig) NewWithDelegate(delegationTarget genericapiserver.Deleg
440429
return false, err
441430
}
442431
return true, nil
443-
}, hookContext.StopCh); err != nil {
432+
}, hookContext.Done()); err != nil {
444433
return fmt.Errorf("failed to wait for apiserver-identity lease %s to be created: %v",
445434
s.GenericAPIServer.APIServerID, err)
446435
}
@@ -456,14 +445,14 @@ func (c completedConfig) NewWithDelegate(delegationTarget genericapiserver.Deleg
456445
// to register all built-in resources when the generic apiservers install APIs.
457446
s.GenericAPIServer.StorageVersionManager.UpdateStorageVersions(hookContext.LoopbackClientConfig, s.GenericAPIServer.APIServerID)
458447
return false, nil
459-
}, hookContext.StopCh)
448+
}, hookContext.Done())
460449
// Once the storage version updater finishes the first round of update,
461450
// the PostStartHook will return to unblock /healthz. The handler chain
462451
// won't block write requests anymore. Check every second since it's not
463452
// expensive.
464453
wait.PollImmediateUntil(1*time.Second, func() (bool, error) {
465454
return s.GenericAPIServer.StorageVersionManager.Completed(), nil
466-
}, hookContext.StopCh)
455+
}, hookContext.Done())
467456
return nil
468457
})
469458
}
@@ -477,14 +466,14 @@ func (s *APIAggregator) PrepareRun() (preparedAPIAggregator, error) {
477466
// add post start hook before generic PrepareRun in order to be before /healthz installation
478467
if s.openAPIConfig != nil {
479468
s.GenericAPIServer.AddPostStartHookOrDie("apiservice-openapi-controller", func(context genericapiserver.PostStartHookContext) error {
480-
go s.openAPIAggregationController.Run(context.StopCh)
469+
go s.openAPIAggregationController.Run(context.Done())
481470
return nil
482471
})
483472
}
484473

485474
if s.openAPIV3Config != nil {
486475
s.GenericAPIServer.AddPostStartHookOrDie("apiservice-openapiv3-controller", func(context genericapiserver.PostStartHookContext) error {
487-
go s.openAPIV3AggregationController.Run(context.StopCh)
476+
go s.openAPIV3AggregationController.Run(context.Done())
488477
return nil
489478
})
490479
}

staging/src/k8s.io/sample-apiserver/pkg/cmd/server/start.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,8 @@ func (o WardleServerOptions) RunWardleServer(ctx context.Context) error {
204204
}
205205

206206
server.GenericAPIServer.AddPostStartHookOrDie("start-sample-server-informers", func(context genericapiserver.PostStartHookContext) error {
207-
config.GenericConfig.SharedInformerFactory.Start(context.StopCh)
208-
o.SharedInformerFactory.Start(context.StopCh)
207+
config.GenericConfig.SharedInformerFactory.Start(context.Done())
208+
o.SharedInformerFactory.Start(context.Done())
209209
return nil
210210
})
211211

0 commit comments

Comments
 (0)