MCPcopy Create free account
hub / github.com/kubernetes/sample-controller / Run

Method Run

controller.go:162–188  ·  view source on GitHub ↗

Run will set up the event handlers for types we are interested in, as well as syncing informer caches and starting workers. It will block until stopCh is closed, at which point it will shutdown the workqueue and wait for workers to finish processing their current work items.

(ctx context.Context, workers int)

Source from the content-addressed store, hash-verified

160// is closed, at which point it will shutdown the workqueue and wait for
161// workers to finish processing their current work items.
162func (c *Controller) Run(ctx context.Context, workers int) error {
163 defer utilruntime.HandleCrash()
164 defer c.workqueue.ShutDown()
165 logger := klog.FromContext(ctx)
166
167 // Start the informer factories to begin populating the informer caches
168 logger.Info("Starting Foo controller")
169
170 // Wait for the caches to be synced before starting workers
171 logger.Info("Waiting for informer caches to sync")
172
173 if ok := cache.WaitForCacheSync(ctx.Done(), c.deploymentsSynced, c.foosSynced); !ok {
174 return fmt.Errorf("failed to wait for caches to sync")
175 }
176
177 logger.Info("Starting workers", "count", workers)
178 // Launch two workers to process Foo resources
179 for i := 0; i < workers; i++ {
180 go wait.UntilWithContext(ctx, c.runWorker, time.Second)
181 }
182
183 logger.Info("Started workers")
184 <-ctx.Done()
185 logger.Info("Shutting down workers")
186
187 return nil
188}
189
190// runWorker is a long-running function that will continually call the
191// processNextWorkItem function in order to read and process a message on the

Callers 1

mainFunction · 0.95

Calls 1

WaitForCacheSyncMethod · 0.65

Tested by

no test coverage detected