Skip to content

Commit 347d3b0

Browse files
committed
Retry when rapidly changing a LoadBalancer Service
Our test may conflict with another controller despite patching with "force=true" and no "resourceVersion". When this happens, retry once in the test.
1 parent d052059 commit 347d3b0

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

internal/controller/postgrescluster/patroni_test.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@ import (
2626
"testing"
2727
"time"
2828

29+
"github.com/pkg/errors"
2930
"go.opentelemetry.io/otel"
3031
"gotest.tools/v3/assert"
3132
appsv1 "k8s.io/api/apps/v1"
3233
corev1 "k8s.io/api/core/v1"
34+
apierrors "k8s.io/apimachinery/pkg/api/errors"
3335
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3436
"k8s.io/apimachinery/pkg/types"
3537
"k8s.io/apimachinery/pkg/util/rand"
@@ -210,7 +212,17 @@ func TestReconcilePatroniLeaderLease(t *testing.T) {
210212
cluster.Spec.Service.Type = changeType
211213

212214
after, err := reconciler.reconcilePatroniLeaderLease(ctx, cluster)
213-
assert.NilError(t, err)
215+
216+
// LoadBalancers are provisioned by a separate controller that
217+
// updates the Service soon after creation. The API may return
218+
// a conflict error when we race to update it, even though we
219+
// don't send a resourceVersion in our payload. Retry.
220+
if apierrors.IsConflict(err) {
221+
t.Log("conflict:", err)
222+
after, err = reconciler.reconcilePatroniLeaderLease(ctx, cluster)
223+
}
224+
225+
assert.NilError(t, err, "\n%#v", errors.Unwrap(err))
214226
assert.Equal(t, after.Spec.ClusterIP, before.Spec.ClusterIP,
215227
"expected to keep the same ClusterIP")
216228
})

internal/controller/postgrescluster/pgbouncer_test.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@ import (
2121
"context"
2222
"testing"
2323

24+
"github.com/pkg/errors"
2425
"gotest.tools/v3/assert"
2526
appsv1 "k8s.io/api/apps/v1"
2627
corev1 "k8s.io/api/core/v1"
28+
apierrors "k8s.io/apimachinery/pkg/api/errors"
2729
"k8s.io/apimachinery/pkg/api/resource"
2830
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2931
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -246,7 +248,17 @@ func TestReconcilePGBouncerService(t *testing.T) {
246248
cluster.Spec.Proxy.PGBouncer.Service.Type = changeType
247249

248250
after, err := reconciler.reconcilePGBouncerService(ctx, cluster)
249-
assert.NilError(t, err)
251+
252+
// LoadBalancers are provisioned by a separate controller that
253+
// updates the Service soon after creation. The API may return
254+
// a conflict error when we race to update it, even though we
255+
// don't send a resourceVersion in our payload. Retry.
256+
if apierrors.IsConflict(err) {
257+
t.Log("conflict:", err)
258+
after, err = reconciler.reconcilePGBouncerService(ctx, cluster)
259+
}
260+
261+
assert.NilError(t, err, "\n%#v", errors.Unwrap(err))
250262
assert.Equal(t, after.Spec.ClusterIP, before.Spec.ClusterIP,
251263
"expected to keep the same ClusterIP")
252264
})

0 commit comments

Comments
 (0)