-
Notifications
You must be signed in to change notification settings - Fork 2
/
Makefile
242 lines (194 loc) · 7.28 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
242
SHELL=/bin/sh
BINARY ?= metabot
BUILD_IMAGE?=packethost/metabot
BUILDER_IMAGE?=packethost/go-build
PACKAGE_NAME?=github.com/packethost/metabot
GIT_VERSION?=$(shell git log -1 --format="%h")
RELEASE_TAG ?= $(shell git tag --points-at HEAD)
GO_FILES := $(shell find . -type f -not -path './vendor/*' -name '*.go')
FROMTAG ?= latest
LDFLAGS ?= -ldflags '-extldflags "-static"'
# which arches can we support
ARCHES=$(patsubst Dockerfile.%,%,$(wildcard Dockerfile.*))
# BUILDARCH is the host architecture
# ARCH is the target architecture
# we need to keep track of them separately
BUILDARCH ?= $(shell uname -m)
BUILDOS ?= $(shell uname -s | tr A-Z a-z)
# canonicalized names for host architecture
ifeq ($(BUILDARCH),aarch64)
BUILDARCH=arm64
endif
ifeq ($(BUILDARCH),x86_64)
BUILDARCH=amd64
endif
# unless otherwise set, I am building for my own architecture, i.e. not cross-compiling
ARCH ?= $(BUILDARCH)
# canonicalized names for target architecture
ifeq ($(ARCH),aarch64)
override ARCH=arm64
endif
ifeq ($(ARCH),x86_64)
override ARCH=amd64
endif
# Manifest tool, until `docker manifest` is fully ready. As of this writing, it remains experimental
MANIFEST_URL = https://github.com/estesp/manifest-tool/releases/download/v0.8.0/manifest-tool-$(BUILDOS)-$(BUILDARCH)
# these macros create a list of valid architectures for pushing manifests
space :=
space +=
comma := ,
prefix_linux = $(addprefix linux/,$(strip $1))
join_platforms = $(subst $(space),$(comma),$(call prefix_linux,$(strip $1)))
DIST_DIR=./dist/bin
DIST_BINARY = $(DIST_DIR)/$(BINARY)-$(ARCH)
BUILD_CMD = GOOS=linux GOARCH=$(ARCH) CGO_ENABLED=0
ifdef DOCKERBUILD
BUILD_CMD = docker run --rm \
-e GOARCH=$(ARCH) \
-e GOOS=linux \
-e CGO_ENABLED=0 \
-v $(CURDIR):/go/src/$(PACKAGE_NAME) \
-w /go/src/$(PACKAGE_NAME) \
$(BUILDER_IMAGE)
endif
pkgs:
ifndef PKG_LIST
$(eval PKG_LIST := $(shell $(BUILD_CMD) go list ./... | grep -v vendor))
endif
.PHONY: fmt-check lint test vet golint
$(DIST_DIR):
mkdir -p $@
## Check the file format
fmt-check:
@if [ -n "$(shell $(BUILD_CMD) gofmt -l ${GO_FILES})" ]; then \
$(BUILD_CMD) gofmt -s -e -d ${GO_FILES}; \
exit 1; \
fi
gometalinter:
ifeq (, $(shell which gometalinter))
go get -u github.com/alecthomas/gometalinter
endif
golint:
ifeq (, $(shell which golint))
go get -u golang.org/x/lint/golint
endif
## Lint the files
lint: pkgs golint gometalinter
@$(BUILD_CMD) gometalinter --disable-all --enable=golint --vendor ./...
## Run unittests
test: pkgs vendor
@$(BUILD_CMD) go test -short ${PKG_LIST}
## Vet the files
vet: pkgs vendor
@$(BUILD_CMD) go vet ${PKG_LIST}
## Read about data race https://golang.org/doc/articles/race_detector.html
## to not test file for race use `// +build !race` at top
## Run data race detector
race: pkgs
@$(BUILD_CMD) go test -race -short ${PKG_LIST}
## Display this help screen
help:
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
## Delete the csi
undeploy:
kubectl delete --now -f releases/v0.0.0.yaml
## Deploy the controller to kubernetes
deploy:
kubectl apply -f releases/v0.0.0.yaml
.PHONY: build build-all image vendor push deploy ci cd dep manifest-tool
## Build the binaries for all supported ARCH
build-all: $(addprefix sub-build-, $(ARCHES))
sub-build-%:
@$(MAKE) ARCH=$* build
## Build the binary for a single ARCH
build: $(DIST_BINARY)
$(DIST_BINARY): $(DIST_DIR) vendor
$(BUILD_CMD) go build -v -o $@ $(LDFLAGS) $(PACKAGE_NAME)
manifest-tool:
ifeq (, $(shell which manifest-tool))
mkdir -p $$GOPATH/bin
curl -L -o $$GOPATH/bin/$@ $(MANIFEST_URL)
chmod +x $$GOPATH/bin/$@
endif
## ensure vendor dependencies are installed
vendor: dep
$(BUILD_CMD) dep ensure -vendor-only
## make the images for all supported ARCH
image-all: $(addprefix sub-image-, $(ARCHES))
sub-image-%:
@$(MAKE) ARCH=$* image
## make the image for a single ARCH
image: register
docker image build -t $(BUILD_IMAGE):latest-$(ARCH) -f Dockerfile.$(ARCH) dist/
# Targets used when cross building.
.PHONY: register
# Enable binfmt adding support for miscellaneous binary formats.
# This is only needed when running non-native binaries.
register:
ifneq ($(BUILDARCH),$(ARCH))
docker run --rm --privileged multiarch/qemu-user-static:register || true
endif
## push the multi-arch manifest
push-manifest: manifest-tool imagetag
# path to credentials based on manifest-tool's requirements here https://github.com/estesp/manifest-tool#sample-usage
manifest-tool push from-args --platforms $(call join_platforms,$(ARCHES)) --template $(BUILD_IMAGE):$(IMAGETAG)-ARCH --target $(BUILD_IMAGE):$(IMAGETAG)
## push the images for all supported ARCH
push-all: imagetag $(addprefix sub-push-, $(ARCHES))
sub-push-%:
@$(MAKE) ARCH=$* push IMAGETAG=$(IMAGETAG)
push: imagetag
docker push $(BUILD_IMAGE):$(IMAGETAG)-$(ARCH)
# ensure we have a real imagetag
imagetag:
ifndef IMAGETAG
$(error IMAGETAG is undefined - run using make <target> IMAGETAG=X.Y.Z)
endif
## tag the images for all supported ARCH
tag-images-all: $(addprefix sub-tag-image-, $(ARCHES))
sub-tag-image-%:
@$(MAKE) ARCH=$* IMAGETAG=$(IMAGETAG) tag-images
tag-images: imagetag
docker tag $(BUILD_IMAGE):$(FROMTAG)-$(ARCH) $(BUILD_IMAGE):$(IMAGETAG)-$(ARCH)
## ensure that a particular tagged image exists across all support archs
pull-images-all: $(addprefix sub-pull-image-, $(ARCHES))
sub-pull-image-%:
@$(MAKE) ARCH=$* IMAGETAG=$(IMAGETAG) pull-images
## ensure that a particular tagged image exists locally; if not, pull it
pull-images: imagetag
ifeq (,$(shell docker image ls -q $(BUILD_IMAGE):$(IMAGETAG)-$(ARCH)))
docker pull $(BUILD_IMAGE):$(IMAGETAG)-$(ARCH)
endif
## clean up all artifacts
clean:
$(eval IMAGE_TAGS := $(shell docker image ls | awk "/^$(subst /,\/,$(BUILD_IMAGE))\s/"' {print $$2}' ))
docker image rm $(addprefix $(BUILD_IMAGE):,$(IMAGE_TAGS))
rm -rf dist/
###############################################################################
# CI/CD
###############################################################################
.PHONY: ci cd build deploy push release confirm pull-images
## Run what CI runs
# race has an issue with alpine, see https://github.com/golang/go/issues/14481
ci-test: fmt-check lint test vet # race
ci-build: build-all
ci-image: image-all
ci: ci-test ci-build ci-image
confirm:
ifndef CONFIRM
$(error CONFIRM is undefined - run using make <target> CONFIRM=true)
endif
cd: confirm
ifndef BRANCH_NAME
$(error BRANCH_NAME is undefined - run using make <target> BRANCH_NAME=var or set an environment variable)
endif
$(MAKE) tag-images-all push-all push-manifest IMAGETAG=${BRANCH_NAME}
$(MAKE) tag-images-all push-all push-manifest IMAGETAG=${GIT_VERSION}
## cut a release by using the latest git tag should only be run for an image that already exists and was pushed out
release: confirm
ifeq (,$(RELEASE_TAG))
$(error RELEASE_TAG is undefined - this means we are trying to do a release at a commit which does not have a release tag)
endif
$(MAKE) pull-images-all IMAGETAG=${GIT_VERSION} # ensure we have the image with the tag ${GIT_VERSION} or pull it
$(MAKE) tag-images-all FROMTAG=${GIT_VERSION} IMAGETAG=${RELEASE_TAG} # tag the pulled image
$(MAKE) push-all push-manifest IMAGETAG=${RELEASE_TAG} # push it
csi: build deploy ## Build and deploy the csi