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

[RayJob] Enable job log streaming by setting PYTHONUNBUFFERED in job container #1375

Merged
merged 13 commits into from
Sep 6, 2023
7 changes: 7 additions & 0 deletions ray-operator/controllers/ray/rayjob_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
const (
RayJobDefaultRequeueDuration = 3 * time.Second
RayJobDefaultClusterSelectorKey = "ray.io/cluster"
PythonUnbufferedEnvVarName = "PYTHONUNBUFFERED"
)

// RayJobReconciler reconciles a RayJob object
Expand Down Expand Up @@ -375,6 +376,12 @@ func (r *RayJobReconciler) getSubmitterTemplate(rayJobInstance *rayv1alpha1.RayJ
r.Log.Info("User-provided command is used", "command", submitterTemplate.Spec.Containers[0].Command)
}

// Set PYTHONUNBUFFERED=1 for real-time logging
submitterTemplate.Spec.Containers[0].Env = append(submitterTemplate.Spec.Containers[0].Env, v1.EnvVar{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: What will happen if users specify PYTHONUNBUFFERED on their own? I am not sure whether defining the same env variable twice in a container will throw an error message or override the first value.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems the second definition will override the first one: https://www.baeldung.com/linux/kubernetes-pod-environment-variables

Name: PythonUnbufferedEnvVarName,
Value: "1",
})

return submitterTemplate, nil
}

Expand Down
12 changes: 12 additions & 0 deletions ray-operator/controllers/ray/rayjob_controller_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,16 @@ func TestGetSubmitterTemplate(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, ([]string{"ray", "job", "submit", "--address", "http://test-url", "--", "echo", "hello", "world"}), submitterTemplate.Spec.Containers[0].Command)
assert.Equal(t, "rayproject/ray:custom-version", submitterTemplate.Spec.Containers[0].Image)

// Test 4: Check default PYTHONUNBUFFERED setting
submitterTemplate, err = r.getSubmitterTemplate(rayJobInstanceWithoutTemplate)
assert.NoError(t, err)
found := false
for _, envVar := range submitterTemplate.Spec.Containers[0].Env {
if envVar.Name == PythonUnbufferedEnvVarName {
assert.Equal(t, "1", envVar.Value)
found = true
}
}
assert.True(t, found)
}
Loading