feat(trainer): Support namespaced TrainingRuntime in the SDK #130
/ok-to-test
Thank you @shaikmoeed for this!
Left some nit-picks..
"""Get the Runtime object prefer namespaced, fall-back to cluster-scoped"""
cluster_thread.get(common_constants.DEFAULT_TIMEOUT)
Same change goes for each occurence here
did you mean to create this in kubernetes/backend_test.py?
this is not a test function and I believe it should be added to the TrainerClient and propagated to the different backends.
Similar to create_train_job, I thought to use create_training_runtime to pass here instead of empty list. Please correct me, if this was not intended?
Done! Reason for test case failures!
Pull Request Test Coverage Report for Build 22127337630
Warning: This coverage report may be inaccurate.
This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.
Details
68 of 86 (79.07%) changed or added relevant lines in 4 files are covered.
16 unchanged lines in 2 files lost coverage.
---
| Changes Missing Coverage | Covered Lines | Changed/Added Lines | % |
| :-----|--------------|--------|---: |
| kubeflow/trainer/types/types.py | 6 | 7 | 85.71%
| kubeflow/trainer/backends/kubernetes/backend_test.py | 28 | 34 | 82.35%
| kubeflow/trainer/backends/kubernetes/backend.py | 32 | 43 | 74.42%
<!-- | Total: | 68 | 86 | 79.07% | -->
| Files with Coverage Reduction | New Missed Lines | % |
| :-----|--------------|--: |
| kubeflow/trainer/backends/kubernetes/backend.py | 8 | 77.89% |
| kubeflow/trainer/backends/kubernetes/backend_test.py | 8 | 93.27% |
<!-- | Total: | 16 | | -->
| Totals |  |
| :-- | --: |
| Change from base Build 22048455236: | -0.2% |
| Covered Lines: | 4337 |
| Relevant Lines: | 5674 |
---
##### 💛 - Coveralls
@abhijeet-dhumal, can you review it again during your free time?
What If only cluster runtimes exist (no TrainingRuntime CRD)? The entire list_runtimes will fail with RuntimeError, which we don't want, right?
What if one type times out and the other succeeds? I think we still should return partial results
nit: cluster scoped runtimes are not really in a namespace
What if it was for example TimeoutError? We will still silently fallback to cluster scoped runtimes, right? I would suggest only treating not found / missing CRD as fall back.
Similar to list_runtimes(), either case will cause a full failure
# The plural for the TrainingRuntime.
Thank you @shaikmoeed!
I'd like all of us to spend more time on designing this properly, since there are a lot of items to consider
/assign @kubeflow/kubeflow-sdk-team
@shaikmoeed
Quick question : What if runtimes with the same name exists in both cluster and namespace scoped ?
IIUC you have implemented namespace scoped priority in get_runtime() where trainingRuntimes get's first priority..
thinking should it be same case for list_runtimes method too ?
And one more thing, In case of list_runtimes it's just appending both even if duplicates comes in..
So for end user how user will be able to know the kind of runtime via list_runtimes's list items ?
What if we introduce kind and namespace(optional) params in Runtime dataclass here : https://github.com/kubeflow/sdk/blob/49a5087bb1154c0505cbe2a004db17d4d373c82a/kubeflow/trainer/types/types.py#L251
So that
for runtime in runtimes:
print(f"{runtime.name} ({runtime.kind}, ns={runtime.namespace})")
# Output:
# torch-runtime (TrainingRuntime, ns=team-a)
# torch-runtime (ClusterTrainingRuntime, ns=None)
# custom-runtime (TrainingRuntime, ns=team-a)
WDYT? @kramaranya @szaher ?
I'd be in favor of adding a scope field instead, with values like "project" and "cluster", so we don't expose any k8s terminology to users.
Regarding namespace I don't think AI practitioners need to see it, since they already know they're working in their project context (set at client init time).
cc @andreyvelich @astefanutti
+1 for a scope field, though I wonder whether it could be more symmetrical to the resolution logic in the get_runtime and not returning the ClusterTrainingRuntimes that are "overwritten" in the client namespace. This way I don't think any additional field would be needed.
Shall we configure this setting during TrainerClient() initialization?
I am curious if we ever have a use-case when users want to create a single TrainerClient() which can work with TrainingRuntime and ClusterTrainingRuntime ?
Hey, @shaikmoeed! Are you still working on this PR? Did you have a chance to address the feedback?
Hey, @shaikmoeed! Are you still working on this PR? Did you have a chance to address the feedback?
@kramaranya Haven't got a chance to address the feedback. Will update the PR by next week.
Hey @shaikmoeed! Are you still working on this PR? It would be great if we could include this feature in 0.3 release (we aim to cut the release this week)
Hey @shaikmoeed! Are you still working on this PR? It would be great if we could include this feature in 0.3 release (we aim to cut the release this week)
@kramaranya Sorry for the delay, I have addressed all the comments! Can you please provide your review?
@kramaranya @abhijeet-dhumal @andreyvelich Request for review!
QQ: It seems relying on kind field being present. K8s API doesn't always return kind on retrieved objects (it's often omitted). If missing, scope stays None.
what if we use type check which doesn't rely on K8s api response fileds.. something like :.
scope = (
"cluster"
if isinstance(runtime_cr, models.TrainerV1alpha1ClusterTrainingRuntime)
else "project"
)
Thinking .. Scope is derived from naming convention. Adding a runtime like "my-custom-runtime" silently gets scope="cluster" even if it's namespaced.
Consider suggestion provided by @kramaranya from here :
https://github.com/kubeflow/sdk/pull/130#discussion_r2583080641, Something like :
create_runtime_type(name="my-custom-runtime", scope="project")
In this case explicit is better than implicit
wdyt @shaikmoeed ?
Following up on @astefanutti's comment about symmetry between list_runtimes() and get_runtime().
According to current behavior and if we consider separate scope arg :
If Namespace has "torch-runtime", cluster also has "torch-runtime"
client.list_runtimes() will return :
- torch-runtime (scope=project)
- torch-runtime (scope=cluster) ...duplicate name
client.get_runtime("torch-runtime")
Returns one:
- torch-runtime (scope=project) .. namespaced wins
so user sees duplicate names in list, but get_runtime() silently picks one.
This creates confusion — "which one will user actually get?"
so what if :
client.list_runtimes()
Returns:
- torch-runtime (scope=project) ... namespaced, shadows cluster
client.get_runtime("torch-runtime")
Returns:
- torch-runtime (scope=project) ... matches what list shows
here are some edge cases i could think of on top of my head :
WDYT? @shaikmoeed
The docstring says this helper returns a mocked TrainJob object, but it now also returns namespaced TrainingRuntime objects; update the docstring to reflect the expanded behavior to avoid misleading future test maintenance.
The docstring says this helper returns a list of mocked TrainJob objects, but the new branch also returns TrainingRuntime lists; update the docstring so the mocked behavior is accurate.
The Runtime.scope allowed value "project" is ambiguous for a Kubernetes namespaced resource; consider renaming the scope values to "namespace"/"namespaced" (and updating backend/tests) or introducing an enum so the public SDK surface matches the underlying scoping semantics.
The invalid-CR exception message has a stray quote after the runtime object and references "replicatedJobs" while the code checks replicated_jobs, which makes the error output confusing when debugging.
f"{runtime_cr} is invalid — missing one or more required fields: "
"metadata.name, spec.ml_policy, spec.template.spec.replicated_jobs.\n"
This test helper hardcodes kind="TrainingRuntime" even though the PR adds constants.TRAINING_RUNTIME_KIND; using the constant here avoids drift if the kind ever changes and keeps tests aligned with the SDK constants.
kind=constants.TRAINING_RUNTIME_KIND,
Pull request overview
Adds SDK support for namespaced TrainingRuntime resources alongside existing cluster-scoped ClusterTrainingRuntime, enabling users to list/get runtimes from their namespace and fall back to cluster runtimes as needed.
Changes:
scope to types.Runtime and populate it when converting runtime CRs to SDK types.get_runtime().TrainingRuntime list/get behavior.Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.
| File | Description |
| ---- | ----------- |
| kubeflow/trainer/types/types.py | Introduces Runtime.scope with validation of allowed values. |
| kubeflow/trainer/constants/constants.py | Adds kind/plural constants for namespaced TrainingRuntime. |
| kubeflow/trainer/backends/kubernetes/backend.py | Implements list/get support for namespaced runtimes and sets Runtime.scope. |
| kubeflow/trainer/backends/kubernetes/backend_test.py | Extends mocks and expected outputs for namespaced runtime scenarios. |
Yeah, agree. Made the relevant changes!
Thanks, it simplifies code as well! Updated with changes!
RuntimeScope.NAMESPACE is set to the string value "project", which is inconsistent with Kubernetes terminology and the PR intent (namespaced runtimes) and will be confusing if this value is ever surfaced/serialized; consider renaming the value to "namespace" (or "namespaced") to match the concept being modeled.
list_runtimes() uses async_req=True but immediately blocks on each .get() sequentially, which adds unnecessary latency; start both async requests first and then wait for both results (handling errors per-scope) to keep the end-to-end call closer to the slower of the two API calls rather than the sum.
Pull request overview
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
Pull request overview
Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.
<details>
<summary>Comments suppressed due to low confidence (1)</summary>
kubeflow/trainer/backends/kubernetes/backend_test.py:735
@pytest.mark.parametrize(
"test_case",
[
TestCase(
name="valid flow with all defaults",
expected_status=SUCCESS,
config={"name": TORCH_RUNTIME},
expected_output=create_runtime_type(
name=TORCH_RUNTIME,
scope=types.RuntimeScope.NAMESPACE,
),
),
TestCase(
name="timeout error when getting runtime",
expected_status=FAILED,
config={"name": TIMEOUT},
expected_error=TimeoutError,
),
TestCase(
name="runtime error when getting runtime",
expected_status=FAILED,
config={"name": RUNTIME},
expected_error=RuntimeError,
),
],
)
</details>
@andreyvelich Addressed your comments. Can you please take a look now?
[APPROVALNOTIFIER] This PR is APPROVED
This pull-request has been approved by: <a href="https://github.com/kubeflow/sdk/pull/130#pullrequestreview-3820286002" title="Approved">andreyvelich</a>
The full list of commands accepted by this bot can be found here.
The pull request process is described here
<details >
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment
</details>
<!-- META={"approvers":[]} -->
abhijeet-dhumal
shaikmoeed
<!-- Thanks for sending a pull request! Here are some tips for you:
1. If this is your first time, check our contributor guidelines: https://www.kubeflow.org/docs/about/contributing
2. To know more about Kubeflow SDK, check the developer guide:
https://github.com/kubeflow/sdk/blob/main/CONTRIBUTING.md
3. If you want faster PR reviews, check how: https://git.k8s.io/community/contributors/guide/pull-requests.md#best-practices-for-faster-reviews
-->
What this PR does / why we need it:
Add support to list/get namespaced TrainingRuntime.
Which issue(s) this PR fixes:
Fixes #88
Checklist: