Skip to content

Commit

Permalink
Don't require the code be placed inside GOPATH. (#396)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Matous Jobanek <[email protected]>
  • Loading branch information
metlos and MatousJobanek authored Jan 19, 2024
1 parent 15f9e5e commit 0ae75bc
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 1 addition & 2 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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.
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.
30 changes: 28 additions & 2 deletions make/generate.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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).
Expand Down

0 comments on commit 0ae75bc

Please sign in to comment.