Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a flag to enable/disable worker init container injection #1069

Merged
merged 14 commits into from
May 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions helm-chart/kuberay-operator/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ singleNamespaceInstall: false
# watchNamespace: ray-user-namespace

# Environment variables
env: []
# - name: EXAMPLE_ENV
# value: "1"
env:
# If not set or set to true, kuberay auto injects an init container waiting for ray GCS.
# If false, you will need to inject your own init container to ensure ray GCS is up before the ray workers start.
# Warning: we highly recommend setting to true and let kuberay handle for you.
# - name: ENABLE_INIT_CONTAINER_INJECTION
# value: "true"
56 changes: 36 additions & 20 deletions ray-operator/controllers/ray/common/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package common
import (
"bytes"
"fmt"
"os"
"strconv"
"strings"

Expand All @@ -28,6 +29,9 @@ const (
ObjectStoreMemoryKey = "object-store-memory"
// TODO (davidxia): should be a const in upstream ray-project/ray
AllowSlowStorageEnvVar = "RAY_OBJECT_STORE_ALLOW_SLOW_STORAGE"
// If set to true, kuberay auto injects an init container waiting for ray GCS.
// If false, you will need to inject your own init container to ensure ray GCS is up before the ray workers start.
EnableInitContainerInjectionEnvKey = "ENABLE_INIT_CONTAINER_INJECTION"
)

var log = logf.Log.WithName("RayCluster-Controller")
Expand Down Expand Up @@ -180,6 +184,13 @@ func autoscalerSupportIsStable(rayVersion string) bool {
}
}

func getEnableInitContainerInjection() bool {
if s := os.Getenv(EnableInitContainerInjectionEnvKey); strings.ToLower(s) == "false" {
return false
}
return true
}

// DefaultWorkerPodTemplate sets the config values
func DefaultWorkerPodTemplate(instance rayiov1alpha1.RayCluster, workerSpec rayiov1alpha1.WorkerGroupSpec, podName string, fqdnRayIP string, headPort string) v1.PodTemplateSpec {
podTemplate := workerSpec.Template
Expand All @@ -191,27 +202,32 @@ func DefaultWorkerPodTemplate(instance rayiov1alpha1.RayCluster, workerSpec rayi

// The Ray worker should only start once the GCS server is ready.
rayContainerIndex := getRayContainerIndex(podTemplate.Spec)
// Do not modify `deepCopyRayContainer` anywhere.
deepCopyRayContainer := podTemplate.Spec.Containers[rayContainerIndex].DeepCopy()
initContainer := v1.Container{
Name: "wait-gcs-ready",
Image: podTemplate.Spec.Containers[rayContainerIndex].Image,
ImagePullPolicy: v1.PullIfNotPresent,
Command: []string{"/bin/bash", "-lc", "--"},
Args: []string{
fmt.Sprintf("until ray health-check --address %s:%s > /dev/null 2>&1; do echo wait for GCS to be ready; sleep 5; done", fqdnRayIP, headPort),
},
SecurityContext: podTemplate.Spec.Containers[rayContainerIndex].SecurityContext.DeepCopy(),
// This init container requires certain environment variables to establish a secure connection with the Ray head using TLS authentication.
// Additionally, some of these environment variables may reference files stored in volumes, so we need to include both the `Env` and `VolumeMounts` fields here.
// For more details, please refer to: https://docs.ray.io/en/latest/ray-core/configure.html#tls-authentication.
Env: deepCopyRayContainer.Env,
VolumeMounts: deepCopyRayContainer.VolumeMounts,
// If users specify ResourceQuota for the namespace, the init container need to specify resource explicitly.
Resources: deepCopyRayContainer.Resources,
}
podTemplate.Spec.InitContainers = append(podTemplate.Spec.InitContainers, initContainer)

// only inject init container only when ENABLE_INIT_CONTAINER_INJECTION is true
enableInitContainerInjection := getEnableInitContainerInjection()
ByronHsu marked this conversation as resolved.
Show resolved Hide resolved

if enableInitContainerInjection {
// Do not modify `deepCopyRayContainer` anywhere.
deepCopyRayContainer := podTemplate.Spec.Containers[rayContainerIndex].DeepCopy()
initContainer := v1.Container{
Name: "wait-gcs-ready",
Image: podTemplate.Spec.Containers[rayContainerIndex].Image,
ImagePullPolicy: v1.PullIfNotPresent,
Command: []string{"/bin/bash", "-lc", "--"},
Args: []string{
fmt.Sprintf("until ray health-check --address %s:%s > /dev/null 2>&1; do echo wait for GCS to be ready; sleep 5; done", fqdnRayIP, headPort),
},
SecurityContext: podTemplate.Spec.Containers[rayContainerIndex].SecurityContext.DeepCopy(),
// This init container requires certain environment variables to establish a secure connection with the Ray head using TLS authentication.
// Additionally, some of these environment variables may reference files stored in volumes, so we need to include both the `Env` and `VolumeMounts` fields here.
// For more details, please refer to: https://docs.ray.io/en/latest/ray-core/configure.html#tls-authentication.
Env: deepCopyRayContainer.Env,
VolumeMounts: deepCopyRayContainer.VolumeMounts,
// If users specify ResourceQuota for the namespace, the init container need to specify resource explicitly.
Resources: deepCopyRayContainer.Resources,
}
podTemplate.Spec.InitContainers = append(podTemplate.Spec.InitContainers, initContainer)
}
// If the replica of workers is more than 1, `ObjectMeta.Name` may cause name conflict errors.
// Hence, we set `ObjectMeta.Name` to an empty string, and use GenerateName to prevent name conflicts.
podTemplate.ObjectMeta.Name = ""
Expand Down
26 changes: 26 additions & 0 deletions ray-operator/controllers/ray/common/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package common

import (
"fmt"
"os"
"reflect"
"sort"
"strings"
Expand Down Expand Up @@ -979,3 +980,28 @@ func TestSetMissingRayStartParamsBlock(t *testing.T) {
rayStartParams = setMissingRayStartParams(rayStartParams, rayiov1alpha1.WorkerNode, headPort, fqdnRayIP)
assert.Equal(t, "false", rayStartParams["block"], fmt.Sprintf("Expected `%v` but got `%v`", "false", rayStartParams["block"]))
}

func TestGetCustomWorkerInitImage(t *testing.T) {
ByronHsu marked this conversation as resolved.
Show resolved Hide resolved
// cleanup
defer os.Unsetenv(EnableInitContainerInjectionEnvKey)

// not set the env
b := getEnableInitContainerInjection()
assert.True(t, b)
// set the env with "true"
os.Setenv(EnableInitContainerInjectionEnvKey, "true")
b = getEnableInitContainerInjection()
assert.True(t, b)
// set the env with "True"
os.Setenv(EnableInitContainerInjectionEnvKey, "True")
b = getEnableInitContainerInjection()
assert.True(t, b)
// set the env with "false"
os.Setenv(EnableInitContainerInjectionEnvKey, "false")
b = getEnableInitContainerInjection()
assert.False(t, b)
// set the env with "False"
os.Setenv(EnableInitContainerInjectionEnvKey, "False")
b = getEnableInitContainerInjection()
assert.False(t, b)
}