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

🏃 ⚠ Support for Go modules #175

Merged
merged 11 commits into from
Mar 29, 2019
Merged

🏃 ⚠ Support for Go modules #175

merged 11 commits into from
Mar 29, 2019

Conversation

andreykaipov
Copy link
Contributor

This PR adds support for Go modules, both from a development and consumer point of view.

Development still works from within the GOPATH, but GO111MODULE=on must be set when running tests.

In addition, the CRD generator can now be run from outside of the GOPATH, so long as the project it's running against specifies the repo/module name in a PROJECT file.

I've also replaced gometalinter with golangci-lint in the test script as the former is deprecated and doesn't work with Go modules, while the latter does and has feature parity.

To make the review less of a headache, I've left the re-vendoring in a separate commit.

Please let me know if I missed anything!

This involves just a `go mod init sigs.k8s.io/controller-tools`.
Before vendoring or tidying our new depenendecies, we must first
get the project to build.
From within the testData project:

```
$ go mod init sigs.k8s.io/controller-tools/pkg/crd/generator/testData
$ go get -m k8s.io/api@kubernetes-1.13.4 # pin api versions
$ go get -m k8s.io/apimachinery@kubernetes-1.13.4
$ go build -o manager ./cmd/manager/
$ go mod tidy
```
The repo/module name is now read from a PROJECT file in the
repo the generator is run against. It must be present.
- golangci-lint has feature parity, and is a lot quicker too.
- gometalinter is deprecated and doesn't work nicely with Go modules.
- Use Go 1.12
- Get rid of dep in favor of Go modules
- Get rid of golint and gometalinter in favor of golangci-lint
Now that the project can be built and the tests pass,
we do a `go mod vendor` followed by a `go mod tidy`.
@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Mar 24, 2019
@k8s-ci-robot k8s-ci-robot added the size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. label Mar 24, 2019
@andreykaipov
Copy link
Contributor Author

/assign @DirectXMan12

@droot
Copy link
Contributor

droot commented Mar 25, 2019

/assign @Mengqi

@k8s-ci-robot
Copy link
Contributor

@droot: GitHub didn't allow me to assign the following users: mengqi.

Note that only kubernetes-sigs members and repo collaborators can be assigned and that issues/PRs can only have 10 assignees at the same time.
For more information please see the contributor guide

In response to this:

/assign @Mengqi

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.

@droot
Copy link
Contributor

droot commented Mar 25, 2019

@andreykaipov I am not much familiar with GO modules, will the existing kubebuilder projects (which uses dep and Gopkg.toml etc.) will continue to work after this change ?

@andreykaipov
Copy link
Contributor Author

andreykaipov commented Mar 25, 2019

Yup! The only commit in this PR that changes the logic of a generator is 626472c.

