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 go mod tidy to be executed after the scaffolding api #2037

Merged
merged 1 commit into from
Mar 1, 2021

Conversation

dastergon
Copy link
Contributor

This PR adds an extra step in the API creation subcommand to update go.mod after scaffolding the API to make sure we have a fresh go.mod in cases of new dependencies introduced by the generated controllers.

My setup

  • Go: 1.16 darwin/arm64
  • Kubebuilder: version.Version{KubeBuilderVersion:"2.3.2", KubernetesVendor:"unknown", GitCommit:"5da27b892ae310e875c8719d94a5a04302c597d0", BuildDate:"2021-02-22T21:16:26-08:00", GoOs:"darwin", GoArch:"arm64"}
  • operator-sdk: v1.4.2, commit: "4b083393be65589358b3e0416573df04f4ae8d9b
  • Kubernetes: v1.19.4

What is the issue?

Initially, I used the operator-sdk to create a new operator, but, after creating an API I stumbled upon the following issue:

$ operator-sdk init --domain example.com --repo github.com/example/memcached-operator
$ operator-sdk create api --group cache --version v1alpha1 --kind Memcached --resource --controller
Writing scaffold for you to edit...
api/v1alpha1/memcached_types.go
controllers/memcached_controller.go
....
/Users/xxxxxx/memcached-operator/bin/controller-gen object:headerFile="hack/boilerplate.go.txt" paths="./..."
Error: go [-e -json -compiled=true -test=false -export=false -deps=true -find=false -tags ignore_autogenerated -- ./...]: exit status 1: go: github.com/example/memcached-operator/controllers: package github.com/go-logr/logr imported from implicitly required module; to add missing requirements, run:
        go get github.com/go-logr/logr@v0.3.0
....

FATA[0000] failed to create API with "go.kubebuilder.io/v3": exit status 2

Then, after investigating the code, I found that the error was coming from kubebuilder code and consequently, I looked at generating the controller via kubebuilder to double-check. After running the basic commands, I got the same error:

$ kubebuilder init --domain my.domain
$ kubebuilder create api --group webapp --version v1 --kind Guestbook
...
/Users/xxxxx/go/bin/controller-gen object:headerFile="hack/boilerplate.go.txt" paths="./..."
Error: go [list -e -json -compiled=true -test=false -export=false -deps=true -find=false -tags ignore_autogenerated -- ./...]: exit status 1: go: example/controllers: package github.com/go-logr/logr imported from implicitly required module; to add missing requirements, run:
        go get github.com/go-logr/logr@v0.1.0
....
run `controller-gen object:headerFile=hack/boilerplate.go.txt paths=./... -w` to see all available markers, or `controller-gen object:headerFile=hack/boilerplate.go.txt paths=./... -h` for usage
make: *** [generate] Error 1
2021/02/24 22:24:31 failed to create API: exit status 2

Then, to double-check I executed the tests of a fresh clone of the kubebuilder repo and I received the same error:

$ make test
....
Preparing test directory /tmp/kubebuilder/test
Running kubebuilder commands in test directory /tmp/kubebuilder/test
Restoring cached project 'init'
Creating api and controller
Create Resource [y/n]
Create Controller [y/n]
Writing scaffold for you to edit...
api/v1beta1/bee_types.go
controllers/bee_controller.go
Running make:
$ make generate
go: creating new go.mod: module tmp
Downloading sigs.k8s.io/controller-tools/cmd/controller-gen@v0.4.1
go get: added sigs.k8s.io/controller-tools v0.4.1
/tmp/kubebuilder/test/bin/controller-gen object:headerFile="hack/boilerplate.go.txt" paths="./..."
Error: go [-e -json -compiled=true -test=false -export=false -deps=true -find=false -tags ignore_autogenerated -- ./...]: exit status 1: go: kubebuilder.io/test/controllers: package github.com/go-logr/logr imported from implicitly required module; to add missing requirements, run:
        go get github.com/go-logr/logr@v0.3.0
....
2021/02/24 22:33:24 failed to create API with "go.kubebuilder.io/v3": exit status 2
make: *** [test-integration] Error 1

How I solved the issue

Instead of introducing go mod tidy in the Makefile template, I decided to add it in the PostScaffolding func of API creation subcommand for both v2 and v3, since a similar action is done in init subcommand as well.

After I made the code changes, the tests in kubebuilder passed successfully. Also, I executed make generate to update the mock data for this PR.

PTAL. I am open to alternative solutions, in case this one is adding extra complexity.

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Feb 24, 2021
@k8s-ci-robot
Copy link
Contributor

