Skip to content

Commit d5d308b

Browse files
Check if workerGroupSpec is not empty before accessing it
1 parent a761951 commit d5d308b

File tree

1 file changed

+32
-21
lines changed

1 file changed

+32
-21
lines changed

pkg/controllers/raycluster_webhook.go

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -100,24 +100,25 @@ func (w *rayClusterWebhook) Default(ctx context.Context, obj runtime.Object) err
100100
}
101101

102102
// WorkerGroupSpec
103-
104-
// Append the list of environment variables for the worker container
105-
for _, envVar := range envVarList() {
106-
rayCluster.Spec.WorkerGroupSpecs[0].Template.Spec.Containers[0].Env = upsert(rayCluster.Spec.WorkerGroupSpecs[0].Template.Spec.Containers[0].Env, envVar, withEnvVarName(envVar.Name))
107-
}
108-
109-
// Append the CA volumes
110-
for _, caVol := range caVolumes(rayCluster) {
111-
rayCluster.Spec.WorkerGroupSpecs[0].Template.Spec.Volumes = upsert(rayCluster.Spec.WorkerGroupSpecs[0].Template.Spec.Volumes, caVol, withVolumeName(caVol.Name))
112-
}
113-
114-
// Append the certificate volume mounts
115-
for _, mount := range certVolumeMounts() {
116-
rayCluster.Spec.WorkerGroupSpecs[0].Template.Spec.Containers[0].VolumeMounts = upsert(rayCluster.Spec.WorkerGroupSpecs[0].Template.Spec.Containers[0].VolumeMounts, mount, byVolumeMountName)
103+
if len(rayCluster.Spec.WorkerGroupSpecs) != 0 {
104+
// Append the list of environment variables for the worker container
105+
for _, envVar := range envVarList() {
106+
rayCluster.Spec.WorkerGroupSpecs[0].Template.Spec.Containers[0].Env = upsert(rayCluster.Spec.WorkerGroupSpecs[0].Template.Spec.Containers[0].Env, envVar, withEnvVarName(envVar.Name))
107+
}
108+
109+
// Append the CA volumes
110+
for _, caVol := range caVolumes(rayCluster) {
111+
rayCluster.Spec.WorkerGroupSpecs[0].Template.Spec.Volumes = upsert(rayCluster.Spec.WorkerGroupSpecs[0].Template.Spec.Volumes, caVol, withVolumeName(caVol.Name))
112+
}
113+
114+
// Append the certificate volume mounts
115+
for _, mount := range certVolumeMounts() {
116+
rayCluster.Spec.WorkerGroupSpecs[0].Template.Spec.Containers[0].VolumeMounts = upsert(rayCluster.Spec.WorkerGroupSpecs[0].Template.Spec.Containers[0].VolumeMounts, mount, byVolumeMountName)
117+
}
118+
119+
// Append the create-cert Init Container
120+
rayCluster.Spec.WorkerGroupSpecs[0].Template.Spec.InitContainers = upsert(rayCluster.Spec.WorkerGroupSpecs[0].Template.Spec.InitContainers, rayWorkerInitContainer(), withContainerName(initContainerName))
117121
}
118-
119-
// Append the create-cert Init Container
120-
rayCluster.Spec.WorkerGroupSpecs[0].Template.Spec.InitContainers = upsert(rayCluster.Spec.WorkerGroupSpecs[0].Template.Spec.InitContainers, rayWorkerInitContainer(), withContainerName(initContainerName))
121122
}
122123

123124
return nil
@@ -386,6 +387,10 @@ func validateHeadInitContainer(rayCluster *rayv1.RayCluster, domain string) fiel
386387
func validateWorkerInitContainer(rayCluster *rayv1.RayCluster) field.ErrorList {
387388
var allErrors field.ErrorList
388389

390+
if len(rayCluster.Spec.WorkerGroupSpecs) == 0 {
391+
return allErrors
392+
}
393+
389394
if err := contains(rayCluster.Spec.WorkerGroupSpecs[0].Template.Spec.InitContainers, rayWorkerInitContainer(), byContainerName,
390395
field.NewPath("spec", "workerGroupSpecs", "0", "template", "spec", "initContainers"),
391396
"create-cert Init Container is immutable"); err != nil {
@@ -404,10 +409,12 @@ func validateCaVolumes(rayCluster *rayv1.RayCluster) field.ErrorList {
404409
"ca-vol and server-cert Secret volumes are immutable"); err != nil {
405410
allErrors = append(allErrors, err)
406411
}
407-
if err := contains(rayCluster.Spec.WorkerGroupSpecs[0].Template.Spec.Volumes, caVol, byVolumeName,
408-
field.NewPath("spec", "workerGroupSpecs", "0", "template", "spec", "volumes"),
409-
"ca-vol and server-cert Secret volumes are immutable"); err != nil {
410-
allErrors = append(allErrors, err)
412+
if len(rayCluster.Spec.WorkerGroupSpecs) != 0 {
413+
if err := contains(rayCluster.Spec.WorkerGroupSpecs[0].Template.Spec.Volumes, caVol, byVolumeName,
414+
field.NewPath("spec", "workerGroupSpecs", "0", "template", "spec", "volumes"),
415+
"ca-vol and server-cert Secret volumes are immutable"); err != nil {
416+
allErrors = append(allErrors, err)
417+
}
411418
}
412419
}
413420

@@ -431,6 +438,10 @@ func validateHeadEnvVars(rayCluster *rayv1.RayCluster) field.ErrorList {
431438
func validateWorkerEnvVars(rayCluster *rayv1.RayCluster) field.ErrorList {
432439
var allErrors field.ErrorList
433440

441+
if len(rayCluster.Spec.WorkerGroupSpecs) == 0 {
442+
return allErrors
443+
}
444+
434445
for _, envVar := range envVarList() {
435446
if err := contains(rayCluster.Spec.WorkerGroupSpecs[0].Template.Spec.Containers[0].Env, envVar, byEnvVarName,
436447
field.NewPath("spec", "workerGroupSpecs", "0", "template", "spec", "containers", strconv.Itoa(0), "env"),

0 commit comments

Comments
 (0)