From 9aa3aae99d2d50b29b67c72b40c23ff41f924348 Mon Sep 17 00:00:00 2001 From: Alvaro Aleman Date: Sun, 28 Apr 2024 18:26:18 +0200 Subject: [PATCH] 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 --- examples/workqueue/main.go | 10 +++++----- transport/cert_rotation.go | 7 +++++-- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/examples/workqueue/main.go b/examples/workqueue/main.go index e854840a..b8825dc1 100644 --- a/examples/workqueue/main.go +++ b/examples/workqueue/main.go @@ -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. diff --git a/transport/cert_rotation.go b/transport/cert_rotation.go index dc22b6ec..e76f6581 100644 --- a/transport/cert_rotation.go +++ b/transport/cert_rotation.go @@ -47,14 +47,17 @@ type dynamicClientCert struct { connDialer *connrotation.Dialer // queue only ever has one item, but it has nice error handling backoff/retry semantics - queue workqueue.RateLimitingInterface + queue workqueue.TypedRateLimitingInterface[string] } func certRotatingDialer(reload reloadFunc, dial utilnet.DialFunc) *dynamicClientCert { d := &dynamicClientCert{ reload: reload, connDialer: connrotation.NewDialer(connrotation.DialFunc(dial)), - queue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "DynamicClientCertificate"), + queue: workqueue.NewTypedRateLimitingQueueWithConfig( + workqueue.DefaultTypedControllerRateLimiter[string](), + workqueue.TypedRateLimitingQueueConfig[string]{Name: "DynamicClientCertificate"}, + ), } return d