Skip to content

Commit f897573

Browse files
committed
Fix review findings
1 parent a06e2f9 commit f897573

File tree

3 files changed

+41
-15
lines changed

3 files changed

+41
-15
lines changed

internal/util/client/client.go

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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

3637
var (
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
}

internal/util/client/client_test.go

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ import (
2626
"github.com/pkg/errors"
2727
apierrors "k8s.io/apimachinery/pkg/api/errors"
2828
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
29+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2930
"k8s.io/apimachinery/pkg/runtime"
3031
"k8s.io/apimachinery/pkg/runtime/schema"
32+
"k8s.io/apimachinery/pkg/util/wait"
3133
"k8s.io/utils/ptr"
3234
"sigs.k8s.io/controller-runtime/pkg/client"
3335
"sigs.k8s.io/controller-runtime/pkg/client/fake"
@@ -38,7 +40,12 @@ import (
3840

3941
func Test_WaitForCacheToBeUpToDate(t *testing.T) {
4042
// Modify timeout to speed up test
41-
waitForCacheTimeout = 1 * time.Second
43+
waitBackoff = wait.Backoff{
44+
Duration: 25 * time.Microsecond,
45+
Cap: 2 * time.Second,
46+
Factor: 1.2,
47+
Steps: 5,
48+
}
4249

4350
tests := []struct {
4451
name string
@@ -92,7 +99,7 @@ func Test_WaitForCacheToBeUpToDate(t *testing.T) {
9299
machine("machine-4", "4", nil),
93100
},
94101
clientResponses: map[client.ObjectKey][]client.Object{},
95-
wantErr: "failed to wait for up-to-date Machine objects in the cache after Machine creation: timed out after 1s: [" +
102+
wantErr: "failed to wait for up-to-date Machine objects in the cache after Machine creation: timed out: [" +
96103
"machines.cluster.x-k8s.io \"machine-1\" not found, " +
97104
"machines.cluster.x-k8s.io \"machine-2\" not found, " +
98105
"machines.cluster.x-k8s.io \"machine-3\" not found, " +
@@ -201,7 +208,12 @@ func Test_WaitForCacheToBeUpToDate(t *testing.T) {
201208

202209
func Test_WaitForObjectsToBeDeletedFromTheCache(t *testing.T) {
203210
// Modify timeout to speed up test
204-
waitForCacheTimeout = 1 * time.Second
211+
waitBackoff = wait.Backoff{
212+
Duration: 25 * time.Microsecond,
213+
Cap: 2 * time.Second,
214+
Factor: 1.2,
215+
Steps: 5,
216+
}
205217

206218
tests := []struct {
207219
name string
@@ -212,6 +224,13 @@ func Test_WaitForObjectsToBeDeletedFromTheCache(t *testing.T) {
212224
{
213225
name: "no-op if no objects are passed in",
214226
},
227+
{
228+
name: "error if Unstructured is used",
229+
objs: []client.Object{
230+
&unstructured.Unstructured{},
231+
},
232+
wantErr: "failed to wait for up-to-date objects in the cache after Machine deletion: Unstructured is not supported",
233+
},
215234
{
216235
name: "success if objects are going away instantly (not found)",
217236
objs: []client.Object{
@@ -306,7 +325,7 @@ func Test_WaitForObjectsToBeDeletedFromTheCache(t *testing.T) {
306325
machine("machine-4", "7", nil),
307326
},
308327
},
309-
wantErr: "failed to wait for up-to-date Machine objects in the cache after Machine deletion: timed out after 1s: [" +
328+
wantErr: "failed to wait for up-to-date Machine objects in the cache after Machine deletion: timed out: [" +
310329
"default/machine-1 still exists, " +
311330
"default/machine-2 still exists, " +
312331
"default/machine-3 still exists, " +

internal/util/client/metrics.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ var (
3030
waitDurationMetric = prometheus.NewHistogramVec(prometheus.HistogramOpts{
3131
Name: "capi_client_cache_wait_duration_seconds",
3232
Help: "Duration that we waited for the cache to be up-to-date in seconds, broken down by kind and status",
33-
Buckets: []float64{0.00001, 0.000025, 0.00005, 0.0001, 0.00025, 0.0005, 0.001, 0.005, 0.025, 0.05, 0.1, 0.2, 0.4, 0.6, 0.8, 1.0},
33+
Buckets: []float64{0.00001, 0.000025, 0.00005, 0.0001, 0.00025, 0.0005, 0.001, 0.005, 0.025, 0.05, 0.1, 0.2, 0.4, 0.6, 0.8, 1.0, 2.5, 5, 10},
3434
}, []string{"kind", "status"})
3535
)

0 commit comments

Comments
 (0)