fix: support string duration values in JSON/YAML config files#21468
fix: support string duration values in JSON/YAML config files#21468crawfordxx wants to merge 1 commit intoetcd-io:mainfrom
Conversation
|
Hi @crawfordxx. Thanks for your PR. I'm waiting for a etcd-io member to verify that this patch is reasonable to test. If it is, they should reply with Regular contributors should join the org to skip this step. Once the patch is verified, the new status will be reflected by the I understand the commands that are listed here. DetailsInstructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. |
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: crawfordxx The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
time.Duration fields like watch-progress-notify-interval do not properly unmarshal from JSON strings (e.g. "1m"). This is because time.Duration is just an int64 and does not implement json.Unmarshaler. Add preprocessDurationFields() that converts string duration values to nanosecond integers before YAML/JSON unmarshaling. This applies to all duration config fields, matching the behavior of command-line flags which use time.ParseDuration. Fixes etcd-io#20342 Signed-off-by: majianhan <majianhan@kylinos.cn>
7f6fba3 to
c6708a3
Compare
Summary
watch-progress-notify-intervaland all othertime.Durationconfig fields failing to unmarshal from string values (e.g."1m","500ms") in JSON/YAML config filespreprocessDurationFields()that converts string duration values to nanosecond integers before unmarshaling, matching the behavior of command-line flagsbackend-batch-interval,grpc-keepalive-min-time,grpc-keepalive-interval,grpc-keepalive-timeout,corrupt-check-time,compact-hash-check-time,compaction-sleep-interval,watch-progress-notify-interval,warning-apply-duration,warning-unary-request-duration,downgrade-check-timeFixes #20342
Root Cause
time.Durationis anint64in Go and does not implementjson.Unmarshaler. Whensigs.k8s.io/yamlconverts YAML to JSON then usesencoding/json, string values like"1m"cannot be unmarshaled intotime.Durationfields. Only raw nanosecond integers work, which is not user-friendly.Solution
Before passing config bytes to
yaml.Unmarshal, pre-process known duration field keys: if the value is a string, parse it withtime.ParseDurationand replace it with the equivalent nanosecond integer. Numeric values pass through unchanged.This approach is minimally invasive - it does not change the
Configstruct's public API or field types, and follows the existing pattern of translating config file values inconfigFromFile(similar to how URL string fields are handled).Test plan
watch-progress-notify-intervalparses correctlypreprocessDurationFieldshandles string, numeric, non-duration, and invalid inputsembedpackage tests pass