forked from projectcalico/kube-controllers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
392 lines (337 loc) · 15.4 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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# Shortcut targets
default: build
## Build binary for current platform
all: build
## Run the tests for the current platform/architecture
test: ut fv
###############################################################################
# Both native and cross architecture builds are supported.
# The target architecture is select by setting the ARCH variable.
# When ARCH is undefined it is set to the detected host architecture.
# When ARCH differs from the host architecture a crossbuild will be performed.
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)
# 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
# list of arches *not* to build when doing *-all
# until s390x works correctly
EXCLUDEARCH ?= s390x
VALIDARCHES = $(filter-out $(EXCLUDEARCH),$(ARCHES))
# Determine which OS.
OS?=$(shell uname -s | tr A-Z a-z)
###############################################################################
GO_BUILD_VER?=v0.17
K8S_VERSION?=v1.10.4
HYPERKUBE_IMAGE?=gcr.io/google_containers/hyperkube-$(ARCH):$(K8S_VERSION)
ETCD_VERSION?=v3.3.7
ETCD_IMAGE?=quay.io/coreos/etcd:$(ETCD_VERSION)-$(BUILDARCH)
# If building on amd64 omit the arch in the container name.
ifeq ($(BUILDARCH),amd64)
ETCD_IMAGE=quay.io/coreos/etcd:$(ETCD_VERSION)
endif
# Makefile configuration options
CONTAINER_NAME=calico/kube-controllers
PACKAGE_NAME?=github.com/projectcalico/kube-controllers
CALICO_BUILD?=calico/go-build:$(GO_BUILD_VER)
LIBCALICOGO_PATH?=none
LOCAL_USER_ID?=$(shell id -u $$USER)
# Get version from git.
GIT_VERSION?=$(shell git describe --tags --dirty)
DOCKER_GO_BUILD := mkdir -p .go-pkg-cache && \
docker run --rm \
--net=host \
-e LOCAL_USER_ID=$(LOCAL_USER_ID) \
-v $${PWD}:/go/src/$(PACKAGE_NAME):rw \
-v $${PWD}/.go-pkg-cache:/go/pkg:rw \
-w /go/src/$(PACKAGE_NAME) \
$(CALICO_BUILD)
SRCFILES=cmd/kube-controllers/main.go $(shell find pkg -name '*.go')
## Removes all build artifacts.
clean:
rm -rf bin image.created-$(ARCH)
-docker rmi $(CONTAINER_NAME)
rm -f tests/fv/fv.test
###############################################################################
# Building the binary
###############################################################################
build: bin/kube-controllers-linux-$(ARCH) bin/check-status-linux-$(ARCH)
build-all: $(addprefix sub-build-,$(VALIDARCHES))
sub-build-%:
$(MAKE) build ARCH=$*
# Populates the vendor directory.
vendor: glide.lock
# Ensure that the glide cache directory exists.
mkdir -p $(HOME)/.glide
# To build without Docker just run "glide install -strip-vendor"
if [ "$(LIBCALICOGO_PATH)" != "none" ]; then \
EXTRA_DOCKER_BIND="-v $(LIBCALICOGO_PATH):/go/src/github.com/projectcalico/libcalico-go:ro"; \
fi; \
docker run --rm \
-v $(CURDIR):/go/src/$(PACKAGE_NAME):rw $$EXTRA_DOCKER_BIND \
-v $(HOME)/.glide:/home/user/.glide:rw \
-e LOCAL_USER_ID=$(LOCAL_USER_ID) \
-w /go/src/$(PACKAGE_NAME) \
$(CALICO_BUILD) glide install -strip-vendor
# Default the libcalico repo and version but allow them to be overridden
LIBCALICO_REPO?=github.com/projectcalico/libcalico-go
LIBCALICO_VERSION?=$(shell git ls-remote [email protected]:projectcalico/libcalico-go master 2>/dev/null | cut -f 1)
## Update libcalico pin in glide.yaml
update-libcalico:
docker run --rm \
-v $(CURDIR):/go/src/$(PACKAGE_NAME):rw $$EXTRA_DOCKER_BIND \
-v $(HOME)/.glide:/home/user/.glide:rw \
-e LOCAL_USER_ID=$(LOCAL_USER_ID) \
-w /go/src/$(PACKAGE_NAME) \
$(CALICO_BUILD) sh -c '\
echo "Updating libcalico to $(LIBCALICO_VERSION) from $(LIBCALICO_REPO)"; \
export OLD_VER=$$(grep --after 50 libcalico-go glide.yaml |grep --max-count=1 --only-matching --perl-regexp "version:\s*\K[\.0-9a-z]+") ;\
echo "Old version: $$OLD_VER";\
if [ $(LIBCALICO_VERSION) != $$OLD_VER ]; then \
sed -i "s/$$OLD_VER/$(LIBCALICO_VERSION)/" glide.yaml && \
if [ $(LIBCALICO_REPO) != "github.com/projectcalico/libcalico-go" ]; then \
glide mirror set https://github.com/projectcalico/libcalico-go $(LIBCALICO_REPO) --vcs git; glide mirror list; \
fi;\
OUTPUT=`mktemp`;\
glide up --strip-vendor; glide up --strip-vendor 2>&1 | tee $$OUTPUT; \
if ! grep "\[WARN\]" $$OUTPUT; then true; else false; fi; \
fi'
bin/kube-controllers-linux-$(ARCH): vendor $(SRCFILES)
mkdir -p bin
-mkdir -p .go-pkg-cache
docker run --rm \
-e GOOS=$(OS) -e GOARCH=$(ARCH) \
-v $(CURDIR):/go/src/$(PACKAGE_NAME):ro \
-v $(CURDIR)/bin:/go/src/$(PACKAGE_NAME)/bin \
-w /go/src/$(PACKAGE_NAME) \
-e LOCAL_USER_ID=$(LOCAL_USER_ID) \
-v $(CURDIR)/.go-pkg-cache:/go-cache/:rw \
-e GOCACHE=/go-cache \
$(CALICO_BUILD) go build -v -o bin/kube-controllers-$(OS)-$(ARCH) -ldflags "-X main.VERSION=$(GIT_VERSION)" ./cmd/kube-controllers/
bin/check-status-linux-$(ARCH): vendor $(SRCFILES)
mkdir -p bin
-mkdir -p .go-pkg-cache
docker run --rm \
-e GOOS=$(OS) -e GOARCH=$(ARCH) \
-v $(CURDIR):/go/src/$(PACKAGE_NAME):ro \
-v $(CURDIR)/bin:/go/src/$(PACKAGE_NAME)/bin \
-w /go/src/$(PACKAGE_NAME) \
-e LOCAL_USER_ID=$(LOCAL_USER_ID) \
-v $(CURDIR)/.go-pkg-cache:/go-cache/:rw \
-e GOCACHE=/go-cache \
$(CALICO_BUILD) go build -v -o bin/check-status-$(OS)-$(ARCH) -ldflags "-X main.VERSION=$(GIT_VERSION)" ./cmd/check-status/
###############################################################################
# Building the image
###############################################################################
## Builds the controller binary and docker image.
image: image.created-$(ARCH)
image-all: $(addprefix sub-image-,$(VALIDARCHES))
sub-image-%:
$(MAKE) image ARCH=$*
image.created-$(ARCH): bin/kube-controllers-linux-$(ARCH) bin/check-status-linux-$(ARCH)
# Build the docker image for the policy controller.
docker build -t $(CONTAINER_NAME):latest-$(ARCH) --build-arg QEMU_IMAGE=$(CALICO_BUILD) -f Dockerfile.$(ARCH) .
ifeq ($(ARCH),amd64)
# Need amd64 builds tagged as :latest because Semaphore depends on that
docker tag $(CONTAINER_NAME):latest-$(ARCH) $(CONTAINER_NAME):latest
endif
touch $@
# ensure we have a real imagetag
imagetag:
ifndef IMAGETAG
$(error IMAGETAG is undefined - run using make <target> IMAGETAG=X.Y.Z)
endif
## push one arch
push: imagetag
docker push $(CONTAINER_NAME):$(IMAGETAG)-$(ARCH)
docker push quay.io/$(CONTAINER_NAME):$(IMAGETAG)-$(ARCH)
ifeq ($(ARCH),amd64)
docker push $(CONTAINER_NAME):$(IMAGETAG)
docker push quay.io/$(CONTAINER_NAME):$(IMAGETAG)
endif
push-all: imagetag $(addprefix sub-push-,$(VALIDARCHES))
sub-push-%:
$(MAKE) push ARCH=$* IMAGETAG=$(IMAGETAG)
## tag images of one arch
tag-images: imagetag
docker tag $(CONTAINER_NAME):latest-$(ARCH) $(CONTAINER_NAME):$(IMAGETAG)-$(ARCH)
docker tag $(CONTAINER_NAME):latest-$(ARCH) quay.io/$(CONTAINER_NAME):$(IMAGETAG)-$(ARCH)
ifeq ($(ARCH),amd64)
docker tag $(CONTAINER_NAME):latest-$(ARCH) $(CONTAINER_NAME):$(IMAGETAG)
docker tag $(CONTAINER_NAME):latest-$(ARCH) quay.io/$(CONTAINER_NAME):$(IMAGETAG)
endif
## tag images of all archs
tag-images-all: imagetag $(addprefix sub-tag-images-,$(VALIDARCHES))
sub-tag-images-%:
$(MAKE) tag-images ARCH=$* IMAGETAG=$(IMAGETAG)
###############################################################################
# Static checks
###############################################################################
.PHONY: static-checks
## Perform static checks on the code.
static-checks: vendor check-copyright
docker run --rm \
-e LOCAL_USER_ID=$(LOCAL_USER_ID) \
-v $(CURDIR):/go/src/$(PACKAGE_NAME) \
-w /go/src/$(PACKAGE_NAME) \
$(CALICO_BUILD) gometalinter --deadline=300s --disable-all --enable=goimports --enable=vet --enable=errcheck --vendor -s test_utils ./...
.PHONY: fix
## Fix static checks
fix goimports:
goimports -l -w ./pkg
goimports -l -w ./cmd/kube-controllers/main.go
goimports -l -w ./cmd/check-status/main.go
.PHONY: install-git-hooks
## Install Git hooks
install-git-hooks:
./install-git-hooks
# Make sure that a copyright statement exists on all go files.
check-copyright:
./check-copyrights.sh
###############################################################################
# Tests
###############################################################################
## Run the unit tests in a container.
ut: vendor
-mkdir -p .go-pkg-cache
docker run --rm --privileged --net=host \
-e LOCAL_USER_ID=$(LOCAL_USER_ID) \
-v $(CURDIR)/.go-pkg-cache:/go/pkg/:rw \
-v $(CURDIR):/go/src/$(PACKAGE_NAME):rw \
-w /go/src/$(PACKAGE_NAME) \
$(CALICO_BUILD) sh -c 'WHAT=$(WHAT) SKIP=$(SKIP) ./run-uts'
.PHONY: fv
## Build and run the FV tests.
GINKGO_FOCUS?=.*
fv: tests/fv/fv.test image
@echo Running Go FVs.
cd tests/fv && ETCD_IMAGE=$(ETCD_IMAGE) HYPERKUBE_IMAGE=$(HYPERKUBE_IMAGE) CONTAINER_NAME=$(CONTAINER_NAME):latest-$(ARCH) PRIVATE_KEY=`pwd`/private.key ./fv.test -ginkgo.slowSpecThreshold 30 -ginkgo.focus $(GINKGO_FOCUS)
tests/fv/fv.test: $(shell find ./tests -type f -name '*.go' -print)
# We pre-build the test binary so that we can run it outside a container and allow it
# to interact with docker.
$(DOCKER_GO_BUILD) go test ./tests/fv -c --tags fvtests -o tests/fv/fv.test
###############################################################################
# CI
###############################################################################
.PHONY: ci
ci: clean image-all static-checks ut fv
###############################################################################
# CD
###############################################################################
.PHONY: cd
## Deploys images to registry
cd:
ifndef CONFIRM
$(error CONFIRM is undefined - run using make <target> CONFIRM=true)
endif
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 IMAGETAG=${BRANCH_NAME} EXCLUDEARCH="$(EXCLUDEARCH)"
$(MAKE) tag-images-all push-all IMAGETAG=$(shell git describe --tags --dirty --always --long) EXCLUDEARCH="$(EXCLUDEARCH)"
###############################################################################
# Release
###############################################################################
PREVIOUS_RELEASE=$(shell git describe --tags --abbrev=0)
## Tags and builds a release from start to finish.
release: release-prereqs
$(MAKE) VERSION=$(VERSION) release-tag
$(MAKE) VERSION=$(VERSION) release-build
$(MAKE) VERSION=$(VERSION) release-verify
@echo ""
@echo "Release build complete. Next, push the produced images."
@echo ""
@echo " make VERSION=$(VERSION) release-publish"
@echo ""
## Produces a git tag for the release.
release-tag: release-prereqs release-notes
git tag $(VERSION) -F release-notes-$(VERSION)
@echo ""
@echo "Now you can build the release:"
@echo ""
@echo " make VERSION=$(VERSION) release-build"
@echo ""
## Produces a clean build of release artifacts at the specified version.
release-build: release-prereqs clean
# Check that the correct code is checked out.
ifneq ($(VERSION), $(GIT_VERSION))
$(error Attempt to build $(VERSION) from $(GIT_VERSION))
endif
$(MAKE) image
$(MAKE) tag-images IMAGETAG=$(VERSION)
# Generate the `latest` images.
$(MAKE) tag-images IMAGETAG=latest
## Verifies the release artifacts produces by `make release-build` are correct.
release-verify: release-prereqs
# Check the reported version is correct for each release artifact.
if ! docker run $(CONTAINER_NAME):$(VERSION) -v | grep '^$(VERSION)$$'; then echo "Reported version:" `docker run $(CONTAINER_NAME):$(VERSION) -v` "\nExpected version: $(VERSION)"; false; else echo "\nVersion check passed\n"; fi
if ! docker run quay.io/$(CONTAINER_NAME):$(VERSION) -v | grep '^$(VERSION)$$'; then echo "Reported version:" `docker run quay.io/$(CONTAINER_NAME):$(VERSION) -v` "\nExpected version: $(VERSION)"; false; else echo "\nVersion check passed\n"; fi
# Run FV tests against the produced image. We only run the subset tagged as release tests.
$(MAKE) CONTAINER_NAME=$(CONTAINER_NAME):$(VERSION) GINKGO_FOCUS="Release" fv
## Generates release notes based on commits in this version.
release-notes: release-prereqs
mkdir -p dist
echo "# Changelog" > release-notes-$(VERSION)
sh -c "git cherry -v $(PREVIOUS_RELEASE) | cut '-d ' -f 2- | sed 's/^/- /' >> release-notes-$(VERSION)"
## Pushes a github release and release artifacts produced by `make release-build`.
release-publish: release-prereqs
# Push the git tag.
git push origin $(VERSION)
# Push images.
$(MAKE) push IMAGETAG=$(VERSION) ARCH=$(ARCH)
@echo "Finalize the GitHub release based on the pushed tag."
@echo ""
@echo " https://$(PACKAGE_NAME)/releases/tag/$(VERSION)"
@echo ""
@echo "If this is the latest stable release, then run the following to push 'latest' images."
@echo ""
@echo " make VERSION=$(VERSION) release-publish-latest"
@echo ""
# WARNING: Only run this target if this release is the latest stable release. Do NOT
# run this target for alpha / beta / release candidate builds, or patches to earlier Calico versions.
## Pushes `latest` release images. WARNING: Only run this for latest stable releases.
release-publish-latest: release-prereqs
# Check latest versions match.
if ! docker run $(CONTAINER_NAME):latest -v | grep '^$(VERSION)$$'; then echo "Reported version:" `docker run $(CONTAINER_NAME):latest -v` "\nExpected version: $(VERSION)"; false; else echo "\nVersion check passed\n"; fi
if ! docker run quay.io/$(CONTAINER_NAME):latest -v | grep '^$(VERSION)$$'; then echo "Reported version:" `docker run quay.io/$(CONTAINER_NAME):latest -v` "\nExpected version: $(VERSION)"; false; else echo "\nVersion check passed\n"; fi
$(MAKE) push IMAGETAG=latest ARCH=$(ARCH)
# release-prereqs checks that the environment is configured properly to create a release.
release-prereqs:
ifndef VERSION
$(error VERSION is undefined - run using make release VERSION=vX.Y.Z)
endif
###############################################################################
# Developer helper scripts (not used by build or test)
###############################################################################
.PHONY: help
## Display this help text.
help: # Some kind of magic from https://gist.github.com/rcmachado/af3db315e31383502660
$(info Available targets)
@awk '/^[a-zA-Z\-\_0-9\/]+:/ { \
nb = sub( /^## /, "", helpMsg ); \
if(nb == 0) { \
helpMsg = $$0; \
nb = sub( /^[^:]*:.* ## /, "", helpMsg ); \
} \
if (nb) \
printf "\033[1;31m%-" width "s\033[0m %s\n", $$1, helpMsg; \
} \
{ helpMsg = $$0 }' \
width=20 \
$(MAKEFILE_LIST)