-
Notifications
You must be signed in to change notification settings - Fork 83
/
GNUmakefile
246 lines (210 loc) · 7.6 KB
/
GNUmakefile
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
SHELL = bash
default: lint check test dev
GIT_COMMIT := $(shell git rev-parse --short HEAD)
GIT_DIRTY := $(if $(shell git status --porcelain),+CHANGES)
GO_LDFLAGS := "$(GO_LDFLAGS) -X github.com/hashicorp/nomad-autoscaler/version.GitCommit=$(GIT_COMMIT)$(GIT_DIRTY)"
GO_TAGS := ""
# Attempt to use gotestsum for running tests, otherwise fallback to go test.
GO_TEST_CMD = $(if $(shell command -v gotestsum 2>/dev/null),gotestsum --,go test)
# Respect $GOBIN if set in environment or via $GOENV file.
BIN := $(shell go env GOBIN)
ifndef BIN
BIN := $(GOPATH)/bin
endif
HELP_FORMAT=" \033[36m%-25s\033[0m %s\n"
.PHONY: help
help: ## Display this usage information
@echo "Valid targets:"
@grep -E '^[^ ]+:.*?## .*$$' $(MAKEFILE_LIST) | \
sort | \
awk 'BEGIN {FS = ":.*?## "}; \
{printf $(HELP_FORMAT), $$1, $$2}'
@echo ""
.PHONY: tools
tools: lint-tools test-tools generate-tools
.PHONY: generate-tools
generate-tools: ## Install the tools used to generate code
@echo "==> Installing code generate tools..."
go install github.com/bufbuild/buf/cmd/[email protected]
go install github.com/golang/protobuf/[email protected]
@echo "==> Done"
.PHONY: test-tools
test-tools: ## Install the tools used to run tests
@echo "==> Installing test tools..."
go install gotest.tools/[email protected]
@echo "==> Done"
.PHONY: lint-tools
lint-tools: ## Install the tools used to lint
@echo "==> Installing lint tools..."
go install github.com/golangci/golangci-lint/cmd/[email protected]
go install honnef.co/go/tools/cmd/[email protected]
go install github.com/hashicorp/go-hclog/[email protected]
go install github.com/hashicorp/hcl/v2/cmd/hclfmt@d0c4fa8b0bbc2e4eeccd1ed2a32c2089ed8c5cf1
@echo "==> Done"
pkg/%/nomad-autoscaler: GO_OUT ?= $@
pkg/windows_%/nomad-autoscaler: GO_OUT = [email protected]
pkg/%/nomad-autoscaler: ## Build Nomad Autoscaler for GOOS_GOARCH, e.g. pkg/linux_amd64/nomad-autoscaler
@echo "==> Building $@ with tags $(GO_TAGS)..."
@CGO_ENABLED=0 \
GOOS=$(firstword $(subst _, ,$*)) \
GOARCH=$(lastword $(subst _, ,$*)) \
go build -trimpath -ldflags $(GO_LDFLAGS) -tags "$(GO_TAGS)" -o $(GO_OUT)
.PRECIOUS: pkg/%/nomad-autoscaler
pkg/%.zip: pkg/%/nomad-autoscaler ## Build and zip Nomad Autoscaler for GOOS_GOARCH, e.g. pkg/linux_amd64.zip
@echo "==> Packaging for $@..."
@cp LICENSE $(dir $<)LICENSE.txt
zip -j $@ $(dir $<)*
.PHONY: dev
dev: lint ## Build for the current development version
@echo "==> Building autoscaler..."
@CGO_ENABLED=0 GOPROXY=direct go build \
-ldflags $(GO_LDFLAGS) \
-tags "$(GO_TAGS)" \
-o ./bin/nomad-autoscaler
@rm -f $(BIN)/nomad-autoscaler && cp ./bin/nomad-autoscaler $(BIN)/
@echo "==> Done"
.PHONY: proto
proto: ## Generate the protocol buffers
@echo "==> Generating proto bindings..."
@buf --config tools/buf/buf.yaml --template tools/buf/buf.gen.yaml generate
@echo "==> Done"
.PHONY: lint
lint: lint-tools generate-tools hclfmt ## Lint the source code
@echo "==> Linting source code..."
@GOPROXY=direct \
golangci-lint run -j 1 --build-tags "$(GO_TAGS)" --timeout=8m
@staticcheck ./...
@hclogvet .
@buf lint --config=./tools/buf/buf.yaml
@echo "==> Done"
.PHONY: hclfmt
hclfmt: ## Format HCL files with hclfmt
@echo "--> Formatting HCL"
@find . -name '.git' -prune \
-o \( -name '*.nomad' -o -name '*.hcl' -o -name '*.tf' \) \
-print0 | xargs -0 hclfmt -w
@if (git status -s | grep -q -e '\.hcl$$' -e '\.nomad$$' -e '\.tf$$'); then echo the following HCL files are out of sync; git status -s | grep -e '\.hcl$$' -e '\.nomad$$' -e '\.tf$$'; exit 1; fi
.PHONY: check
check: tools check-sdk check-root-mod check-protobuf ## Lint the source code and check other properties
.PHONY: check-sdk
check-sdk: ## Checks the SDK pkg is isolated
@echo "==> Checking SDK package is isolated..."
@if go list --test -f '{{ join .Deps "\n" }}' ./sdk | grep github.com/hashicorp/nomad-autoscaler/ | grep -v -e /nomad-autoscaler/sdk/ -e nomad-autoscaler/sdk.test; \
then echo " /sdk package depends the ^^ above internal packages. Remove such dependency"; \
exit 1; fi
@echo "==> Done"
.PHONY: check-root-mod
check-root-mod: ## Checks the root Go mod is tidy
@echo "==> Checking Go mod and Go sum..."
@go mod tidy
@if (git status --porcelain | grep -Eq "go\.(mod|sum)"); then \
echo go.mod or go.sum needs updating; \
git --no-pager diff go.mod; \
git --no-pager diff go.sum; \
exit 1; fi
@echo "==> Done"
.PHONY: check-protobuf
check-protobuf: ## Checks the protobuf files are in-sync
@$(MAKE) proto
@echo "==> Checking proto files are in-sync..."
@if (git status -s | grep -q .pb.go); then echo the following proto files are out of sync; git status -s | grep .pb.go; exit 1; fi
@echo "==> Done"
.PHONY: test
test: ## Test the source code
@$(MAKE) -C plugins/test
@echo "==> Testing source code..."
@GOPROXY=direct \
$(GO_TEST_CMD) -v -race -cover ./... -tags "$(GO_TAGS)"
@echo "==> Done"
.PHONY: clean-plugins
clean-plugins:
@echo "==> Cleaning plugins..."
@rm -rf ./bin/plugins/
@echo "==> Done"
.PHONY: clean
clean: clean-plugins
@echo "==> Cleaning build artifacts..."
@rm -f ./bin/nomad-autoscaler
@echo "==> Done"
bin/plugins/nomad-apm:
@echo "==> Building $@..."
@mkdir -p $$(dirname $@)
@cd ./plugins/builtin/apm/nomad && go build -o ../../../../$@
@echo "==> Done"
bin/plugins/nomad-target:
@echo "==> Building $@..."
@mkdir -p $$(dirname $@)
@cd ./plugins/builtin/target/nomad && go build -o ../../../../$@
@echo "==> Done"
bin/plugins/prometheus:
@echo "==> Building $@..."
@mkdir -p $$(dirname $@)
@cd ./plugins/builtin/apm/prometheus && go build -o ../../../../$@
@echo "==> Done"
bin/plugins/target-value:
@echo "==> Building $@..."
@mkdir -p $$(dirname $@)
@cd ./plugins/builtin/strategy/target-value && go build -o ../../../../$@
@echo "==> Done"
bin/plugins/fixed-value:
@echo "==> Building $@..."
@mkdir -p $$(dirname $@)
@cd ./plugins/builtin/strategy/fixed-value && go build -o ../../../../$@
@echo "==> Done"
bin/plugins/pass-through:
@echo "==> Building $@..."
@mkdir -p $$(dirname $@)
@cd ./plugins/builtin/strategy/pass-through && go build -o ../../../../$@
@echo "==> Done"
bin/plugins/threshold:
@echo "==> Building $@..."
@mkdir -p $$(dirname $@)
@cd ./plugins/builtin/strategy/threshold && go build -o ../../../../$@
@echo "==> Done"
bin/plugins/aws-asg:
@echo "==> Building $@..."
@mkdir -p $$(dirname $@)
@cd ./plugins/builtin/target/aws-asg && go build -o ../../../../$@
@echo "==> Done"
bin/plugins/datadog:
@echo "==> Building $@..."
@mkdir -p $$(dirname $@)
@cd ./plugins/builtin/apm/datadog && go build -o ../../../../$@
@echo "==> Done"
bin/plugins/azure-vmss:
@echo "==> Building $@..."
@mkdir -p $$(dirname $@)
@cd ./plugins/builtin/target/azure-vmss && go build -o ../../../../$@
@echo "==> Done"
bin/plugins/gce-mig:
@echo "==> Building $@..."
@mkdir -p $$(dirname $@)
@cd ./plugins/builtin/target/gce-mig && go build -o ../../../../$@
@echo "==> Done"
.PHONY: plugins
plugins: \
bin/plugins/nomad-apm \
bin/plugins/nomad-target \
bin/plugins/prometheus \
bin/plugins/target-value \
bin/plugins/fixed-value \
bin/plugins/pass-through \
bin/plugins/threshold \
bin/plugins/aws-asg \
bin/plugins/datadog \
bin/plugins/azure-vmss \
bin/plugins/gce-mig
.PHONY: tidy-test-plugin-mods
tidy-test-plugin-mods:
@echo "==> Tidying test plugin Go modules..."
@cd plugins/test/noop-apm && go mod tidy
@cd plugins/test/noop-strategy && go mod tidy
@cd plugins/test/noop-target && go mod tidy
@echo "==> Done"
.PHONY: version
version:
ifneq (,$(wildcard version/version_ent.go))
@$(CURDIR)/scripts/version.sh version/version.go version/version_ent.go
else
@$(CURDIR)/scripts/version.sh version/version.go version/version.go
endif