forked from hashicorp/consul-dataplane
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
147 lines (122 loc) · 6.02 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
SHELL := /usr/bin/env bash -euo pipefail -c
REPO_NAME ?= $(shell basename "$(CURDIR)")
PRODUCT_NAME ?= $(REPO_NAME)
BIN_NAME ?= $(PRODUCT_NAME)
GOPATH ?= $(shell go env GOPATH)
GOBIN ?= $(GOPATH)/bin
# 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)
OS ?= $(shell uname | tr [[:upper:]] [[:lower:]])
PLATFORM = $(OS)/$(ARCH)
DIST = dist/$(PLATFORM)
BIN = $(DIST)/$(BIN_NAME)
VERSION = $(shell ./build-scripts/version.sh pkg/version/version.go)
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)
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 '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
$(eval OS := linux)
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
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 LAST_RELEASE_GIT_TAG
@changelog-build \
-last-release $(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 LAST_RELEASE_GIT_TAG)
endif
##@ 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)