|
| 1 | +# Shortcut targets |
| 2 | +default: build |
| 3 | + |
| 4 | +## Build binary for current platform |
| 5 | +all: build |
| 6 | + |
| 7 | +## Run the tests for the current platform/architecture |
| 8 | +test: ut |
| 9 | + |
| 10 | +############################################################################### |
| 11 | +# Both native and cross architecture builds are supported. |
| 12 | +# The target architecture is select by setting the ARCH variable. |
| 13 | +# When ARCH is undefined it is set to the detected host architecture. |
| 14 | +# When ARCH differs from the host architecture a crossbuild will be performed. |
| 15 | +ARCHES=$(patsubst flexvol/docker/Dockerfile.%,%,$(wildcard flexvol/docker/Dockerfile.*)) |
| 16 | + |
| 17 | +# BUILDARCH is the host architecture |
| 18 | +# ARCH is the target architecture |
| 19 | +# we need to keep track of them separately |
| 20 | +BUILDARCH ?= $(shell uname -m) |
| 21 | + |
| 22 | +# canonicalized names for host architecture |
| 23 | +ifeq ($(BUILDARCH),aarch64) |
| 24 | + BUILDARCH=arm64 |
| 25 | +endif |
| 26 | +ifeq ($(BUILDARCH),x86_64) |
| 27 | + BUILDARCH=amd64 |
| 28 | +endif |
| 29 | + |
| 30 | +# unless otherwise set, I am building for my own architecture, i.e. not cross-compiling |
| 31 | +ARCH ?= $(BUILDARCH) |
| 32 | + |
| 33 | +# canonicalized names for target architecture |
| 34 | +ifeq ($(ARCH),aarch64) |
| 35 | + override ARCH=arm64 |
| 36 | +endif |
| 37 | +ifeq ($(ARCH),x86_64) |
| 38 | + override ARCH=amd64 |
| 39 | +endif |
| 40 | + |
| 41 | + |
| 42 | +# list of arches *not* to build when doing *-all |
| 43 | +# until s390x works correctly |
| 44 | +EXCLUDEARCH ?= s390x |
| 45 | +VALIDARCHES = $(filter-out $(EXCLUDEARCH),$(ARCHES)) |
| 46 | + |
| 47 | + |
| 48 | +############################################################################### |
| 49 | +GO_BUILD_VER?=v0.17 |
| 50 | +GO_BUILD_CONTAINER?=calico/go-build:$(GO_BUILD_VER) |
| 51 | +PROTOC_VER?=v0.1 |
| 52 | +PROTOC_CONTAINER?=calico/protoc:$(PROTOC_VER)-$(BUILDARCH) |
| 53 | + |
| 54 | +# Figure out the users UID/GID. These are needed to run docker containers |
| 55 | +# as the current user and ensure that files built inside containers are |
| 56 | +# owned by the current user. |
| 57 | +LOCAL_USER_ID:=$(shell id -u) |
| 58 | +MY_GID:=$(shell id -g) |
| 59 | + |
| 60 | +PACKAGE_NAME?=github.com/projectcalico/pod2daemon |
| 61 | +SRC_FILES=$(shell find -name '*.go' |grep -v vendor) |
| 62 | +CONTAINER_NAME?=calico/pod2daemon-flexvol |
| 63 | + |
| 64 | +# Pre-configured docker run command that runs as this user with the repo |
| 65 | +# checked out to /code, uses the --rm flag to avoid leaving the container |
| 66 | +# around afterwards. |
| 67 | +DOCKER_RUN_RM:=docker run --rm --user $(MY_UID):$(MY_GID) -v ${CURDIR}:/code |
| 68 | + |
| 69 | +.PHONY: clean |
| 70 | +## Clean enough that a new release build will be clean |
| 71 | +clean: |
| 72 | + find . -name '*.created-$(ARCH)' -exec rm -f {} + |
| 73 | + |
| 74 | + docker rmi $(CONTAINER_NAME):latest-$(ARCH) || true |
| 75 | + docker rmi $(CONTAINER_NAME):$(VERSION)-$(ARCH) || true |
| 76 | +ifeq ($(ARCH),amd64) |
| 77 | + docker rmi $(CONTAINER_NAME):latest || true |
| 78 | + docker rmi $(CONTAINER_NAME):$(VERSION) || true |
| 79 | +endif |
| 80 | +############################################################################### |
| 81 | +# Building the binary |
| 82 | +############################################################################### |
| 83 | +.PHONY: build-all |
| 84 | +## Build the binaries for all architectures and platforms |
| 85 | +build-all: $(addprefix bin/flexvol-,$(VALIDARCHES)) |
| 86 | + |
| 87 | +.PHONY: build |
| 88 | +## Build the binary for the current architecture and platform |
| 89 | +build: bin/flexvol-$(ARCH) |
| 90 | + |
| 91 | +## Create the vendor directory |
| 92 | +vendor: Gopkg.toml |
| 93 | + $(DOCKER_GO_BUILD) dep ensure |
| 94 | + |
| 95 | +bin/flexvol-amd64: ARCH=amd64 |
| 96 | +bin/flexvol-arm64: ARCH=arm64 |
| 97 | +bin/flexvol-ppc64le: ARCH=ppc64le |
| 98 | +bin/flexvol-s390x: ARCH=s390x |
| 99 | +bin/flexvol-%: vendor $(SRC_FILES) |
| 100 | + mkdir -p bin |
| 101 | + -mkdir -p .go-pkg-cache |
| 102 | + docker run --rm -ti \ |
| 103 | + -v $(CURDIR):/go/src/$(PACKAGE_NAME):ro \ |
| 104 | + -v $(CURDIR)/bin:/go/src/$(PACKAGE_NAME)/bin \ |
| 105 | + -e LOCAL_USER_ID=$(LOCAL_USER_ID) \ |
| 106 | + -v $(CURDIR)/.go-pkg-cache:/go-cache/:rw \ |
| 107 | + -e GOCACHE=/go-cache \ |
| 108 | + -w /go/src/$(PACKAGE_NAME) \ |
| 109 | + $(GO_BUILD_CONTAINER) go build -v -o bin/flexvol-$(ARCH) flexvol/flexvoldriver.go |
| 110 | + |
| 111 | +############################################################################### |
| 112 | +# Building the image |
| 113 | +############################################################################### |
| 114 | +CONTAINER_CREATED=.pod2daemon-flexvol.created-$(ARCH) |
| 115 | +.PHONY: image calico/pod2daemon-flexvol |
| 116 | +image: $(CONTAINER_NAME) |
| 117 | +image-all: $(addprefix sub-image-,$(VALIDARCHES)) |
| 118 | +sub-image-%: |
| 119 | + $(MAKE) image ARCH=$* |
| 120 | + |
| 121 | +$(CONTAINER_NAME): $(CONTAINER_CREATED) |
| 122 | +$(CONTAINER_CREATED): Dockerfile.$(ARCH) bin/flexvol-$(ARCH) |
| 123 | + docker build -t $(CONTAINER_NAME):latest-$(ARCH) --build-arg QEMU_IMAGE=$(CALICO_BUILD) -f Dockerfile.$(ARCH) . |
| 124 | +ifeq ($(ARCH),amd64) |
| 125 | + docker tag $(CONTAINER_NAME):latest-$(ARCH) $(CONTAINER_NAME):latest |
| 126 | +endif |
| 127 | + touch $@ |
| 128 | + |
| 129 | +# ensure we have a real imagetag |
| 130 | +imagetag: |
| 131 | +ifndef IMAGETAG |
| 132 | + $(error IMAGETAG is undefined - run using make <target> IMAGETAG=X.Y.Z) |
| 133 | +endif |
| 134 | + |
| 135 | +## push one arch |
| 136 | +push: imagetag |
| 137 | + docker push $(CONTAINER_NAME):$(IMAGETAG)-$(ARCH) |
| 138 | + docker push quay.io/$(CONTAINER_NAME):$(IMAGETAG)-$(ARCH) |
| 139 | +ifeq ($(ARCH),amd64) |
| 140 | + docker push $(CONTAINER_NAME):$(IMAGETAG) |
| 141 | + docker push quay.io/$(CONTAINER_NAME):$(IMAGETAG) |
| 142 | +endif |
| 143 | + |
| 144 | +push-all: imagetag $(addprefix sub-push-,$(VALIDARCHES)) |
| 145 | +sub-push-%: |
| 146 | + $(MAKE) push ARCH=$* IMAGETAG=$(IMAGETAG) |
| 147 | + |
| 148 | +## tag images of one arch |
| 149 | +tag-images: imagetag |
| 150 | + docker tag $(CONTAINER_NAME):latest-$(ARCH) $(CONTAINER_NAME):$(IMAGETAG)-$(ARCH) |
| 151 | + docker tag $(CONTAINER_NAME):latest-$(ARCH) quay.io/$(CONTAINER_NAME):$(IMAGETAG)-$(ARCH) |
| 152 | +ifeq ($(ARCH),amd64) |
| 153 | + docker tag $(CONTAINER_NAME):latest-$(ARCH) $(CONTAINER_NAME):$(IMAGETAG) |
| 154 | + docker tag $(CONTAINER_NAME):latest-$(ARCH) quay.io/$(CONTAINER_NAME):$(IMAGETAG) |
| 155 | +endif |
| 156 | + |
| 157 | +## tag images of all archs |
| 158 | +tag-images-all: imagetag $(addprefix sub-tag-images-,$(VALIDARCHES)) |
| 159 | +sub-tag-images-%: |
| 160 | + $(MAKE) tag-images ARCH=$* IMAGETAG=$(IMAGETAG) |
| 161 | + |
| 162 | +############################################################################### |
| 163 | +# Static checks |
| 164 | +############################################################################### |
| 165 | +## Perform static checks on the code. |
| 166 | +.PHONY: static-checks |
| 167 | +static-checks: vendor |
| 168 | + docker run --rm \ |
| 169 | + -e LOCAL_USER_ID=$(LOCAL_USER_ID) \ |
| 170 | + -v $(CURDIR):/go/src/$(PACKAGE_NAME) \ |
| 171 | + -w /go/src/$(PACKAGE_NAME) \ |
| 172 | + $(GO_BUILD_CONTAINER) gometalinter --deadline=300s --disable-all --enable=goimports --vendor ./... |
| 173 | + |
| 174 | +.PHONY: fix |
| 175 | +## Fix static checks |
| 176 | +fix: |
| 177 | + goimports -w $(SRC_FILES) |
| 178 | + |
| 179 | +############################################################################### |
| 180 | +# UTs |
| 181 | +############################################################################### |
| 182 | +.PHONY: ut |
| 183 | +## Run the tests in a container. Useful for CI, Mac dev |
| 184 | +ut: $(SRC_FILES) |
| 185 | + docker run --rm -v $(CURDIR):/go/src/$(PACKAGE_NAME):rw \ |
| 186 | + -e LOCAL_USER_ID=$(LOCAL_USER_ID) \ |
| 187 | + -w /go/src/$(PACKAGE_NAME) \ |
| 188 | + $(GO_BUILD_CONTAINER) go test -v ./... |
| 189 | + |
| 190 | +############################################################################### |
| 191 | +# CI |
| 192 | +############################################################################### |
| 193 | +.PHONY: ci |
| 194 | +## Run what CI runs |
| 195 | +ci: clean build-all static-checks ut |
| 196 | + |
| 197 | +############################################################################### |
| 198 | +# CD |
| 199 | +############################################################################### |
| 200 | +.PHONY: cd |
| 201 | +## Deploys images to registry |
| 202 | +cd: image-all |
| 203 | +ifndef CONFIRM |
| 204 | + $(error CONFIRM is undefined - run using make <target> CONFIRM=true) |
| 205 | +endif |
| 206 | +ifndef BRANCH_NAME |
| 207 | + $(error BRANCH_NAME is undefined - run using make <target> BRANCH_NAME=var or set an environment variable) |
| 208 | +endif |
| 209 | + $(MAKE) tag-images-all push-all IMAGETAG=${BRANCH_NAME} EXCLUDEARCH="$(EXCLUDEARCH)" |
| 210 | + $(MAKE) tag-images-all push-all IMAGETAG=$(shell git describe --tags --dirty --always --long) EXCLUDEARCH="$(EXCLUDEARCH)" |
| 211 | + |
| 212 | +############################################################################### |
| 213 | +# Release |
| 214 | +############################################################################### |
| 215 | +PREVIOUS_RELEASE=$(shell git describe --tags --abbrev=0) |
| 216 | + |
| 217 | +## Tags and builds a release from start to finish. |
| 218 | +release: release-prereqs |
| 219 | + $(MAKE) VERSION=$(VERSION) release-tag |
| 220 | + $(MAKE) VERSION=$(VERSION) release-build |
| 221 | + $(MAKE) VERSION=$(VERSION) release-verify |
| 222 | + |
| 223 | + @echo "" |
| 224 | + @echo "Release build complete. Next, push the produced images." |
| 225 | + @echo "" |
| 226 | + @echo " make VERSION=$(VERSION) release-publish" |
| 227 | + @echo "" |
| 228 | + |
| 229 | +## Produces a git tag for the release. |
| 230 | +release-tag: release-prereqs release-notes |
| 231 | + git tag $(VERSION) -F release-notes-$(VERSION) |
| 232 | + @echo "" |
| 233 | + @echo "Now you can build the release:" |
| 234 | + @echo "" |
| 235 | + @echo " make VERSION=$(VERSION) release-build" |
| 236 | + @echo "" |
| 237 | + |
| 238 | +## Produces a clean build of release artifacts at the specified version. |
| 239 | +release-build: release-prereqs clean |
| 240 | + $(MAKE) image |
| 241 | + $(MAKE) tag-images IMAGETAG=$(VERSION) |
| 242 | + # Generate the `latest` images. |
| 243 | + $(MAKE) tag-images IMAGETAG=latest |
| 244 | + |
| 245 | +## Verifies the release artifacts produces by `make release-build` are correct. |
| 246 | +release-verify: release-prereqs |
| 247 | + # TODO: Check the reported version is correct for each release artifact. Uncomment when binary supports version command. |
| 248 | + # if ! docker run $(CONTAINER_NAME):$(VERSION)-$(ARCH) version | grep 'Version:\s*$(VERSION)$$'; then \ |
| 249 | + # echo "Reported version:" `docker run --rm $(CONTAINER_NAME):$(VERSION)-$(ARCH) version` "\nExpected version: $(VERSION)"; \ |
| 250 | + # false; \ |
| 251 | + # else \ |
| 252 | + # echo "Version check passed\n"; \ |
| 253 | + # fi |
| 254 | + |
| 255 | +## Generates release notes based on commits in this version. |
| 256 | +release-notes: release-prereqs |
| 257 | + mkdir -p dist |
| 258 | + echo "# Changelog" > release-notes-$(VERSION) |
| 259 | + sh -c "git cherry -v $(PREVIOUS_RELEASE) | cut '-d ' -f 2- | sed 's/^/- /' >> release-notes-$(VERSION)" |
| 260 | + |
| 261 | +## Pushes a github release and release artifacts produced by `make release-build`. |
| 262 | +release-publish: release-prereqs |
| 263 | + # Push the git tag. |
| 264 | + git push origin $(VERSION) |
| 265 | + |
| 266 | + # Push images. |
| 267 | + $(MAKE) push IMAGETAG=$(VERSION) ARCH=$(ARCH) |
| 268 | + |
| 269 | + @echo "Finalize the GitHub release based on the pushed tag." |
| 270 | + @echo "" |
| 271 | + @echo " https://$(PACKAGE_NAME)/releases/tag/$(VERSION)" |
| 272 | + @echo "" |
| 273 | + @echo "If this is the latest stable release, then run the following to push 'latest' images." |
| 274 | + @echo "" |
| 275 | + @echo " make VERSION=$(VERSION) release-publish-latest" |
| 276 | + @echo "" |
| 277 | + |
| 278 | +# WARNING: Only run this target if this release is the latest stable release. Do NOT |
| 279 | +# run this target for alpha / beta / release candidate builds, or patches to earlier Calico versions. |
| 280 | +## Pushes `latest` release images. WARNING: Only run this for latest stable releases. |
| 281 | +release-publish-latest: release-prereqs |
| 282 | + $(MAKE) push IMAGETAG=latest ARCH=$(ARCH) |
| 283 | + |
| 284 | +# release-prereqs checks that the environment is configured properly to create a release. |
| 285 | +release-prereqs: |
| 286 | +ifndef VERSION |
| 287 | + $(error VERSION is undefined - run using make release VERSION=vX.Y.Z) |
| 288 | +endif |
| 289 | + |
| 290 | +############################################################################### |
| 291 | +# Developer helper scripts (not used by build or test) |
| 292 | +############################################################################### |
| 293 | +.PHONY: help |
| 294 | +## Display this help text |
| 295 | +help: # Some kind of magic from https://gist.github.com/rcmachado/af3db315e31383502660 |
| 296 | + @awk '/^[a-zA-Z\-\_0-9\/]+:/ { \ |
| 297 | + nb = sub( /^## /, "", helpMsg ); \ |
| 298 | + if(nb == 0) { \ |
| 299 | + helpMsg = $$0; \ |
| 300 | + nb = sub( /^[^:]*:.* ## /, "", helpMsg ); \ |
| 301 | + } \ |
| 302 | + if (nb) \ |
| 303 | + printf "\033[1;31m%-" width "s\033[0m %s\n", $$1, helpMsg; \ |
| 304 | + } \ |
| 305 | + { helpMsg = $$0 }' \ |
| 306 | + width=20 \ |
| 307 | + $(MAKEFILE_LIST) |
0 commit comments