fix(spark): validate requested job statuses #874
[APPROVALNOTIFIER] This PR is NOT APPROVED
This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign kramaranya for approval. For more information see the Kubernetes Code Review Process.
The full list of commands accepted by this bot can be found here.
<details open>
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":["kramaranya"]} -->
Please add this check as well.
valid_statuses = set(SparkJobStatus)
if isinstance(status, str):
raise ValueError(
f"status must be a set of SparkJobStatus values, e.g. "
f"{{SparkJobStatus.COMPLETED}}; got {status!r}"
)
if not status or not set(status).issubset(valid_statuses):
Requested changes.
/ok-to-test
/retest
Added an explicit check to reject bare string inputs with a clear ValueError before collection validation (05436a6). Added regression coverage for bare strings while preserving valid string-set and enum-set inputs.
Hi @tariq-hasan, @szaher ,@Shekharrajak could you please take a look at my PR #874 and review it when you get a chance
No need for the comments. The code is self-explanatory.
valid_statuses = set(SparkJobStatus)
if isinstance(status, str):
raise ValueError(
f"status must be a set of SparkJobStatus values, e.g. "
f"{{SparkJobStatus.COMPLETED}}; got {status!r}"
)
Requested changes.
Thanks for taking another look, @tariq-hasan! You're right, the code reads fine without them. I've removed both comments and applied your suggestion as-is in 91c5857, and the tests still pass locally.
Whenever you have a moment, I'd really appreciate another review. Thanks again for your patience and the helpful feedback!
Hi @tariq-hasan, a quick heads-up on the failing example-validation (1.32.0) check. It doesn't look related to this change. The notebook passes a valid {SparkJobStatus.COMPLETED}, so the new validation lets it through, but the Spark job itself goes to FAILED in the cluster a few seconds after submission.
It seems to be the controller/Helm chart mismatch described in #933 (the same failure shows up on #910), which #930 should take care of. Once that lands, I'll rebase this branch on main so the checks run again on the fixed setup.
Thanks a lot for your time!
Thanks for the contribution @Karman580!
/lgtm
Please rebase on main once #930 merges to pass all the Spark E2E workflows.
New changes are detected. LGTM label has been removed.
<!-- coderabbit-ai-generated:v1 -->
<!-- This is an auto-generated comment: summarize by coderabbit.ai -->
<!-- review_stack_entry_start -->
<a href="https://app.coderabbit.ai/change-stack/kubeflow/sdk/pull/874?scope=ghh_OusAJGA4HG8ljoOV0wJvVx8NmnZIjc2DdjmxOu0xZWA&cs_source=review_comment"><picture><source media="(prefers-color-scheme: dark)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-stack-buttons-v1/explain-change-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-stack-buttons-v1/explain-change.svg"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-stack-buttons-v1/explain-change.svg" alt="Explain Change →" height="32"></picture></a>
<!-- review_stack_entry_end -->
<!-- recent_review_status_start -->
No actionable comments were generated in the recent review.
<!-- recent_review_status_end -->
<!-- walkthrough_start -->
<details>
<summary><strong>Walkthrough</strong></summary>
<dl>
<dd>
<br>
The Kubernetes backend now validates requested statuses before polling. It rejects bare strings, empty targets, and values outside SparkJobStatus with ValueError. Documentation and tests describe these conditions.
Changes: Spark job status validation
|Layer / File(s)|Summary|
|:---|:---|
|Validate status targets <br> kubeflow/spark/api/spark_client.py, kubeflow/spark/backends/base.py, kubeflow/spark/backends/kubernetes/backend.py, kubeflow/spark/backends/kubernetes/backend_test.py|The Kubernetes backend rejects bare-string, empty, or invalid status targets before polling. It also rejects nonpositive polling intervals. The documentation and tests cover these conditions, including acceptance of a valid plain-string status inside a set.|
<!-- change_assessment_start -->
Estimated code review effort: 2 (Simple) | ~10 minutes
<!-- change_assessment_commit:"53413fa4f3a0f973937ef2bbb427be791003573c" -->
<!-- change_assessment_priority:{"version":1,"urgency":"p2","manual":false} -->
Change: Bug fix · Severity of issue fixed: Medium
<!-- change_assessment_end -->
Possibly related issues
Suggested reviewers: goku2099
</dd>
</dl>
</details>
<!-- walkthrough_end -->
<details>
<summary><strong>Merge Risk:</strong> <strong><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/risk-unassessed-16.svg" width="16" height="16" align="absmiddle" alt=""></picture> Minimal</strong> · up to <code>53413</code></summary>
<dl>
<dd>
<br>
<!-- final_review_risk_start -->
<!-- final_review_risk_label
Merge Risk: ⚪ Minimal · up to 53413
-->
<!-- final_review_risk_coverage:{"sourceCommitId":"53413fa4f3a0f973937ef2bbb427be791003573c","coveredCommitId":"53413fa4f3a0f973937ef2bbb427be791003573c","kind":"reviewed"} -->
Spark job status waits now fail fast with a ValueError on bare strings, empty sets, or invalid statuses, instead of timing out. The change is small, documented, and covered by tests, so merge-readiness risk is minimal.
<!-- final_review_risk_end -->
</dd>
</dl>
</details>
<details>
<summary><strong>Security architecture risk:</strong> <strong><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/risk-unassessed-16.svg" width="16" height="16" align="absmiddle" alt=""></picture> Minimal</strong> · up to <code>53413</code></summary>
<dl>
<dd>
<br>
<!-- architecture_review_start -->
<!-- architecture_review_label
Security Architecture Review
Security architecture risk: ⚪ Minimal · up to 53413
-->
Invalid status targets now fail before the first Kubernetes job lookup. Valid status sets still use the same read-only wait, with no change to job access or job state.
Retained concerns
No architecture-level concerns identified.
<details>
<summary><strong>Security review details</strong></summary>
<dl>
<dd>
<br>
Security Blast Radius
</dd>
</dl>
</details>
<!-- architecture_review_end -->
</dd>
</dl>
</details>
<!-- pre_merge_checks_walkthrough_start -->
<details>
<summary><strong>Pre-merge checks: <sup><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-counter-passed-14-v2.svg" width="14" height="14" align="middle" alt="Passed" title="Passed"></picture></sup> 4</strong></summary>
<dl>
<dd>
<br>
| Check name | Status | Explanation |
| :------------------------ | :--------------------------------------------------------------------------------------------: | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Description Check | !Passed | Check skipped - CodeRabbit’s high-level summary is enabled. |
| Title check | !Passed | The title "fix(spark): validate requested job statuses" matches the main change. The PR adds ValueError validation to KubernetesBackend.wait_for_job_status for empty or invalid status collections and … |
| Linked Issues check | !Passed | Issue #873 requires KubernetesBackend.wait_for_job_status() to raise ValueError before polling when the requested status is invalid or empty. The reviewed head adds this check in kubeflow/spark/b… |kubeflow/spark/backends/kubernetes/backend.py
| Out of Scope Changes check | !Passed | The whole-PR diff against the merge base contains a backend change in that adds only the status validation and updates the Raises: docstring. I checke… |
<details>
<summary><strong>Full details: Title check</strong></summary>
<dl>
<dd>
<br>
Explanation
The title "fix(spark): validate requested job statuses" matches the main change. The PR adds ValueError validation to KubernetesBackend.wait_for_job_status for empty or invalid status collections and bare strings, and updates the related Raises docs and tests. The title is specific and concise, and it names the affected area (spark) and the change type (fix). It does not mention the bare-string rejection, but that is a detail, not the main point.
</dd>
</dl>
</details>
<details>
<summary><strong>Full details: Linked Issues check</strong></summary>
<dl>
<dd>
<br>
Explanation
Issue #873 requires KubernetesBackend.wait_for_job_status() to raise ValueError before polling when the requested status is invalid or empty. The reviewed head adds this check in kubeflow/spark/backends/kubernetes/backend.py, after the existing polling_interval check and before start_time and the polling loop. The check raises for a bare str, for an empty collection, and for any member outside set(SparkJobStatus). Because SparkJobStatus is a str enum, {"Completed"}, {SparkJobStatus.COMPLETED}, lists, and tuples still pass through set(status). The issue's proposed validation uses set(SparkJobStatus), and the code matches it. The issue's bare-string behavior change is also implemented, so a bare "Completed" now raises. The issue asks for regression tests for {"Complete"}, set(), and {"Completed"}. The PR description reports these cases added to the parametrized test_wait_for_job_status in kubeflow/spark/backends/kubernetes/backend_test.py. I did not re-run the test file. The test evidence comes from the author's report and the change summary. The issue's None default note is conditional and not a current requirement. The docstring updates are consistent with the code.
</dd>
</dl>
</details>
<details>
<summary><strong>Full details: Out of Scope Changes check</strong></summary>
<dl>
<dd>
<br>
Explanation
The whole-PR diff against the merge base contains a backend change in kubeflow/spark/backends/kubernetes/backend.py that adds only the status validation and updates the Raises: docstring. I checked this diff directly. The existing polling_interval <= 0 check is unchanged; the docstring wording now reflects existing behavior. Other changes are documentation-only updates in kubeflow/spark/api/spark_client.py and kubeflow/spark/backends/base.py, which describe the same ValueError conditions. The test file changes cover the issue's regression cases. No change was found that is unrelated to #873.
</dd>
</dl>
</details>
</dd>
</dl>
</details>
<!-- pre_merge_checks_walkthrough_end -->
<!-- finishing_touch_checkbox_start -->
<details>
<summary><strong>Finishing Touches</strong></summary>
<dl>
<dd>
<br>
<details open>
<summary><strong>Generate unit tests (beta)</strong></summary>
<dl>
<dd>
<br>
</dd>
</dl>
</details>
</dd>
</dl>
</details>
<!-- finishing_touch_checkbox_end -->
<!-- autopilot:start -->
<!-- autopilot:end -->
<!-- recent_review_start -->
<h6>
<details>
<summary><strong><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/info.svg" width="12" height="12" align="absmiddle" alt=""></picture> Recent review info</strong></summary>
<dl>
<dd>
<br>
<!-- review_info_content -->
<details>
<summary><strong><picture><source media="(prefers-color-scheme: dark)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/cog-dark.svg"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/cog.svg" width="16" height="16" align="absmiddle" alt=""></picture> Run configuration</strong></summary>
<dl>
<dd>
<br>
</dd>
</dl>
</details>
<details>
<summary><strong><picture><source media="(prefers-color-scheme: dark)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/git-commit-horizontal-dark.svg"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/git-commit-horizontal.svg" width="16" height="16" align="absmiddle" alt=""></picture> Commits</strong></summary>
<dl>
<dd>
<br>
Reviewing files that changed from the base of the PR and between 8961a455ad36c99d1e39cf0e1f36dbd4e11826b6 and 53413fa4f3a0f973937ef2bbb427be791003573c.
</dd>
</dl>
</details>
<details>
<summary><strong><picture><source media="(prefers-color-scheme: dark)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/file-plus-corner-dark.svg"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/file-plus-corner.svg" width="16" height="16" align="absmiddle" alt=""></picture> Files selected for processing: 4</strong></summary>
<dl>
<dd>
<br>
kubeflow/spark/api/spark_client.py kubeflow/spark/backends/base.pykubeflow/spark/backends/kubernetes/backend.py
kubeflow/spark/backends/kubernetes/backend_test.py`
</dd>
</dl>
</details>
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 5 remain after this review.
</dd>
</dl>
</details>
</h6>
<!-- recent_review_end -->
@tariq-hasan Rebased on main now that #930 is in. The rebase was clean and the diff is unchanged; the comment cleanup from 91c5857 is now 53413fa. Spark Examples E2E passes on all four matrix versions (1.34.3, 1.35.8, 1.36.4, 1.37.0). #930 replaced the old 1.32.0 example-validation job, so that check no longer runs.
Locally, all 11 wait_for_job_status cases pass (enum set, plain-string set, invalid value, empty set, bare string), along with the full Spark suite (289 tests). Prow dropped lgtm on the push, so this needs it reapplied.
Hi @tariq-hasan, I've rebased on the latest main, and all 20 checks are now passing, including all Spark E2E workflows. Could you please take another look and reapply /lgtm when convenient
tariq-hasan
Karman580
What this PR does / why we need it:
Problem:
SparkClient.wait_for_job_status()does not check the requestedstatusagainstSparkJobStatus. If a value can never match the job's status, the Kubernetes backend keeps polling untiltimeoutinstead of reporting invalid input. Two cases:{"Complete"}, which is the Trainer/Optimizer spelling. Spark uses"Completed".Solution:
KubernetesBackend.wait_for_job_status()now validates the requested statuses againstset(SparkJobStatus)before polling. It raisesValueErrorif the collection is empty or contains a value that is not a validSparkJobStatus.status.issubset(...)checks in the Trainer and Optimizer Kubernetes backends.set(SparkJobStatus)rather than a hard-coded list, so it stays correct if enum members are added, as #690 proposes.Raises:docs in the backend, theRuntimeBackendbase interface, andSparkClientare updated to match.Compatibility:
SparkJobStatusis astrenum, so enum members and correctly spelled plain strings such as{"Completed"}behave as before.status: set[SparkJobStatus]signature and its{SparkJobStatus.COMPLETED}default are unchanged.- invalid values
- an empty set
- a bare string that is not an exact single status, such as
"Complete". A barestris outside theset[...]type hint.Tests (run locally on macOS):
test_wait_for_job_statusinkubeflow/spark/backends/kubernetes/backend_test.py:-
{"Complete"}raisesValueError-
set()raisesValueError-
{"Completed"}(plain string) returns the jobThe existing enum case
{SparkJobStatus.COMPLETED}is unchanged and still passes.uv run pytest -q kubeflow/spark/backends/kubernetes/backend_test.py -k wait_for_job_status: 10 passed.ValueErrorcases fail. That confirms they cover the defect.make verifydirectly:uv lock --check,ruff check,ruff format --check kubeflow,ty check kubeflow/hub. All passed. The Makefile's GNU sed check stopsmakeitself on macOS.make test-pythondirectly:uv run coverage run --source=kubeflow -m pytest ./kubeflow/. 700 passed.Note: #690 also modifies
wait_for_job_status, in the docstring and theRuntimeErrormessage. The overlap is small, and I am happy to rebase on whichever lands first.Which issue(s) this PR fixes:
Fixes #873
Checklist:
<!-- This is an auto-generated comment: release notes by coderabbit.ai -->
<!-- coderabbit-ai-generated:v1 -->
Summary by CodeRabbit
Bug Fixes
Job-status polling now rejects zero or negative polling intervals, empty status sets, bare-string statuses, and unrecognized status values with a
ValueError.Documentation
Updated the polling method documentation to describe the conditions that raise
ValueError.<!-- end of auto-generated comment: release notes by coderabbit.ai -->