|
| 1 | +# Copyright 2016 The Kubernetes Authors. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +# The binary to build (just the basename). |
| 16 | +BIN := tgik-controller |
| 17 | + |
| 18 | +# This repo's root import path (under GOPATH). |
| 19 | +PKG := github.com/jbeda/tgik-controller |
| 20 | + |
| 21 | +# Where to push the docker image. |
| 22 | +REGISTRY ?= jbeda |
| 23 | + |
| 24 | +# Which architecture to build - see $(ALL_ARCH) for options. |
| 25 | +ARCH ?= amd64 |
| 26 | + |
| 27 | +# This version-strategy uses git tags to set the version string |
| 28 | +VERSION := $(shell git describe --tags --always --dirty) |
| 29 | +# |
| 30 | +# This version-strategy uses a manual value to set the version string |
| 31 | +#VERSION := 1.2.3 |
| 32 | + |
| 33 | +### |
| 34 | +### These variables should not need tweaking. |
| 35 | +### |
| 36 | + |
| 37 | +ALL_ARCH := amd64 arm arm64 ppc64le |
| 38 | + |
| 39 | +# Set default base image dynamically for each arch |
| 40 | +ifeq ($(ARCH),amd64) |
| 41 | + BASEIMAGE?=alpine |
| 42 | +endif |
| 43 | +ifeq ($(ARCH),arm) |
| 44 | + BASEIMAGE?=armel/busybox |
| 45 | +endif |
| 46 | +ifeq ($(ARCH),arm64) |
| 47 | + BASEIMAGE?=aarch64/busybox |
| 48 | +endif |
| 49 | +ifeq ($(ARCH),ppc64le) |
| 50 | + BASEIMAGE?=ppc64le/busybox |
| 51 | +endif |
| 52 | + |
| 53 | +IMAGE := $(REGISTRY)/$(BIN)-$(ARCH) |
| 54 | + |
| 55 | +BUILD_IMAGE ?= golang:1.8-alpine |
| 56 | + |
| 57 | +DOCKER_MOUNT_MODE=delegated |
| 58 | + |
| 59 | +# If you want to build all binaries, see the 'all-build' rule. |
| 60 | +# If you want to build all containers, see the 'all-container' rule. |
| 61 | +# If you want to build AND push all containers, see the 'all-push' rule. |
| 62 | +all: build |
| 63 | + |
| 64 | +build-%: |
| 65 | + @$(MAKE) --no-print-directory ARCH=$* build |
| 66 | + |
| 67 | +container-%: |
| 68 | + @$(MAKE) --no-print-directory ARCH=$* container |
| 69 | + |
| 70 | +push-%: |
| 71 | + @$(MAKE) --no-print-directory ARCH=$* push |
| 72 | + |
| 73 | +all-build: $(addprefix build-, $(ALL_ARCH)) |
| 74 | + |
| 75 | +all-container: $(addprefix container-, $(ALL_ARCH)) |
| 76 | + |
| 77 | +all-push: $(addprefix push-, $(ALL_ARCH)) |
| 78 | + |
| 79 | +build: bin/$(ARCH)/$(BIN) |
| 80 | + |
| 81 | +bin/$(ARCH)/$(BIN): build-dirs |
| 82 | + @echo "building: $@" |
| 83 | + @docker run \ |
| 84 | + -ti \ |
| 85 | + -u $$(id -u):$$(id -g) \ |
| 86 | + -v $$(pwd)/.go:/go:$(DOCKER_MOUNT_MODE) \ |
| 87 | + -v $$(pwd):/go/src/$(PKG):$(DOCKER_MOUNT_MODE) \ |
| 88 | + -v $$(pwd)/bin/$(ARCH):/go/bin:$(DOCKER_MOUNT_MODE) \ |
| 89 | + -v $$(pwd)/bin/$(ARCH):/go/bin/linux_$(ARCH):$(DOCKER_MOUNT_MODE) \ |
| 90 | + -v $$(pwd)/.go/std/$(ARCH):/usr/local/go/pkg/linux_$(ARCH)_static:$(DOCKER_MOUNT_MODE) \ |
| 91 | + -w /go/src/$(PKG) \ |
| 92 | + $(BUILD_IMAGE) \ |
| 93 | + /bin/sh -c " \ |
| 94 | + ARCH=$(ARCH) \ |
| 95 | + VERSION=$(VERSION) \ |
| 96 | + PKG=$(PKG) \ |
| 97 | + ./build/build.sh \ |
| 98 | + " |
| 99 | + |
| 100 | +DOTFILE_IMAGE = $(subst :,_,$(subst /,_,$(IMAGE))-$(VERSION)) |
| 101 | + |
| 102 | +container: .container-$(DOTFILE_IMAGE) container-name |
| 103 | +.container-$(DOTFILE_IMAGE): bin/$(ARCH)/$(BIN) Dockerfile.in |
| 104 | + @sed \ |
| 105 | + -e 's|ARG_BIN|$(BIN)|g' \ |
| 106 | + -e 's|ARG_ARCH|$(ARCH)|g' \ |
| 107 | + -e 's|ARG_FROM|$(BASEIMAGE)|g' \ |
| 108 | + Dockerfile.in > .dockerfile-$(ARCH) |
| 109 | + @docker build -t $(IMAGE):$(VERSION) -f .dockerfile-$(ARCH) . |
| 110 | + @docker images -q $(IMAGE):$(VERSION) > $@ |
| 111 | + |
| 112 | +container-name: |
| 113 | + @echo "container: $(IMAGE):$(VERSION)" |
| 114 | + |
| 115 | +push: .push-$(DOTFILE_IMAGE) push-name |
| 116 | +.push-$(DOTFILE_IMAGE): .container-$(DOTFILE_IMAGE) |
| 117 | +ifeq ($(findstring gcr.io,$(REGISTRY)),gcr.io) |
| 118 | + @gcloud docker -- push $(IMAGE):$(VERSION) |
| 119 | +else |
| 120 | + @docker push $(IMAGE):$(VERSION) |
| 121 | +endif |
| 122 | + @docker images -q $(IMAGE):$(VERSION) > $@ |
| 123 | + |
| 124 | +push-name: |
| 125 | + @echo "pushed: $(IMAGE):$(VERSION)" |
| 126 | + |
| 127 | +version: |
| 128 | + @echo $(VERSION) |
| 129 | + |
| 130 | +test: build-dirs |
| 131 | + @docker run \ |
| 132 | + -ti \ |
| 133 | + -u $$(id -u):$$(id -g) \ |
| 134 | + -v $$(pwd)/.go:/go:$(DOCKER_MOUNT_MODE) \ |
| 135 | + -v $$(pwd):/go/src/$(PKG):$(DOCKER_MOUNT_MODE) \ |
| 136 | + -v $$(pwd)/bin/$(ARCH):/go/bin:$(DOCKER_MOUNT_MODE) \ |
| 137 | + -v $$(pwd)/.go/std/$(ARCH):/usr/local/go/pkg/linux_$(ARCH)_static:$(DOCKER_MOUNT_MODE) \ |
| 138 | + -w /go/src/$(PKG) \ |
| 139 | + $(BUILD_IMAGE) \ |
| 140 | + /bin/sh -c " \ |
| 141 | + ./build/test.sh \ |
| 142 | + " |
| 143 | + |
| 144 | +build-dirs: |
| 145 | + @mkdir -p bin/$(ARCH) |
| 146 | + @mkdir -p .go/src/$(PKG) .go/pkg .go/bin .go/std/$(ARCH) |
| 147 | + |
| 148 | +clean: container-clean bin-clean |
| 149 | + |
| 150 | +container-clean: |
| 151 | + rm -rf .container-* .dockerfile-* .push-* |
| 152 | + |
| 153 | +bin-clean: |
| 154 | + rm -rf .go bin |
0 commit comments