Skip to content

Commit d21790c

Browse files
committed
configure auth for Ray autoscaler
Signed-off-by: Andrew Sy Kim <[email protected]>
1 parent 8415823 commit d21790c

File tree

1 file changed

+26
-28
lines changed
  • ray-operator/controllers/ray/common

1 file changed

+26
-28
lines changed

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

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,12 @@ func DefaultHeadPodTemplate(ctx context.Context, instance rayv1.RayCluster, head
199199
autoscalerImage := podTemplate.Spec.Containers[utils.RayContainerIndex].Image
200200
// inject autoscaler container into head pod
201201
autoscalerContainer := BuildAutoscalerContainer(autoscalerImage)
202+
203+
// Configure RAY_AUTH_TOKEN and RAY_AUTH_MODE if auth is enabled.
204+
if utils.IsAuthEnabled(&instance.Spec) {
205+
setContainerTokenAuthEnvVars(instance.Name, &autoscalerContainer)
206+
}
207+
202208
// Merge the user overrides from autoscalerOptions into the autoscaler container config.
203209
mergeAutoscalerOverrides(&autoscalerContainer, instance.Spec.AutoscalerOptions)
204210
podTemplate.Spec.Containers = append(podTemplate.Spec.Containers, autoscalerContainer)
@@ -222,7 +228,7 @@ func DefaultHeadPodTemplate(ctx context.Context, instance rayv1.RayCluster, head
222228
}
223229

224230
if utils.IsAuthEnabled(&instance.Spec) {
225-
setTokenAuthEnvVars(instance.Name, &podTemplate)
231+
configureTokenAuth(instance.Name, &podTemplate)
226232
}
227233

228234
return podTemplate
@@ -240,15 +246,29 @@ func setAutoscalerV2EnvVars(podTemplate *corev1.PodTemplateSpec) {
240246
})
241247
}
242248

243-
// setTokenAuthEnvVars sets environment variables required for Ray token authentication
244-
func setTokenAuthEnvVars(clusterName string, podTemplate *corev1.PodTemplateSpec) {
245-
podTemplate.Spec.Containers[utils.RayContainerIndex].Env = append(podTemplate.Spec.Containers[utils.RayContainerIndex].Env, corev1.EnvVar{
249+
// configureTokenAuth sets environment variables required for Ray token authentication
250+
func configureTokenAuth(clusterName string, podTemplate *corev1.PodTemplateSpec) {
251+
setContainerTokenAuthEnvVars(clusterName, &podTemplate.Spec.Containers[utils.RayContainerIndex])
252+
253+
// Configure auth token for wait-gcs-ready init container if it exists
254+
for i, initContainer := range podTemplate.Spec.InitContainers {
255+
if initContainer.Name != "wait-gcs-ready" {
256+
continue
257+
}
258+
259+
setContainerTokenAuthEnvVars(clusterName, &podTemplate.Spec.InitContainers[i])
260+
}
261+
}
262+
263+
// setContainerTokenAuthEnvVars sets Ray authentication env vars for a container.
264+
func setContainerTokenAuthEnvVars(clusterName string, container *corev1.Container) {
265+
container.Env = append(container.Env, corev1.EnvVar{
246266
Name: utils.RAY_AUTH_MODE_ENV_VAR,
247267
Value: "token",
248268
})
249269

250270
secretName := utils.CheckName(clusterName)
251-
podTemplate.Spec.Containers[utils.RayContainerIndex].Env = append(podTemplate.Spec.Containers[utils.RayContainerIndex].Env, corev1.EnvVar{
271+
container.Env = append(container.Env, corev1.EnvVar{
252272
Name: utils.RAY_AUTH_TOKEN_ENV_VAR,
253273
ValueFrom: &corev1.EnvVarSource{
254274
SecretKeyRef: &corev1.SecretKeySelector{
@@ -257,28 +277,6 @@ func setTokenAuthEnvVars(clusterName string, podTemplate *corev1.PodTemplateSpec
257277
},
258278
},
259279
})
260-
261-
// Configure auth token for wait-gcs-ready init container if it exists
262-
for i, initContainer := range podTemplate.Spec.InitContainers {
263-
if initContainer.Name != "wait-gcs-ready" {
264-
continue
265-
}
266-
267-
podTemplate.Spec.InitContainers[i].Env = append(podTemplate.Spec.InitContainers[i].Env, corev1.EnvVar{
268-
Name: utils.RAY_AUTH_MODE_ENV_VAR,
269-
Value: "token",
270-
})
271-
272-
podTemplate.Spec.InitContainers[i].Env = append(podTemplate.Spec.InitContainers[i].Env, corev1.EnvVar{
273-
Name: utils.RAY_AUTH_TOKEN_ENV_VAR,
274-
ValueFrom: &corev1.EnvVarSource{
275-
SecretKeyRef: &corev1.SecretKeySelector{
276-
LocalObjectReference: corev1.LocalObjectReference{Name: secretName},
277-
Key: utils.RAY_AUTH_TOKEN_SECRET_KEY,
278-
},
279-
},
280-
})
281-
}
282280
}
283281

284282
func getEnableInitContainerInjection() bool {
@@ -404,7 +402,7 @@ func DefaultWorkerPodTemplate(ctx context.Context, instance rayv1.RayCluster, wo
404402
}
405403

406404
if utils.IsAuthEnabled(&instance.Spec) {
407-
setTokenAuthEnvVars(instance.Name, &podTemplate)
405+
configureTokenAuth(instance.Name, &podTemplate)
408406
}
409407

410408
return podTemplate

0 commit comments

Comments
 (0)