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

internal/generate: align CRD generator with controller-gen #2201

Merged

Conversation

estroz
Copy link
Member

@estroz estroz commented Nov 15, 2019

Description of the change:

  • cmd/operator-sdk: replace CRD scaffold with CRD generator
  • internal/scaffold: remove CRD scaffold and tests
  • internal/generate/crd: CRD generator
  • test/test-framework: update operator with new CRD generator
  • internal/util/fileutil: DotPath prepends "./" or "." to a path depending on OS
  • doc: discuss storage version setting in CRDs
  • test/test-framework: use storage version annotation in APIs

Motivation for the change: This PR aligns the CRD generator with controller-gen's. By using the same option strings one would pass to controller-gen to configure the CRD generator, we can easily transition this command to a Makefile, as well as more easily debug CRD generation errors.

Note that a resource is no longer required to run the generator for Go projects. It will update each CRD that corresponds to an API. For non-go projects a resource is still required.

Closes #2042

@openshift-ci-robot openshift-ci-robot added the size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. label Nov 15, 2019
@estroz estroz added refactoring size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. and removed size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. labels Nov 15, 2019
@openshift-ci-robot
Copy link

@estroz: The following test failed, say /retest to rerun them all:

Test name Commit Details Rerun command
ci/prow/sanity 02e2019 link /test sanity

Full PR test history. Your PR dashboard. Please help us cut down on flakes by linking to an open issue when you hit one in your PR.

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. I understand the commands that are listed here.

@estroz
Copy link
Member Author

estroz commented Nov 15, 2019

/hold

blocked by #2202

@openshift-ci-robot openshift-ci-robot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Nov 15, 2019
cmd/operator-sdk: replace CRD scaffold with CRD generator

internal/generate/util: CachedGenerator wraps controller-tools'
generator runtime for cached manifest generation

internal/generate/crd: CRD-specific generator

test/test-framework: update operator with new CRD generator

internal/util/fileutil: DotPath prepends "./" or ".\" to a path
depending on OS
test/test-framework: use storage version annotation in APIs
@estroz estroz force-pushed the align-crd-generator-controller-gen branch from 02e2019 to 8827ac5 Compare November 15, 2019 19:20

// NewGo returns a CRD generator configured to generate a
// CustomResourceDefintion manifest from scratch using data in resource.
func NewNonGo(inputDir, outputDir string, resource scaffold.Resource) genutil.Generator {
Copy link
Contributor

Choose a reason for hiding this comment

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

WDYT about it is a NewGenerator and the info if is or not a go project be an attribute of the Generator object?

Copy link
Member Author

@estroz estroz Nov 18, 2019

Choose a reason for hiding this comment

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

I'm going to think about this a bit more. We're effectively doing that with current code using projutil.IsOperatorGo(), I just don't like using booleans as exported function parameters when I don't have to.

@estroz estroz force-pushed the align-crd-generator-controller-gen branch from 15515b7 to cb33ad1 Compare November 18, 2019 19:06
@estroz estroz force-pushed the align-crd-generator-controller-gen branch from 1c2e07e to 6820652 Compare December 6, 2019 22:27
Copy link
Member

@joelanford joelanford left a comment

Choose a reason for hiding this comment

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

/lgtm

Just a couple of questions/observations

Do we need a CHANGELOG entry for this? It strikes me as more of a refactoring with no user-facing changes, but wanted to double check at least.

```

**Notes:**
- - Your CRD *must* specify exactly one [storage version][crd-storage-version]. Use the `+kubebuilder:storageversion` [marker][crd-markers] to indicate the GVK that should be used to store data by the API server. This marker should be in a comment above your `Memcached` type.
Copy link
Member

Choose a reason for hiding this comment

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

I noticed that without manually adding +kubebuilder:storageversion in advance, operator-sdk add api errors out when another version is added to an existing CRD with the same group & kind.

This problem may be out-of-scope for this PR, but something we may want to think about whether there's a more graceful way to handle from a user experience perspective.

Here's the error I get:

$ operator-sdk generate crds
INFO[0000] Running CRD generator.
example.com/go-operator/pkg/apis/example/v1:-: CRD for GoOperator.example.com has no storage version
Error: error generating CRDs from APIs in pkg/apis: error generating CRD manifests: error running CRD generator: not all generators ran successfully
Usage:
  operator-sdk generate crds [flags]

Flags:
  -h, --help   help for crds

Global Flags:
      --verbose   Enable verbose logging

It's a good hint, but could be improved by telling the user how to fix the problem. Can we put this note into the error message, nearly verbatim and skip outputting the usage?

Copy link
Member

Choose a reason for hiding this comment

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

Also, I just noticed that it's possible to define +kubebuilder:storageversion in both versions of a CRD's types.go files and controller-gen happily creates a CRD where storage: true exists in both versions. That should also be an error right?

Copy link
Member Author

Choose a reason for hiding this comment

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

👍 on not printing usage. By note do you mean:

example.com/go-operator/pkg/apis/example/v1:-: CRD for GoOperator.example.com has no storage version

Two CRD versions with storage: true is an error, but controller-tools does not consider this an error during generation. I will submit an issue if one does not exist already upstream.

Copy link
Member

Choose a reason for hiding this comment

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

Oh I meant based on the note that you have in the user guide:

Something like:

Your CRD must specify exactly one storage version. Use the +kubebuilder:storageversion marker to indicate the GVK that should be used to store data by the API server. This marker should be in a comment above your CRDs root storage type in _types.go.

See https://book.kubebuilder.io/multiversion-tutorial/api-changes.html#storage-versions

Copy link
Member Author

Choose a reason for hiding this comment

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

Generator errors are not returned by Runtime.Run() so we can't check if the error returned pertains to no +kubebuilder:storageversion annotation. How about linking https://book.kubebuilder.io/reference/generating-crd.html in an error message for Runtime.Run() errors? This should cover this and other errors, but isn't as specific.

@openshift-ci-robot openshift-ci-robot added the lgtm Indicates that a PR is ready to be merged. label Jan 6, 2020
@joelanford
Copy link
Member

Oh also, is the code coverage decrease related to this PR? If so, is it possible to bump that back up?

@openshift-ci-robot
Copy link

New changes are detected. LGTM label has been removed.

@openshift-ci-robot openshift-ci-robot removed the lgtm Indicates that a PR is ready to be merged. label Jan 6, 2020
@estroz estroz merged commit 9a32696 into operator-framework:master Jan 6, 2020
@estroz estroz deleted the align-crd-generator-controller-gen branch January 6, 2020 22:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. 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.

CRD generation via generate openapi does not surface errors for invalid tags
4 participants