Merge pull request #125154 from alvaroaleman/rename

Sample Controller: Use one variable for key throughout

Kubernetes-commit: a080057dabef91bdb185448ee4ffee2dd2400a21
This commit is contained in:
Kubernetes Publisher 2024-06-04 12:53:25 -07:00
commit 3bbd1b7ee2
3 changed files with 13 additions and 13 deletions

View File

@ -204,27 +204,27 @@ func (c *Controller) processNextWorkItem(ctx context.Context) bool {
} }
// We wrap this block in a func so we can defer c.workqueue.Done. // We wrap this block in a func so we can defer c.workqueue.Done.
err := func(key string) error { err := func() error {
// We call Done here so the workqueue knows we have finished // We call Done here so the workqueue knows we have finished
// processing this item. We also must remember to call Forget if we // processing this item. We also must remember to call Forget if we
// do not want this work item being re-queued. For example, we do // do not want this work item being re-queued. For example, we do
// not call Forget if a transient error occurs, instead the item is // not call Forget if a transient error occurs, instead the item is
// put back on the workqueue and attempted again after a back-off // put back on the workqueue and attempted again after a back-off
// period. // period.
defer c.workqueue.Done(key) defer c.workqueue.Done(obj)
// Run the syncHandler, passing it the namespace/name string of the // Run the syncHandler, passing it the namespace/name string of the
// Foo resource to be synced. // Foo resource to be synced.
if err := c.syncHandler(ctx, key); err != nil { if err := c.syncHandler(ctx, obj); err != nil {
// Put the item back on the workqueue to handle any transient errors. // Put the item back on the workqueue to handle any transient errors.
c.workqueue.AddRateLimited(key) c.workqueue.AddRateLimited(obj)
return fmt.Errorf("error syncing '%s': %s, requeuing", key, err.Error()) return fmt.Errorf("error syncing '%s': %s, requeuing", obj, err.Error())
} }
// Finally, if no error occurs we Forget this item so it does not // Finally, if no error occurs we Forget this item so it does not
// get queued again until another change happens. // get queued again until another change happens.
c.workqueue.Forget(obj) c.workqueue.Forget(obj)
logger.Info("Successfully synced", "resourceName", key) logger.Info("Successfully synced", "resourceName", obj)
return nil return nil
}(obj) }()
if err != nil { if err != nil {
utilruntime.HandleError(err) utilruntime.HandleError(err)

4
go.mod
View File

@ -7,8 +7,8 @@ go 1.22.0
require ( require (
golang.org/x/time v0.3.0 golang.org/x/time v0.3.0
k8s.io/api v0.0.0-20240531003526-c114cd746b5a k8s.io/api v0.0.0-20240531003526-c114cd746b5a
k8s.io/apimachinery v0.0.0-20240530220031-733a95eb52c3 k8s.io/apimachinery v0.0.0-20240603234208-703232ea6da4
k8s.io/client-go v0.0.0-20240531003927-52e5651101ed k8s.io/client-go v0.0.0-20240604003933-7c6e307a725f
k8s.io/code-generator v0.0.0-20240531004635-2ec7af029e9a k8s.io/code-generator v0.0.0-20240531004635-2ec7af029e9a
k8s.io/klog/v2 v2.120.1 k8s.io/klog/v2 v2.120.1
) )

8
go.sum
View File

@ -148,10 +148,10 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
k8s.io/api v0.0.0-20240531003526-c114cd746b5a h1:P8nQ3iz4FxeKN26Y8fz9qoEkUZy/DkQPtBmkVyriAS0= k8s.io/api v0.0.0-20240531003526-c114cd746b5a h1:P8nQ3iz4FxeKN26Y8fz9qoEkUZy/DkQPtBmkVyriAS0=
k8s.io/api v0.0.0-20240531003526-c114cd746b5a/go.mod h1:2VfykmUr8OqDStfcJWPvSW182MtoxAMeWsJHXwxqzXo= k8s.io/api v0.0.0-20240531003526-c114cd746b5a/go.mod h1:2VfykmUr8OqDStfcJWPvSW182MtoxAMeWsJHXwxqzXo=
k8s.io/apimachinery v0.0.0-20240530220031-733a95eb52c3 h1:wKdO12WPN63kX3M6aJQqn6IaacuD1sZw1CswMGfjmJk= k8s.io/apimachinery v0.0.0-20240603234208-703232ea6da4 h1:On2XjWF6loaEJ/GLZH+ZyovYHWfOZUgl9qUdC8Mn05o=
k8s.io/apimachinery v0.0.0-20240530220031-733a95eb52c3/go.mod h1:ClkKrTMwhmMjgsEHpX2w3F+YKj0ctDOaAxqL7clxG0U= k8s.io/apimachinery v0.0.0-20240603234208-703232ea6da4/go.mod h1:ClkKrTMwhmMjgsEHpX2w3F+YKj0ctDOaAxqL7clxG0U=
k8s.io/client-go v0.0.0-20240531003927-52e5651101ed h1:0caFt79cyI5V9Kx+Mgx+1Ew7Y7P5Xbs2JTg6Obt+UeQ= k8s.io/client-go v0.0.0-20240604003933-7c6e307a725f h1:7A1fNtVKUFjDecsZm2r/Ec3DtdVhuylFo6Hst/cD+HI=
k8s.io/client-go v0.0.0-20240531003927-52e5651101ed/go.mod h1:eKPERGqe84mnn/p9CKDZxsnjDJAMSlwr4L/be7kWLh0= k8s.io/client-go v0.0.0-20240604003933-7c6e307a725f/go.mod h1:5farkvv+2p9AXn5JduImtwwykMh2JcZQO9mlYXp2qEA=
k8s.io/code-generator v0.0.0-20240531004635-2ec7af029e9a h1:BEO+bjC1wFnvpUTswqNXY69EsVjFTsm+oJT+P+v7s6Q= k8s.io/code-generator v0.0.0-20240531004635-2ec7af029e9a h1:BEO+bjC1wFnvpUTswqNXY69EsVjFTsm+oJT+P+v7s6Q=
k8s.io/code-generator v0.0.0-20240531004635-2ec7af029e9a/go.mod h1:Atvd4cTf/ejISaIw7BTDEj+yW3kqFMoSa/R8rG5aPRU= k8s.io/code-generator v0.0.0-20240531004635-2ec7af029e9a/go.mod h1:Atvd4cTf/ejISaIw7BTDEj+yW3kqFMoSa/R8rG5aPRU=
k8s.io/gengo/v2 v2.0.0-20240228010128-51d4e06bde70 h1:NGrVE502P0s0/1hudf8zjgwki1X/TByhmAoILTarmzo= k8s.io/gengo/v2 v2.0.0-20240228010128-51d4e06bde70 h1:NGrVE502P0s0/1hudf8zjgwki1X/TByhmAoILTarmzo=