github.com/codeready-toolchain/api@v0.0.0-20240507023248-73662d6db2c5/make/generate.mk (about) 1 # current groupname and version of the operators'API 2 API_GROUPNAME=toolchain 3 API_FULL_GROUPNAME=toolchain.dev.openshift.com 4 API_VERSION:=v1alpha1 5 6 # how to dispatch the CRD files per repository (space-separated lists) 7 # !!! IMPORTANT !!! - when there is a new CRD added or an existing one removed or renamed, don't forget to change it also here: https://github.com/codeready-toolchain/toolchain-cicd/blob/master/scripts/add-cluster.sh#L52-L73 8 HOST_CLUSTER_CRDS:=masteruserrecords nstemplatetiers usersignups bannedusers notifications spaces spacebindings socialevents tiertemplates toolchainstatuses toolchainclusters toolchainconfigs usertiers proxyplugins spacerequests spacebindingrequests spaceprovisionerconfigs 9 MEMBER_CLUSTER_CRDS:=useraccounts nstemplatesets memberstatuses idlers toolchainclusters memberoperatorconfigs spacerequests workspaces spacebindingrequests 10 11 PATH_TO_CRD_BASES=config/crd/bases 12 13 PROJECT_DIR := $(shell pwd) 14 # openapi-gen requires the GOPATH env var be set and the codebase be present within it. 15 # Let's not require $GOPATH be set up in the user's environment and the checkout be 16 # placed in it. 17 # Instead, fake it locally. 18 FAKE_GOPATH=$(PROJECT_DIR)/.fake-gopath 19 # The root of all codeready-toolchain repos in the GOPATH 20 CRT_IN_GOPATH=$(FAKE_GOPATH)/src/github.com/codeready-toolchain 21 # This gives the GOPATH as understood by the go compiler even if the env var is not explicitly set. 22 # We use this to find the packages that are already downloaded locally to save on the network traffic 23 # when persuading openapi-gen that our codebase is checked out under the GOPATH. 24 LOCAL_GOPATH=`$(GO) env GOPATH` 25 26 .PHONY: generate 27 ## Generate deepcopy, openapi and CRD files after the API was modified 28 generate: generate-deepcopy-and-crds generate-openapi dispatch-crds copy-reg-service-template 29 30 .PHONY: generate-deepcopy-and-crds 31 generate-deepcopy-and-crds: remove-config controller-gen 32 @echo "Re-generating the deepcopy go file & the Toolchain CRD files... " 33 $(Q)$(CONTROLLER_GEN) crd \ 34 object paths="./..." output:crd:artifacts:config=$(PATH_TO_CRD_BASES) 35 36 .PHONY: generate-openapi 37 generate-openapi: openapi-gen 38 @echo "re-generating the openapi go file..." 39 @## First, let's clean up anything that might have been left around... 40 @rm -Rf $(FAKE_GOPATH) 41 mkdir -p $(FAKE_GOPATH) 42 @mkdir -p $(CRT_IN_GOPATH) 43 @## link the packages from the local GOPATH to not have to download them again 44 @if [ -d $(LOCAL_GOPATH)/pkg ]; then cd $(FAKE_GOPATH) && ln -s $(LOCAL_GOPATH)/pkg; fi 45 @## link our codebase to the appropriate place in the fake GOPATH 46 @cd $(CRT_IN_GOPATH) && ln -s ../../../.. api 47 @## run openapi-gen from within the fake GOPATH (otherwise the package paths would be relative 48 @## and function names would be different) 49 GOPATH=$(FAKE_GOPATH) \ 50 && cd $(CRT_IN_GOPATH)/api \ 51 && $(OPENAPI_GEN) --input-dirs ./api/$(API_VERSION)/ \ 52 --output-package github.com/codeready-toolchain/api/api/$(API_VERSION) \ 53 --output-file-base zz_generated.openapi \ 54 --go-header-file=make/go-header.txt 55 @## clean up the mess 56 rm -Rf $(FAKE_GOPATH) 57 58 # make sure that that the `host-operator` and `member-operator` repositories exist locally 59 # and that they don't have any pending changes (except for the CRD files). 60 # The reasonning here is that when a change is made in the `api` repository, the resulting changes 61 # in the `host-operator` and `member-operator` repositories can be pushed at the same time on GitHub, 62 # without having to wait for some other feature or fix to be completed. 63 # TODO: we could even go further and checkout new branches from `master` in the 'host-operator' 64 # and 'member-operator` repositories and give them the name of the current branch in this repo. 65 # The developer would have 3 branches with the same name and could then push to GitHub at the 66 # same time... 67 host_repo_status := $(shell cd ../host-operator && git status -s | grep -v ${PATH_TO_CRD_BASES}) 68 member_repo_status := $(shell cd ../member-operator && git status -s | grep -v ${PATH_TO_CRD_BASES}) 69 70 PHONY: prepare-host-operator 71 prepare-host-operator: ../host-operator 72 ifdef host_repo_status 73 @echo "The local '../host-operator' repository has pending changes. Please stash them or commit them, first." 74 @exit 1 75 endif 76 ifneq ($(wildcard ../host-operator/${PATH_TO_CRD_BASES}/*.yaml),) 77 @-find ../host-operator/${PATH_TO_CRD_BASES} -type f | grep -v "cr\.yaml" | xargs rm || true 78 else 79 @-mkdir -p ../host-operator/${PATH_TO_CRD_BASES} 80 endif 81 82 PHONY: prepare-member-operator 83 prepare-member-operator: ../member-operator 84 ifdef member_repo_status 85 @echo "The local '../member-operator' repository has pending changes. Please stash them or commit them, first." 86 @exit 1 87 endif 88 ifneq ($(wildcard ../member-operator/${PATH_TO_CRD_BASES}/*.yaml),) 89 @-find ../member-operator/${PATH_TO_CRD_BASES} -type f | grep -v "cr\.yaml" | xargs rm || true 90 else 91 @-mkdir -p ../member-operator/${PATH_TO_CRD_BASES} 92 endif 93 94 .PHONY: dispatch-crds 95 dispatch-crds: prepare-host-operator prepare-member-operator 96 @echo "Dispatching CRD files in the 'host-operator' and 'member-operator' repositories..." 97 # Dispatching CRD files to operator repositories 98 @for crd in $(HOST_CLUSTER_CRDS) ; do \ 99 cp ${PATH_TO_CRD_BASES}/$(API_FULL_GROUPNAME)_$${crd}.yaml ../host-operator/${PATH_TO_CRD_BASES}/$(API_FULL_GROUPNAME)_$${crd}.yaml ; \ 100 done 101 @for crd in $(MEMBER_CLUSTER_CRDS) ; do \ 102 cp ${PATH_TO_CRD_BASES}/$(API_FULL_GROUPNAME)_$${crd}.yaml ../member-operator/${PATH_TO_CRD_BASES}/$(API_FULL_GROUPNAME)_$${crd}.yaml ; \ 103 done 104 # Now let's remove the CRDs from config/crd/bases directory 105 @for crd in $(HOST_CLUSTER_CRDS) $(MEMBER_CLUSTER_CRDS) ; do \ 106 rm ${PATH_TO_CRD_BASES}/$(API_FULL_GROUPNAME)_$${crd}.yaml 2>/dev/null || true; \ 107 done 108 @if [[ "$$(ls -A ${PATH_TO_CRD_BASES}/*yaml || true)" ]]; then \ 109 echo "ERROR: some CRD files were not dispatched: $$(ls -A ${PATH_TO_CRD_BASES}/*yaml)"; \ 110 echo "Please update this Makefile accordingly."; \ 111 exit 1; \ 112 fi 113 @echo "Dispatch successfuly finished \o/" 114 115 .PHONY: copy-reg-service-template 116 copy-reg-service-template: 117 cp ../registration-service/deploy/registration-service.yaml ../host-operator/deploy/registration-service/registration-service.yaml 118 119 120 CONTROLLER_GEN = $(PROJECT_DIR)/bin/controller-gen 121 controller-gen: ## Download controller-gen locally if necessary. 122 GOBIN=$(PROJECT_DIR)/bin $(GO) install sigs.k8s.io/controller-tools/cmd/controller-gen 123 124 OPENAPI_GEN = $(PROJECT_DIR)/bin/openapi-gen 125 openapi-gen: ## Download openapi-gen locally if necessary. 126 GOBIN=$(PROJECT_DIR)/bin $(GO) install k8s.io/kube-openapi/cmd/openapi-gen