Skip to content

feat: [cel] add CEL-based assertions and offline VAP evaluation#569

Open
crandles wants to merge 3 commits into
kubernetes-sigs:mainfrom
crandles:cel-core
Open

feat: [cel] add CEL-based assertions and offline VAP evaluation#569
crandles wants to merge 3 commits into
kubernetes-sigs:mainfrom
crandles:cel-core

Conversation

@crandles

@crandles crandles commented May 3, 2026

Copy link
Copy Markdown
Contributor

What type of PR is this?

/kind feature

What this PR does / why we need it:

Adds a cel package for writing CEL-based assertions against Kubernetes objects in tests. Same expression language the API server uses for CRD validation and admission policies, so tests don't drift from the policies they exercise.

Split out from #562 -- this PR includes the following packages: cel (Evaluator, Bindings, Libraries, program cache), cel/policy (offline VAP evaluation, FromVAP), and a small offline examples/cel test that exercises both.

The cel/decoder, cel/feature, cel/wait sub-packages and the live-cluster example will follow in a second PR layered on this one. (WIP: crandles#3)

Design doc: docs/cel-assertions.md.

Which issue(s) this PR fixes:

Fixes #

Special notes for your reviewer:

Does this PR introduce a user-facing change?

Added a `cel` package for writing CEL-based assertions against Kubernetes objects in tests, including offline ValidatingAdmissionPolicy evaluation.

crandles added 3 commits May 3, 2026 13:06
Signed-off-by: Chris Randles <randles.chris@gmail.com>
Signed-off-by: Chris Randles <randles.chris@gmail.com>
Signed-off-by: Chris Randles <randles.chris@gmail.com>
@k8s-ci-robot k8s-ci-robot added the kind/feature Categorizes issue or PR as related to a new feature. label May 3, 2026
@k8s-ci-robot

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: crandles
Once this PR has been reviewed and has the lgtm label, please assign shwethakumbla 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 sig/testing Categorizes an issue or PR as relevant to SIG Testing. label May 3, 2026
@k8s-ci-robot

Copy link
Copy Markdown
Contributor

Hi @crandles. Thanks for your PR.

I'm waiting for a kubernetes-sigs member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work.

Tip

We noticed you've done this a few times! Consider joining the org to skip this step and gain /lgtm and other bot rights. We recommend asking approvers on your previous PRs to sponsor you.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

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 needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels May 3, 2026
@k8s-ci-robot

Copy link
Copy Markdown
Contributor

PR needs rebase.

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 needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label May 8, 2026
@vladimirvivien

Copy link
Copy Markdown
Contributor

/ok-to-test

@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels May 8, 2026
@vladimirvivien

Copy link
Copy Markdown
Contributor

@crandles - At the risk of completely misrepresenting your intent with your API design, I am going to propose a design that simplifies the API a bit. You welcome to push back if you think I am missing your point.

  1. One entry point Evaluator
  2. Add your objects and expressions ahead of time
  3. Collapse Eval, Check
  4. Change Check to always return Result (covering Policy.Check)
  5. Keep Check() with no param; or change with Check(expressions...string)

Single assertion:

Current:

  ev, err := celpkg.NewEvaluator()

  if err := ev.Assert(
      "object.spec.replicas >= 1",
      celpkg.ObjectBinding(dep),
  ); err != nil {
      t.Fatal(err)
  }

Proposed:

ev, err := cel.NewEvaluator(cel.WithObjectBinding(dep))

// or ev.AddObjectBinding(obj)

ev.AddExpression("object.spec.replicas >= 1")
result, err := ev.Check(). // or ev.Evaluate()
if result.Passed() { ... }

Multi-validation policy:

Today:

  pol := policy.Policy{
      Name: "deployment-replicas",
      Validations: []policy.Validation{
          {Expression: "object.spec.replicas >= 1",   Message: "replicas must be at least 1"},
          {Expression: "object.spec.replicas <= 100", Message: "replicas must not exceed 100"},
      },
  }
  ev, _ := celpkg.NewEvaluator()
  res := pol.Check(ev, newDeployment("cel-demo", 2))

Proposed:

ev, _ := cel.NewEvaluator(cel.WithObjectBinding(newDeployment("cel-demo", 2)))
ev.AddExpression("object.spec.replicas >= 1",   "replicas must be at least 1")
ev.AddExpression("object.spec.replicas <= 100", "replicas must not exceed 100")
result, err := ev.Check()
if result.Passed(){ ... }

Let's start with that. Let me know what you think

@vladimirvivien vladimirvivien left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

See my PR comment.

@crandles

Copy link
Copy Markdown
Contributor Author

I think I like your proposal -- no objections with the interface. The struct field options appears overly verbose, with no strong benefit.

Will push up changes shortly.

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. kind/feature Categorizes issue or PR as related to a new feature. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. sig/testing Categorizes an issue or PR as relevant to SIG Testing. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants