Skip to content
This repository has been archived by the owner on Nov 25, 2024. It is now read-only.

Modify the golang.mk to support go modules #74

Merged
merged 3 commits into from
Oct 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 57 additions & 7 deletions makelib/golang.mk
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ DEP := $(TOOLS_HOST_DIR)/dep-$(DEP_VERSION)
GOJUNIT := $(TOOLS_HOST_DIR)/go-junit-report
GOCOVER_COBERTURA := $(TOOLS_HOST_DIR)/gocover-cobertura
GOIMPORTS := $(TOOLS_HOST_DIR)/goimports
CONTROLLER_GEN_VERSION ?= v0.2.1
CONTROLLERGEN := $(TOOLS_HOST_DIR)/controller-gen
soorena776 marked this conversation as resolved.
Show resolved Hide resolved

GO := go
GOHOST := GOOS=$(GOHOSTOS) GOARCH=$(GOHOSTARCH) go
Expand Down Expand Up @@ -132,6 +134,16 @@ endif
GO_COMMON_FLAGS = $(GO_BUILDFLAGS) -tags '$(GO_TAGS)'
GO_STATIC_FLAGS = $(GO_COMMON_FLAGS) $(GO_PKG_STATIC_FLAGS) -installsuffix static -ldflags '$(GO_LDFLAGS)'

export GO111MODULE

# switch for go modules
ifeq ($(GO111MODULE),on)
soorena776 marked this conversation as resolved.
Show resolved Hide resolved

# set GOPATH to $(GO_PKG_DIR), so that the go modules are installed there, instead of the default $HOME/go/pkg/mod
export GOPATH=$(abspath $(GO_PKG_DIR))

endif

# ====================================================================================
# Go Targets

Expand All @@ -140,7 +152,7 @@ go.init: go.vendor.lite
$(ERR) unsupported go version. Please make install one of the following supported version: '$(GO_SUPPORTED_VERSIONS)' ;\
exit 1 ;\
fi
@if [ "$(realpath ../../../..)" != "$(realpath $(GOPATH))" ]; then \
@if [ "$(GO111MODULE)" != "on" ] && [ "$(realpath ../../../..)" != "$(realpath $(GOPATH))" ]; then \
soorena776 marked this conversation as resolved.
Show resolved Hide resolved
$(WARN) the source directory is not relative to the GOPATH at $(GOPATH) or you are you using symlinks. The build might run into issue. Please move the source directory to be at $(GOPATH)/src/$(GO_PROJECT) ;\
fi

Expand Down Expand Up @@ -217,6 +229,25 @@ go.imports.fix: $(GOIMPORTS)

go.validate: go.vet go.fmt

ifeq ($(GO111MODULE),on)

go.vendor.lite go.vendor.check:
@$(INFO) verify dependencies have expected content
@$(GOHOST) mod verify || $(FAIL)
@$(OK) go modules dependencies verified

go.vendor.update:
@$(INFO) update go modules
@$(GOHOST) get -u ./... || $(FAIL)
@$(OK) update go modules

go.vendor:
@$(INFO) go mod vendor
@$(GOHOST) mod vendor || $(FAIL)
@$(OK) go mod vendor

else

go.vendor.lite: $(DEP)
# dep ensure blindly updates the whole vendor tree causing everything to be rebuilt. This workaround
# will only call dep ensure if the .lock file changes or if the vendor dir is non-existent.
Expand All @@ -241,17 +272,23 @@ go.vendor: $(DEP)
@$(DEP) ensure || $(FAIL)
@$(OK) dep ensure

endif

go.clean:
@# `go modules` creates read-only folders under WORK_DIR
@# make all folders within WORK_DIR writable, so they can be deleted
@if [ -d $(WORK_DIR) ]; then chmod -R +w $(WORK_DIR); fi

@rm -fr $(GO_BIN_DIR) $(GO_TEST_DIR)

go.distclean:
@rm -rf $(GO_VENDOR_DIR) $(GO_PKG_DIR)

go.generate: $(GOIMPORTS)
go.generate: $(GOIMPORTS) $(CONTROLLERGEN)
@$(INFO) go generate $(PLATFORM)
@CGO_ENABLED=0 $(GOHOST) generate $(GO_COMMON_FLAGS) $(GO_PACKAGES) $(GO_INTEGRATION_TEST_PACKAGES) || $(FAIL)
@$(OK) go generate $(PLATFORM)
@CGO_ENABLED=0 CONTROLLERGEN=$(CONTROLLERGEN) $(GOHOST) generate $(GO_COMMON_FLAGS) $(GO_PACKAGES) $(GO_INTEGRATION_TEST_PACKAGES) || $(FAIL)
@find $(GO_SUBDIRS) $(GO_INTEGRATION_TESTS_SUBDIRS) -type f -name 'zz_generated*' -exec $(GOIMPORTS) -l -w -local $(GO_PROJECT) {} \;
@$(OK) go generate $(PLATFORM)

soorena776 marked this conversation as resolved.
Show resolved Hide resolved

.PHONY: go.init go.build go.install go.test.unit go.test.integration go.test.codecov go.lint go.vet go.fmt go.generate
Expand Down Expand Up @@ -335,20 +372,33 @@ $(GOFMT):
$(GOIMPORTS):
@$(INFO) installing goimports
@mkdir -p $(TOOLS_HOST_DIR)/tmp-imports || $(FAIL)
@GOPATH=$(TOOLS_HOST_DIR)/tmp-imports GOBIN=$(TOOLS_HOST_DIR) $(GOHOST) get -u golang.org/x/tools/cmd/goimports || rm -fr $(TOOLS_HOST_DIR)/tmp-imports || $(FAIL)
@GO111MODULE=off GOPATH=$(TOOLS_HOST_DIR)/tmp-imports GOBIN=$(TOOLS_HOST_DIR) $(GOHOST) get -u golang.org/x/tools/cmd/goimports || rm -fr $(TOOLS_HOST_DIR)/tmp-imports || $(FAIL)
soorena776 marked this conversation as resolved.
Show resolved Hide resolved
@rm -fr $(TOOLS_HOST_DIR)/tmp-imports
@$(OK) installing goimports

