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 make target for generating crds through controller-gen #629

Merged
merged 1 commit into from
Mar 5, 2021

Conversation

mattcui
Copy link
Contributor

@mattcui mattcui commented Mar 1, 2021

Changes

This PR is for changing to use controller-gen tool from kubebuilder to generate CRDs

Fixes #579

-->

Submitter Checklist

  • Includes tests if functionality changed/was added
  • Includes docs if changes are user-facing
  • Set a kind label on this PR
  • Release notes block has been filled in, or marked NONE

See the contributor guide
for details on coding conventions, github and prow interactions, and the code review process.

Release Notes

Added a script and make target to install controller-gen tool and generate CRDs with this tool

@openshift-ci-robot
Copy link

Skipping CI for Draft Pull Request.
If you want CI signal for your change, please convert it to an actual PR.
You can still manually trigger a test run with /test all

@openshift-ci-robot openshift-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 Label for when a PR has specified a release note labels Mar 1, 2021
@mattcui
Copy link
Contributor Author

mattcui commented Mar 1, 2021

I am able to use controller-gen tool to generate CRDs successfully, the name of generated manifests is different with existing ones, the generated name has shipwright.io_ prefix and plural noun like below:

deploy/crds# ls
build.yaml     buildstrategy.yaml         shipwright.io_buildruns.yaml  shipwright.io_buildstrategies.yaml
buildrun.yaml  clusterbuildstrategy.yaml  shipwright.io_builds.yaml     shipwright.io_clusterbuildstrategies.yaml

So the 1st question is whether we need to use these generated names or rename to existing ones? Also there lead to another question about the location/directory where to save these crd files. The operator-sdk 1.x migration doc recommends to follow kubebuilder's convention to save these crds under config/crd/bases, we may still keep as it's for now or move to config/crd/bases for future to moving to operator-sdk 1.x.

I also compared the content of generated manifests with existing ones. There is not much different, there added a new annotation for controller-gen.kubebuilder.io/version in newly generated ones by controller-gen, and an empty status section at the bottom the file (Need to see if it's harmless to us).

# diff build.yaml shipwright.io_builds.yaml
0a1,2
>
> ---
3a6,8
>   annotations:
>     controller-gen.kubebuilder.io/version: v0.4.1
>   creationTimestamp: null
270a276,281
> status:
>   acceptedNames:
>     kind: ""
>     plural: ""
>   conditions: []
>   storedVersions: []

/cc @qu1queee @zhangtbj

@mattcui mattcui changed the title Add make target for generating crds through controller-gen WIP: Add make target for generating crds through controller-gen Mar 2, 2021
@zhangtbj
Copy link
Contributor

zhangtbj commented Mar 2, 2021

Thanks Matt!

So the 1st question is whether we need to use these generated names or rename to existing ones?

Can controller-gen tool generate CRD name as our original name by leverage its command parameter directly? If no, I suggest to switch to the new name directly. The second folder problem is similar. I prefer to use a native/default way of the tool (no patch).

BTW, can we add our DEVELOPMENT.md document to tell new developer how to use it?
https://github.com/shipwright-io/build/blob/master/DEVELOPMENT.md

Comment on lines 16 to 21
if [ ! -f "${GOPATH}/bin/controller-gen" ] ; then
echo "# Installing controller-gen..."
go get sigs.k8s.io/controller-tools/cmd/controller-gen@"${CONTROLLER_GEN_VERSION}"
fi
Copy link
Contributor

Choose a reason for hiding this comment

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

Until we can use the Go 1.16 improved go get/install that does not fiddle with the current Go modules file, I would suggest to do the installation part in a temporary directory.

Suggested change
if [ ! -f "${GOPATH}/bin/controller-gen" ] ; then
echo "# Installing controller-gen..."
go get sigs.k8s.io/controller-tools/cmd/controller-gen@"${CONTROLLER_GEN_VERSION}"
fi
if [ ! -f "${GOPATH}/bin/controller-gen" ] ; then
echo "# Installing controller-gen..."
pushd "$(mktemp -d)" >/dev/null 2>&1
go get sigs.k8s.io/controller-tools/cmd/controller-gen@"${CONTROLLER_GEN_VERSION}"
popd >/dev/null 2>&1
fi

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, Matthias. I updated the code according to your suggestion, BTW, do you think if it's needed to clean up this temporary dir after installation? Thanks.

@mattcui mattcui force-pushed the gen_crd branch 2 times, most recently from 04d7466 to beada30 Compare March 3, 2021 13:40
@mattcui
Copy link
Contributor Author

mattcui commented Mar 3, 2021

Thanks Matt!

So the 1st question is whether we need to use these generated names or rename to existing ones?

Can controller-gen tool generate CRD name as our original name by leverage its command parameter directly? If no, I suggest to switch to the new name directly. The second folder problem is similar. I prefer to use a native/default way of the tool (no patch).

BTW, can we add our DEVELOPMENT.md document to tell new developer how to use it?
https://github.com/shipwright-io/build/blob/master/DEVELOPMENT.md

Yes, I added a simple instruction in this doc.

@mattcui mattcui marked this pull request as ready for review March 3, 2021 13:43
@mattcui mattcui changed the title WIP: Add make target for generating crds through controller-gen Add make target for generating crds through controller-gen Mar 3, 2021
@openshift-ci-robot openshift-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 Mar 3, 2021
@mattcui mattcui requested a review from HeavyWombat March 3, 2021 13:44
Copy link
Contributor

@HeavyWombat HeavyWombat left a comment

Choose a reason for hiding this comment

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

Quick question, I tried it locally and with the proposed GO111MODULE fix it was able to create the custom resource definition files. However, the file paths were different to the ones we currently have:

# New files
deploy/crds/shipwright.io_buildruns.yaml
deploy/crds/shipwright.io_builds.yaml
deploy/crds/shipwright.io_buildstrategies.yaml
deploy/crds/shipwright.io_clusterbuildstrategies.yaml

# Current files
ls -1 deploy/crds/
build.yaml
buildrun.yaml
buildstrategy.yaml
clusterbuildstrategy.yaml

if [ ! -f "${GOPATH}/bin/controller-gen" ] ; then
echo "# Installing controller-gen..."
pushd "$(mktemp -d)" >/dev/null 2>&1
go get sigs.k8s.io/controller-tools/cmd/controller-gen@"${CONTROLLER_GEN_VERSION}"
Copy link
Contributor

Choose a reason for hiding this comment

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

Just found out that in this particular case we have to make sure that it uses Go Modules otherwise you cannot specify a specific version. Tested it in an empty Docker container and with the environment variable it works for me.

Suggested change
go get sigs.k8s.io/controller-tools/cmd/controller-gen@"${CONTROLLER_GEN_VERSION}"
GO111MODULE=on go get sigs.k8s.io/controller-tools/cmd/controller-gen@"${CONTROLLER_GEN_VERSION}"

Copy link
Contributor

Choose a reason for hiding this comment

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

+1, just hit the same issue

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, @HeavyWombat , I added.

@qu1queee qu1queee self-requested a review March 4, 2021 10:44
@qu1queee
Copy link
Contributor

qu1queee commented Mar 4, 2021

@mattcui @HeavyWombat the names where changed in here . I think also the operator-sdk (when doing an operator-sdk generate crds) usually tries to create the files with the same syntax <scheme group>_<crd plural name>.yaml . I´m wondering it those files renames are wrong and if this syntax is a best practice. @imjasonh any opinions on this?, this files rename went via the PR where we change the domain name.

@mattcui one more thing, so I compared the files and I see this will introduce new fields in the CRD definition, as follows:

$ dyff between build.yaml shipwright.io_builds.yaml
     _        __  __
   _| |_   _ / _|/ _|  between github.com/shipwright-io/build/deploy/crds/build.yaml
 / _' | | | | |_| |_       and github.com/shipwright-io/build/deploy/crds/shipwright.io_builds.yaml
| (_| | |_| |  _|  _|
 \__,_|\__, |_| |_|   returned two differences
        |___/

(root level)
+ one map entry added:
  status:
  │ acceptedNames:
  │ │ kind:
  │ │ plural:
  │ conditions: []
  │ storedVersions: []

metadata
  + two map entries added:
    annotations:
    │ controller-gen.kubebuilder.io/version: v0.4.1
    creationTimestamp: null
$ dyff between buildrun.yaml shipwright.io_buildruns.yaml
     _        __  __
   _| |_   _ / _|/ _|  between github.com/shipwright-io/build/deploy/crds/buildrun.yaml
 / _' | | | | |_| |_       and github.com/shipwright-io/build/deploy/crds/shipwright.io_buildruns.yaml
| (_| | |_| |  _|  _|
 \__,_|\__, |_| |_|   returned two differences
        |___/

(root level)
+ one map entry added:
  status:
  │ acceptedNames:
  │ │ kind:
  │ │ plural:
  │ conditions: []
  │ storedVersions: []

metadata
  + two map entries added:
    annotations:
    │ controller-gen.kubebuilder.io/version: v0.4.1
    creationTimestamp: null
$ dyff between buildstrategy.yaml shipwright.io_buildstrategies.yaml
     _        __  __
   _| |_   _ / _|/ _|  between github.com/shipwright-io/build/deploy/crds/buildstrategy.yaml
 / _' | | | | |_| |_       and github.com/shipwright-io/build/deploy/crds/shipwright.io_buildstrategies.yaml
| (_| | |_| |  _|  _|
 \__,_|\__, |_| |_|   returned two differences
        |___/

(root level)
+ one map entry added:
  status:
  │ acceptedNames:
  │ │ kind:
  │ │ plural:
  │ conditions: []
  │ storedVersions: []

metadata
  + two map entries added:
    annotations:
    │ controller-gen.kubebuilder.io/version: v0.4.1
    creationTimestamp: null
$ dyff between clusterbuildstrategy.yaml shipwright.io_clusterbuildstrategies.yaml
     _        __  __
   _| |_   _ / _|/ _|  between github.com/shipwright-io/build/deploy/crds/clusterbuildstrategy.yaml
 / _' | | | | |_| |_       and github.com/shipwright-io/build/deploy/crds/shipwright.io_clusterbuildstrategies.yaml
| (_| | |_| |  _|  _|
 \__,_|\__, |_| |_|   returned two differences
        |___/

(root level)
+ one map entry added:
  status:
  │ acceptedNames:
  │ │ kind:
  │ │ plural:
  │ conditions: []
  │ storedVersions: []

metadata
  + two map entries added:
    annotations:
    │ controller-gen.kubebuilder.io/version: v0.4.1
    creationTimestamp: null

any ideas on why this is happening?

Copy link
Contributor

@qu1queee qu1queee left a comment

Choose a reason for hiding this comment

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

Asking some questions on the current setup, overall this looks good!

@mattcui
Copy link
Contributor Author

mattcui commented Mar 5, 2021

@mattcui @HeavyWombat the names where changed in here . I think also the operator-sdk (when doing an operator-sdk generate crds) usually tries to create the files with the same syntax <scheme group>_<crd plural name>.yaml . I´m wondering it those files renames are wrong and if this syntax is a best practice. @imjasonh any opinions on this?, this files rename went via the PR where we change the domain name.

@mattcui one more thing, so I compared the files and I see this will introduce new fields in the CRD definition, as follows:

$ dyff between build.yaml shipwright.io_builds.yaml
     _        __  __
   _| |_   _ / _|/ _|  between github.com/shipwright-io/build/deploy/crds/build.yaml
 / _' | | | | |_| |_       and github.com/shipwright-io/build/deploy/crds/shipwright.io_builds.yaml
| (_| | |_| |  _|  _|
 \__,_|\__, |_| |_|   returned two differences
        |___/

(root level)
+ one map entry added:
  status:
  │ acceptedNames:
  │ │ kind:
  │ │ plural:
  │ conditions: []
  │ storedVersions: []

metadata
  + two map entries added:
    annotations:
    │ controller-gen.kubebuilder.io/version: v0.4.1
    creationTimestamp: null
$ dyff between buildrun.yaml shipwright.io_buildruns.yaml
     _        __  __
   _| |_   _ / _|/ _|  between github.com/shipwright-io/build/deploy/crds/buildrun.yaml
 / _' | | | | |_| |_       and github.com/shipwright-io/build/deploy/crds/shipwright.io_buildruns.yaml
| (_| | |_| |  _|  _|
 \__,_|\__, |_| |_|   returned two differences
        |___/

(root level)
+ one map entry added:
  status:
  │ acceptedNames:
  │ │ kind:
  │ │ plural:
  │ conditions: []
  │ storedVersions: []

metadata
  + two map entries added:
    annotations:
    │ controller-gen.kubebuilder.io/version: v0.4.1
    creationTimestamp: null
$ dyff between buildstrategy.yaml shipwright.io_buildstrategies.yaml
     _        __  __
   _| |_   _ / _|/ _|  between github.com/shipwright-io/build/deploy/crds/buildstrategy.yaml
 / _' | | | | |_| |_       and github.com/shipwright-io/build/deploy/crds/shipwright.io_buildstrategies.yaml
| (_| | |_| |  _|  _|
 \__,_|\__, |_| |_|   returned two differences
        |___/

(root level)
+ one map entry added:
  status:
  │ acceptedNames:
  │ │ kind:
  │ │ plural:
  │ conditions: []
  │ storedVersions: []

metadata
  + two map entries added:
    annotations:
    │ controller-gen.kubebuilder.io/version: v0.4.1
    creationTimestamp: null
$ dyff between clusterbuildstrategy.yaml shipwright.io_clusterbuildstrategies.yaml
     _        __  __
   _| |_   _ / _|/ _|  between github.com/shipwright-io/build/deploy/crds/clusterbuildstrategy.yaml
 / _' | | | | |_| |_       and github.com/shipwright-io/build/deploy/crds/shipwright.io_clusterbuildstrategies.yaml
| (_| | |_| |  _|  _|
 \__,_|\__, |_| |_|   returned two differences
        |___/

(root level)
+ one map entry added:
  status:
  │ acceptedNames:
  │ │ kind:
  │ │ plural:
  │ conditions: []
  │ storedVersions: []

metadata
  + two map entries added:
    annotations:
    │ controller-gen.kubebuilder.io/version: v0.4.1
    creationTimestamp: null

any ideas on why this is happening?

I also posted the diff in my comment , the 1st difference, I think, it's controller-gen that adds itself controller-gen.kubebuilder.io/version: v0.4.1 in the annotation. For the 2nd difference, the code and explanation are here -> https://github.com/kubernetes-sigs/controller-tools/blob/master/pkg/crd/spec.go#L167-L173, I think it's Ok if it's harmless to us.

@qu1queee qu1queee mentioned this pull request Mar 5, 2021
4 tasks
@qu1queee qu1queee self-requested a review March 5, 2021 09:19
Copy link
Contributor

@qu1queee qu1queee left a comment

Choose a reason for hiding this comment

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

thanks
/lgtm
/approve

@openshift-ci-robot openshift-ci-robot added the lgtm Indicates that a PR is ready to be merged. label Mar 5, 2021
@openshift-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: qu1queee

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

@openshift-ci-robot openshift-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Mar 5, 2021
@openshift-merge-robot openshift-merge-robot merged commit fa82dd8 into shipwright-io:master Mar 5, 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. lgtm Indicates that a PR is ready to be merged. release-note Label for when a PR has specified a release note
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Generate CRD manifests without operator-sdk
6 participants