mirror of
https://github.com/kubernetes/client-go.git
synced 2025-09-04 08:35:10 +00:00
Use the generic/typed workqueue throughout
This change makes us use the generic workqueue throughout the project in order to improve type safety and readability of the code. Kubernetes-commit: 6d0ac8c561a7ac66c21e4ee7bd1976c2ecedbf32
This commit is contained in:
committed by
Kubernetes Publisher
parent
d3682da14f
commit
9aa3aae99d
@@ -37,12 +37,12 @@ import (
|
||||
// Controller demonstrates how to implement a controller with client-go.
|
||||
type Controller struct {
|
||||
indexer cache.Indexer
|
||||
queue workqueue.RateLimitingInterface
|
||||
queue workqueue.TypedRateLimitingInterface[string]
|
||||
informer cache.Controller
|
||||
}
|
||||
|
||||
// NewController creates a new Controller.
|
||||
func NewController(queue workqueue.RateLimitingInterface, indexer cache.Indexer, informer cache.Controller) *Controller {
|
||||
func NewController(queue workqueue.TypedRateLimitingInterface[string], indexer cache.Indexer, informer cache.Controller) *Controller {
|
||||
return &Controller{
|
||||
informer: informer,
|
||||
indexer: indexer,
|
||||
@@ -62,7 +62,7 @@ func (c *Controller) processNextItem() bool {
|
||||
defer c.queue.Done(key)
|
||||
|
||||
// Invoke the method containing the business logic
|
||||
err := c.syncToStdout(key.(string))
|
||||
err := c.syncToStdout(key)
|
||||
// Handle the error if something went wrong during the execution of the business logic
|
||||
c.handleErr(err, key)
|
||||
return true
|
||||
@@ -90,7 +90,7 @@ func (c *Controller) syncToStdout(key string) error {
|
||||
}
|
||||
|
||||
// handleErr checks if an error happened and makes sure we will retry later.
|
||||
func (c *Controller) handleErr(err error, key interface{}) {
|
||||
func (c *Controller) handleErr(err error, key string) {
|
||||
if err == nil {
|
||||
// Forget about the #AddRateLimited history of the key on every successful synchronization.
|
||||
// This ensures that future processing of updates for this key is not delayed because of
|
||||
@@ -168,7 +168,7 @@ func main() {
|
||||
podListWatcher := cache.NewListWatchFromClient(clientset.CoreV1().RESTClient(), "pods", v1.NamespaceDefault, fields.Everything())
|
||||
|
||||
// create the workqueue
|
||||
queue := workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter())
|
||||
queue := workqueue.NewTypedRateLimitingQueue(workqueue.DefaultTypedControllerRateLimiter[string]())
|
||||
|
||||
// Bind the workqueue to a cache with the help of an informer. This way we make sure that
|
||||
// whenever the cache is updated, the pod key is added to the workqueue.
|
||||
|
Reference in New Issue
Block a user