$(GOJUNIT):
@$(INFO) installing go-junit-report
@mkdir -p $(TOOLS_HOST_DIR)/tmp-junit || $(FAIL)
@GOPATH=$(TOOLS_HOST_DIR)/tmp-junit GOBIN=$(TOOLS_HOST_DIR) $(GOHOST) get github.com/jstemmer/go-junit-report || rm -fr $(TOOLS_HOST_DIR)/tmp-junit || $(FAIL)
@GO111MODULE=off GOPATH=$(TOOLS_HOST_DIR)/tmp-junit GOBIN=$(TOOLS_HOST_DIR) $(GOHOST) get github.com/jstemmer/go-junit-report || rm -fr $(TOOLS_HOST_DIR)/tmp-junit || $(FAIL)
@rm -fr $(TOOLS_HOST_DIR)/tmp-junit
@$(OK) installing go-junit-report

$(GOCOVER_COBERTURA):
@$(INFO) installing gocover-cobertura
@mkdir -p $(TOOLS_HOST_DIR)/tmp-gocover-cobertura || $(FAIL)
@GOPATH=$(TOOLS_HOST_DIR)/tmp-gocover-cobertura GOBIN=$(TOOLS_HOST_DIR) $(GOHOST) get github.com/t-yuki/gocover-cobertura || rm -fr $(TOOLS_HOST_DIR)/tmp-covcover-cobertura || $(FAIL)
@GO111MODULE=off GOPATH=$(TOOLS_HOST_DIR)/tmp-gocover-cobertura GOBIN=$(TOOLS_HOST_DIR) $(GOHOST) get github.com/t-yuki/gocover-cobertura || rm -fr $(TOOLS_HOST_DIR)/tmp-covcover-cobertura || $(FAIL)
@rm -fr $(TOOLS_HOST_DIR)/tmp-gocover-cobertura
@$(OK) installing gocover-cobertura

$(CONTROLLERGEN):
@$(INFO) installing controller-gen @$(CONTROLLER_GEN_VERSION)
@mkdir -p $(TOOLS_HOST_DIR)/tmp-controllergen || $(FAIL)

@# `go get` only supports versioned go packages when GO111MODULE=on
@# since $(TOOLS_HOST_DIR) is under $(GO_PROJECT), make a temp folder which has a go.mod,
@# so that $(GO_PROJECT)/go.mod doesn't get modified because of running `go get`
@cd $(TOOLS_HOST_DIR)/tmp-controllergen; rm -f go.mod; GO111MODULE=on $(GOHOST) mod init tmp-controllergen
@cd $(TOOLS_HOST_DIR)/tmp-controllergen; GOPATH=$(abspath $(GO_PKG_DIR)) GO111MODULE=on GOBIN=$(TOOLS_HOST_DIR) $(GOHOST) get sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_GEN_VERSION) || $(FAIL)
@rm -fr $(TOOLS_HOST_DIR)/tmp-controllergen

@$(OK) installing controller-gen @$(CONTROLLER_GEN_VERSION)
31 changes: 28 additions & 3 deletions makelib/kubebuilder.mk
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,30 @@ TEST_ASSET_ETCD := $(KUBEBUILDER)/etcd
TEST_ASSET_KUBECTL := $(KUBEBUILDER)/kubectl
export TEST_ASSET_KUBE_APISERVER TEST_ASSET_ETCD TEST_ASSET_KUBECTL

# ====================================================================================
# Setup environment

-include golang.mk

ifeq ($(CRD_DIR),)
$(error please set CRD_DIR (directories where CRD manifests will be saved) prior to including kubebuilder.mk)
endif

ifeq ($(API_DIR),)
$(error please set API_DIR ()(directories where KUBEBUILDER API types are defined) prior to including kubebuilder.mk)
endif

# ====================================================================================
# Kubebuilder Targets

# Generate manifests e.g. CRD, RBAC etc.
kubebuilder.manifests: $(CONTROLLERGEN)
@$(INFO) Generating CRD manifests
@# first delete the CRD_DIR, to remove the CRDs of types that no longer exist
@rm -rf $(CRD_DIR)
@$(CONTROLLERGEN) crd:trivialVersions=true paths=$(API_DIR) output:dir=$(CRD_DIR)
@$(OK) Generating CRD manifests

# ====================================================================================
# Common Targets

Expand All @@ -35,8 +59,8 @@ test.init: $(KUBEBUILDER)

define KUBEBULDER_HELPTEXT
Kubebuilder Targets:
bin run kubebuilder binary, pass args by setting ARGS=""
codegen run code generation
bin Run kubebuilder binary, pass args by setting ARGS=""
manifests Generates Kubernetes custom resources manifests (e.g. CRDs RBACs, ...)

endef
export KUBEBULDER_HELPTEXT
Expand All @@ -45,11 +69,12 @@ kubebuilder.help:
@echo "$$KUBEBULDER_HELPTEXT"

help-special: kubebuilder.help
manifests: kubebuilder.manifests

kubebuilder.bin: $(KUBEBUILDER)
@$(KUBEBUILDER)/kubebuilder $(ARGS)

.PHONY: codegen kubebuilder.help kubebuilder.bin
.PHONY: kubebuilder.help kubebuilder.bin kubebuilder.manifests

# ====================================================================================
# tools
Expand Down