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

Add support for validating XRD's openAPIV3Schema #145

Merged
merged 2 commits into from
Feb 2, 2022

Conversation

tnthornton
Copy link
Member

@tnthornton tnthornton commented Jan 31, 2022

Description of your changes

This change set adds the following new features to xpls:

  • initial support for validating the openAPIV3Schema definition in an XRD using CRD's validation mechanisms
  • adds functionality to warn the end user if a validator cannot be found for a GVK

In addition, the go mod minimum version requirement was bumped in order to account for repo name changes in github.com/googleapis/gnostic that transitive dependencies weren't able to handle very well.

Fixes #https://github.com/upbound/up-ls/issues/59

I have:

  • Read and followed Upbound's contribution process.
  • Run make reviewable to ensure this PR is ready for review.

How has this code been tested

Beyond unit tests, general functional tests were performed to verify we saw the expected warns and errors. You can see in the screenshots below what that looks like:

openAPIV3Schema validation:

Screen Shot 2022-01-31 at 12 28 19 PM

warn end user if GVK is missing due to XRD's definition being broken:

Screen Shot 2022-01-31 at 1 47 09 PM

github.com/docker/distribution => github.com/docker/distribution v0.0.0-20191216044856-a8371794149d
github.com/docker/docker => github.com/moby/moby v17.12.0-ce-rc1.0.20200618181300-9dc6525e6118+incompatible
github.com/golang/tools => ./internal/vendor/golang.org/x/tools
github.com/googleapis/gnostic => github.com/google/gnostic v0.5.5
Copy link
Member Author

Choose a reason for hiding this comment

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

See note about needing to go to 1.17. This change was rooted in updating k8s.io/apiextensions-apiserver to v0.23.3.

- adds initial support for validating the openAPIV3Schema definition in an XRD using CRD's validation mechanisms
- adds functionality to warn the end user if a validator cannot be found for a GVK
- updates the required version of Go to 1.17 in order to resolve some dependencies in googleapis

Signed-off-by: Taylor Thornton <thornton.tn@gmail.com>
Copy link
Contributor

@hasheddan hasheddan left a comment

Choose a reason for hiding this comment

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

@tnthornton awesome to see this! A few nitpicks and questions, but mostly looks ready to go to me!

@@ -157,6 +161,15 @@ func validatorsFromV1Beta1XRD(x *xpextv1beta1.CompositeResourceDefinition, acc m
}

func validatorsFromV1XRD(x *xpextv1.CompositeResourceDefinition, acc map[schema.GroupVersionKind]*validator.ObjectValidator) error {
errs := validateOpenAPIV3Schema(x)
if errs != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if errs != nil {
if len(errs) != 0 {

Current implementation panics on zero-length slice.


// paths are return from CRD validations using map[key].field notation
func replaceMapKeys(fieldVal string) string {
sampleRegexp := regexp.MustCompile(`(\[([a-zA-Z]*)\])`)
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe we can avoid calling this on every invocation of replaceMapKeys (i.e. compile once then use it where necessary)


type cleaner func(string) string

// if the validations were all move to spec.validation, update the path
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// if the validations were all move to spec.validation, update the path
// if the validations were all moved to spec.validation, update the path

return strings.Replace(fieldVal, "spec.validation", "spec.versions[0].schema", 1)
}

// paths are return from CRD validations using map[key].field notation
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// paths are return from CRD validations using map[key].field notation
// paths are returned from CRD validations using map[key].field notation

}

func trimInvalidDollarSign(fieldVal string) string {
if idx := strings.Index(fieldVal, ".$"); idx != -1 {
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we always expect .$ leading in the path if present? If so, I believe this is functionally equivalent to TrimPrefix, if not it looks like we are cutting off everything before the .$, which I believe would truncate our field path

Copy link
Member Author

@tnthornton tnthornton Feb 2, 2022

Choose a reason for hiding this comment

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

This should being doing the opposite of TrimPrefix. This handles the case where we have a path like so:

spec.versions[0].schema.openAPIV3Schema.$ref"

This function returns:

spec.versions[0].schema.openAPIV3Schema

The reason for that is the first string is not a valid yaml path. So I do what I can to get the error close to the problem point and highlight that they are using an invalid field.

FWIW, I also tried TrimSuffix, however the key with a .$ prefix is not guaranteed to be $ref. The end user can type whatever they want, which is why I ultimately went with the above approach.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah! My apologies, I thought I saw fieldVal[idx:], which I would have caught if I looked closer at your unit test!

Copy link
Member Author

Choose a reason for hiding this comment

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

Ha! No worries 👍

fix typos in doc strings

Signed-off-by: Taylor Thornton <thornton.tn@gmail.com>
}

func trimInvalidDollarSign(fieldVal string) string {
if idx := strings.Index(fieldVal, ".$"); idx != -1 {
Copy link
Contributor

Choose a reason for hiding this comment

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

Ah! My apologies, I thought I saw fieldVal[idx:], which I would have caught if I looked closer at your unit test!

@tnthornton tnthornton merged commit fe57cf0 into upbound:main Feb 2, 2022
@tnthornton tnthornton deleted the xrd-openapi branch February 3, 2022 00:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants