forked from VirtusLab/kubedrainer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
241 lines (200 loc) · 7.2 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
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
# Setup variables for the Makefile
NAME := kubedrainer
PKG := github.com/VirtusLab/$(NAME)
REPO := virtuslab/$(NAME)
DOCKER_REGISTRY := quay.io
# Set POSIX sh for maximum interoperability
SHELL := /bin/sh
PATH := $(GOPATH)/bin:$(PATH)
# Set an output prefix, which is the local directory if not specified
PREFIX?=$(shell pwd)
# Set the main.go path for go command
BUILD_PATH := ./cmd/$(NAME)
# Set any default go build tags
BUILDTAGS :=
# Set the build dir, where built cross-compiled binaries will be output
BUILDDIR := ${PREFIX}/cross
# Populate version variables
# Add to compile time flags
VERSION := $(shell cat VERSION.txt)
GITCOMMIT := $(shell git rev-parse --short HEAD)
GITBRANCH := $(shell git rev-parse --abbrev-ref HEAD)
GITUNTRACKEDCHANGES := $(shell git status --porcelain --untracked-files=no)
GITIGNOREDBUTTRACKEDCHANGES := $(shell git ls-files -i --exclude-standard)
ifneq ($(GITUNTRACKEDCHANGES),)
GITCOMMIT := $(GITCOMMIT)-dirty
endif
ifneq ($(GITIGNOREDBUTTRACKEDCHANGES),)
GITCOMMIT := $(GITCOMMIT)-dirty
endif
VERSION_TAG := $(VERSION)-$(GITCOMMIT)
LATEST_TAG := latest
CTIMEVAR=-X $(PKG)/version.GITCOMMIT=$(GITCOMMIT) -X $(PKG)/version.VERSION=$(VERSION)
GO_FLAGS=-ldflags "-w $(CTIMEVAR)"
GO_FLAGS_STATIC=-ldflags "-w $(CTIMEVAR) -extldflags -static"
# List the GOOS and GOARCH to build
GOOSARCHES = darwin/amd64 linux/arm linux/arm64 linux/amd64 windows/amd64
PACKAGES = $(shell go list -f '{{.ImportPath}}/' ./...)
ARGS ?= $(EXTRA_ARGS)
.DEFAULT_GOAL := help
.PHONY: all
all: clean mod verify build docker-build ## Ensure deps, test, verify, docker build
@echo "+ $@"
.PHONY: init
init: ## Initializes this Makefile dependencies
@echo "+ $@"
@# https://github.com/golang/go/issues/32502
GO111MODULE=off go get -u golang.org/x/lint/golint
GO111MODULE=off go get -u honnef.co/go/tools/cmd/staticcheck
GO111MODULE=off go get -u golang.org/x/tools/cmd/goimports
GO111MODULE=off go get -u github.com/mrtazz/checkmake
GO111MODULE=off go get -u github.com/jessfraz/junk/sembump
.PHONY: mod
mod: ## Downloads dependencies and updates go.mod and go.sum
@echo "+ $@"
go mod tidy
.PHONY: build
build: $(NAME) ## Builds a dynamic executable or package
@echo "+ $@"
$(NAME): $(wildcard *.go) $(wildcard */*.go) VERSION.txt
@echo "+ $@"
go build -tags "$(BUILDTAGS)" ${GO_FLAGS} -o $(NAME) $(BUILD_PATH)
.PHONY: static
static: ## Builds a static executable
@echo "+ $@"
CGO_ENABLED=0 go build \
-tags "$(BUILDTAGS) static_build" \
${GO_FLAGS_STATIC} -o $(NAME) $(BUILD_PATH)
.PHONY: fmt
fmt: ## Verifies all files have been `gofmt`ed
@echo "+ $@"
@go fmt $(PACKAGES)
.PHONY: lint
lint: ## Verifies `golint` passes
@echo "+ $@"
@golint -set_exit_status $(PACKAGES)
.PHONY: goimports
goimports: ## Verifies `goimports` passes
@echo "+ $@"
@goimports -l -e $(shell find . -type f -name '*.go')
.PHONY: test
test: ## Runs the go tests
@echo "+ $@"
@RUNNING_TESTS=1 go test -v -tags "$(BUILDTAGS) cgo" $(PACKAGES)
.PHONY: vet
vet: ## Verifies `go vet` passes
@echo "+ $@"
@go vet $(PACKAGES)
.PHONY: staticcheck
staticcheck: ## Verifies `staticcheck` passes
@echo "+ $@"
@staticcheck $(PACKAGES)
.PHONY: install
install: ## Installs the executable
@echo "+ $@"
@go install -tags "$(BUILDTAGS)" ${GO_FLAGS} $(BUILD_PATH)
.PHONY: run
run: ## Run the executable, you can use EXTRA_ARGS
@echo "+ $@"
@go run -tags "$(BUILDTAGS)" ${GO_FLAGS} $(BUILD_PATH)/main.go $(ARGS)
define buildrelease
GOOS=$(1) GOARCH=$(2) CGO_ENABLED=0 go build \
-o $(BUILDDIR)/$(NAME)-$(1)-$(2) \
-a -tags "$(BUILDTAGS) static_build netgo" \
-installsuffix netgo ${GO_FLAGS_STATIC} $(BUILD_PATH);
md5sum $(BUILDDIR)/$(NAME)-$(1)-$(2) > $(BUILDDIR)/$(NAME)-$(1)-$(2).md5;
sha256sum $(BUILDDIR)/$(NAME)-$(1)-$(2) > $(BUILDDIR)/$(NAME)-$(1)-$(2).sha256;
endef
.PHONY: release
release: $(wildcard *.go) $(wildcard */*.go) VERSION.txt ## Builds the cross-compiled binaries, naming them in such a way for release (eg. binary-GOOS-GOARCH)
@echo "+ $@"
$(foreach GOOSARCH,$(GOOSARCHES), $(call buildrelease,$(subst /,,$(dir $(GOOSARCH))),$(notdir $(GOOSARCH))))
.PHONY: verify
verify: fmt lint vet staticcheck goimports test ## Runs a fmt, lint, vet, staticcheck, goimports and test
.PHONY: cover
cover: ## Runs go test with coverage
@echo "" > coverage.txt
@for d in $(PACKAGES); do \
RUNNING_TESTS=1 go test -race -coverprofile=profile.out -covermode=atomic "$$d"; \
if [ -f profile.out ]; then \
cat profile.out >> coverage.txt; \
rm profile.out; \
fi; \
done;
.PHONY: clean
clean: ## Cleanup any build binaries or packages
@echo "+ $@"
go clean
$(RM) $(NAME) || echo "Couldn't delete, not there."
$(RM) test$(NAME) || echo "Couldn't delete, not there."
$(RM) -r $(BUILDDIR) || echo "Couldn't delete, not there."
$(RM) coverage.txt || echo "Couldn't delete, not there."
.PHONY: spring-clean
spring-clean: ## Cleanup git ignored files (interactive)
@echo "+ $@"
git clean -Xdi
.PHONY: docker-build
docker-build: ## Build the container
@echo "+ $@"
docker build -t $(REPO):$(GITCOMMIT) .
.PHONY: docker-login
docker-login: ## Log in into the repository
@echo "+ $@"
@docker login -u="${DOCKER_USER}" -p="${DOCKER_PASS}" $(DOCKER_REGISTRY)
.PHONY: docker-images
docker-images: ## List all local containers
@echo "+ $@"
@docker images
.PHONY: docker-push
docker-push: ## Push the container
@echo "+ $@"
@docker tag $(REPO):$(GITCOMMIT) $(DOCKER_REGISTRY)/$(REPO):$(VERSION)
@docker tag $(REPO):$(GITCOMMIT) $(DOCKER_REGISTRY)/$(REPO):$(VERSION_TAG)
@docker tag $(REPO):$(GITCOMMIT) $(DOCKER_REGISTRY)/$(REPO):$(LATEST_TAG)
@docker push $(DOCKER_REGISTRY)/$(REPO):$(VERSION)
@docker push $(DOCKER_REGISTRY)/$(REPO):$(VERSION_TAG)
@docker push $(DOCKER_REGISTRY)/$(REPO):$(LATEST_TAG)
.PHONY: bump-version
BUMP := patch
bump-version: ## Bump the version in the version file. Set BUMP to [ patch | major | minor ]
@echo "+ $@"
$(eval NEW_VERSION=$(shell sembump --kind $(BUMP) $(VERSION)))
@echo "Bumping VERSION.txt from $(VERSION) to $(NEW_VERSION)"
@echo $(NEW_VERSION) > VERSION.txt
@echo "Updating version from $(VERSION) to $(NEW_VERSION) in README.md"
sed -i s/$(VERSION)/$(NEW_VERSION)/g README.md
sed -i s/$(VERSION)/$(NEW_VERSION)/g internal/version/version.go
sed -i s/$(VERSION)/$(NEW_VERSION)/g examples/kubernetes.yaml
git add VERSION.txt README.md internal/version/version.go examples/kubernetes.yaml
git commit -vseam "Bump version to $(NEW_VERSION)"
@echo "Run make tag to create and push the tag for new version $(NEW_VERSION)"
.PHONY: tag
tag: ## Create a new git tag to prepare to build a release
@echo "+ $@"
git tag -a $(VERSION) -m "$(VERSION)"
git push origin $(VERSION)
.PHONY: status
status: ## Shows general status
@echo "+ $@"
@echo "Commit: $(GITCOMMIT), VERSION: $(VERSION)"
@echo
ifneq ($(GITUNTRACKEDCHANGES),)
@echo "Changed files:"
@git status --porcelain --untracked-files=no
@echo
endif
ifneq ($(GITIGNOREDBUTTRACKEDCHANGES),)
@echo "Ignored but tracked files:"
@git ls-files -i --exclude-standard
@echo
endif
@echo "Dependencies:"
@go list -mod=readonly -m all
@echo
.PHONY: checkmake
checkmake: ## Check this Makefile
@echo "+ $@"
@checkmake Makefile
.PHONY: help
help:
@grep -Eh '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'