processNextWorkItem will read a single work item off the workqueue and attempt to process it, by calling the syncHandler.
(ctx context.Context)
| 198 | // processNextWorkItem will read a single work item off the workqueue and |
| 199 | // attempt to process it, by calling the syncHandler. |
| 200 | func (c *Controller) processNextWorkItem(ctx context.Context) bool { |
| 201 | objRef, shutdown := c.workqueue.Get() |
| 202 | logger := klog.FromContext(ctx) |
| 203 | |
| 204 | if shutdown { |
| 205 | return false |
| 206 | } |
| 207 | |
| 208 | // We call Done at the end of this func so the workqueue knows we have |
| 209 | // finished processing this item. We also must remember to call Forget |
| 210 | // if we do not want this work item being re-queued. For example, we do |
| 211 | // not call Forget if a transient error occurs, instead the item is |
| 212 | // put back on the workqueue and attempted again after a back-off |
| 213 | // period. |
| 214 | defer c.workqueue.Done(objRef) |
| 215 | |
| 216 | // Run the syncHandler, passing it the structured reference to the object to be synced. |
| 217 | err := c.syncHandler(ctx, objRef) |
| 218 | if err == nil { |
| 219 | // If no error occurs then we Forget this item so it does not |
| 220 | // get queued again until another change happens. |
| 221 | c.workqueue.Forget(objRef) |
| 222 | logger.Info("Successfully synced", "objectName", objRef) |
| 223 | return true |
| 224 | } |
| 225 | // there was a failure so be sure to report it. This method allows for |
| 226 | // pluggable error handling which can be used for things like |
| 227 | // cluster-monitoring. |
| 228 | utilruntime.HandleErrorWithContext(ctx, err, "Error syncing; requeuing for later retry", "objectReference", objRef) |
| 229 | // since we failed, we should requeue the item to work on later. This |
| 230 | // method will add a backoff to avoid hotlooping on particular items |
| 231 | // (they're probably still not going to work right away) and overall |
| 232 | // controller protection (everything I've done is broken, this controller |
| 233 | // needs to calm down or it can starve other useful work) cases. |
| 234 | c.workqueue.AddRateLimited(objRef) |
| 235 | return true |
| 236 | } |
| 237 | |
| 238 | // syncHandler compares the actual state with the desired, and attempts to |
| 239 | // converge the two. It then updates the Status block of the Foo resource |
no test coverage detected