forked from projectcalico/libcalico-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Makefile
266 lines (223 loc) · 10.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
PACKAGE_NAME=github.com/projectcalico/libcalico-go
GO_BUILD_VER=v0.49
# Used so semaphore can trigger the update pin pipelines in projects that have this project as a dependency.
SEMAPHORE_AUTO_PIN_UPDATE_PROJECT_IDS=$(SEMAPHORE_TYPHA_PROJECT_ID) $(SEMAPHORE_KUBE_CONTROLLERS_PROJECT_ID) \
$(SEMAPHORE_CALICOCTL_PROJECT_ID) $(SEMAPHORE_CNI_PROJECT_ID)
# libcalico-go still relies on vendoring
GOMOD_VENDOR = true
LOCAL_CHECKS = vendor goimports check-gen-files
###############################################################################
# Download and include Makefile.common
# Additions to EXTRA_DOCKER_ARGS need to happen before the include since
# that variable is evaluated when we declare DOCKER_RUN and siblings.
###############################################################################
MAKE_BRANCH?=$(GO_BUILD_VER)
MAKE_REPO?=https://raw.githubusercontent.com/projectcalico/go-build/$(MAKE_BRANCH)
Makefile.common: Makefile.common.$(MAKE_BRANCH)
cp "$<" "$@"
Makefile.common.$(MAKE_BRANCH):
# Clean up any files downloaded from other branches so they don't accumulate.
rm -f Makefile.common.*
curl --fail $(MAKE_REPO)/Makefile.common -o "$@"
include Makefile.common
###############################################################################
BINDIR?=bin
# 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
clean:
rm -rf .go-pkg-cache vendor $(BINDIR) checkouts Makefile.common*
find . -name '*.coverprofile' -type f -delete
###############################################################################
# Building the binary
###############################################################################
# Build the vendor directory.
vendor: mod-download
$(DOCKER_GO_BUILD) go mod 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: gen-crds
rm -rf $(GENERATED_FILES)
$(MAKE) $(GENERATED_FILES)
## Force a rebuild of custom resource definition yamls
gen-crds: vendor bin/controller-gen
rm -rf config
$(DOCKER_RUN) $(CALICO_BUILD) sh -c './bin/controller-gen crd:crdVersions=v1 paths=./lib/apis/... output:crd:dir=config/crd/'
@rm config/crd/_.yaml
patch -s -p0 < ./config.patch
# Used for generating CRD files.
bin/controller-gen:
# Download a version of controller-gen that has been hacked to support additional types (e.g., float).
# We can remove this once we update the Calico v3 APIs to use only types which are supported by the upstream controller-gen
# tooling. Some examples: float, all the types in the numorstring package, etc.
mkdir -p bin
wget -O $@ https://github.com/caseydavenport/controller-tools/releases/download/float-support/controller-gen && chmod +x $@
$(BINDIR)/deepcopy-gen: vendor
$(DOCKER_GO_BUILD) sh -c 'go build -o $@ $(PACKAGE_NAME)/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 "$(PACKAGE_NAME)/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 "$(PACKAGE_NAME)/lib/apis/v3" \
--bounding-dirs "github.com/projectcalico/libcalico-go" \
--output-file-base zz_generated.deepcopy'
###############################################################################
# Static checks
###############################################################################
# TODO: re-enable all linters
LINT_ARGS += --disable gosimple,unused,structcheck,errcheck,deadcode,varcheck,ineffassign,staticcheck,govet
.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
###############################################################################
# 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
$(DOCKER_RUN) --privileged $(CALICO_BUILD) \
sh -c 'cd /go/src/$(PACKAGE_NAME) && ginkgo -r --skipPackage vendor -skip "\[Datastore\]" -focus="$(GINKGO_FOCUS)" $(WHAT)'
.PHONY:fv
## Run functional tests against a real datastore in a container.
fv: vendor run-etcd run-etcd-tls run-kubernetes-master run-coredns
$(DOCKER_RUN) --privileged --dns \
$(shell docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' coredns) \
$(CALICO_BUILD) sh -c 'cd /go/src/$(PACKAGE_NAME) && ginkgo -r --skipPackage vendor -focus "$(GINKGO_FOCUS).*\[Datastore\]|\[Datastore\].*$(GINKGO_FOCUS)" $(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} kube-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} kube-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.
while ! docker run \
--net=host \
--rm \
-v $(CURDIR):/manifests \
gcr.io/google_containers/hyperkube-amd64:${K8S_VERSION} kubectl \
--server=http://127.0.0.1:8080 \
apply -f /manifests/config/crd; do echo "Waiting for CRDs to apply..."; sleep 2; done
# 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} 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} kubectl \
--server=http://localhost:8080 \
apply -f /manifests/test/namespaces.yaml
# Wait until some resources we expect to exist are present.
while ! docker exec st-apiserver kubectl get serviceaccount default; do echo "Waiting for default serviceaccount to be created..."; sleep 2; done
## 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
run-coredns: stop-coredns
docker run \
--detach \
--name coredns \
--rm \
-v $(shell pwd)/test/coredns:/etc/coredns \
-w /etc/coredns \
coredns/coredns:$(COREDNS_VERSION)
stop-coredns:
-docker rm -f coredns
st:
@echo "No STs available"
###############################################################################
# CI
###############################################################################
.PHONY: ci
## Run what CI runs
ci: clean static-checks test