refactor(apiextensions-apiserver): Make NonStructuralSchema controller context-aware

This commit is contained in:
Aditi Gupta
2025-09-23 22:09:03 -07:00
parent 203793fe65
commit a28e452b44
2 changed files with 18 additions and 11 deletions

View File

@@ -244,7 +244,7 @@ func (c completedConfig) New(delegationTarget genericapiserver.DelegationTarget)
go namingController.Run(hookContext.Done())
go establishingController.RunWithContext(hookContext)
go nonStructuralSchemaController.Run(5, hookContext.Done())
go nonStructuralSchemaController.RunWithContext(5, hookContext)
go apiApprovalController.Run(5, hookContext.Done())
go finalizingController.Run(5, hookContext.Done())

View File

@@ -48,7 +48,7 @@ type ConditionController struct {
crdSynced cache.InformerSynced
// To allow injection for testing.
syncFn func(key string) error
syncFn func(ctx context.Context, key string) error
queue workqueue.TypedRateLimitingInterface[string]
@@ -132,7 +132,7 @@ func calculateCondition(in *apiextensionsv1.CustomResourceDefinition) *apiextens
return cond
}
func (c *ConditionController) sync(key string) error {
func (c *ConditionController) sync(ctx context.Context, key string) error {
inCustomResourceDefinition, err := c.crdLister.Get(key)
if apierrors.IsNotFound(err) {
return nil
@@ -169,7 +169,7 @@ func (c *ConditionController) sync(key string) error {
apiextensionshelpers.SetCRDCondition(crd, *cond)
}
_, err = c.crdClient.CustomResourceDefinitions().UpdateStatus(context.TODO(), crd, metav1.UpdateOptions{})
_, err = c.crdClient.CustomResourceDefinitions().UpdateStatus(ctx, crd, metav1.UpdateOptions{})
if apierrors.IsNotFound(err) || apierrors.IsConflict(err) {
// deleted or changed in the meantime, we'll get called again
return nil
@@ -188,38 +188,45 @@ func (c *ConditionController) sync(key string) error {
}
// Run starts the controller.
//
//logcheck:context // RunWithContext should be used instead of Run in code which supports contextual logging.
func (c *ConditionController) Run(workers int, stopCh <-chan struct{}) {
c.RunWithContext(workers, wait.ContextForChannel(stopCh))
}
// RunWithContext is a context-aware version of Run.
func (c *ConditionController) RunWithContext(workers int, ctx context.Context) {
defer utilruntime.HandleCrash()
defer c.queue.ShutDown()
klog.Infof("Starting NonStructuralSchemaConditionController")
defer klog.Infof("Shutting down NonStructuralSchemaConditionController")
if !cache.WaitForCacheSync(stopCh, c.crdSynced) {
if !cache.WaitForCacheSync(ctx.Done(), c.crdSynced) {
return
}
for i := 0; i < workers; i++ {
go wait.Until(c.runWorker, time.Second, stopCh)
go wait.UntilWithContext(ctx, c.runWorker, time.Second)
}
<-stopCh
<-ctx.Done()
}
func (c *ConditionController) runWorker() {
for c.processNextWorkItem() {
func (c *ConditionController) runWorker(ctx context.Context) {
for c.processNextWorkItem(ctx) {
}
}
// processNextWorkItem deals with one key off the queue. It returns false when it's time to quit.
func (c *ConditionController) processNextWorkItem() bool {
func (c *ConditionController) processNextWorkItem(ctx context.Context) bool {
key, quit := c.queue.Get()
if quit {
return false
}
defer c.queue.Done(key)
err := c.syncFn(key)
err := c.syncFn(ctx, key)
if err == nil {
c.queue.Forget(key)
return true