@@ -25,6 +25,7 @@ import (
2525
2626 "github.com/pkg/errors"
2727 apierrors "k8s.io/apimachinery/pkg/api/errors"
28+ "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2829 kerrors "k8s.io/apimachinery/pkg/util/errors"
2930 "k8s.io/apimachinery/pkg/util/wait"
3031 "k8s.io/klog/v2"
@@ -34,13 +35,14 @@ import (
3435)
3536
3637var (
37- // waitForCacheTimeout is the timeout used when waiting for the cache to become up-to-date.
38- waitForCacheTimeout = 10 * time .Second
39-
40- // waitForCacheInterval is the timeout used when waiting for the cache to become up-to-date.
41- // This interval seems pretty low, but based on tests it's realistic that the cache is up-to-date
42- // that quickly.
43- waitForCacheInterval = 100 * time .Microsecond
38+ // waitBackoff is the timeout used when waiting for the cache to become up-to-date.
39+ // This adds up to ~ 10 seconds max wait duration.
40+ waitBackoff = wait.Backoff {
41+ Duration : 25 * time .Microsecond ,
42+ Cap : 2 * time .Second ,
43+ Factor : 1.2 ,
44+ Steps : 63 ,
45+ }
4446)
4547
4648// WaitForCacheToBeUpToDate waits until the cache is up-to-date in the sense of that the cache contains
@@ -127,10 +129,15 @@ func waitFor[T client.Object](ctx context.Context, c client.Client, action strin
127129 return nil
128130 }
129131
132+ var o any = objs [0 ]
133+ if _ , ok := o .(* unstructured.Unstructured ); ok {
134+ return errors .Errorf ("failed to wait for up-to-date objects in the cache after %s: Unstructured is not supported" , action )
135+ }
136+
130137 // All objects have the same type, so we can just take the GVK of the first object.
131138 objGVK , err := apiutil .GVKForObject (objs [0 ], c .Scheme ())
132139 if err != nil {
133- return err
140+ return errors . Wrapf ( err , "failed to wait for up-to-date objects in the cache after %s" , action )
134141 }
135142
136143 log := ctrl .LoggerFrom (ctx )
@@ -147,7 +154,7 @@ func waitFor[T client.Object](ctx context.Context, c client.Client, action strin
147154 now := time .Now ()
148155
149156 var pollErrs []error
150- err = wait .PollUntilContextTimeout (ctx , waitForCacheInterval , waitForCacheTimeout , true , func (ctx context.Context ) (bool , error ) {
157+ err = wait .ExponentialBackoffWithContext (ctx , waitBackoff , func (ctx context.Context ) (bool , error ) {
151158 pollErrs = nil
152159
153160 for _ , desiredObj := range desiredObjects {
@@ -177,7 +184,7 @@ func waitFor[T client.Object](ctx context.Context, c client.Client, action strin
177184 var errSuffix string
178185 if err != nil {
179186 if wait .Interrupted (err ) {
180- errSuffix = fmt . Sprintf ( ": timed out after %s" , waitForCacheTimeout )
187+ errSuffix = ": timed out"
181188 } else {
182189 errSuffix = fmt .Sprintf (": %s" , err .Error ())
183190 }
0 commit comments