Skip to content

fix(trainer): reject ContainerPatch env entries with both value and valueFrom - #884

Open
SAURABHSALVE wants to merge 1 commit into
kubeflow:mainfrom
SAURABHSALVE:fix/trainer-containerpatch-valuefrom
Open

SAURABHSALVE wants to merge 1 commit into
kubeflow:mainfrom
SAURABHSALVE:fix/trainer-containerpatch-valuefrom

Conversation

@SAURABHSALVE

@SAURABHSALVE SAURABHSALVE commented Oct 20, 2026 •

Copy link
Copy Markdown

What this PR does / why we need it:

ContainerPatch.__post_init__ validates each env entry but only checks that at least one of value/valueFrom is present. It never rejects an entry that sets both, even though the Kubernetes API treats them as mutually exclusive: EnvVar.valueFrom's own description is "Cannot be used if value is not empty." Such a patch currently passes SDK validation and only fails later, server-side, when the TrainJob is actually created.

This adds a check that rejects an env entry when value is a non-empty string and valueFrom is also set, matching Kubernetes' own rule. An empty-string value alongside valueFrom is still accepted, since the Kubernetes API only disallows the combination when value is not empty.

from kubeflow.trainer.options.kubernetes import ContainerPatch

ContainerPatch(
    name="main",
    env=[{
        "name": "LOG_LEVEL",
        "value": "debug",
        "valueFrom": {"fieldRef": {"fieldPath": "metadata.name"}},
    }],
)
# Before: no error -- accepted, then rejected server-side at TrainJob creation.
# After:  ValueError: env 'value' and 'valueFrom' are mutually exclusive

Tests:

  • Added a case to the existing test_container_patch_validation parametrize for the new rejection.
  • Added test_container_patch_accepts_valid_env_entries covering value only, valueFrom only, and an empty-string value with valueFrom, to confirm previously-valid inputs still pass.

Which issue(s) this PR fixes:

Fixes #883

Checklist:

  • Docs included if any changes are user facing

🤖 Generated with Claude Code

…alueFrom

ContainerPatch.__post_init__ validated that each env entry has at
least one of value/valueFrom, but never rejected having both. The
Kubernetes API treats these as mutually exclusive (EnvVar.valueFrom:
"Cannot be used if value is not empty"), so such a patch passed SDK
validation and only failed later when the TrainJob was created.

Reject env entries where value is a non-empty string and valueFrom is
also set. An empty string value alongside valueFrom is still accepted,
matching the Kubernetes API's own exemption for an empty value.

Fixes kubeflow#883

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Signed-off-by: Saurabh Vijay Salve <saurabhsalve9999@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 andreyvelich 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

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): ContainerPatch accepts env entries with both value and valueFrom set

1 participant