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

🐛 Don't generate description on metadata of CRD structural schema #511

Merged

Conversation

kevindelgado
Copy link
Contributor

What

Ensures that generated CRDs do not have a description generated from godoc comments on the ObjectMeta of the corresponding go type.

Why

Fixes #386

CRD structural schema requires that if metadata is specified, then only restrictions on metadata.name and metadata.generateName are allowed.

metadata.description is not allowed

xref: kubernetes/kubernetes#86800

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Oct 23, 2020
@k8s-ci-robot k8s-ci-robot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Oct 23, 2020
@kevindelgado
Copy link
Contributor Author

/assign @DirectXMan12
/cc @tamalsaha

@k8s-ci-robot
Copy link
Contributor

@kevindelgado: GitHub didn't allow me to request PR reviews from the following users: tamalsaha.

Note that only kubernetes-sigs members and repo collaborators can review this PR, and authors cannot review their own PRs.

In response to this:

/assign @DirectXMan12
/cc @tamalsaha

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/test-infra repository.

@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Nov 9, 2020
@kevindelgado
Copy link
Contributor Author

Was waiting for #499 to land with the basic gen_integration_test before nudging this, but that's merged now, can you take a look @DirectXMan12

pkg/crd/gen.go Outdated Show resolved Hide resolved
@kevindelgado
Copy link
Contributor Author

friendly ping @DirectXMan12

@DirectXMan12
Copy link
Contributor

/lgtm
/approve

@k8s-ci-robot k8s-ci-robot added lgtm "Looks good to me", indicates that a PR is ready to be merged. approved Indicates a PR has been approved by an approver from all required OWNERS files. labels Nov 23, 2020
@DirectXMan12
Copy link
Contributor

/hold

@k8s-ci-robot k8s-ci-robot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Nov 23, 2020
@DirectXMan12
Copy link
Contributor

/lgtm cancel
/approve cancel

we should be doing this in the output openapi, not in the input godoc

@k8s-ci-robot k8s-ci-robot removed lgtm "Looks good to me", indicates that a PR is ready to be merged. approved Indicates a PR has been approved by an approver from all required OWNERS files. labels Nov 23, 2020
@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Nov 24, 2020
@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Nov 24, 2020
Copy link
Contributor Author

@kevindelgado kevindelgado left a comment

Choose a reason for hiding this comment

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

metadata description is now stripped on the output open API schema instead of the input godoc.

It definitely doesn't feel as clean as the one line fix on the input, but I get that we might want to hang onto the raw godoc for as long as possible.

Let me know if you have any suggestions to clean this up. PTAL @DirectXMan12

pkg/crd/gen.go Outdated
if m, ok := v.Properties["metadata"]; ok {
meta := &m
if meta.Description != "" {
fmt.Fprintf(os.Stderr, "Warning: metadata description unsupported. Removing description: %s\n", meta.Description)
Copy link
Contributor Author

@kevindelgado kevindelgado Nov 24, 2020

Choose a reason for hiding this comment

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

Is this warning actually useful or can we assume that users know their ObjectMeta godoc comments will not make it into the CRD schema?

Copy link

@whalecold whalecold Nov 25, 2020

Choose a reason for hiding this comment

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

Hi, I test your code in myself environment but it don't work well because of the other fields may container the struct JSONSchemaPropsalso. I try to changed the code as follow and it works. That's only a suggestion.

	if v == nil {
		return
	}
	for str, property := range v.Properties {
		if str == "metadata" {
			if property.Description != "" {
				fmt.Fprintf(os.Stderr, "Warning: version metadata description unsupported. Removing description: %s\n", property.Description)
				property.Description = ""
			}
		}
		if property.Items != nil {
			removeDescriptionFromMetadataProps(property.Items.Schema)
			for i := range property.Items.JSONSchemas {
				removeDescriptionFromMetadataProps(&property.Items.JSONSchemas[i])
			}
		}
		if property.AdditionalProperties != nil {
			removeDescriptionFromMetadataProps(property.AdditionalProperties.Schema)
		}
		if property.AdditionalItems != nil {
			removeDescriptionFromMetadataProps(property.AdditionalItems.Schema)
		}
		removeDescriptionFromMetadataProps(&property)
		v.Properties[str] = property
	}

Choose a reason for hiding this comment

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

friendly ping @kevindelgado

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks!

Copy link
Contributor

Choose a reason for hiding this comment

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

top-level metadata shouldn't have any fields -- it's explicitly stripped because the kubernetes API server replaces/ignores most of it.

AFAIK, the API server only enforces this constraint on the top-level metadata field, not any embedded metadata, so it should be sufficient to simply clear description from the the root metadata field, and leave it at that.

@whalecold if this doesn't work, can you please provide an example that's giving you issues

Copy link
Contributor

Choose a reason for hiding this comment

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

I think this warning might actually just confuse people too, so prob best to remove it -- it's gonna hit a lot of codebases and doesn't really have a material impact on them.

if crdVersions[i] == "v1beta1" {
removeDefaultsFromSchemas(crd.(*apiextlegacy.CustomResourceDefinition))
removeDescriptionFromMetadataLegacy(crd.(*apiextlegacy.CustomResourceDefinition))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not loving the duplicated functions for v1 and v1beta1, but I couldn't find a cleaner way to make it work. I thought of passing the generic runtime.Object into a single removeDescriptionFromMetadata() function, but it didn't seem any better since we already have to do the crdVersions check for removing defaults. Let me know if there's a better way.

@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Dec 8, 2020
@kevindelgado
Copy link
Contributor Author

PTAL @DirectXMan12

pkg/crd/gen.go Outdated
}
// property var reference is fine -- we handle the persistence of the modfications on the line below
//nolint:gosec
removeDescriptionFromMetadataProps(&property)
Copy link
Contributor

Choose a reason for hiding this comment

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

why are we recursing here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

See feedback from @whalecold here

I took it to mean that there could be objects embedded within the object (e.g. within the spec field), is that wrong? See the first commit for the non-recursive approach.

@whalecold can you provide an example of where this breaks if you don't recurse the fields that contain addition JSONSchemaProps?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Reverted back to modifying only the top level metadata description, as @DirectXMan12 and I tested that kube apiserver accepts embedded objects with metadata descriptions.

@kevindelgado kevindelgado force-pushed the pr/386-no-desc-metadata branch 2 times, most recently from 0615953 to 474f612 Compare February 16, 2021 23:07
@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Feb 16, 2021
@DirectXMan12
Copy link
Contributor

/lgtm
/approve

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

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: DirectXMan12, kevindelgado

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 17, 2021
@DirectXMan12
Copy link
Contributor

/hold cancel

@k8s-ci-robot k8s-ci-robot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Feb 17, 2021
@k8s-ci-robot k8s-ci-robot merged commit ea8d4ea into kubernetes-sigs:master Feb 17, 2021
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. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Avoid generating description in metadata of CRD structural schema
4 participants