Welcome @dastergon!

It looks like this is your first PR to kubernetes-sigs/kubebuilder 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes-sigs/kubebuilder has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot k8s-ci-robot added the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Feb 24, 2021
@k8s-ci-robot
Copy link
Contributor

Hi @dastergon. Thanks for your PR.

I'm waiting for a kubernetes-sigs member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

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 the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Feb 24, 2021
@dastergon dastergon changed the title (🐛) Fix error when generating new APIs 🐛 Fix error when generating new APIs Feb 24, 2021
@camilamacedo86
Copy link
Member

/ok-to-test

@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Feb 25, 2021
@dastergon dastergon force-pushed the fix-api-generation branch 2 times, most recently from be89258 to c59eb20 Compare February 25, 2021 10:00
Copy link
Member

@camilamacedo86 camilamacedo86 left a comment

Choose a reason for hiding this comment

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

Could you please squash the commits?

@k8s-ci-robot k8s-ci-robot added size/S Denotes a PR that changes 10-29 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Feb 25, 2021
@dastergon
Copy link
Contributor Author

Hi @camilamacedo86,

I rebased and squashed the commits.

@dastergon
Copy link
Contributor Author

@camilamacedo86 it looks like that the issue is fixed, but there's a new go.sum issue with the generated mock data. What do you think could be causing it?

@camilamacedo86
Copy link
Member

Hi @dastergon,

The #2040 will solve that. Thank you.

@Adirio
Copy link
Contributor

Adirio commented Feb 25, 2021

  • Go: 1.16 darwin/arm64

Can you try with go 1.15 please? Go 1.16 is not officially supported.

@dastergon
Copy link
Contributor Author

dastergon commented Feb 25, 2021

@Adirio I own an MB Air with Apple Silicon and I can only use Go >=1.16.

@camilamacedo86
Copy link
Member

camilamacedo86 commented Feb 25, 2021

Hi @dastergon,

MB Air with Apple Silicon and I can only use Go >=1.16.

I am not sure why you can only use the latest release of GO. I also use Mac and 1.15 on it.

For we officially support 1.16 we need to upgrade the KB tool and the files which are scaffolded by it.
However, we cannot do that until it gets done for controller-runtime and k8s org. It usually takes a while after the go version is released. See that go 1.16 just was released.

You are free to use go 1.16. However, we cannot ensure that it will work well for all scenarios yet.

Could you please rebase it with the master for we are able to get your PR merged.

@dastergon
Copy link
Contributor Author

dastergon commented Feb 25, 2021

@camilamacedo86 rebase done ✅

I use 1.16 because it now supports M1 (Apple Silicon).

Copy link
Contributor

@Adirio Adirio left a comment

Choose a reason for hiding this comment

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

I think this error is related to go1.16 somehow, so I would consider this a feature (✨) unless we can replicate this with go1.15, in which case I would keep the bugfix (🐛) emoji.

pkg/plugins/golang/v2/api.go Outdated Show resolved Hide resolved
@k8s-ci-robot k8s-ci-robot removed the size/S Denotes a PR that changes 10-29 lines, ignoring generated files. label Feb 25, 2021
@k8s-ci-robot k8s-ci-robot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Feb 25, 2021
Copy link
Contributor

@Adirio Adirio left a comment

Choose a reason for hiding this comment

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

/lgtm
/hold

Holding until we can check if this is a bug-fix or a new feature

@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 Feb 25, 2021
@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Feb 25, 2021
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: Adirio, dastergon

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 25, 2021
@camilamacedo86
Copy link
Member

HI @dastergon,

Could you change your commit message for something such as ✨:Add go mod tidy to be executed after scaffold an api generation. Then, it solved the @Adirio concern and imo we can get it merged.

@dastergon dastergon changed the title 🐛 Fix error when generating new APIs ✨: Add go mod tidy to be executed after the scaffolding api Mar 1, 2021
@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Mar 1, 2021
@dastergon
Copy link
Contributor Author

@camilamacedo86 I rebased and updated the commit message.

@Adirio
Copy link
Contributor

Adirio commented Mar 1, 2021

/lgtm
/unhold

@k8s-ci-robot k8s-ci-robot added lgtm "Looks good to me", indicates that a PR is ready to be merged. and removed do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. labels Mar 1, 2021
@k8s-ci-robot k8s-ci-robot merged commit 2b46403 into kubernetes-sigs:master Mar 1, 2021
@dastergon dastergon deleted the fix-api-generation branch March 1, 2021 21:43
@Adirio Adirio mentioned this pull request Mar 1, 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. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. 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.

None yet

4 participants