Skip to content

Commit 75e32a1

Browse files
committed
feat: add ValidateRayClusterSpec in raycluster webhook, copy necessary func and variables to /ray/v1
1 parent 4f5b6a4 commit 75e32a1

File tree

4 files changed

+46
-15
lines changed

4 files changed

+46
-15
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package v1
2+
3+
// In KubeRay, the Ray container must be the first application container in a head or worker Pod.
4+
const RayContainerIndex = 0
5+
6+
// Use as container env variable
7+
const RAY_REDIS_ADDRESS = "RAY_REDIS_ADDRESS"
8+
9+
// Ray GCS FT related annotations
10+
const RayFTEnabledAnnotationKey = "ray.io/ft-enabled"

ray-operator/apis/ray/v1/raycluster_webhook.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package v1
22

33
import (
4+
"fmt"
45
"regexp"
56

67
apierrors "k8s.io/apimachinery/pkg/api/errors"
@@ -19,8 +20,6 @@ var (
1920
nameRegex, _ = regexp.Compile("^[a-z]([-a-z0-9]*[a-z0-9])?$")
2021
)
2122

22-
const RayFTEnabledAnnotationKey = "ray.io/ft-enabled"
23-
2423
func (r *RayCluster) SetupWebhookWithManager(mgr ctrl.Manager) error {
2524
return ctrl.NewWebhookManagedBy(mgr).
2625
For(r).
@@ -96,13 +95,22 @@ func (r *RayCluster) validateWorkerGroups() *field.Error {
9695

9796
func (r *RayCluster) ValidateRayClusterSpec() *field.Error {
9897
if r.Annotations[RayFTEnabledAnnotationKey] == "false" && r.Spec.GcsFaultToleranceOptions != nil {
99-
return field.Invalid(field.NewPath("spec").Child("gcsFaultToleranceOptions"), r.Spec.GcsFaultToleranceOptions, "GcsFaultToleranceOptions should be nil when ray.io/ft-enabled is disabled")
98+
return field.Invalid(
99+
field.NewPath("spec").Child("gcsFaultToleranceOptions"),
100+
r.Spec.GcsFaultToleranceOptions,
101+
fmt.Sprintf("GcsFaultToleranceOptions should be nil when %s is disabled", RayFTEnabledAnnotationKey),
102+
)
100103
}
101-
if r.Annotations[RayFTEnabledAnnotationKey] != "true" && r.Spec.HeadGroupSpec.Template.Spec.Containers[0].Env != nil {
102-
for _, env := range r.Spec.HeadGroupSpec.Template.Spec.Containers[0].Env {
103-
if env.Name == "RAY_REDIS_ADDRESS" {
104-
return field.Invalid(field.NewPath("spec").Child("headGroupSpec").Child("template").Child("spec").Child("containers").Index(0).Child("env"), env.Name, "RAY_REDIS_ADDRESS should not be set when ray.io/ft-enabled is disabled")
105-
}
104+
if r.Annotations[RayFTEnabledAnnotationKey] != "true" &&
105+
len(r.Spec.HeadGroupSpec.Template.Spec.Containers) > 0 &&
106+
r.Spec.HeadGroupSpec.Template.Spec.Containers[RayContainerIndex].Env != nil {
107+
108+
if EnvVarExists(RAY_REDIS_ADDRESS, r.Spec.HeadGroupSpec.Template.Spec.Containers[RayContainerIndex].Env) {
109+
return field.Invalid(
110+
field.NewPath("spec").Child("headGroupSpec").Child("template").Child("spec").Child("containers").Index(0).Child("env"),
111+
RAY_REDIS_ADDRESS,
112+
fmt.Sprintf("%s should not be set when %s is disabled", RAY_REDIS_ADDRESS, RayFTEnabledAnnotationKey),
113+
)
106114
}
107115
}
108116
return nil

ray-operator/apis/ray/v1/raycluster_webhook_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package v1
22

33
import (
4+
"fmt"
45
"testing"
56

67
"github.com/stretchr/testify/assert"
@@ -26,7 +27,7 @@ func TestValidateRayClusterSpec(t *testing.T) {
2627
},
2728
gcsFaultToleranceOptions: &GcsFaultToleranceOptions{},
2829
expectError: true,
29-
errorMessage: "GcsFaultToleranceOptions should be nil when ray.io/ft-enabled is disabled",
30+
errorMessage: fmt.Sprintf("GcsFaultToleranceOptions should be nil when %s is disabled", RayFTEnabledAnnotationKey),
3031
},
3132
{
3233
name: "FT disabled with RAY_REDIS_ADDRESS set",
@@ -35,24 +36,24 @@ func TestValidateRayClusterSpec(t *testing.T) {
3536
},
3637
envVars: []corev1.EnvVar{
3738
{
38-
Name: "RAY_REDIS_ADDRESS",
39+
Name: RAY_REDIS_ADDRESS,
3940
Value: "redis://127.0.0.1:6379",
4041
},
4142
},
4243
expectError: true,
43-
errorMessage: "RAY_REDIS_ADDRESS should not be set when ray.io/ft-enabled is disabled",
44+
errorMessage: fmt.Sprintf("%s should not be set when %s is disabled", RAY_REDIS_ADDRESS, RayFTEnabledAnnotationKey),
4445
},
4546
{
4647
name: "FT not set with RAY_REDIS_ADDRESS set",
4748
annotations: map[string]string{},
4849
envVars: []corev1.EnvVar{
4950
{
50-
Name: "RAY_REDIS_ADDRESS",
51+
Name: RAY_REDIS_ADDRESS,
5152
Value: "redis://127.0.0.1:6379",
5253
},
5354
},
5455
expectError: true,
55-
errorMessage: "RAY_REDIS_ADDRESS should not be set when ray.io/ft-enabled is disabled",
56+
errorMessage: fmt.Sprintf("%s should not be set when %s is disabled", RAY_REDIS_ADDRESS, RayFTEnabledAnnotationKey),
5657
},
5758
{
5859
name: "FT disabled with other environment variables set",
@@ -104,7 +105,7 @@ func TestValidateRayClusterSpec(t *testing.T) {
104105
},
105106
envVars: []corev1.EnvVar{
106107
{
107-
Name: "RAY_REDIS_ADDRESS",
108+
Name: RAY_REDIS_ADDRESS,
108109
Value: "redis://127.0.0.1:6379",
109110
},
110111
},
@@ -196,7 +197,7 @@ func TestValidateRayCluster(t *testing.T) {
196197
},
197198
GcsFaultToleranceOptions: &GcsFaultToleranceOptions{},
198199
expectError: true,
199-
errorMessage: "GcsFaultToleranceOptions should be nil when ray.io/ft-enabled is disabled",
200+
errorMessage: fmt.Sprintf("GcsFaultToleranceOptions should be nil when %s is disabled", RayFTEnabledAnnotationKey),
200201
},
201202
{
202203
name: "Valid RayCluster",

ray-operator/apis/ray/v1/utils.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package v1
2+
3+
import corev1 "k8s.io/api/core/v1"
4+
5+
func EnvVarExists(envName string, envVars []corev1.EnvVar) bool {
6+
for _, env := range envVars {
7+
if env.Name == envName {
8+
return true
9+
}
10+
}
11+
return false
12+
}

0 commit comments

Comments
 (0)