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

Escape CEL Reserved Keywords when Generating Validation Rules #255

Merged
merged 3 commits into from
Aug 21, 2023

Conversation

dalton-hill-0
Copy link
Contributor

Description of your changes

Fixes #254

Wraps CEL reserved keywords found in Validation Rules (i.e., __{keyword}__).

For instance, if the object has a field "namespace", this would be the before and after in zz_<obj>_types.go

- // +kubebuilder:validation:XValidation:rule="self.managementPolicy == 'ObserveOnly' || has(self.forProvider.namespace)",message="namespace is a required parameter"
+ // +kubebuilder:validation:XValidation:rule="self.managementPolicy == 'ObserveOnly' || has(self.forProvider.__namespace__)",message="namespace is a required parameter"

The same diff, for the generated CRD

            x-kubernetes-validations:
            - message: namespace is a required parameter
-             rule: self.managementPolicy == 'ObserveOnly' || has(self.forProvider.namespace)
+             rule: self.managementPolicy == 'ObserveOnly' || has(self.forProvider.__namespace__)

I have:

  • Read and followed Crossplane's contribution process.
  • Run make reviewable to ensure this PR is ready for review.
  • Added backport release-x.y labels to auto-backport this PR if necessary.

How has this code been tested

  • Updated existing unit tests to include Validation Rules.
  • Manually wrapped code in generated CRDs to ensure the escaping actually resolves the issue.

Signed-off-by: dalton hill <dalton.hill.0@protonmail.com>
@Upbound-CLA
Copy link

Upbound-CLA commented Aug 14, 2023

CLA assistant check
All committers have signed the CLA.

Signed-off-by: dalton hill <dalton.hill.0@protonmail.com>
@dalton-hill-0
Copy link
Contributor Author

PR is ready for review
@ulucinar @sergenyalcin

Copy link
Contributor

@lsviben lsviben left a comment

Choose a reason for hiding this comment

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

Minor comment, otherwise LGTM. Thanks @dalton-hill-0 for the contribution!

pkg/types/builder_test.go Outdated Show resolved Hide resolved
Signed-off-by: dalton hill <dalton.hill.0@protonmail.com>
@@ -444,3 +455,12 @@ func fieldPathWithWildcard(parts []string) string {
}
return seg.String()
}

func sanitizePath(p string) string {
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit:

Suggested change
func sanitizePath(p string) string {
func escapeName(p string) string {


var (
// ref: https://github.com/google/cel-spec/blob/v0.6.0/doc/langdef.md#syntax
celReservedKeywords = []string{"true", "false", "null", "in", "as", "break", "const", "continue",
Copy link
Collaborator

@ulucinar ulucinar Aug 21, 2023

Choose a reason for hiding this comment

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

nit: We may use a map[string]struct{} (or the higher-level "k8s.io/apimachinery/pkg/util/sets.Set ) for O(1) lookups. Also the list we search has a relatively static small size.

@ulucinar ulucinar merged commit e620c62 into crossplane:main Aug 21, 2023
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Escape CEL Reserved Keywords in Validation Rules
4 participants