Skip to content

fix(trainer): merge repeated Labels and Annotations options - #1005

Open
Karman580 wants to merge 1 commit into
kubeflow:mainfrom
Karman580:fix/trainer-merge-labels-annotations
Open

Karman580 wants to merge 1 commit into
kubeflow:mainfrom
Karman580:fix/trainer-merge-labels-annotations

Conversation

@Karman580

@Karman580 Karman580 commented Nov 29, 2026 •

Copy link
Copy Markdown

What this PR does / why we need it:

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

Reproduction script from #1004 after the fix:

labels      : {'team': 'ml-platform', 'experiment': 'exp-42'}
annotations : {'owner': 'alice', 'ticket': 'JIRA-1'}

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):

Fixes #1004

Checklist:

  • Docs included if any changes are user facing

Summary by CodeRabbit

  • Bug Fixes
    • Repeated label and annotation options now combine their entries. Later options overwrite conflicting values while preserving other entries.

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.

Fixes kubeflow#1004

Signed-off-by: Karman Singh Talwar <flyingoffkarmansinghtalwar@gmail.com>
@google-oss-prow

Copy link
Copy Markdown
Contributor

[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 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

@coderabbitai

coderabbitai Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

Explain Change →

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.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Low

Suggested reviewers: fiona-waters, neha-nimsatkar

Merge Risk: Minimal · up to 173b1

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: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed 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 Passed [#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 Passed 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
Recent review info

Run configuration

  • Source mode: Auto
  • Repository: kubeflow/sdk
  • Branch: main
  • File: .coderabbit.yaml
  • Commit: 8961a45
  • Layers: Repository YAML
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: cd86332e-b735-4c08-b7d9-0d3ebc1a578c
Commits

Reviewing files that changed from the base of the PR and between 8961a45 and 173b133.

Files selected for processing: 3

  • kubeflow/trainer/backends/kubernetes/backend_test.py
  • kubeflow/trainer/options/kubernetes.py
  • kubeflow/trainer/options/kubernetes_test.py

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

@Karman580

Copy link
Copy Markdown
Author

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: Ready to Merge

Development

Successfully merging this pull request may close these issues.

bug(trainer): repeated Labels / Annotations options overwrite each other instead of merging

1 participant