forked from projectcalico/kube-controllers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
289 lines (243 loc) · 11.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
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
###############################################################################
# The build architecture is select by setting the ARCH variable.
# For example: When building on ppc64le you could use ARCH=ppc64le make <....>.
# When ARCH is undefined it defaults to amd64.
ARCH?=amd64
ifeq ($(ARCH),amd64)
ARCHTAG?=
GO_BUILD_VER?=v0.12
endif
ifeq ($(ARCH),ppc64le)
ARCHTAG:=-ppc64le
GO_BUILD_VER?=latest
endif
ifeq ($(ARCH),s390x)
ARCHTAG:=-s390x
GO_BUILD_VER?=latest
endif
HYPERKUBE_IMAGE?=gcr.io/google_containers/hyperkube-$(ARCH):v1.8.0-beta.1
ETCD_IMAGE?=quay.io/coreos/etcd:v3.2.5$(ARCHTAG)
.PHONY: all binary build test clean help image
default: help
# Makefile configuration options
CONTAINER_NAME=calico/kube-controllers$(ARCHTAG)
PACKAGE_NAME?=github.com/projectcalico/kube-controllers
CALICO_BUILD?=calico/go-build$(ARCHTAG):$(GO_BUILD_VER)
LIBCALICOGO_PATH?=none
LOCAL_USER_ID?=$(shell id -u $$USER)
# Determine which OS.
OS?=$(shell uname -s | tr A-Z a-z)
# 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/github.com/projectcalico/kube-controllers:rw \
-v $${PWD}/.go-pkg-cache:/go/pkg:rw \
-w /go/src/github.com/projectcalico/kube-controllers \
$(CALICO_BUILD)
###############################################################################
# Build targets
###############################################################################
## Builds the controller binary and docker image.
image: image.created$(ARCHTAG)
image.created$(ARCHTAG): dist/kube-controllers-linux-$(ARCH)
# Build the docker image for the policy controller.
docker build -t $(CONTAINER_NAME) -f Dockerfile$(ARCHTAG) .
touch $@
dist/kube-controllers-linux-$(ARCH):
$(MAKE) OS=linux ARCH=$(ARCH) binary-containerized
# Populates the vendor directory.
.PHONY: vendor
vendor: vendor/.up-to-date
vendor/.up-to-date: glide.yaml
# 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) \
$(CALICO_BUILD) \
/bin/sh -c 'cd /go/src/$(PACKAGE_NAME) && glide install -strip-vendor'
touch vendor/.up-to-date
# Builds the controller binary.
binary: vendor
# Don't try to "install" the intermediate build files (.a .o) when not on linux
# since there are no write permissions for them in our linux build container.
if [ "$(OS)" == "linux" ]; then \
INSTALL_FLAG=" -i "; \
fi; \
GOOS=$(OS) GOARCH=$(ARCH) CGO_ENABLED=0 go build -v $$INSTALL_FLAG -o dist/kube-controllers-$(OS)-$(ARCH) \
-ldflags "-X main.VERSION=$(GIT_VERSION)" ./main.go
## Builds the controller binary in a container.
build: binary-containerized
binary-containerized: vendor
mkdir -p dist
-mkdir -p .go-pkg-cache
docker run --rm \
-e OS=$(OS) -e ARCH=$(ARCH) \
-v $(CURDIR):/go/src/$(PACKAGE_NAME):ro \
-v $(CURDIR)/dist:/go/src/$(PACKAGE_NAME)/dist \
-e LOCAL_USER_ID=$(LOCAL_USER_ID) \
-v $(CURDIR)/.go-pkg-cache:/go/pkg/:rw \
$(CALICO_BUILD) sh -c '\
cd /go/src/$(PACKAGE_NAME) && \
make OS=$(OS) ARCH=$(ARCH) binary'
###############################################################################
# Test targets
###############################################################################
## Builds the code and runs all tests.
ci: clean image check-copyright ut fv
## 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 \
$(CALICO_BUILD) sh -c 'cd /go/src/$(PACKAGE_NAME) && 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) ./fv.test -ginkgo.slowSpecThreshold 30 -ginkgo.focus $(GINKGO_FOCUS)
GET_CONTAINER_IP := docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}'
K8S_VERSION=1.7.5
## Runs system tests.
st: image run-etcd run-k8s-apiserver
./tests/system/apiserver-reconnection.sh $(ARCHTAG)
$(MAKE) stop-k8s-apiserver stop-etcd
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
.PHONY: run-k8s-apiserver stop-k8s-apiserver run-etcd stop-etcd
run-k8s-apiserver: stop-k8s-apiserver
ETCD_IP=`$(GET_CONTAINER_IP) st-etcd` && \
docker run --detach \
--name st-apiserver \
gcr.io/google_containers/hyperkube-$(ARCH):v$(K8S_VERSION) \
/hyperkube apiserver --etcd-servers=http://$${ETCD_IP}:2379 \
--service-cluster-ip-range=10.101.0.0/16 -v=10 \
--authorization-mode=RBAC
stop-k8s-apiserver:
@-docker rm -f st-apiserver
run-etcd: stop-etcd
docker run --detach \
--name st-etcd $(ETCD_IMAGE) \
etcd \
--advertise-client-urls "http://127.0.0.1:2379,http://127.0.0.1:4001" \
--listen-client-urls "http://0.0.0.0:2379,http://0.0.0.0:4001"
stop-etcd:
@-docker rm -f st-etcd
# Make sure that a copyright statement exists on all go files.
check-copyright:
./check-copyrights.sh
###############################################################################
# Release targets
###############################################################################
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
docker tag $(CONTAINER_NAME) $(CONTAINER_NAME):$(VERSION)
docker tag $(CONTAINER_NAME) quay.io/$(CONTAINER_NAME):$(VERSION)
# Generate the `latest` images.
docker tag $(CONTAINER_NAME) quay.io/$(CONTAINER_NAME):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 calico/kube-controllers:$(VERSION) -v | grep '^$(VERSION)$$'; then echo "Reported version:" `docker run calico/kube-controllers:$(VERSION) -v` "\nExpected version: $(VERSION)"; false; else echo "\nVersion check passed\n"; fi
if ! docker run quay.io/calico/kube-controllers:$(VERSION) -v | grep '^$(VERSION)$$'; then echo "Reported version:" `docker run quay.io/calico/kube-controllers:$(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=calico/kube-controllers:$(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.
docker push calico/kube-controllers:$(VERSION)
docker push quay.io/calico/kube-controllers:$(VERSION)
@echo "Finalize the GitHub release based on the pushed tag."
@echo ""
@echo " https://github.com/projectcalico/kube-controllers/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 calico/kube-controllers:latest -v | grep '^$(VERSION)$$'; then echo "Reported version:" `docker run calico/kube-controllers:latest -v` "\nExpected version: $(VERSION)"; false; else echo "\nVersion check passed\n"; fi
if ! docker run quay.io/calico/kube-controllers:latest -v | grep '^$(VERSION)$$'; then echo "Reported version:" `docker run quay.io/calico/kube-controllers:latest -v` "\nExpected version: $(VERSION)"; false; else echo "\nVersion check passed\n"; fi
docker push calico/kube-controllers:latest
docker push quay.io/calico/kube-controllers:latest
# 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
## Removes all build artifacts.
clean:
rm -rf dist image.created$(ARCHTAG)
-docker rmi $(CONTAINER_NAME)
rm -f st-kubeconfig.yaml
rm -f tests/fv/fv.test
###############################################################################
# Utilities
###############################################################################
.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)
goimports:
goimports -l -w ./pkg
goimports -l -w ./main.go