Skip to content

feat: enable use of CEL expressions for CR valueFrom#2868

Open
alexandernorth wants to merge 13 commits intokubernetes:mainfrom
alexandernorth:feature/custom-resource-cel-expressions
Open

feat: enable use of CEL expressions for CR valueFrom#2868
alexandernorth wants to merge 13 commits intokubernetes:mainfrom
alexandernorth:feature/custom-resource-cel-expressions

Conversation

@alexandernorth
Copy link
Copy Markdown

@alexandernorth alexandernorth commented Feb 6, 2026

What this PR does / why we need it:

Adds CEL (Common Expression Language) support to Custom Resource State metrics for transforming and extracting values from custom resources.

CEL expressions enable more complex calculations, conditional logic, array/object iteration, and dynamic label generation directly in metric configurations, with a low cost overhead. This is more flexible than the existing path-based approach for complex data structures (multi-level, iteration, etc).

Examples:

Static number:

valueFrom:
  celExpr: "1.0"

Iterate over arrays adding dynamic labels:

valueFrom:
  celExpr: "value.map(c, WithLabels(c.status, {'type': c.type}))"

Path-based valueFrom continues to work unchanged for backwards compatibility.

The documentation is also updated to include examples and details on the implementation customresourcestate-metrics.md

How does this change affect the cardinality of KSM: (increases, decreases or does not change cardinality)
None.

Which issue(s) this PR fixes: (optional, in fixes #<issue number>(, fixes #<issue_number>, ...) format, will close the issue(s) when PR gets merged)
I did not find a specific comment, but it does solve a TODO regarding multi-length valueFrom paths: #1958 (comment)

@k8s-ci-robot
Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: alexandernorth
Once this PR has been reviewed and has the lgtm label, please assign mrueg for approval. For more information see the 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

@k8s-ci-robot k8s-ci-robot added the needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. label Feb 6, 2026
@k8s-ci-robot k8s-ci-robot requested a review from mrueg February 6, 2026 16:15
@k8s-ci-robot
Copy link
Copy Markdown
Contributor

This issue is currently awaiting triage.

If kube-state-metrics contributors determine this is a relevant issue, they will accept it by applying the triage/accepted label and provide further guidance.

The triage/accepted label can be added by org members by writing /triage accepted in a comment.

Details

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.

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Feb 6, 2026
@github-project-automation github-project-automation Bot moved this to Needs Triage in SIG Instrumentation Feb 6, 2026
@k8s-ci-robot k8s-ci-robot added the size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. label Feb 6, 2026
@alexandernorth alexandernorth changed the title Feature/custom resource cel expressions feat: enable use of CEL expressions for CR valueFrom Feb 6, 2026
@amy
Copy link
Copy Markdown

amy commented Apr 23, 2026

Is there any chance you could provide a sample CEL expression on picking up an array? So this part: array/object iteration @alexandernorth Thank you!

@alexandernorth
Copy link
Copy Markdown
Author

Is there any chance you could provide a sample CEL expression on picking up an array? So this part: array/object iteration @alexandernorth Thank you!

Hi @amy as discussed in the slack channel, I added documentation for using CEL Expressions in docs/metrics/extend/customresourcestate-metrics.md#cel-expressions. Hope this helps!

@alexandernorth alexandernorth force-pushed the feature/custom-resource-cel-expressions branch from c1cfeb2 to 516d53a Compare April 23, 2026 15:05
@amy
Copy link
Copy Markdown

amy commented Apr 23, 2026

Is there any chance you could provide a sample CEL expression on picking up an array? So this part: array/object iteration @alexandernorth Thank you!

Hi @amy as discussed in the slack channel, I added documentation for using CEL Expressions in docs/metrics/extend/customresourcestate-metrics.md#cel-expressions. Hope this helps!

amazing! Hope this gets merged 🤞

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds CEL (Common Expression Language) support to CustomResourceState Gauge valueFrom, enabling expression-based value extraction/transformation and dynamic labels (via WithLabels) while preserving the existing path-based behavior for backward compatibility.

Changes:

  • Introduces a ValueFrom config type that supports either pathValueFrom (legacy path extraction) or celExpr (CEL extraction).
  • Refactors Gauge value extraction behind a valueExtractor strategy with separate path-based and CEL-based implementations.
  • Adds a CEL helper library (WithLabels) plus extensive unit tests and documentation updates.

Reviewed changes

Copilot reviewed 14 out of 14 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
pkg/customresourcestate/registry_factory.go Wires Gauge compilation to a value-extractor strategy and updates StateSet to use the new ValueFrom type.
pkg/customresourcestate/registry_factory_test.go Updates/reshapes tests; adds an error-case test for invalid CEL+labelFromKey config.
pkg/customresourcestate/path_value_extractor.go Extracts prior path-based Gauge value extraction logic into a dedicated strategy.
pkg/customresourcestate/path_value_extractor_test.go Re-homes and expands path-based extraction tests.
pkg/customresourcestate/interfaces.go Introduces the valueExtractor interface.
pkg/customresourcestate/config_metrics_types.go Adds the ValueFrom struct and (JSON/YAML) unmarshalling to preserve legacy config shapes.
pkg/customresourcestate/config_metrics_types_test.go Tests ValueFrom unmarshalling for JSON/YAML, including CEL form.
pkg/customresourcestate/cel_value_extractor.go Implements CEL-based extraction and label merging behavior.
pkg/customresourcestate/cel_value_extractor_test.go Adds coverage for WithLabels, conversions, real-CR-like examples, and compilation failures.
pkg/cel/withlabels.go Defines the WithLabels CEL value type.
pkg/cel/library/library.go Registers WithLabels as a CEL function in a KSM CEL library.
pkg/cel/README.md Documents the CEL extension library and WithLabels.
go.mod Promotes cel-go to a direct dependency.
docs/metrics/extend/customresourcestate-metrics.md Adds a CEL quick start and detailed CEL configuration documentation for CustomResourceState Gauges.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@@ -196,7 +196,8 @@ func newCompiledMetric(m Metric) (compiledMetric, error) {
if err != nil {
return nil, fmt.Errorf("each.stateSet: %w", err)
}
Comment on lines +56 to +57
// Ensure fields are mutually exclusive
if len(valueFromStruct.PathValueFrom) > 0 && valueFromStruct.CelExpr != "" {
Comment on lines +41 to +43
// unmarshallValueFrom unmarshalls ValueFrom from either a string slice or struct.
func (vf *ValueFrom) unmarshallValueFrom(unmarshal func(interface{}) error) error {
var stringSlice []string
Comment on lines 215 to 220
type compiledGauge struct {
compiledCommon
labelFromKey string
ValueFrom valuePath
extractor valueExtractor
NilIsZero bool
}
Comment on lines +108 to +114
labels := make(map[string]string)
addPathLabels(v, s.labelFromPath, labels)
// Apply AdditionalLabels last so they are not overwritten
for k, v := range value.Labels {
labels[k] = v
}
value.Labels = labels
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files.

Projects

Status: Needs Triage

Development

Successfully merging this pull request may close these issues.

4 participants