Skip to content

Commit ecafab2

Browse files
Add unit tests for raycluster_webhook.go
1 parent 4106ddb commit ecafab2

File tree

2 files changed

+430
-32
lines changed

2 files changed

+430
-32
lines changed

pkg/controllers/raycluster_webhook.go

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

102102
// WorkerGroupSpec
103-
if len(rayCluster.Spec.WorkerGroupSpecs) != 0 {
103+
for i := range rayCluster.Spec.WorkerGroupSpecs {
104+
workerSpec := &rayCluster.Spec.WorkerGroupSpecs[i]
105+
104106
// Append the list of environment variables for the worker container
105107
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))
108+
workerSpec.Template.Spec.Containers[0].Env = upsert(workerSpec.Template.Spec.Containers[0].Env, envVar, withEnvVarName(envVar.Name))
107109
}
108110

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-
}
111+
// Append the CA volumes
112+
for _, caVol := range caVolumes(rayCluster) {
113+
workerSpec.Template.Spec.Volumes = upsert(workerSpec.Template.Spec.Volumes, caVol, withVolumeName(caVol.Name))
114+
}
113115

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-
}
116+
// Append the certificate volume mounts
117+
for _, mount := range certVolumeMounts() {
118+
workerSpec.Template.Spec.Containers[0].VolumeMounts = upsert(workerSpec.Template.Spec.Containers[0].VolumeMounts, mount, byVolumeMountName)
119+
}
118120

119-
// Append the create-cert Init Container
120-
rayCluster.Spec.WorkerGroupSpecs[0].Template.Spec.InitContainers = upsert(rayCluster.Spec.WorkerGroupSpecs[0].Template.Spec.InitContainers, rayWorkerInitContainer(w.Config), withContainerName(initContainerName))
121+
// Append the create-cert Init Container
122+
workerSpec.Template.Spec.InitContainers = upsert(workerSpec.Template.Spec.InitContainers, rayWorkerInitContainer(w.Config), withContainerName(initContainerName))
121123
}
122124
}
123125

@@ -387,14 +389,13 @@ func validateHeadInitContainer(rayCluster *rayv1.RayCluster, config *config.Kube
387389
func validateWorkerInitContainer(rayCluster *rayv1.RayCluster, config *config.KubeRayConfiguration) field.ErrorList {
388390
var allErrors field.ErrorList
389391

390-
if len(rayCluster.Spec.WorkerGroupSpecs) == 0 {
391-
return allErrors
392-
}
393-
394-
if err := contains(rayCluster.Spec.WorkerGroupSpecs[0].Template.Spec.InitContainers, rayWorkerInitContainer(config), byContainerName,
395-
field.NewPath("spec", "workerGroupSpecs", "0", "template", "spec", "initContainers"),
396-
"create-cert Init Container is immutable"); err != nil {
397-
allErrors = append(allErrors, err)
392+
for i := range rayCluster.Spec.WorkerGroupSpecs {
393+
workerSpec := &rayCluster.Spec.WorkerGroupSpecs[i]
394+
if err := contains(workerSpec.Template.Spec.InitContainers, rayWorkerInitContainer(config), byContainerName,
395+
field.NewPath("spec", "workerGroupSpecs", strconv.Itoa(i), "template", "spec", "initContainers"),
396+
"create-cert Init Container is immutable"); err != nil {
397+
allErrors = append(allErrors, err)
398+
}
398399
}
399400

400401
return allErrors
@@ -409,9 +410,10 @@ func validateCaVolumes(rayCluster *rayv1.RayCluster) field.ErrorList {
409410
"ca-vol and server-cert Secret volumes are immutable"); err != nil {
410411
allErrors = append(allErrors, err)
411412
}
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"),
413+
for i := range rayCluster.Spec.WorkerGroupSpecs {
414+
workerSpec := &rayCluster.Spec.WorkerGroupSpecs[i]
415+
if err := contains(workerSpec.Template.Spec.Volumes, caVol, byVolumeName,
416+
field.NewPath("spec", "workerGroupSpecs", strconv.Itoa(i), "template", "spec", "volumes"),
415417
"ca-vol and server-cert Secret volumes are immutable"); err != nil {
416418
allErrors = append(allErrors, err)
417419
}
@@ -438,15 +440,14 @@ func validateHeadEnvVars(rayCluster *rayv1.RayCluster) field.ErrorList {
438440
func validateWorkerEnvVars(rayCluster *rayv1.RayCluster) field.ErrorList {
439441
var allErrors field.ErrorList
440442

441-
if len(rayCluster.Spec.WorkerGroupSpecs) == 0 {
442-
return allErrors
443-
}
444-
445-
for _, envVar := range envVarList() {
446-
if err := contains(rayCluster.Spec.WorkerGroupSpecs[0].Template.Spec.Containers[0].Env, envVar, byEnvVarName,
447-
field.NewPath("spec", "workerGroupSpecs", "0", "template", "spec", "containers", strconv.Itoa(0), "env"),
448-
"RAY_TLS related environment variables are immutable"); err != nil {
449-
allErrors = append(allErrors, err)
443+
for i := range rayCluster.Spec.WorkerGroupSpecs {
444+
workerSpec := &rayCluster.Spec.WorkerGroupSpecs[i]
445+
for _, envVar := range envVarList() {
446+
if err := contains(workerSpec.Template.Spec.Containers[0].Env, envVar, byEnvVarName,
447+
field.NewPath("spec", "workerGroupSpecs", strconv.Itoa(i), "template", "spec", "containers", strconv.Itoa(0), "env"),
448+
"RAY_TLS related environment variables are immutable"); err != nil {
449+
allErrors = append(allErrors, err)
450+
}
450451
}
451452
}
452453

0 commit comments

Comments
 (0)