You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix(trainer): merge repeated Labels and Annotations options - #1005
Labels and Annotations assigned their dict to job_spec["metadata"], so when options contained more than one of them, every instance except the last was dropped without an error. This change merges into the existing metadata instead (setdefault(...).update(...)), with the later option winning on a duplicate key. That matches the Spark Labels / Annotations options, and the job spec no longer holds a reference to the caller's dict.
Tests:
backend_test.py: new test_train case with two Labels and two Annotations, checking the metadata of the body sent to create_namespaced_custom_object.
options/kubernetes_test.py: repeated Labels / Annotations merge, later value wins, and the first option's dict is not mutated.
Both new tests fail on current main (1 failed, 15 passed for backend_test.py::test_train, 2 failed for the new option test) and pass with the fix.
Local results:
$ uv run pytest -q kubeflow
724 passed in 182.69s (0:03:02)
$ uv run ruff check --show-fixes kubeflow
All checks passed!
$ uv run ruff format --check kubeflow
93 files already formatted
Users who passed a single Labels / Annotations see no change. The behaviour only changes when an option is repeated, where the earlier keys used to be lost.
Which issue(s) this PR fixes(optional, in Fixes #<issue number>, #<issue number>, ... format, will close the issue(s) when PR gets merged):
Labels and Annotations assigned their dict to the TrainJob metadata, so
when options contained more than one of them only the last one was kept
and the rest were dropped without any error. This breaks the common
pattern of composing shared default options with per-job ones.
Merge into the existing metadata instead, with the later option winning
on a duplicate key. This matches the Spark Labels and Annotations
options and no longer aliases the caller's dict.
Fixeskubeflow#1004
Signed-off-by: Karman Singh Talwar <flyingoffkarmansinghtalwar@gmail.com>
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
Needs approval from an approver in each of these files:
No actionable comments were generated in the recent review.
Walkthrough
Repeated Kubernetes Labels and Annotations options now merge their values into job metadata. Later values replace earlier values for duplicate keys. Tests cover option behavior and the resulting TrainJob metadata.
Changes: Kubernetes metadata options
Layer / File(s)
Summary
Merge labels and annotations kubeflow/trainer/options/kubernetes.py, kubeflow/trainer/options/kubernetes_test.py
Labels and Annotations merge supplied values into existing metadata. Tests check that later values win on duplicate keys, other keys remain, and the first input dictionary is unchanged.
Verify merged metadata in training kubeflow/trainer/backends/kubernetes/backend_test.py
A test_train case verifies that repeated label and annotation options appear together in TrainJob metadata.
Repeated label and annotation options now accumulate instead of silently dropping earlier values. The change is small and tested, and I found no outstanding merge-readiness risk.
Security architecture risk: Low · up to 173b1
Repeated options now keep earlier labels and annotations unless a later option overrides the same key. Kubernetes submission permissions do not change. The remaining risk is limited to custom options that reuse a metadata dictionary: a submission can modify that dictionary even if it fails before creating a TrainJob.
Retained concerns
Low · reliability · inferred: If a preceding callable seeds labels or annotations with a shared dictionary, the new merge mutates it in place. The base left that dictionary untouched. The mutation can survive a failure before submission and affect later or concurrent submissions that reuse the same map.
Security review details
Security Blast Radius
inferred — The direct effect is a different metadata payload on the requested TrainJob in the configured namespace. The base already let a caller submit the same keys and values through one option; downstream policy effects in a live cluster remain unverified.
Trust Boundaries and Controls
observed — The options still check for a Kubernetes backend before changing the local job specification. The backend submits through the same configured Kubernetes client; the merge introduces no separate identity or authorization step.
Hardening Proposals
proposed — Merge into a new dictionary and assign it to the job specification. This preserves later-key precedence without modifying a dictionary supplied by an earlier option.
Pre-merge checks: 4
Check name
Status
Explanation
Description Check
Check skipped - CodeRabbit’s high-level summary is enabled.
Title check
The title "fix(trainer): merge repeated Labels and Annotations options" states the main change. The PR changes the Labels and Annotations options to merge metadata instead of replacing it, with later …
Linked Issues check
[#1004] requires repeated Labels and Annotations options to accumulate keys, with later values winning on duplicate keys. The head diff for kubeflow/trainer/options/kubernetes.py replaces the assignme…
Out of Scope Changes check
The whole-PR scope relative to the merge base 8961a45 covers three files. kubeflow/trainer/options/kubernetes.py contains the two-line fix for Labels and Annotations. …
Full details: Title check
Explanation
The title "fix(trainer): merge repeated Labels and Annotations options" states the main change. The PR changes the Labels and Annotations options to merge metadata instead of replacing it, with later values winning on duplicate keys. The title names the component (trainer), the changed options, and the behavior. It matches the raw summary and the linked issue #1004 objective.
Full details: Linked Issues check
Explanation
[#1004] requires repeated Labels and Annotations options to accumulate keys, with later values winning on duplicate keys. The head diff for kubeflow/trainer/options/kubernetes.py replaces the assignments at Labels.call and Annotations.call with metadata.setdefault("labels", {}).update(self.labels) and the same pattern for annotations. Each option now merges into the shared job_spec metadata, so earlier keys survive and later options overwrite duplicates. The job spec also stores a new dict, not the caller's dict, so the first option's dictionary is not mutated. The backend reads metadata.labels and metadata.annotations after all options run, so the merged values reach ObjectMeta. The PR adds backend_test.py::test_train with repeated options and an options test for merge, precedence, and non-mutation. The PR description reports these tests fail on main and pass with the change. I did not re-run the tests. The single-option behaviour is unchanged because setdefault followed by update yields the same mapping.
Full details: Out of Scope Changes check
Explanation
The whole-PR scope relative to the merge base 8961a45 covers three files. kubeflow/trainer/options/kubernetes.py contains the two-line fix for Labels and Annotations. kubeflow/trainer/options/kubernetes_test.py and kubeflow/trainer/backends/kubernetes/backend_test.py contain tests for the same behaviour. No change touches RuntimePatch or unrelated code, which the issue lists as a separate problem (#911). All changes support objective #1004.
Finishing Touches
Generate unit tests (beta)
Create a new PR
Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.
Could a maintainer add ok-to-test so the unit test, code quality and E2E workflows can run? They are waiting for approval since this is from a fork. The full unit suite passes locally (724 passed).
This branch has not been deployed
No deployments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this PR does / why we need it:
LabelsandAnnotationsassigned their dict tojob_spec["metadata"], so whenoptionscontained more than one of them, every instance except the last was dropped without an error. This change merges into the existing metadata instead (setdefault(...).update(...)), with the later option winning on a duplicate key. That matches the SparkLabels/Annotationsoptions, and the job spec no longer holds a reference to the caller's dict.Tests:
backend_test.py: newtest_traincase with twoLabelsand twoAnnotations, checking the metadata of the body sent tocreate_namespaced_custom_object.options/kubernetes_test.py: repeatedLabels/Annotationsmerge, later value wins, and the first option's dict is not mutated.Both new tests fail on current
main(1 failed, 15 passedforbackend_test.py::test_train,2 failedfor the new option test) and pass with the fix.Local results:
Reproduction script from #1004 after the fix:
Users who passed a single
Labels/Annotationssee no change. The behaviour only changes when an option is repeated, where the earlier keys used to be lost.Which issue(s) this PR fixes (optional, in
Fixes #<issue number>, #<issue number>, ...format, will close the issue(s) when PR gets merged):Fixes #1004
Checklist:
Summary by CodeRabbit