Skip to content

Commit 5922f36

Browse files
committed
[Refactor] Use RAYCLUSTER_DEFAULT_REQUEUE_SECONDS_ENV as timeout of status check in tests
1 parent 1594e88 commit 5922f36

File tree

4 files changed

+29
-9
lines changed

4 files changed

+29
-9
lines changed

ray-operator/controllers/ray/raycluster_controller.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"fmt"
66
"os"
77
"reflect"
8-
"strconv"
98
"strings"
109
"time"
1110

@@ -382,10 +381,9 @@ func (r *RayClusterReconciler) rayClusterReconcile(ctx context.Context, request
382381
// Unconditionally requeue after the number of seconds specified in the
383382
// environment variable RAYCLUSTER_DEFAULT_REQUEUE_SECONDS_ENV. If the
384383
// environment variable is not set, requeue after the default value.
385-
requeueAfterSeconds, err := strconv.Atoi(os.Getenv(utils.RAYCLUSTER_DEFAULT_REQUEUE_SECONDS_ENV))
386-
if err != nil {
384+
requeueAfterSeconds, fellback := utils.GetEnvInt(utils.RAYCLUSTER_DEFAULT_REQUEUE_SECONDS_ENV, utils.RAYCLUSTER_DEFAULT_REQUEUE_SECONDS)
385+
if fellback {
387386
r.Log.Info(fmt.Sprintf("Environment variable %s is not set, using default value of %d seconds", utils.RAYCLUSTER_DEFAULT_REQUEUE_SECONDS_ENV, utils.RAYCLUSTER_DEFAULT_REQUEUE_SECONDS), "cluster name", request.Name)
388-
requeueAfterSeconds = utils.RAYCLUSTER_DEFAULT_REQUEUE_SECONDS
389387
}
390388
r.Log.Info("Unconditional requeue after", "cluster name", request.Name, "seconds", requeueAfterSeconds)
391389
return ctrl.Result{RequeueAfter: time.Duration(requeueAfterSeconds) * time.Second}, nil

ray-operator/controllers/ray/raycluster_controller_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ var _ = Context("Inside the default namespace", func() {
205205
// We need to figure out the behavior. See https://github.com/ray-project/kuberay/issues/1736 for more details.
206206
Eventually(
207207
getClusterState(ctx, "default", myRayCluster.Name),
208-
time.Second*(utils.RAYCLUSTER_DEFAULT_REQUEUE_SECONDS+5), time.Millisecond*500).Should(Equal(rayv1.Ready))
208+
time.Second*(time.Duration(utils.MustGetEnvInt(utils.RAYCLUSTER_DEFAULT_REQUEUE_SECONDS_ENV)+5)), time.Millisecond*500).Should(Equal(rayv1.Ready))
209209
})
210210

211211
It("should re-create a deleted worker", func() {
@@ -311,7 +311,7 @@ var _ = Context("Inside the default namespace", func() {
311311
It("cluster's .status.state should be updated to 'suspended' shortly after all Pods are terminated", func() {
312312
Eventually(
313313
getClusterState(ctx, "default", myRayCluster.Name),
314-
time.Second*(utils.RAYCLUSTER_DEFAULT_REQUEUE_SECONDS+5), time.Millisecond*500).Should(Equal(rayv1.Suspended))
314+
time.Second*(time.Duration(utils.MustGetEnvInt(utils.RAYCLUSTER_DEFAULT_REQUEUE_SECONDS_ENV)+5)), time.Millisecond*500).Should(Equal(rayv1.Suspended))
315315
})
316316

317317
It("set suspend to false and then revert it to true before all Pods are running", func() {
@@ -363,7 +363,7 @@ var _ = Context("Inside the default namespace", func() {
363363
// RayCluster should be in Suspended state.
364364
Eventually(
365365
getClusterState(ctx, "default", myRayCluster.Name),
366-
time.Second*(utils.RAYCLUSTER_DEFAULT_REQUEUE_SECONDS+5), time.Millisecond*500).Should(Equal(rayv1.Suspended))
366+
time.Second*(time.Duration(utils.MustGetEnvInt(utils.RAYCLUSTER_DEFAULT_REQUEUE_SECONDS_ENV)+5)), time.Millisecond*500).Should(Equal(rayv1.Suspended))
367367
})
368368

369369
It("should run all head and worker pods if un-suspended", func() {
@@ -402,7 +402,7 @@ var _ = Context("Inside the default namespace", func() {
402402
It("cluster's .status.state should be updated back to 'ready' after being un-suspended", func() {
403403
Eventually(
404404
getClusterState(ctx, "default", myRayCluster.Name),
405-
time.Second*(utils.RAYCLUSTER_DEFAULT_REQUEUE_SECONDS+5), time.Millisecond*500).Should(Equal(rayv1.Ready))
405+
time.Second*(time.Duration(utils.MustGetEnvInt(utils.RAYCLUSTER_DEFAULT_REQUEUE_SECONDS_ENV)+5)), time.Millisecond*500).Should(Equal(rayv1.Ready))
406406
})
407407
})
408408
})

ray-operator/controllers/ray/rayjob_controller_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ var _ = Context("Inside the default namespace", func() {
270270
// The RayCluster.Status.State should be Ready.
271271
Eventually(
272272
getClusterState(ctx, "default", myRayCluster.Name),
273-
time.Second*(utils.RAYCLUSTER_DEFAULT_REQUEUE_SECONDS+5), time.Millisecond*500).Should(Equal(rayv1.Ready))
273+
time.Second*(time.Duration(utils.MustGetEnvInt(utils.RAYCLUSTER_DEFAULT_REQUEUE_SECONDS_ENV)+5)), time.Millisecond*500).Should(Equal(rayv1.Ready))
274274
})
275275

276276
It("Dashboard URL should be set", func() {

ray-operator/controllers/ray/utils/constant.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
package utils
22

3+
import (
4+
"fmt"
5+
"os"
6+
"strconv"
7+
)
8+
39
const (
410

511
// Default application name
@@ -143,3 +149,19 @@ const (
143149
HeadService ServiceType = "headService"
144150
ServingService ServiceType = "serveService"
145151
)
152+
153+
func GetEnvInt(name string, fallback int) (value int, fellback bool) {
154+
v, err := strconv.Atoi(os.Getenv(name))
155+
if err != nil {
156+
v = fallback
157+
}
158+
return v, err != nil
159+
}
160+
161+
func MustGetEnvInt(name string) (value int) {
162+
v, err := strconv.Atoi(os.Getenv(name))
163+
if err != nil {
164+
panic(fmt.Sprintf("env %s must be set: %v", name, err))
165+
}
166+
return v
167+
}

0 commit comments

Comments
 (0)