-
Notifications
You must be signed in to change notification settings - Fork 49
/
Makefile
299 lines (256 loc) · 10.7 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
###############################################################################
# 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)
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
GO_BUILD_VER ?= v0.16
# for building, we use the go-build image for the *host* architecture, even if the target is different
# the one for the host should contain all the necessary cross-compilation tools.
# cross-compilation is only supported on amd64.
GO_BUILD_CONTAINER ?= calico/go-build:$(GO_BUILD_VER)-$(BUILDARCH)
# quay.io not following naming convention for amd64 images.
ifeq ($(BUILDARCH),amd64)
ETCD_IMAGE ?= quay.io/coreos/etcd:v3.2.5
else
ETCD_IMAGE ?= quay.io/coreos/etcd:v3.2.5-$(BUILDARCH)
endif
BUSYBOX_IMAGE_VERSION ?= latest
BUSYBOX_IMAGE ?= $(BUILDARCH)/busybox:$(BUSYBOX_IMAGE_VERSION)
DIND_IMAGE_VERSION ?= 18.05.0-dind
DIND_IMAGE ?= $(BUILDARCH)/docker:$(DIND_IMAGE_VERSION)
# Disable make's implicit rules, which are not useful for golang, and slow down the build
# considerably.
.SUFFIXES:
SRC_FILES=$(shell find . -type f -name '*.go')
# These variables can be overridden by setting an environment variable.
LOCAL_IP_ENV?=$(shell ip route get 8.8.8.8 | head -1 | awk '{print $$7}')
# Can choose different docker versions see list here - https://hub.docker.com/_/docker/
HOST_CHECKOUT_DIR?=$(CURDIR)
CONTAINER_NAME?=calico/libnetwork-plugin
PLUGIN_LOCATION?=$(CURDIR)/dist/libnetwork-plugin-$(ARCH)
# To run with non-native docker (e.g. on Windows or OSX) you might need to overide this variable
LOCAL_USER_ID?=$(shell id -u $$USER)
help:
@echo "Makefile for libnetwork-plugin."
@echo
@echo "For any target, set ARCH=<target> to build for a given target."
@echo "For example, to build for arm64:"
@echo
@echo " make image ARCH=arm64"
@echo
@echo "Builds:"
@echo " make build Run the build in a container for the current docker OS and ARCH."
@echo " make build-all Run the build in a container for all archs"
@echo " make image Build the calico/libnetwork-plugin image."
@echo " make image-all Build the calico/libnetwork-plugin image for all archs."
@echo " make all Builds the image and runs tests."
@echo
@echo "Tests:"
@echo " make test-containerized Run tests in a container"
@echo
@echo "Maintenance:"
@echo " make clean Remove binary files."
@echo " make help Display this help text."
@echo "-----------------------------------------"
@echo "ARCH (target): $(ARCH)"
@echo "BUILDARCH (host): $(BUILDARCH)"
@echo "GO_BUILD_CONTAINER: $(GO_BUILD_CONTAINER)"
@echo "BUSYBOX_IMAGE: $(BUSYBOX_IMAGE)"
@echo "DIND_IMAGE: $(DIND_IMAGE)"
@echo "ETCDIMAGE: $(ETCDIMAGE)"
@echo "-----------------------------------------"
default: all
all: image test-containerized
# Use this to populate the vendor directory after checking out the repository.
# To update upstream dependencies, delete the glide.lock file first.
vendor: glide.yaml
# Ensure that the glide cache directory exists.
mkdir -p $(HOME)/.glide
# To build without Docker just run "glide install -strip-vendor"
docker run --rm \
-v $(CURDIR):/go/src/github.com/projectcalico/libnetwork-plugin:rw \
-v $(HOME)/.glide:/home/user/.glide:rw \
-e LOCAL_USER_ID=$(LOCAL_USER_ID) \
$(GO_BUILD_CONTAINER) /bin/sh -c ' \
cd /go/src/github.com/projectcalico/libnetwork-plugin && \
glide install -strip-vendor'
install:
CGO_ENABLED=0 go install github.com/projectcalico/libnetwork-plugin
###############################################################################
# Building the binary
###############################################################################
build: dist/libnetwork-plugin-$(ARCH)
build-all: $(addprefix sub-build-,$(ARCHES))
sub-build-%:
$(MAKE) build ARCH=$*
# Run the build in a container. Useful for CI
dist/libnetwork-plugin-$(ARCH): vendor
-mkdir -p dist
-mkdir -p .go-pkg-cache
docker run --rm \
-v $(CURDIR):/go/src/github.com/projectcalico/libnetwork-plugin:ro \
-v $(CURDIR)/dist:/go/src/github.com/projectcalico/libnetwork-plugin/dist \
-v $(CURDIR)/.go-pkg-cache:/go/pkg/:rw \
-e LOCAL_USER_ID=$(LOCAL_USER_ID) \
-e ARCH=$(ARCH) \
$(GO_BUILD_CONTAINER) sh -c '\
cd /go/src/github.com/projectcalico/libnetwork-plugin && \
make binary'
binary: $(SRC_FILES) vendor
CGO_ENABLED=0 GOARCH=$(ARCH) go build -v -i -o dist/libnetwork-plugin-$(ARCH) -ldflags "-X main.VERSION=$(shell git describe --tags --dirty) -s -w" main.go
###############################################################################
# Building the image
###############################################################################
$(CONTAINER_NAME): image
image: image.created-$(ARCH)
image-all: $(addprefix sub-image-,$(ARCHES))
sub-image-%:
$(MAKE) image ARCH=$*
image.created-$(ARCH): dist/libnetwork-plugin-$(ARCH)
docker build -t $(CONTAINER_NAME):latest-$(ARCH) -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
###############################################################################
# CI/CD
###############################################################################
.PHONY: ci cd
## Builds the code and runs all tests.
ci: image test-containerized
## 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 push IMAGETAG=${BRANCH_NAME}
$(MAKE) tag-images push IMAGETAG=$(shell git describe --tags --dirty --always --long)
###############################################################################
# tag and push images of any tag
###############################################################################
## push all arches
push-all: imagetag $(addprefix sub-push-,$(ARCHES))
sub-push-%:
$(MAKE) push ARCH=$* IMAGETAG=$(IMAGETAG)
## 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
## 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-,$(ARCHES))
sub-tag-images-%:
$(MAKE) tag-images ARCH=$* IMAGETAG=$(IMAGETAG)
# Perform static checks on the code. The golint checks are allowed to fail, the others must pass.
.PHONY: static-checks
static-checks: vendor
docker run --rm \
-e LOCAL_USER_ID=$(LOCAL_USER_ID) \
-v $(CURDIR):/go/src/github.com/projectcalico/libnetwork-plugin \
$(GO_BUILD_CONTAINER) sh -c '\
cd /go/src/github.com/projectcalico/libnetwork-plugin && \
gometalinter --deadline=30s --disable-all --enable=goimports --enable=vet --enable=errcheck --enable=varcheck --enable=unused --enable=dupl $$(glide nv)'
run-etcd:
@-docker rm -f calico-etcd
docker run --detach \
--net=host \
--name calico-etcd $(ETCD_IMAGE) \
etcd \
--advertise-client-urls "http://$(LOCAL_IP_ENV):2379,http://127.0.0.1:2379" \
--listen-client-urls "http://0.0.0.0:2379"
###############################################################################
# Release
###############################################################################
release: clean
ifndef VERSION
$(error VERSION is undefined - run using make release VERSION=vX.Y.Z)
endif
git tag $(VERSION)
$(MAKE) image
$(MAKE) tag-images IMAGETAG=$(VERSION)
# Generate the `latest` images.
$(MAKE) tag-images IMAGETAG=latest
# Check that the version output appears on a line of its own (the -x option to grep).
# Tests that the "git tag" makes it into the binary. Main point is to catch "-dirty" builds
@echo "Checking if the tag made it into the binary"
docker run --rm $(CONTAINER_NAME):$(VERSION) -v | grep -x $(VERSION) || (echo "Reported version:" `docker run --rm $(CONTAINER_NAME):$(VERSION) -v` "\nExpected version: $(VERSION)" && exit 1)
@echo "Now push the tag and images. Then create a release on Github and attach the dist/libnetwork-plugin binary"
@echo "git push origin $(VERSION)"
# Push images.
$(MAKE) push IMAGETAG=$(VERSION) ARCH=$(ARCH)
clean:
rm -rf dist image.created-*
-docker rmi $(CONTAINER_NAME)
run-plugin: run-etcd dist/libnetwork-plugin-$(ARCH)
-docker rm -f dind
docker run -tid -h test --name dind --privileged $(ADDITIONAL_DIND_ARGS) \
-e ETCD_ENDPOINTS=http://$(LOCAL_IP_ENV):2379 \
-p 5375:2375 \
-v $(PLUGIN_LOCATION):/libnetwork-plugin \
$(DIND_IMAGE) --cluster-store=etcd://$(LOCAL_IP_ENV):2379
# View the logs by running 'docker exec dind cat plugin.log'
docker exec -tid --privileged dind sh -c 'sysctl -w net.ipv6.conf.default.disable_ipv6=0'
docker exec -tid --privileged dind sh -c '/libnetwork-plugin 2>>/plugin.log'
# To speak to this docker:
# export DOCKER_HOST=localhost:5375
.PHONY: test
# Run the unit tests.
test:
CGO_ENABLED=0 ginkgo -v tests/*
test-containerized: dist/libnetwork-plugin-$(ARCH)
ifeq ($(BUILDARCH),$(ARCH))
docker run -t --rm --net=host \
-v $(CURDIR):/go/src/github.com/projectcalico/libnetwork-plugin \
-v /var/run/docker.sock:/var/run/docker.sock \
-e PLUGIN_LOCATION=$(CURDIR)/dist/libnetwork-plugin-$(ARCH) \
-e LOCAL_USER_ID=0 \
-e ARCH=$(ARCH) \
-e BUSYBOX_IMAGE=$(BUSYBOX_IMAGE) \
$(GO_BUILD_CONTAINER) sh -c '\
cd /go/src/github.com/projectcalico/libnetwork-plugin && \
make test'
else
@echo Test-containerized is not supported when cross building.
endif