- 
                Notifications
    You must be signed in to change notification settings 
- Fork 2.1k
feat: add valueType field for explicit duration parsing in custom resources #2797
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
feat: add valueType field for explicit duration parsing in custom resources #2797
Conversation
- Add valueType field to MetricGauge struct (duration, quantity, default) - Implement parseDurationValue() function using time.ParseDuration - Add duration parsing support in gauge value extraction - Add 18 comprehensive test cases covering all scenarios - Document valueType field with cert-manager examples This enables explicit parsing of Go duration strings (e.g., '2160h', '30m') to seconds for metrics, particularly useful for cert-manager Certificate resources and other CRs with duration fields. Signed-off-by: SoumyaRaikwar <somuraik@gmail.com>
| This issue is currently awaiting triage. If kube-state-metrics contributors determine this is a relevant issue, they will accept it by applying the  The  Instructions 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: SoumyaRaikwar The full list of commands accepted by this bot can be found here. 
Needs approval from an approver in each of these files:
 Approvers can indicate their approval by writing  | 
| @nmn3m could you please review my pr | 
| var gotFloat float64 | ||
| var err error | ||
|  | ||
| switch c.valueType { | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The value type switching logic is duplicated in two places
Suggestion: Extract this into a helper function:
func (c *compiledGauge) parseValue(value interface{}) (float64, error) {
    switch c.valueType {
    case ValueTypeDuration:
        return parseDurationValue(value)
    case ValueTypeQuantity, ValueTypeDefault:
        return toFloat64(value, c.NilIsZero)
    default:
        return 0, fmt.Errorf("unknown valueType: %s", c.valueType)
    }
}Then use it in both locations:
gotFloat, err := c.parseValue(it)
| // Check if explicit valueType is specified | ||
| var value float64 | ||
| var err error | ||
| switch c.valueType { | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As mentioned above.
| @SoumyaRaikwar, There are linting issues in docs file, please read summary at line  | 
| // The parsed duration is converted to seconds as a float64 for Prometheus compatibility. | ||
| ValueTypeDuration ValueType = "duration" | ||
| // ValueTypeQuantity parses Kubernetes resource quantities (e.g., "250m" for millicores, "1Gi" for memory). | ||
| ValueTypeQuantity ValueType = "quantity" | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure the difference between ValueTypeDefault and ValueTypeQuantity , Both cases call toFloat64(), which already includes automatic quantity detection.
What this PR does
This PR adds a
valueTypefield to custom resource state metrics gauge configuration, enabling explicit parsing of Go duration strings to seconds. This is particularly useful for resources like cert-manager Certificates that store durations as strings (e.g., "2160h", "720h").Currently, kube-state-metrics automatically detects value types but fails to parse Go duration strings, resulting in errors like:
Which issue(s) this PR fixes:
Fixes #2625
Implementation Details
Code Changes
ValueTypefield toMetricGaugestruct with options:duration,quantity, and default (empty string)parseDurationValue()function using Go'stime.ParseDurationvalueTypein both direct path and optimization code pathsDocumentation
customresourcestate-metrics.mdtime.ParseDurationformats)Example Usage
Resulting metrics:
Supported Duration Formats
The
durationvalueType supports Go'stime.ParseDurationformat:"1h","24h","2160h"(90 days)"30m","90m""45s""500ms""100us"or"100µs""1000ns""1h30m45s","2h15m30s"All durations are converted to seconds as float64 for Prometheus compatibility.
Backward Compatibility
Fully backward compatible
valueTypefield continues to use existing auto-detection behavior