Skip to content

fix(trainer): extract runtime metadata from SDK Runtime in get_runtime - #192

Merged
google-oss-prow[bot] merged 3 commits into
kubeflow:mainfrom
priyank766:fix/get-runtime-metadata-extraction
Sep 1, 2026
Merged

fix(trainer): extract runtime metadata from SDK Runtime in get_runtime#192
google-oss-prow[bot] merged 3 commits into
kubeflow:mainfrom
priyank766:fix/get-runtime-metadata-extraction

Conversation

@priyank766

Copy link
Copy Markdown
Contributor

What this PR does

  • Updates get_runtime() to extract runtime metadata (framework, image, num_nodes, device, device_count, trainer_type) from rt.trainer and rt.pretrained_model, aligning with the Kubeflow SDK's types.Runtime dataclass.
  • Retains backward-compatible fallback for objects with .spec.
  • Updates _get_runtime_image to check client.get_runtime(name) first (which resolves both namespaced and cluster-scoped runtimes) before falling back to CustomObjectsApi.
  • Refactors extraction into _extract_runtime_metadata to keep cyclomatic complexity low.
  • Adds comprehensive unit tests in kubeflow_mcp/trainer/api/discovery_test.py.

Fixes #191

How it was tested

  • uv run pytest kubeflow_mcp/trainer/api/discovery_test.py (14 passed)
  • uv run pytest (516 passed, all unit & conformance schema snapshot tests pass)
  • uv run ruff check . and uv run ruff format --check . (clean)

Extract framework, image, num_nodes, device, device_count, and trainer_type from the SDK Runtime.trainer object in get_runtime(), while keeping backward-compatible spec fallback.

Signed-off-by: priyank <priyank8445@gmail.com>
Copilot AI balanced review requested due to automatic review settings August 31, 2026 12:45

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Comment thread kubeflow_mcp/trainer/api/discovery.py Outdated
if image:
data["image"] = str(image)
num_nodes = getattr(trainer, "num_nodes", None)
if num_nodes:

@abhijeet-dhumal abhijeet-dhumal Sep 1, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
if num_nodes:
if num_nodes is not None:

if num_nodes skips when num_nodes is 0, because 0 is falsy in Python.
But 0 is a valid value so the response drops num_nodes entirely.

class TestGetRuntime:
@patch("kubeflow_mcp.trainer.api.discovery.get_trainer_client")
def test_get_runtime_extracts_sdk_trainer_metadata(self, mock_client_fn):
mock_trainer = MagicMock()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thinking would it be good to add a test with real Runtime / RuntimeTrainer objects instead of MagicMock so this doesn't slip through again : https://github.com/kubeflow/mcp-server/pull/192/changes#r3901070952

return data


def get_runtime(name: str, include_packages: bool = False) -> dict[str, Any]:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

include_packages=True triggers a second client.get_runtime() via _get_runtime_image(). Thinking .. we already have rt in get_runtime().. can we pass trainer.image through (or the whole rt) to avoid duplicate API calls on the slow path. WDYT?

Comment thread kubeflow_mcp/trainer/api/discovery.py Outdated


def get_runtime(name: str, include_packages: bool = False) -> dict[str, Any]:
"""Get ClusterTrainingRuntime configuration.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Docstring still says "ClusterTrainingRuntime" only. SDK resolves both cluster-scoped and namespaced runtimes right?.. worth updating while this is not merged yet..

…date docstrings

Pass the already resolved runtime object to _fetch_packages_via_pod() and _get_runtime_image() to prevent duplicate client.get_runtime() calls on include_packages=True. Also update docstrings to reflect that get_runtime and list_runtimes resolve both namespaced TrainingRuntimes and cluster-scoped ClusterTrainingRuntimes.

Signed-off-by: priyank <priyank8445@gmail.com>
… Runtime types

Update num_nodes check from truthy check to is not None so 0 is preserved. Add unit tests using real SDK Runtime and RuntimeTrainer objects.

Signed-off-by: priyank <priyank8445@gmail.com>
@priyank766

priyank766 commented Sep 1, 2026

Copy link
Copy Markdown
Contributor Author

🚀 Thanks for the great feedback and suggestions @abhijeet-dhumal! Updated:

  1. Changed if num_nodes: to if num_nodes is not None: so 0 is preserved.
  2. Passed the resolved rt object into _fetch_packages_via_pod() and _get_runtime_image() to avoid duplicate API calls.
  3. Updated get_runtime() and list_runtimes() docstrings to mention both TrainingRuntime and ClusterTrainingRuntime.
  4. Added unit tests using real SDK Runtime and RuntimeTrainer.

@abhijeet-dhumal

Copy link
Copy Markdown
Member

/ok-to-test

@google-oss-prow google-oss-prow Bot added the ok-to-test Approve CI for external contributors label Sep 1, 2026
@abhijeet-dhumal

Copy link
Copy Markdown
Member

Thanks @priyank766 !
/lgtm
/approve

@google-oss-prow google-oss-prow Bot added the lgtm Looks good to me — approved by a reviewer label Sep 1, 2026
@google-oss-prow

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: abhijeet-dhumal

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

@google-oss-prow google-oss-prow Bot added the approved Approved by an approver in OWNERS label Sep 1, 2026
@google-oss-prow
google-oss-prow Bot merged commit ee80e39 into kubeflow:main Sep 1, 2026
17 of 18 checks passed
@google-oss-prow google-oss-prow Bot added this to the v0.1 milestone Sep 1, 2026
@priyank766
priyank766 deleted the fix/get-runtime-metadata-extraction branch September 1, 2026 15:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Approved by an approver in OWNERS lgtm Looks good to me — approved by a reviewer ok-to-test Approve CI for external contributors size/L

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(trainer): extract runtime metadata from SDK Runtime object in get_runtime

3 participants