@@ -45,6 +45,7 @@ import (
4545 autoscalingv2 "k8s.io/api/autoscaling/v2"
4646 batchv1 "k8s.io/api/batch/v1"
4747 corev1 "k8s.io/api/core/v1"
48+ "k8s.io/apimachinery/pkg/api/errors"
4849 "k8s.io/apimachinery/pkg/api/resource"
4950 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
5051 "k8s.io/apimachinery/pkg/util/wait"
@@ -1247,3 +1248,47 @@ func SetupTestEnvironment(image string, numNodes, gpusPerNode int, gpuTypes stri
12471248 gom .Expect (os .Setenv ("DEPLOY_HPA" , "false" )).To (gom .Succeed ()) // tests create their own HPAs if needed
12481249 gom .Expect (os .Setenv ("VLLM_SVC_ENABLED" , "false" )).To (gom .Succeed ()) // tests deploy their own Service
12491250}
1251+
1252+ // DeleteAllVariantAutoscalings deletes all VariantAutoscaling objects in a namespace
1253+ // and waits for them to be fully removed. This is useful for ensuring a clean test state.
1254+ // Returns the number of VAs that were deleted.
1255+ func DeleteAllVariantAutoscalings (ctx context.Context , crClient client.Client , namespace string ) (int , error ) {
1256+ vaList := & v1alpha1.VariantAutoscalingList {}
1257+ if err := crClient .List (ctx , vaList , client .InNamespace (namespace )); err != nil {
1258+ return 0 , fmt .Errorf ("failed to list VariantAutoscaling objects: %w" , err )
1259+ }
1260+
1261+ if len (vaList .Items ) == 0 {
1262+ return 0 , nil
1263+ }
1264+
1265+ deletedCount := 0
1266+ for i := range vaList .Items {
1267+ va := & vaList .Items [i ]
1268+ if err := crClient .Delete (ctx , va ); err != nil {
1269+ if errors .IsNotFound (err ) {
1270+ continue // Already deleted
1271+ }
1272+ return deletedCount , fmt .Errorf ("failed to delete VA %s: %w" , va .Name , err )
1273+ }
1274+ deletedCount ++
1275+ }
1276+
1277+ // Wait for all VAs to be fully deleted (with timeout)
1278+ waitCtx , cancel := context .WithTimeout (ctx , 30 * time .Second )
1279+ defer cancel ()
1280+
1281+ err := wait .PollUntilContextTimeout (waitCtx , 2 * time .Second , 30 * time .Second , true , func (ctx context.Context ) (bool , error ) {
1282+ remainingVAs := & v1alpha1.VariantAutoscalingList {}
1283+ if err := crClient .List (ctx , remainingVAs , client .InNamespace (namespace )); err != nil {
1284+ return false , err
1285+ }
1286+ return len (remainingVAs .Items ) == 0 , nil
1287+ })
1288+
1289+ if err != nil {
1290+ return deletedCount , fmt .Errorf ("timeout waiting for VAs to be deleted: %w" , err )
1291+ }
1292+
1293+ return deletedCount , nil
1294+ }
0 commit comments