Skip to content

Add pydantic Duration type - #2784

Merged
kubernetes-prow[bot] merged 2 commits into
kubernetes-client:masterfrom
chala2001:pydantic-duration
Nov 21, 2026
Merged

kubernetes-prow[bot] merged 2 commits into
kubernetes-client:masterfrom
chala2001:pydantic-duration

Conversation

@chala2001

Copy link
Copy Markdown
Contributor

What type of PR is this?

/kind feature

What this PR does / why we need it:

Adds kubernetes.utils.Duration, a pydantic type for metav1.Duration. It reads and writes Go duration strings like 720h0m0s, so custom resource models can use it with CustomObjectsApi.

Also adds the missing type hint on parse_duration.

Which issue(s) this PR fixes:

Fixes #2764

Special notes for your reviewer:

Values are stored as timedelta, so anything below a microsecond gets rounded.

Does this PR introduce a user-facing change?

Add `kubernetes.utils.Duration`, a pydantic type for metav1.Duration.

@kubernetes-prow kubernetes-prow Bot added release-note Denotes a PR that will be considered when it comes time to generate release notes. kind/feature Categorizes issue or PR as related to a new feature. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Nov 15, 2026
@kubernetes-prow
kubernetes-prow Bot requested review from roycaihw and yliaog November 15, 2026 09:43
@kubernetes-prow kubernetes-prow Bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Nov 15, 2026
@chala2001

Copy link
Copy Markdown
Contributor Author

@yliaog could you please review this ?

@yliaog

yliaog commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

OverflowError escapes Pydantic validation uncaught (duration.py in diff):

_parse_go_duration() delegates to durationpy.from_str(value), which passes the computed microseconds directly to datetime.timedelta(microseconds=...).

For very large inputs that exceed timedelta's range (e.g., "99999999999h" or "1" + "0"*310 + "h"), durationpy.from_str() raises OverflowError — not ValueError.

Pydantic v2 only converts ValueError / AssertionError into ValidationError, so Spec(ttl="99999999999h") crashes with an unhandled OverflowError instead of raising ValidationError.

Suggested fix:

python
try:
return durationpy.from_str(value)
except OverflowError as exc:
raise ValueError("Duration out of range: {}".format(value)) from exc

And add a case like "99999999999h" to test_rejects_invalid_durations.

@chala2001

Copy link
Copy Markdown
Contributor Author

Thanks @yliaog, good catch. With durationpy 0.10 this did raise OverflowError. 0.11 (our minimum in requirements.txt) already wraps it in DurationError, which is a ValueError, so it was fine there. I added an except OverflowError that raises ValueError anyway, and both of your examples to the invalid durations test. Checked on 0.10 and 0.11, fixed in 03d55e8.

@yliaog

yliaog commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

thanks for the PR
/lgtm
/approve

@kubernetes-prow kubernetes-prow Bot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Nov 21, 2026
@kubernetes-prow

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: chala2001, yliaog

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

@kubernetes-prow kubernetes-prow Bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Nov 21, 2026
@kubernetes-prow
kubernetes-prow Bot merged commit 50b6723 into kubernetes-client:master Nov 21, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/feature Categorizes issue or PR as related to a new feature. lgtm "Looks good to me", indicates that a PR is ready to be merged. release-note Denotes a PR that will be considered when it comes time to generate release notes. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Pydantic type representing meta Duration

2 participants