diff --git a/makelib/golang.mk b/makelib/golang.mk index 0597ef6..de3db86 100644 --- a/makelib/golang.mk +++ b/makelib/golang.mk @@ -78,13 +78,11 @@ ifneq ($(GO_TEST_SUITE),) GO_TEST_FLAGS += -run '$(GO_TEST_SUITE)' endif -# no GOPATH is set, since go modules are being used -GOPATH := - -# enable go modules -export GO111MODULE=on +GOPATH := $(shell go env GOPATH) # setup tools used during the build +DEP_VERSION=v0.5.1 +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 @@ -140,19 +138,35 @@ endif GO_COMMON_FLAGS = $(GO_BUILDFLAGS) -tags '$(GO_TAGS)' GO_STATIC_FLAGS = $(GO_COMMON_FLAGS) $(GO_PKG_STATIC_FLAGS) -installsuffix static -ldflags '$(GO_LDFLAGS)' +GO_INIT_REQUIRED_TARGETS = go.vendor.lite +GO_VENDOR_TARGET = go.vendor +# switch for go modules +ifeq ($(GO111MODULE),on) +# no GOPATH is set, since go modules are being used +export GOPATH := + +GO_STATIC_FLAGS += -mod=vendor +GO_INIT_REQUIRED_TARGETS = go.mod.verify +GO_VENDOR_TARGET = go.mod.vendor + +endif + # ==================================================================================== # Go Targets -go.init: go.vendor +go.init: $(GO_INIT_REQUIRED_TARGETS) @if ! `$(GO) version | grep -q -E '\bgo($(GO_SUPPORTED_VERSIONS))\b'`; then \ $(ERR) unsupported go version. Please make install one of the following supported version: '$(GO_SUPPORTED_VERSIONS)' ;\ exit 1 ;\ fi + @if [ "$(GO111MODULE)" != "on" ] && [ "$(realpath ../../../..)" != "$(realpath $(GOPATH))" ]; then \ + $(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 go.build: @$(INFO) go build $(PLATFORM) - $(foreach p,$(GO_STATIC_PACKAGES),@CGO_ENABLED=0 $(GO) build -mod=vendor -v -i -o $(GO_OUT_DIR)/$(lastword $(subst /, ,$(p)))$(GO_OUT_EXT) $(GO_STATIC_FLAGS) $(p) || $(FAIL) ${\n}) - $(foreach p,$(GO_TEST_PACKAGES) $(GO_LONGHAUL_TEST_PACKAGES),@CGO_ENABLED=0 $(GO) test -mod=vendor -i -c -o $(GO_TEST_OUTPUT)/$(lastword $(subst /, ,$(p)))$(GO_OUT_EXT) $(GO_STATIC_FLAGS) $(p) || $(FAIL ${\n})) + $(foreach p,$(GO_STATIC_PACKAGES),@CGO_ENABLED=0 $(GO) build -v -i -o $(GO_OUT_DIR)/$(lastword $(subst /, ,$(p)))$(GO_OUT_EXT) $(GO_STATIC_FLAGS) $(p) || $(FAIL) ${\n}) + $(foreach p,$(GO_TEST_PACKAGES) $(GO_LONGHAUL_TEST_PACKAGES),@CGO_ENABLED=0 $(GO) test -i -c -o $(GO_TEST_OUTPUT)/$(lastword $(subst /, ,$(p)))$(GO_OUT_EXT) $(GO_STATIC_FLAGS) $(p) || $(FAIL ${\n})) @$(MAKE) go.writeable.mod.folder @$(OK) go build $(PLATFORM) @@ -225,41 +239,64 @@ go.imports.fix: $(GOIMPORTS) go.validate: go.vet go.fmt -go.vendor.verify: +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. + @if [ ! -d $(GO_VENDOR_DIR) ]; then \ + $(MAKE) vendor; \ + elif ! $(DEP) ensure -no-vendor -dry-run &> /dev/null; then \ + $(MAKE) vendor; \ + fi + +go.vendor.check: $(DEP) + @$(INFO) checking if vendor deps changed + @$(DEP) check -skip-vendor || $(FAIL) + @$(OK) vendor deps have not changed + +go.vendor.update: $(DEP) + @$(INFO) updating vendor deps + @$(DEP) ensure -update -v || $(FAIL) + @$(OK) updating vendor deps + +go.vendor: $(DEP) + @$(INFO) dep ensure + @$(DEP) ensure || $(FAIL) + @$(OK) dep ensure + +go.mod.verify: @$(INFO) verify dependencies have expected content - @$(GO) mod verify || $(FAIL) + @GO111MODULE=$(GO111MODULE) $(GO) mod verify || $(FAIL) @$(OK) go modules dependencies verified -go.vendor: +go.mod.vendor: @$(INFO) go mod vendor - @$(GO) mod vendor || $(FAIL) + @GO111MODULE=$(GO111MODULE) $(GO) mod vendor || $(FAIL) @$(MAKE) go.writeable.mod.folder @$(OK) go mod vendor +# makes sure the pkg/mod folder is writable (otherwise it won't be deleted) +go.writeable.mod.folder: + @echo + @#check if go modules is enabled +ifeq ($(GO111MODULE),on) + @test -d $(shell go env GOPATH)/pkg/mod && find $(shell go env GOPATH)/pkg/mod -type d -exec chmod 777 {} \; || echo +endif + go.clean: @rm -fr $(GO_BIN_DIR) $(GO_TEST_DIR) go.distclean: @rm -rf $(GO_VENDOR_DIR) $(GO_PKG_DIR) -# make sure the pkg/mod folder is writable (otherwise it won't be deleted) -go.writeable.mod.folder: - @test -d ~/go/pkg/mod && find ~/go/pkg/mod -type d -exec chmod 777 {} \; || echo - -# Generate manifests e.g. CRD, RBAC etc. -manifests: $(CONTROLLERGEN) - @$(INFO) Generating CRD manifests - @$(CONTROLLERGEN) crd:trivialVersions=true paths=./aws/... output:dir=$(CRD_DIR) - @$(OK) Generating CRD manifests - go.generate: $(GOIMPORTS) $(CONTROLLERGEN) @$(INFO) 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) + .PHONY: go.init go.build go.install go.test.unit go.test.integration go.test.codecov go.lint go.vet go.fmt go.generate -.PHONY: go.validate go.vendor go.vendor.verify go.clean go.distclean +.PHONY: go.validate go.vendor.lite go.vendor go.vendor.check go.vendor.update go.clean go.distclean go.mod.vendor go.mod.verify # ==================================================================================== # Common Targets @@ -281,21 +318,24 @@ fmt: go.imports fmt.simplify: go.fmt.simplify imports: go.imports imports.fix: go.imports.fix -vendor: go.vendor -vendor.verify: go.vendor.verify +vendor: $(GO_VENDOR_TARGET) +vendor.check: go.vendor.check +vendor.update: go.vendor.update vet: go.vet generate codegen: go.generate define GO_HELPTEXT Go Targets: generate Runs go code generation followed by goimports on generated files. - manifests Generates Kubernetes custom resources manifests (e.g. CRDs RBACs, ...) fmt Checks go source code for formatting issues. fmt.simplify Format, simplify, update source files. imports Checks go source code import lines for issues. imports.fix Updates go source files to fix issues with import lines. vendor Updates vendor packages. - vendor.verify Verify dependencies have expected content. + vendor.check Fail the build if vendor packages have changed. + vendor.update Update vendor dependencies. + go.mod.verify Verify go module dependencies have expected content. + go.mod.vendor Makes vendored copy of go modules dependencies. vet Checks go source code and reports suspicious constructs. test.unit.nocov Runs unit tests without coverage (faster for iterative development) endef @@ -311,6 +351,14 @@ help-special: go.help # ==================================================================================== # Tools install targets +$(DEP): + @$(INFO) installing dep-$(DEP_VERSION) $(HOSTOS)-$(HOSTARCH) + @mkdir -p $(TOOLS_HOST_DIR)/tmp-dep || $(FAIL) + @curl -fsSL -o $(DEP) https://github.com/golang/dep/releases/download/$(DEP_VERSION)/dep-$(HOSTOS)-$(HOSTARCH) || $(FAIL) + @chmod +x $(DEP) || $(FAIL) + @rm -fr $(TOOLS_HOST_DIR)/tmp-dep + @$(OK) installing dep-$(DEP_VERSION) $(HOSTOS)-$(HOSTARCH) + $(GOLANGCILINT): @$(INFO) installing golangci-lint-v$(GOLANGCILINT_VERSION) $(HOSTOS)-$(HOSTARCH) @mkdir -p $(TOOLS_HOST_DIR)/tmp-golangci-lint || $(FAIL) @@ -344,5 +392,7 @@ go.get: @GOBIN=$(TOOLS_HOST_DIR) $(GOHOST) get $(GOPKG) || $(FAIL) @# the below command reverts the go.mod modification because of running `go get` @# otherwise the build becomes dirty and won't be published +ifeq ($(GO111MODULE),on) @$(GOHOST) mod tidy +endif @$(OK) installed $(GOPKG) \ No newline at end of file diff --git a/makelib/k8s_controllers.mk b/makelib/k8s_controllers.mk new file mode 100644 index 0000000..0e9e683 --- /dev/null +++ b/makelib/k8s_controllers.mk @@ -0,0 +1,53 @@ +# Copyright 2016 The Upbound Authors. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# ==================================================================================== +# Options + +# ==================================================================================== +# Setup go environment + +-include golang.mk + +# the packages to be built statically, for example, $(GO_PROJECT)/cmd/mytool +ifeq ($(CRD_DIR),) +$(error please set CRD_DIR prior to including k8scontrollers.mk) +endif + +# ==================================================================================== +# k8scontrollers Targets + +# Generate manifests e.g. CRD, RBAC etc. +manifests: $(CONTROLLERGEN) + @$(INFO) Generating CRD manifests + @$(CONTROLLERGEN) crd:trivialVersions=true paths=./aws/... output:dir=$(CRD_DIR) + @$(OK) Generating CRD manifests + +# ==================================================================================== +# Common Targets + +# ==================================================================================== +# Special Targets + +define K8S_CONTROLLERS_HELPTEXT +Kubernetes Controllers Targets: + manifests Generates Kubernetes custom resources manifests (e.g. CRDs RBACs, ...) +endef +export K8S_CONTROLLERS_HELPTEXT + +help-special: k8s_controllers.help +k8s_controllers.help: + @echo "$$K8S_CONTROLLERS_HELPTEXT" +# ==================================================================================== +# Tools install targets \ No newline at end of file