-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathMakefile
191 lines (159 loc) · 7.65 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
SHELL := /usr/bin/env bash -euo pipefail -c
PRODUCT_NAME ?= consul-dataplane
BIN_NAME ?= $(PRODUCT_NAME)
GOPATH ?= $(shell go env GOPATH)
GOBIN ?= $(GOPATH)/bin
GO_MODULES := $(shell find . -name go.mod -exec dirname {} \; | sort)
# Get local ARCH; on Intel Mac, 'uname -m' returns x86_64 which we turn into amd64.
# Not using 'go env GOOS/GOARCH' here so 'make docker' will work without local Go install.
ARCH ?= $(shell A=$$(uname -m); [ $$A = x86_64 ] && A=amd64; echo $$A)
# Only build for linux so that building the docker image works on M1 Macs.
OS ?= linux
PLATFORM = $(OS)/$(ARCH)
DIST = dist/$(PLATFORM)
BIN = $(DIST)/$(BIN_NAME)
VERSION = $(shell ./build-scripts/version.sh pkg/version/version.go)
GOLANG_VERSION ?= $(shell head -n 1 .go-version)
BOOTSTRAP_PACKAGE_DIR=internal/bootstrap
INTEGRATION_TESTS_SERVER_IMAGE ?= hashicorppreview/consul:1.15-dev
INTEGRATION_TESTS_DATAPLANE_IMAGE ?= $(PRODUCT_NAME)/release-default:$(VERSION)
GIT_COMMIT?=$(shell git rev-parse --short HEAD)
GIT_DIRTY?=$(shell test -n "`git status --porcelain`" && echo "+CHANGES" || true)
GOLDFLAGS=-X github.com/hashicorp/consul-dataplane/pkg/version.GitCommit=$(GIT_COMMIT)$(GIT_DIRTY)
# Get latest revision (no dirty check for now).
REVISION = $(shell git rev-parse HEAD)
# Docker Stuff.
export DOCKER_BUILDKIT=1
BUILD_ARGS = BIN_NAME=$(BIN_NAME) PRODUCT_VERSION=$(VERSION) PRODUCT_REVISION=$(REVISION) GOLANG_VERSION=$(GOLANG_VERSION)
TAG = $(PRODUCT_NAME):$(VERSION)
BA_FLAGS = $(addprefix --build-arg=,$(BUILD_ARGS))
FLAGS = --target $(TARGET) --platform $(PLATFORM) --tag $(TAG) $(BA_FLAGS)
##@ Build
dist: ## make dist directory and ignore everything
mkdir -p $(DIST)
echo '*' > dist/.gitignore
.PHONY: bin
bin: dist ## Build the binary
GOARCH=$(ARCH) GOOS=$(OS) CGO_ENABLED=0 go build -trimpath -buildvcs=false -ldflags="$(GOLDFLAGS)" -o $(BIN) ./cmd/$(BIN_NAME)
.PHONY: dev
dev: bin ## Build binary and copy to the destination
cp $(BIN) $(GOBIN)/$(BIN_NAME)
# DANGER: this target is experimental and could be modified/removed at any time.
.PHONY: skaffold
skaffold: dev ## Build consul-dataplane dev Docker image for use with skaffold or local development.
@docker build -t '$(DEV_IMAGE)' \
--build-arg 'GOLANG_VERSION=$(GOLANG_VERSION)' \
--build-arg 'TARGETARCH=$(ARCH)' \
-f $(CURDIR)/Dockerfile.dev .
.PHONY: docker
docker: bin ## build the release-target docker image
$(eval TARGET := release-default) # there are many targets in the Dockerfile, add more build if you need to customize the target
docker build $(FLAGS) .
@echo 'Image built; run "docker run --rm $(TAG)" to try it out.'
docker-run: docker ## run the image of $(TAG)
docker run --rm $(TAG)
.PHONY: dev-docker
dev-docker: docker ## build docker image and tag the image to local
echo '$(ARCH)'
docker tag '$(PRODUCT_NAME):$(VERSION)' '$(PRODUCT_NAME):local'
##@ Testing
.PHONY: unit-tests
unit-tests: ## unit tests
go test ./...
.PHONY: expand-integration-tests-output-dir
expand-integration-tests-output-dir: ## create directory to support integration tests
# make's built-in realpath function doesn't support non-existent directories
# and intermittently has issues finding newly created ones (so preemptively
# creating it with mkdir isn't an option) so we'll rely on the realpath bin.
ifdef INTEGRATION_TESTS_OUTPUT_DIR
ifeq (, $(shell which realpath))
$(error "GNU Coreutils are required to run the integration-tests target with INTEGRATION_TESTS_OUTPUT_DIR.")
else
EXPANDED_INTEGRATION_TESTS_OUTPUT_DIR = $(shell realpath $(INTEGRATION_TESTS_OUTPUT_DIR))
endif
endif
.PHONY: integration-tests
integration-tests: docker/release-default expand-integration-tests-output-dir ## integration tests
cd integration-tests && go test -v ./ -output-dir="$(EXPANDED_INTEGRATION_TESTS_OUTPUT_DIR)" -dataplane-image="$(INTEGRATION_TESTS_DATAPLANE_IMAGE)" -server-image="$(INTEGRATION_TESTS_SERVER_IMAGE)"
##@ Release
.PHONY: version
version: ## display version
@echo $(VERSION)
##@ Tools
# Our Envoy bootstrap config generation contains a fair amount of logic that
# was already implemented for the `consul connect envoy` command. Eventually,
# this command will go away and be replaced by consul-dataplane, but for now
# we copy the files from the Consul repo, and do a small amount of processing
# to rename the package and remove a dependency on the Consul api module.
.PHONY: copy-bootstrap-config
copy-bootstrap-config: ## copy bootstrap config
for file in bootstrap_config.go bootstrap_config_test.go bootstrap_tpl.go; do \
curl --fail https://raw.githubusercontent.com/hashicorp/consul/main/command/connect/envoy/$$file | \
sed 's/package envoy/package bootstrap/' | \
sed '/github.com\/hashicorp\/consul\/api/d' | \
sed 's/api.IntentionDefaultNamespace/"default"/g' | \
sed '1s:^:// Code generated by make copy-bootstrap-config. DO NOT EDIT.\n:' | \
sed '/"initial_metadata": \[/,/\]/d' | \
gofmt \
> $(BOOTSTRAP_PACKAGE_DIR)/$$file; \
done
.PHONY: changelog
changelog: ## build change log
ifdef DP_LAST_RELEASE_GIT_TAG
go run github.com/hashicorp/go-changelog/cmd/changelog-build@latest \
-last-release $(DP_LAST_RELEASE_GIT_TAG) \
-entries-dir .changelog/ \
-changelog-template .changelog/changelog.tmpl \
-note-template .changelog/note.tmpl \
-this-release $(REVISION)
else
$(error Cannot generate changelog without DP_LAST_RELEASE_GIT_TAG)
endif
.PHONY: check-env
check-env: ## check env
@printenv | grep "DP"
.PHONY: prepare-release
prepare-release:
ifndef DP_RELEASE_VERSION
$(error DP_RELEASE_VERSION is required)
endif
@$(CURDIR)/build-scripts/prepare-release.sh $(CURDIR)/pkg/version/version.go $(DP_RELEASE_VERSION) ""
.PHONY: prepare-dev
prepare-dev:
ifndef DP_NEXT_RELEASE_VERSION
$(error DP_NEXT_RELEASE_VERSION is required)
endif
@$(CURDIR)/build-scripts/prepare-release.sh $(CURDIR)/pkg/version/version.go $(DP_NEXT_RELEASE_VERSION) "dev"
# This generates mocks against public proto packages in consul. At the time of writing,
# only the dns and resource packages are used in consul-dataplane so only mocks for their
# interfaces are generated here.
.PHONY: mocks
mocks:
for pkg in pbdns pbresource; do \
mockery --srcpkg=github.com/hashicorp/consul/proto-public/$$pkg --output ./internal/mocks/$${pkg}mock --outpkg $${pkg}mock --case underscore --all; \
done
.PHONY: go-mod-get
go-mod-get: $(foreach mod,$(GO_MODULES),go-mod-get/$(mod)) ## Run go get and go mod tidy in every module for the given dependency
.PHONY: go-mod-get/%
go-mod-get/%:
ifndef DEP_VERSION
$(error DEP_VERSION is undefined: set this to <dependency>@<version>, e.g. github.com/hashicorp/[email protected])
endif
@echo "--> Running go get ${DEP_VERSION} ($*)"
@cd $* && go get $(DEP_VERSION)
@echo "--> Running go mod tidy ($*)"
@cd $* && go mod tidy
##@ Help
# The help target prints out all targets with their descriptions organized
# beneath their categories. The categories are represented by '##@' and the
# target descriptions by '##'. The awk commands is responsible for reading the
# entire set of makefiles included in this invocation, looking for lines of the
# file as xyz: ## something, and then pretty-format the target and help. Then,
# if there's a line with ##@ something, that gets pretty-printed as a category.
# More info on the usage of ANSI control characters for terminal formatting:
# https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters
# More info on the awk command:
# http://linuxcommand.org/lc3_adv_awk.php
.PHONY: help
help: ## Display this help
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)