This repository has been archived by the owner on Jan 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Makefile
212 lines (184 loc) · 7.09 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
# Shortcut targets
default: build
## Build binary for current platform
all: build
## Run the tests for the current platform/architecture
test: ut test-install-cni
###############################################################################
# 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
###############################################################################
GO_BUILD_VER ?= v0.16
CALICO_BUILD?=calico/go-build:$(GO_BUILD_VER)
SRC_FILES=$(shell find . -type f -name '*.go' -not -path "./vendor/*" -not -path "./gobgp/*")
GOBGPD_VERSION?=$(shell git describe --tags --dirty)
CONTAINER_NAME?=calico/gobgpd
PACKAGE_NAME?=github.com/projectcalico/calico-bgp-daemon
LOCAL_USER_ID?=$(shell id -u $$USER)
BIN=bin/$(ARCH)
clean:
rm -rf vendor
rm -rf gobgp
rm -rf $(BIN)
###############################################################################
# Building the binary
###############################################################################
build: $(BIN)/calico-bgp-daemon
build-all: $(addprefix sub-build-,$(ARCHES))
sub-build-%:
$(MAKE) build ARCH=$*
vendor: glide.lock
mkdir -p $(HOME)/.glide
# To build without Docker just run "glide install -strip-vendor"
docker run --rm \
-v $(CURDIR):/go/src/$(PACKAGE_NAME):rw \
-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'
# instead of 'go get', run `git clone` and then `dep ensure && go build` so the files are cached
gobgp:
git clone -b v1.33 https://github.com/osrg/gobgp gobgp
gobgp/vendor: gobgp
docker run --rm \
-v $(CURDIR)/gobgp:/go/src/github.com/osrg/gobgp \
-w /go/src/github.com/osrg/gobgp \
-e LOCAL_USER_ID=$(LOCAL_USER_ID) \
-e ARCH=$(ARCH) \
-e GOARCH=$(ARCH) \
$(CALICO_BUILD) dep ensure
$(BIN)/gobgp: gobgp gobgp/vendor
mkdir -p $(@D)
docker run --rm \
-v $(CURDIR)/gobgp:/go/src/github.com/osrg/gobgp \
-v $(CURDIR)/$(BIN):/outbin \
-w /go/src/github.com/osrg/gobgp \
-e LOCAL_USER_ID=$(LOCAL_USER_ID) \
-e ARCH=$(ARCH) \
-e GOARCH=$(ARCH) \
$(CALICO_BUILD) go build -v -o /outbin/gobgp github.com/osrg/gobgp/gobgp
$(BIN)/calico-bgp-daemon: $(SRC_FILES) vendor $(BIN)/gobgp
-mkdir -p $(@D)
-mkdir -p .go-pkg-cache
docker run --rm \
-v $(CURDIR):/go/src/$(PACKAGE_NAME) \
-e LOCAL_USER_ID=$(LOCAL_USER_ID) \
-v $(CURDIR)/.go-pkg-cache:/go-cache/:rw \
-e GOCACHE=/go-cache \
-e GOARCH=$(ARCH) \
$(CALICO_BUILD) sh -c '\
cd /go/src/$(PACKAGE_NAME) && \
go build -v -o $(BIN)/calico-bgp-daemon \
-ldflags "-X main.VERSION=$(GOBGPD_VERSION) -s -w" main.go ipam.go k8s.go'
###############################################################################
# Building the image
###############################################################################
image-all: $(addprefix sub-image-,$(ARCHES))
sub-image-%:
$(MAKE) image ARCH=$*
image: $(CONTAINER_NAME)
$(CONTAINER_NAME): build
docker build -t $(CONTAINER_NAME):latest-$(ARCH) -f Dockerfile.$(ARCH) .
# 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-,$(ARCHES))
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-,$(ARCHES))
sub-tag-images-%:
$(MAKE) tag-images ARCH=$* IMAGETAG=$(IMAGETAG)
###############################################################################
# Static checks
###############################################################################
.PHONY: static-checks
## Perform static checks on the code.
static-checks: vendor
docker run --rm \
-e LOCAL_USER_ID=$(LOCAL_USER_ID) \
-v $(CURDIR):/go/src/$(PACKAGE_NAME) \
$(CALICO_BUILD) sh -c '\
cd /go/src/$(PACKAGE_NAME) && \
gometalinter --deadline=300s --disable-all --enable=goimports --vendor -s gobgp ./...'
.PHONY: fix
## Fix static checks
fix:
goimports -w $(SRC_FILES)
###############################################################################
# CI
###############################################################################
.PHONY: ci
## Run what CI runs
ci: static-checks
## Deploys images to registry
cd: image
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)
###############################################################################
# Release
###############################################################################
release: clean
ifndef VERSION
$(error VERSION is undefined - run using make release VERSION=vX.Y.Z)
endif
git tag $(VERSION)
$(MAKE) $(CONTAINER_NAME)
# 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):latest-$(ARCH) -v | grep -x $(VERSION) || (echo "Reported version:" `docker run --rm $(CONTAINER_NAME):latest-$(ARCH) -v` "\nExpected version: $(VERSION)" && exit 1)
$(MAKE) tag IMAGETAG=$(VERSION) ARCH=$(ARCH)
$(MAKE) tag IMAGETAG=latest ARCH=$(ARCH)
$(MAKE) push IMAGETAG=$(VERSION) ARCH=$(ARCH)
$(MAKE) push IMAGETAG=latest ARCH=$(ARCH)
@echo "Now create a release on Github and attach the $(BIN)/gobgpd and $(BIN)/gobgp binaries"
@echo "git push origin $(VERSION)"