Instead of validating the project to be under the GOPATH, it validates a PROJECT file exists. Then, instead of using ./pkg/apis as the import path, it uses <repo-name>/pkg/apis. This works both outside and inside of the GOPATH. The repo name is read from the PROJECT file, and Kubebuilder guarantees that field exists when it trims the GOPATH from the working directory (see https://github.com/kubernetes-sigs/kubebuilder/blob/master/pkg/scaffold/project/project.go#L41).

The goal after this is to get Kubebuilder to use Go modules too (kubernetes-sigs/kubebuilder#506) 😄

@mengqiy
Copy link
Member

mengqiy commented Mar 26, 2019

@andreykaipov IIUC it require GO111MODULE=on to be set to make controller-gen to work for an existing project, right? If so, how do you make it backward-compatible?

@andreykaipov
Copy link
Contributor Author

Setting GO111MODULE=on is only required for testing this project from within the GOPATH since the build scripts now use -mod=vendor (02e1d02). It's not required for just running controller-gen though.

The change is backwards compatible as long as users of Kubebuilder do not delete the generated PROJECT file. That's where controller-gen is now discovering the package name instead of using the DirToGoPkg function.

@ghost
Copy link

ghost commented Mar 26, 2019

Go modules plans on replacing dep entirely, is there need to be backwards compatible there? Most kubernetes projects now have tags to map to kubernetes versions, would it be possible to integrate these mappings, eg. kubernetes-1.13 to allow us to chose target versions where required, or would this throw a spanner in the works?

@mengqiy
Copy link
Member

mengqiy commented Mar 26, 2019

Go modules plans on replacing dep entirely, is there need to be backwards compatible there?

controller-tools follows semver and is at v0.1 ATM. If the changes is not backwards compatible, we need to bump the version to v0.2.

@andreykaipov
Copy link
Contributor Author

andreykaipov commented Mar 27, 2019

So here's how I'm confirming the changes are backwards compatible. We'll create a new project with Kubebuilder, and then manually apply the changes from 626472c to the respective files in our vendor folder.

Create a new project, run dep ensure, and then create a new API to simulate an existing project.

$ mkdir -p $GOPATH/src/hello
$ cd $GOPATH/src/hello
$ kubebuilder init --dep
$ kubebuilder create api --group mygroup --version v1 --kind Abc < <(printf 'y\ny\n')
$ ls config/crds/
mygroup_v1_abc.yaml

Now manually apply the changes from 626472c to vendor/sigs.k8s.io/controller-tools/pkg/crd/generator/generator.go and vendor/sigs.k8s.io/controller-tools/pkg/crd/util/ulti.go.

Confirm controller-gen still works by creating a new API.

$ kubebuilder create api --group mygroup --version v1 --kind Xyz < <(printf 'y\ny\n')
$ ls config/crds/
mygroup_v1_abc.yaml     mygroup_v1_xyz.yaml

Alternatively, run controller-gen directly:

$ make manifests
go run vendor/sigs.k8s.io/controller-tools/cmd/controller-gen/main.go all
CRD manifests generated under '$GOPATH/src/hello/config/crds'
RBAC manifests generated under '$GOPATH/src/hello/config/rbac'

@mengqiy
Copy link
Member

mengqiy commented Mar 28, 2019

@andreykaipov It's not a breaking change from user perspective. That's great.

My only concern now is: if I'm a contributor to controller-tools, I'm in GOPATH and haven't yet set GO111MODULE=on, is it easy to discover that GO111MODULE=on needs to be set? If not, we may want to document it somewhere.

@andreykaipov
Copy link
Contributor Author

andreykaipov commented Mar 28, 2019

@mengqiy That's a good idea. I can add a Development section to the README explaining that.

Edit: okiedoke - added it in 89d33b3. I think Go modules will become the default in Go 1.13 (see golang/go#27227), so we could probably remove the note once that happens. 😄

@mengqiy
Copy link
Member

mengqiy commented Mar 29, 2019

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Mar 29, 2019
@mengqiy mengqiy added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Mar 29, 2019
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

Approval requirements bypassed by manually added approval.

This pull-request has been approved by: andreykaipov

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 merged commit ba932e8 into kubernetes-sigs:master Mar 29, 2019
@DirectXMan12
Copy link
Contributor

Hold the phone. People vendoring this with dep won't get dependency info out of this any more. I think we have to maintain a Gopkg.toml for a bit. At least until Go 1.13.

@DirectXMan12
Copy link
Contributor

We also have to make sure they stay up to date.

@andreykaipov
Copy link
Contributor Author

Sorry, what do you mean by not being able to get the dependency info if vendoring with dep? Taking the Kubebuilder project as an example, I'm able to run a dep ensure -update sigs.k8s.io/controller-tools and have it vendor the latest master successfully, so any consumers of the library using dep should still be able to do so. Maybe I'm overlooking something? 😬

@mengqiy
Copy link
Member

mengqiy commented Apr 4, 2019

This issue may happen if the users don't use kubebuilder, but use controller-runtime and controller-tools directly.

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/XXL Denotes a PR that changes 1000+ lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants