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:
Alvaro Aleman 2024-04-28 18:26:18 +02:00 committed by Kubernetes Publisher
parent d3682da14f
commit 9aa3aae99d
2 changed files with 10 additions and 7 deletions

View File

@ -37,12 +37,12 @@ import (
// Controller demonstrates how to implement a controller with client-go. // Controller demonstrates how to implement a controller with client-go.
type Controller struct { type Controller struct {
indexer cache.Indexer indexer cache.Indexer
queue workqueue.RateLimitingInterface queue workqueue.TypedRateLimitingInterface[string]
informer cache.Controller informer cache.Controller
} }
// NewController creates a new 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{ return &Controller{
informer: informer, informer: informer,
indexer: indexer, indexer: indexer,
@ -62,7 +62,7 @@ func (c *Controller) processNextItem() bool {
defer c.queue.Done(key) defer c.queue.Done(key)
// Invoke the method containing the business logic // 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 // Handle the error if something went wrong during the execution of the business logic
c.handleErr(err, key) c.handleErr(err, key)
return true 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. // 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 { if err == nil {
// Forget about the #AddRateLimited history of the key on every successful synchronization. // 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 // 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()) podListWatcher := cache.NewListWatchFromClient(clientset.CoreV1().RESTClient(), "pods", v1.NamespaceDefault, fields.Everything())
// create the workqueue // 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 // 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. // whenever the cache is updated, the pod key is added to the workqueue.

View File

@ -47,14 +47,17 @@ type dynamicClientCert struct {
connDialer *connrotation.Dialer connDialer *connrotation.Dialer
// queue only ever has one item, but it has nice error handling backoff/retry semantics // 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 { func certRotatingDialer(reload reloadFunc, dial utilnet.DialFunc) *dynamicClientCert {
d := &dynamicClientCert{ d := &dynamicClientCert{
reload: reload, reload: reload,
connDialer: connrotation.NewDialer(connrotation.DialFunc(dial)), 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 return d