Skip to content

Commit be2068f

Browse files
committed
Refactor ValidateRayClusterUpgradeOptions
Signed-off-by: win5923 <ken89@kimo.com>
1 parent 5fb6869 commit be2068f

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

ray-operator/controllers/ray/common/pod.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,11 +168,14 @@ func DefaultHeadPodTemplate(ctx context.Context, instance rayv1.RayCluster, head
168168
// headPort is passed into setMissingRayStartParams but unused there for the head pod.
169169
// To mitigate this awkwardness and reduce code redundancy, unify head and worker pod configuration logic.
170170

171+
log := ctrl.LoggerFrom(ctx)
171172
// Calculate the pod template hash before any modifications
172173
// This ensures the hash reflects the original user-defined template for upgrade detection
173174
templateHash := ""
174175
if hash, err := GeneratePodTemplateHash(instance.Spec.HeadGroupSpec.Template); err == nil {
175176
templateHash = hash
177+
} else {
178+
log.Error(err, "Failed to generate pod template hash for head group")
176179
}
177180

178181
podTemplate := headSpec.Template
@@ -315,13 +318,16 @@ func getEnableProbesInjection() bool {
315318

316319
// DefaultWorkerPodTemplate sets the config values
317320
func DefaultWorkerPodTemplate(ctx context.Context, instance rayv1.RayCluster, workerSpec rayv1.WorkerGroupSpec, podName string, fqdnRayIP string, headPort string, replicaGrpName string, replicaIndex int, numHostIndex int) corev1.PodTemplateSpec {
321+
log := ctrl.LoggerFrom(ctx)
318322
// Calculate the pod template hash before any modifications
319323
// This ensures the hash reflects the original user-defined template for upgrade detection
320324
templateHash := ""
321325
for _, wg := range instance.Spec.WorkerGroupSpecs {
322326
if wg.GroupName == workerSpec.GroupName {
323327
if hash, err := GeneratePodTemplateHash(wg.Template); err == nil {
324328
templateHash = hash
329+
} else {
330+
log.Error(err, "Failed to generate pod template hash for worker group", "groupName", workerSpec.GroupName)
325331
}
326332
break
327333
}

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,19 @@ func ValidateRayClusterMetadata(metadata metav1.ObjectMeta) error {
3838
}
3939

4040
func ValidateRayClusterUpgradeOptions(instance *rayv1.RayCluster) error {
41-
strategy := instance.Spec.UpgradeStrategy
42-
if strategy == nil || strategy.Type == nil || *strategy.Type == rayv1.RayClusterUpgradeNone {
43-
return nil
41+
// only Recreate and None are valid upgradeType
42+
if instance.Spec.UpgradeStrategy != nil && instance.Spec.UpgradeStrategy.Type != nil &&
43+
*instance.Spec.UpgradeStrategy.Type != rayv1.Recreate &&
44+
*instance.Spec.UpgradeStrategy.Type != rayv1.RayClusterUpgradeNone {
45+
return fmt.Errorf("The RayCluster spec is invalid: Spec.UpgradeStrategy.Type value %s is invalid, valid options are %s or %s", *instance.Spec.UpgradeStrategy.Type, rayv1.Recreate, rayv1.RayClusterUpgradeNone)
4446
}
4547

46-
creatorCRDType := GetCRDType(instance.Labels[RayOriginatedFromCRDLabelKey])
47-
if creatorCRDType == RayJobCRD || creatorCRDType == RayServiceCRD {
48-
return fmt.Errorf("upgradeStrategy cannot be set when RayCluster is created by %s", creatorCRDType)
48+
// only allow UpgradeStrategy to be set when RayCluster is created directly by user
49+
if instance.Spec.UpgradeStrategy != nil && instance.Spec.UpgradeStrategy.Type != nil {
50+
creatorCRDType := GetCRDType(instance.Labels[RayOriginatedFromCRDLabelKey])
51+
if creatorCRDType == RayJobCRD || creatorCRDType == RayServiceCRD {
52+
return fmt.Errorf("upgradeStrategy cannot be set when RayCluster is created by %s", creatorCRDType)
53+
}
4954
}
5055
return nil
5156
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1930,6 +1930,15 @@ func TestValidateRayClusterUpgradeOptions(t *testing.T) {
19301930
expectError: true,
19311931
errorMessage: "upgradeStrategy cannot be set when RayCluster is created by RayService",
19321932
},
1933+
{
1934+
name: "Invalid upgrade strategy value",
1935+
upgradeStrategy: &rayv1.RayClusterUpgradeStrategy{
1936+
Type: ptr.To(rayv1.RayClusterUpgradeType("InvalidStrategy")),
1937+
},
1938+
originatedFromCRD: string(RayClusterCRD),
1939+
expectError: true,
1940+
errorMessage: "Spec.UpgradeStrategy.Type value InvalidStrategy is invalid, valid options are Recreate or None",
1941+
},
19331942
}
19341943

19351944
for _, tt := range tests {

0 commit comments

Comments
 (0)