Skip to content
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

OpenAPI-based CEL type library #113312

Merged
merged 8 commits into from
Feb 18, 2023

Conversation

jiahuif
Copy link
Member

@jiahuif jiahuif commented Oct 24, 2022

What type of PR is this?

/kind feature

What this PR does / why we need it:

  • rewrite the CEL schema library to use kube-openapi, based on that of staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/schema/cel, which uses structural.Structural.
  • two implementations of schema resolver, one using client-go OpenAPI v3 discovery mechanism, the other with compiled-in OpenAPI definitions. Compiled-in resolver can be useful for the API server itself while ongoing development of client side CEL evaluation needs the discovery resolver.
  • create an adaptor between CRD Structural and OpenAPI schemas. This is to avoid recursive/deep conversion that takes an O(N) time where N is the depth of nesting in a schema.
  • change existing library for CRDs to use the adaptor.
  • add integration test for above

Special notes for your reviewer:

To limit the size of this PR, it does not include the part where the admission plugin uses the library.

Does this PR introduce a user-facing change?

NONE

Additional documentation e.g., KEPs (Kubernetes Enhancement Proposals), usage docs, etc.:

- [KEP]: https://github.com/kubernetes/enhancements/issues/3488

@k8s-ci-robot k8s-ci-robot added do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. release-note-none Denotes a PR that doesn't merit a release note. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. 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. do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. labels Oct 24, 2022
@k8s-ci-robot k8s-ci-robot added area/apiserver sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. and removed do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels Oct 24, 2022
@jiahuif jiahuif force-pushed the feature/cel/builtins branch 4 times, most recently from b6c7697 to 68225fb Compare October 25, 2022 00:15
@jiahuif jiahuif changed the title [WIP] rewrite the CEL schema library rewrite the CEL schema library Oct 25, 2022
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Oct 25, 2022
Copy link
Contributor

@cici37 cici37 left a comment

Choose a reason for hiding this comment

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

Note: I guess we don't need the compilation and validation files here(might cause confusion). And the code would be tested in #113314. Currently the conversion(between object and ref.Val) failed(possible reason of lacking ref resolver?). Will retest after changes are made. Thank you!

Maybe worth to also add builtin type tests in this PR?

@jiahuif jiahuif force-pushed the feature/cel/builtins branch 2 times, most recently from acdcd86 to 32f791f Compare October 27, 2022 00:07
@leilajal
Copy link
Contributor

/triage accepted

@k8s-ci-robot k8s-ci-robot added triage/accepted Indicates an issue or PR is ready to be actively worked on. and removed needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Oct 27, 2022
Copy link
Contributor

@jpbetz jpbetz left a comment

Choose a reason for hiding this comment

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

Also add tests showing how this can be used with a native types?

// UnstructuredToVal converts a Kubernetes unstructured data element to a CEL Val.
// The root schema of custom resource schema is expected contain type meta and object meta schemas.
// If Embedded resources do not contain type meta and object meta schemas, they will be added automatically.
func UnstructuredToVal(unstructured interface{}, schema *spec.Schema) ref.Val {
Copy link
Contributor

Choose a reason for hiding this comment

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

Given that we primarily intend to support native types with this, should this instead be something like ObjectToVal(obj runtime.Object) ref.Val and we can have the implementation look up the schema and convert the runtime.Object to unstructured using DefaultUnstructuredConverter? This would allow us to change the implementation in the future (e.g. use a reflection based approach instead of conversion to unstructured) without changing the function signature.

Copy link
Member Author

Choose a reason for hiding this comment

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

Current implementation let the caller to specify the schema because eventually the schema needs to be cached. It's better for the caller to maintain the cache to avoid having to provide another set of schema cache invalidation.

@jiahuif jiahuif force-pushed the feature/cel/builtins branch 8 times, most recently from 5accc99 to f619a34 Compare February 8, 2023 23:50
@wenjiaswe
Copy link
Contributor

/test pull-kubernetes-unit

Copy link
Contributor

@jpbetz jpbetz left a comment

Choose a reason for hiding this comment

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

Functionality looks good. The copy-if-changed looks right. The wrapper is nice. Just a few minor comments.

@jiahuif jiahuif force-pushed the feature/cel/builtins branch 2 times, most recently from 4bb675d to b29f618 Compare February 13, 2023 18:38
@jiahuif
Copy link
Member Author

jiahuif commented Feb 13, 2023

/cc @alexzielenski

@jpbetz
Copy link
Contributor

jpbetz commented Feb 13, 2023

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Feb 13, 2023
@k8s-ci-robot
Copy link
Contributor

LGTM label has been added.

Git tree hash: 6d4c70f11001bbb6888a57a3c0ce989f7a28681e

@jiahuif
Copy link
Member Author

jiahuif commented Feb 14, 2023

All comments addressed, could you take another look when available? @liggitt

@liggitt
Copy link
Member

liggitt commented Feb 17, 2023

All comments addressed, could you take another look when available? @liggitt

ack, should be able to look later today

Copy link
Member

@liggitt liggitt left a comment

Choose a reason for hiding this comment

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

/approve

Thanks for all the iterations on this. There are still a couple spots that would be good to be sure we have benchmarks exercising (we might already? I'm not sure), but I'm very happy to see the full deep copies and recursive conversions go away

Comment on lines +66 to +69
for n, prop := range s.Structural.Properties {
s := prop
res[n] = &Structural{Structural: &s}
}
Copy link
Member

Choose a reason for hiding this comment

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

as a follow-up, it would be good to make sure we have benchmarks of validation-time calls that verify even this shallow-copy isn't problematic in terms of allocations

unkeyedItems: items,
}
func makeMapList(sts *schema.Structural, items []interface{}) (rv common.MapList) {
return common.MakeMapList(&model.Structural{Structural: sts}, items)
Copy link
Member

Choose a reason for hiding this comment

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

same comment here about a follow-up benchmark that calls validate with a CRD and CR that ends up exercising this call so we have visibility to allocations

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: jiahuif, liggitt

The full list of commands accepted by this bot can be found here.

The pull request process is described here

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 approved Indicates a PR has been approved by an approver from all required OWNERS files. label Feb 18, 2023
@k8s-triage-robot
Copy link

The Kubernetes project has merge-blocking tests that are currently too flaky to consistently pass.

This bot retests PRs for certain kubernetes repos according to the following rules:

  • The PR does have any do-not-merge/* labels
  • The PR does not have the needs-ok-to-test label
  • The PR is mergeable (does not have a needs-rebase label)
  • The PR is approved (has cncf-cla: yes, lgtm, approved labels)
  • The PR is failing tests required for merge

You can:

/retest

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. area/apiserver area/dependency Issues or PRs related to dependency changes area/test 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. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. release-note-none Denotes a PR that doesn't merit a release note. sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. sig/testing Categorizes an issue or PR as relevant to SIG Testing. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. triage/accepted Indicates an issue or PR is ready to be actively worked on.
Projects
Development

Successfully merging this pull request may close these issues.

None yet

9 participants