diff --git a/.gitignore b/.gitignore index a0cb26d9..c9cb00b7 100644 --- a/.gitignore +++ b/.gitignore @@ -20,3 +20,8 @@ config deploy /bin + +# The openapi-gen tool we use to generate OpenAPI types for the CRDs requires the GOPATH be set. +# We fake it in this directory so that we don't have to require the local environment to have that environment variable +# set. Let's ignore it so that it doesn't get accidentally committed. +/.fake-gopath diff --git a/README.adoc b/README.adoc index 511fd628..8cc0a8c4 100644 --- a/README.adoc +++ b/README.adoc @@ -17,5 +17,4 @@ To re-generate the `zz_generated.deepcopy.go` and `zz_generated.openapi.go` file make generate ``` -IMPORTANT: the codeready-toolchain repositories should be checked out under your GOPATH in order for the generation to generate files in the correct directories. eg. For this repository: `$GOPATH/src/github.com/codeready-toolchain/api` -NOTE: the `make generate` will generate the CRD files in the local `deploy/crds` directory and then dispatch the `.yaml` files in the `host-operator` and `member-operator` repositories, assuming they have been checked out and that *they are in a clean state*, meaning that they have no pending changes, besides previous versions of the CRD files. \ No newline at end of file +NOTE: the `make generate` will generate the CRD files in the local `config/crd/bases` directory and then dispatch the `.yaml` files in the `host-operator` and `member-operator` repositories, assuming they have been checked out and that *they are in a clean state*, meaning that they have no pending changes, besides previous versions of the CRD files. diff --git a/make/generate.mk b/make/generate.mk index c0d166fb..10dfd847 100644 --- a/make/generate.mk +++ b/make/generate.mk @@ -10,6 +10,18 @@ MEMBER_CLUSTER_CRDS:=useraccounts nstemplatesets memberstatuses idlers toolchain PATH_TO_CRD_BASES=config/crd/bases +# openapi-gen requires the GOPATH env var be set and the codebase be present within it. +# Let's not require $GOPATH be set up in the user's environment and the checkout be +# placed in it. +# Instead, fake it locally. +FAKE_GOPATH=$(PROJECT_DIR)/.fake-gopath +# The root of all codeready-toolchain repos in the GOPATH +CRT_IN_GOPATH=$(FAKE_GOPATH)/src/github.com/codeready-toolchain +# This gives the GOPATH as understood by the go compiler even if the env var is not explicitly set. +# We use this to find the packages that are already downloaded locally to save on the network traffic +# when persuading openapi-gen that our codebase is checked out under the GOPATH. +LOCAL_GOPATH=`go env GOPATH` + .PHONY: generate ## Generate deepcopy, openapi and CRD files after the API was modified generate: generate-deepcopy-and-crds generate-openapi dispatch-crds copy-reg-service-template @@ -18,15 +30,29 @@ generate: generate-deepcopy-and-crds generate-openapi dispatch-crds copy-reg-ser generate-deepcopy-and-crds: remove-config controller-gen @echo "Re-generating the deepcopy go file & the Toolchain CRD files... " $(Q)$(CONTROLLER_GEN) crd \ - object paths="./..." output:crd:artifacts:config=config/crd/bases + object paths="./..." output:crd:artifacts:config=$(PATH_TO_CRD_BASES) .PHONY: generate-openapi generate-openapi: openapi-gen @echo "re-generating the openapi go file..." - $(Q)$(OPENAPI_GEN) --input-dirs ./api/$(API_VERSION)/ \ + @## First, let's clean up anything that might have been left around... + @rm -Rf $(FAKE_GOPATH) + mkdir -p $(FAKE_GOPATH) + @mkdir -p $(CRT_IN_GOPATH) + @## link the packages from the local GOPATH to not have to download them again + @if [ -d $(LOCAL_GOPATH)/pkg ]; then cd $(FAKE_GOPATH) && ln -s $(LOCAL_GOPATH)/pkg; fi + @## link our codebase to the appropriate place in the fake GOPATH + @cd $(CRT_IN_GOPATH) && ln -s ../../../.. api + @## run openapi-gen from within the fake GOPATH (otherwise the package paths would be relative + @## and function names would be different) + GOPATH=$(FAKE_GOPATH) \ + && cd $(CRT_IN_GOPATH)/api \ + && $(OPENAPI_GEN) --input-dirs ./api/$(API_VERSION)/ \ --output-package github.com/codeready-toolchain/api/api/$(API_VERSION) \ --output-file-base zz_generated.openapi \ --go-header-file=make/go-header.txt + @## clean up the mess + rm -Rf $(FAKE_GOPATH) # make sure that that the `host-operator` and `member-operator` repositories exist locally # and that they don't have any pending changes (except for the CRD files).