Skip to content

Commit

Permalink
Improve e2e helper updateAndWaitFor to avoid conflicts (#6069)
Browse files Browse the repository at this point in the history
Will retry updates on conflict rather than failing immediately

To fix failures like: https://github.com/projectcontour/contour/actions/runs/7460847887/job/20299759302

Signed-off-by: Sunjay Bhatia <[email protected]>
  • Loading branch information
sunjayBhatia authored Jan 11, 2024
1 parent d8931d4 commit 16fa671
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 25 deletions.
32 changes: 14 additions & 18 deletions test/e2e/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
Expand Down Expand Up @@ -185,26 +184,23 @@ func (e *Echo) DeployN(ns, name string, replicas int32) (func(), *appsv1.Deploym
}

func (e *Echo) ScaleAndWaitDeployment(name, ns string, replicas int32) {
deployment := &appsv1.Deployment{}
key := types.NamespacedName{
Namespace: ns,
Name: name,
deployment := &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: ns,
},
}

require.NoError(e.t, e.client.Get(context.TODO(), key, deployment))

deployment.Spec.Replicas = &replicas

updateAndWaitFor(e.t, e.client, deployment, func(d *appsv1.Deployment) bool {
err := e.client.Get(context.Background(), key, deployment)
if err != nil {
updateAndWaitFor(e.t, e.client, deployment,
func(d *appsv1.Deployment) {
d.Spec.Replicas = ref.To(replicas)
},
func(d *appsv1.Deployment) bool {
if d.Status.Replicas == replicas && d.Status.ReadyReplicas == replicas {
return true
}
return false
}
if deployment.Status.Replicas == replicas && deployment.Status.ReadyReplicas == replicas {
return true
}
return false
}, time.Second, time.Second*10)
}, time.Second, time.Second*10)
}

func (e *Echo) ListPodIPs(ns, name string) ([]string, error) {
Expand Down
18 changes: 11 additions & 7 deletions test/e2e/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import (
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
kubescheme "k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/util/retry"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/config"
"sigs.k8s.io/controller-runtime/pkg/log"
Expand Down Expand Up @@ -334,16 +335,19 @@ func createAndWaitFor[T client.Object](t require.TestingT, client client.Client,
return obj, true
}

func updateAndWaitFor[T client.Object](t require.TestingT, client client.Client, obj T, condition func(T) bool, interval, timeout time.Duration) (T, bool) {
require.NoError(t, client.Update(context.Background(), obj))
func updateAndWaitFor[T client.Object](t require.TestingT, cli client.Client, obj T, mutate func(T), condition func(T) bool, interval, timeout time.Duration) (T, bool) {
key := client.ObjectKeyFromObject(obj)

key := types.NamespacedName{
Namespace: obj.GetNamespace(),
Name: obj.GetName(),
}
require.NoError(t, retry.RetryOnConflict(retry.DefaultBackoff, func() error {
if err := cli.Get(context.TODO(), key, obj); err != nil {
return err
}
mutate(obj)
return cli.Update(context.Background(), obj)
}))

if err := wait.PollUntilContextTimeout(context.Background(), interval, timeout, true, func(ctx context.Context) (bool, error) {
if err := client.Get(ctx, key, obj); err != nil {
if err := cli.Get(ctx, key, obj); err != nil {
// if there was an error, we want to keep
// retrying, so just return false, not an
// error.
Expand Down

0 comments on commit 16fa671

Please sign in to comment.