This repository has been archived by the owner on Feb 19, 2021. It is now read-only.
forked from projectcalico/libcalico-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
301 lines (262 loc) · 11.3 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
# Shortcut targets
default: test
## Build binary
all: test
## Run the tests
test: ut fv
# Define some constants
#######################
K8S_VERSION ?= v1.11.3
ETCD_VERSION ?= v3.3.7
GO_BUILD_VER ?= v0.20
CALICO_BUILD ?= calico/go-build:$(GO_BUILD_VER)
PACKAGE_NAME ?= projectcalico/libcalico-go
LOCAL_USER_ID ?= $(shell id -u $$USER)
BINDIR ?= bin
LIBCALICO-GO_PKG = github.com/projectcalico/libcalico-go
TOP_SRC_DIR = lib
MY_UID := $(shell id -u)
DOCKER_GO_BUILD := mkdir -p .go-pkg-cache && \
docker run --rm \
--net=host \
$(EXTRA_DOCKER_ARGS) \
-e LOCAL_USER_ID=$(LOCAL_USER_ID) \
-v $(HOME)/.glide:/home/user/.glide:rw \
-v $(CURDIR):/go/src/github.com/$(PACKAGE_NAME):rw \
-v $(CURDIR)/.go-pkg-cache:/go/pkg:rw \
-w /go/src/github.com/$(PACKAGE_NAME) \
$(CALICO_BUILD)
# Create a list of files upon which the generated file depends, skip the generated file itself
UPGRADE_SRCS := $(filter-out ./lib/upgrade/migrator/clients/v1/k8s/custom/zz_generated.deepcopy.go, \
$(wildcard ./lib/upgrade/migrator/clients/v1/k8s/custom/*.go))
# Create a list of files upon which the generated file depends, skip the generated file itself
APIS_SRCS := $(filter-out ./lib/apis/v3/zz_generated.deepcopy.go, $(wildcard ./lib/apis/v3/*.go))
# The path, inside libcalico-go, to the cert files required for etcdv3 fv test
TEST_CERT_PATH := test/etcd-ut-certs/
.PHONY: clean
## Removes all .coverprofile files, the vendor dir, and .go-pkg-cache
clean:
find . -name '*.coverprofile' -type f -delete
rm -rf vendor .go-pkg-cache
rm -rf $(BINDIR)
rm -rf checkouts
###############################################################################
# Building the binary
###############################################################################
# Build the vendor directory.
vendor: glide.lock
# To build without Docker just run "glide install -strip-vendor"
# Ensure that the glide cache directory exists.
mkdir -p $(HOME)/.glide
$(DOCKER_GO_BUILD) glide install --strip-vendor
GENERATED_FILES:=./lib/apis/v3/zz_generated.deepcopy.go \
./lib/upgrade/migrator/clients/v1/k8s/custom/zz_generated.deepcopy.go
.PHONY: gen-files
## Force rebuild generated go utilities (e.g. deepcopy-gen) and generated files
gen-files:
rm -rf $(GENERATED_FILES)
$(MAKE) $(GENERATED_FILES)
$(BINDIR)/deepcopy-gen: vendor
$(DOCKER_GO_BUILD) \
sh -c 'go build -o $@ $(LIBCALICO-GO_PKG)/vendor/k8s.io/code-generator/cmd/deepcopy-gen'
./lib/upgrade/migrator/clients/v1/k8s/custom/zz_generated.deepcopy.go: $(UPGRADE_SRCS) $(BINDIR)/deepcopy-gen
$(DOCKER_GO_BUILD) \
sh -c '$(BINDIR)/deepcopy-gen \
--v 1 --logtostderr \
--go-header-file "./docs/boilerplate.go.txt" \
--input-dirs "$(LIBCALICO-GO_PKG)/lib/upgrade/migrator/clients/v1/k8s/custom" \
--bounding-dirs "github.com/projectcalico/libcalico-go" \
--output-file-base zz_generated.deepcopy'
./lib/apis/v3/zz_generated.deepcopy.go: $(APIS_SRCS) $(BINDIR)/deepcopy-gen
$(DOCKER_GO_BUILD) \
sh -c '$(BINDIR)/deepcopy-gen \
--v 1 --logtostderr \
--go-header-file "./docs/boilerplate.go.txt" \
--input-dirs "$(LIBCALICO-GO_PKG)/lib/apis/v3" \
--bounding-dirs "github.com/projectcalico/libcalico-go" \
--output-file-base zz_generated.deepcopy'
###############################################################################
# Static checks
###############################################################################
.PHONY: static-checks
static-checks: check-format check-gen-files
.PHONY: check-gen-files
check-gen-files: $(GENERATED_FILES)
git diff --exit-code -- $(GENERATED_FILES) || (echo "The generated targets changed, please 'make gen-files' and commit the results"; exit 1)
.PHONY: check-format
# Depends on the vendor directory because goimports needs to be able to resolve the imports.
check-format: vendor
@if $(DOCKER_GO_BUILD) goimports -l lib | grep -v zz_generated | grep .; then \
echo "Some files in ./lib are not goimported"; \
false; \
else \
echo "All files in ./lib are goimported"; \
fi
.PHONY: goimports go-fmt format-code
# Format the code using goimports. Depends on the vendor directory because goimports needs
# to be able to resolve the imports.
goimports go-fmt format-code fix: vendor
$(DOCKER_GO_BUILD) goimports -w lib
.PHONY: install-git-hooks
## Install Git hooks
install-git-hooks:
./install-git-hooks
## Check if glide up creates any warnings. Skip if there are any local changes to the glide files.
check-glide-warnings:
@mkdir -p ~/.glide
@if ! git status glide.lock glide.yaml --porcelain | grep "."; then \
$(DOCKER_GO_BUILD) sh -c 'glide up --strip-vendor 2>&1' | grep '\[WARN\]'; RESULT=$$?; \
git checkout -- glide.yaml glide.lock; \
if [ $$RESULT -eq 1 ]; then true; else false; fi; \
else \
echo "Skipping glide checks as there are local updates"; \
fi
# That can leave a present but empty vendor directory, which
# confuses the rest of the Makefile something rotten...
-rm -rf vendor
###############################################################################
# Tests
###############################################################################
.PHONY: ut-cover
## Run the UTs natively with code coverage. This requires a local etcd and local kubernetes master to be running.
ut-cover: vendor
./run-uts
WHAT?=.
GINKGO_FOCUS?=.*
.PHONY:ut
## Run the fast set of unit tests in a container.
ut: vendor
-mkdir -p .go-pkg-cache
docker run --rm -t --privileged --net=host \
-e LOCAL_USER_ID=$(LOCAL_USER_ID) \
-v $(CURDIR):/go/src/github.com/$(PACKAGE_NAME):rw \
-v $(CURDIR)/.go-pkg-cache:/go-cache/:rw \
-e GOCACHE=/go-cache \
$(CALICO_BUILD) sh -c 'cd /go/src/github.com/$(PACKAGE_NAME) && ginkgo -r --skipPackage vendor -skip "\[Datastore\]" -focus="$(GINKGO_FOCUS)" $(GINKGO_ARGS) $(WHAT)'
.PHONY:fv
## Run functional tests against a real datastore in a container.
fv: vendor run-etcd run-etcd-tls run-kubernetes-master
-mkdir -p .go-pkg-cache
docker run --rm -t --privileged --net=host \
-e LOCAL_USER_ID=$(LOCAL_USER_ID) \
-v $(CURDIR):/go/src/github.com/$(PACKAGE_NAME):rw \
-v $(CURDIR)/.go-pkg-cache:/go-cache/:rw \
-e GOCACHE=/go-cache \
$(CALICO_BUILD) sh -c 'cd /go/src/github.com/$(PACKAGE_NAME) && ginkgo -r --skipPackage vendor -focus "$(GINKGO_FOCUS).*\[Datastore\]|\[Datastore\].*$(GINKGO_FOCUS)" $(GINKGO_ARGS) $(WHAT)'
$(MAKE) stop-etcd-tls
## Run etcd, with tls enabled, as a container (calico-etcd-tls)
run-etcd-tls: stop-etcd-tls
docker run --detach \
-v `pwd`/$(TEST_CERT_PATH):/root/etcd-certificates/ \
--net=host \
--entrypoint=/usr/local/bin/etcd \
--name calico-etcd-tls quay.io/coreos/etcd:$(ETCD_VERSION) \
--listen-peer-urls https://127.0.0.1:5008 \
--peer-cert-file=/root/etcd-certificates/server.crt \
--peer-key-file=/root/etcd-certificates/server.key \
--advertise-client-urls https://127.0.0.1:5007 \
--listen-client-urls https://0.0.0.0:5007 \
--trusted-ca-file=/root/etcd-certificates/ca.crt \
--cert-file=/root/etcd-certificates/server.crt \
--key-file=/root/etcd-certificates/server.key \
--client-cert-auth=true \
--data-dir=/var/lib/etcd
## Stop etcd with name calico-etcd-tls
stop-etcd-tls:
-docker rm -f calico-etcd-tls
## Run etcd as a container (calico-etcd)
run-etcd: stop-etcd
docker run --detach \
--net=host \
--entrypoint=/usr/local/bin/etcd \
--name calico-etcd quay.io/coreos/etcd:$(ETCD_VERSION) \
--advertise-client-urls "http://$(LOCAL_IP_ENV):2379,http://127.0.0.1:2379,http://$(LOCAL_IP_ENV):4001,http://127.0.0.1:4001" \
--listen-client-urls "http://0.0.0.0:2379,http://0.0.0.0:4001"
## Run a local kubernetes master with API via hyperkube
run-kubernetes-master: stop-kubernetes-master
# Run a Kubernetes apiserver using Docker.
docker run \
--net=host --name st-apiserver \
--detach \
gcr.io/google_containers/hyperkube-amd64:${K8S_VERSION} \
/hyperkube apiserver \
--bind-address=0.0.0.0 \
--insecure-bind-address=0.0.0.0 \
--etcd-servers=http://127.0.0.1:2379 \
--admission-control=NamespaceLifecycle,LimitRanger,DefaultStorageClass,ResourceQuota \
--service-cluster-ip-range=10.101.0.0/16 \
--v=10 \
--logtostderr=true
# Wait until the apiserver is accepting requests.
while ! docker exec st-apiserver kubectl get nodes; do echo "Waiting for apiserver to come up..."; sleep 2; done
# And run the controller manager.
docker run \
--net=host --name st-controller-manager \
--detach \
gcr.io/google_containers/hyperkube-amd64:${K8S_VERSION} \
/hyperkube controller-manager \
--master=127.0.0.1:8080 \
--min-resync-period=3m \
--allocate-node-cidrs=true \
--cluster-cidr=10.10.0.0/16 \
--v=5
# Create CustomResourceDefinition (CRD) for Calico resources
# from the manifest crds.yaml
docker run \
--net=host \
--rm \
-v $(CURDIR):/manifests \
gcr.io/google_containers/hyperkube-amd64:${K8S_VERSION} \
/hyperkube kubectl \
--server=http://127.0.0.1:8080 \
apply -f /manifests/test/crds.yaml
# Create a Node in the API for the tests to use.
docker run \
--net=host \
--rm \
-v $(CURDIR):/manifests \
gcr.io/google_containers/hyperkube-amd64:${K8S_VERSION} \
/hyperkube kubectl \
--server=http://127.0.0.1:8080 \
apply -f /manifests/test/mock-node.yaml
# Create Namespaces required by namespaced Calico `NetworkPolicy`
# tests from the manifests namespaces.yaml.
docker run \
--net=host \
--rm \
-v $(CURDIR):/manifests \
gcr.io/google_containers/hyperkube-amd64:${K8S_VERSION} \
/hyperkube kubectl \
--server=http://localhost:8080 \
apply -f /manifests/test/namespaces.yaml
## Stop the local kubernetes master
stop-kubernetes-master:
# Delete the cluster role binding.
-docker exec st-apiserver kubectl delete clusterrolebinding anonymous-admin
# Stop master components.
-docker rm -f st-apiserver st-controller-manager
## Stop the etcd container (calico-etcd)
stop-etcd:
-docker rm -f calico-etcd
###############################################################################
# CI
###############################################################################
.PHONY: ci
## Run what CI runs
ci: clean check-glide-warnings static-checks 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=23 \
$(MAKEFILE_LIST)