Skip to content

Commit

Permalink
wait deployment to have available replicas
Browse files Browse the repository at this point in the history
Signed-off-by: shawnh2 <shawnhxh@outlook.com>
  • Loading branch information
shawnh2 committed Jul 18, 2024
1 parent c653e01 commit c011d49
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions test/e2e/tests/load_balancing.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@
package tests

import (
"context"
"fmt"
"testing"
"time"

"github.com/stretchr/testify/require"
appsv1 "k8s.io/api/apps/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
"sigs.k8s.io/controller-runtime/pkg/client"
gwapiv1 "sigs.k8s.io/gateway-api/apis/v1"
gwapiv1a2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
"sigs.k8s.io/gateway-api/conformance/utils/http"
Expand Down Expand Up @@ -48,6 +54,7 @@ var RoundRobinLoadBalancingTest = suite.ConformanceTest{
Name: gwapiv1.ObjectName(gwNN.Name),
}
BackendTrafficPolicyMustBeAccepted(t, suite.Client, types.NamespacedName{Name: "round-robin-lb-policy", Namespace: ns}, suite.ControllerName, ancestorRef)
WaitDeployment(t, suite.Client, types.NamespacedName{Name: "lb-backend-1", Namespace: ns}, 4)

gwAddr := kubernetes.GatewayAndHTTPRoutesMustBeAccepted(t, suite.Client, suite.TimeoutConfig, suite.ControllerName, kubernetes.NewGatewayRef(gwNN), routeNN)

Expand Down Expand Up @@ -113,6 +120,7 @@ var ConsistentHashSourceIPLoadBalancingTest = suite.ConformanceTest{
Name: gwapiv1.ObjectName(gwNN.Name),
}
BackendTrafficPolicyMustBeAccepted(t, suite.Client, types.NamespacedName{Name: "source-ip-lb-policy", Namespace: ns}, suite.ControllerName, ancestorRef)
WaitDeployment(t, suite.Client, types.NamespacedName{Name: "lb-backend-2", Namespace: ns}, 4)

gwAddr := kubernetes.GatewayAndHTTPRoutesMustBeAccepted(t, suite.Client, suite.TimeoutConfig, suite.ControllerName, kubernetes.NewGatewayRef(gwNN), routeNN)

Expand Down Expand Up @@ -175,6 +183,7 @@ var ConsistentHashHeaderLoadBalancingTest = suite.ConformanceTest{
Name: gwapiv1.ObjectName(gwNN.Name),
}
BackendTrafficPolicyMustBeAccepted(t, suite.Client, types.NamespacedName{Name: "header-lb-policy", Namespace: ns}, suite.ControllerName, ancestorRef)
WaitDeployment(t, suite.Client, types.NamespacedName{Name: "lb-backend-3", Namespace: ns}, 4)

gwAddr := kubernetes.GatewayAndHTTPRoutesMustBeAccepted(t, suite.Client, suite.TimeoutConfig, suite.ControllerName, kubernetes.NewGatewayRef(gwNN), routeNN)

Expand Down Expand Up @@ -223,3 +232,25 @@ var ConsistentHashHeaderLoadBalancingTest = suite.ConformanceTest{
})
},
}

// WaitDeployment waits deployment to have expected available replicas.
func WaitDeployment(t *testing.T, cli client.Client, name types.NamespacedName, expectReplicas int32) {
t.Helper()

waitErr := wait.PollUntilContextTimeout(context.Background(), 1*time.Second, 60*time.Second, true, func(ctx context.Context) (bool, error) {
deployment := new(appsv1.Deployment)
err := cli.Get(ctx, name, deployment)
if err != nil {
return false, fmt.Errorf("error fetching Deployment %s: %w", name.String(), err)
}

if deployment.Status.AvailableReplicas == expectReplicas {
return true, nil
}

t.Logf("Deployment not yet accepted: %s", name.String())
return false, nil
})

require.NoErrorf(t, waitErr, "error waiting for Deployment %s to be accepted", name.String())
}

0 comments on commit c011d49

Please sign in to comment.