| Matthias Andreas Benkard | 832a54e | 2019-01-29 09:27:38 +0100 | [diff] [blame] | 1 | # Common User-Settable Flags | 
|  | 2 | # ========================== | 
|  | 3 | PREFIX?=gcr.io/google_containers | 
|  | 4 | FLAGS= | 
|  | 5 | ARCH?=amd64 | 
|  | 6 | GOLANG_VERSION?=1.10 | 
|  | 7 | # You can set this variable for testing and the built image will also be tagged with this name | 
|  | 8 | IMAGE_NAME?=$(PREFIX)/metrics-server-$(ARCH):$(VERSION) | 
|  | 9 |  | 
|  | 10 | # by default, build the current arch's binary | 
|  | 11 | # (this needs to be pre-include, for some reason) | 
|  | 12 | all: _output/$(ARCH)/metrics-server | 
|  | 13 |  | 
|  | 14 | # Constants | 
|  | 15 | # ========= | 
|  | 16 | ALL_ARCHITECTURES=amd64 arm arm64 ppc64le s390x | 
|  | 17 | ML_PLATFORMS=linux/amd64,linux/arm,linux/arm64,linux/ppc64le,linux/s390x | 
|  | 18 |  | 
|  | 19 | # Calculated Variables | 
|  | 20 | # ==================== | 
|  | 21 | REPO_DIR:=$(shell pwd) | 
|  | 22 | LDFLAGS=-w $(VERSION_LDFLAGS) | 
|  | 23 | # get the appropriate version information | 
|  | 24 | include hack/Makefile.buildinfo | 
|  | 25 | # Set default base image dynamically for each arch | 
|  | 26 | ifeq ($(ARCH),amd64) | 
|  | 27 | BASEIMAGE?=busybox | 
|  | 28 | endif | 
|  | 29 | ifeq ($(ARCH),arm) | 
|  | 30 | BASEIMAGE?=arm32v7/busybox | 
|  | 31 | endif | 
|  | 32 | ifeq ($(ARCH),arm64) | 
|  | 33 | BASEIMAGE?=arm64v8/busybox | 
|  | 34 | endif | 
|  | 35 | ifeq ($(ARCH),ppc64le) | 
|  | 36 | BASEIMAGE?=ppc64le/busybox | 
|  | 37 | endif | 
|  | 38 | ifeq ($(ARCH),s390x) | 
|  | 39 | BASEIMAGE?=s390x/busybox | 
|  | 40 | endif | 
|  | 41 |  | 
|  | 42 |  | 
|  | 43 | # Rules | 
|  | 44 | # ===== | 
|  | 45 |  | 
|  | 46 | .PHONY: all test-unit container container-* clean container-only container-only-* tmp-dir push do-push-* sub-push-* | 
|  | 47 |  | 
|  | 48 | # Build Rules | 
|  | 49 | # ----------- | 
|  | 50 |  | 
|  | 51 | pkg/generated/openapi/zz_generated.openapi.go: | 
|  | 52 | go run vendor/k8s.io/kube-openapi/cmd/openapi-gen/openapi-gen.go --logtostderr -i k8s.io/metrics/pkg/apis/metrics/v1beta1,k8s.io/apimachinery/pkg/apis/meta/v1,k8s.io/apimachinery/pkg/api/resource,k8s.io/apimachinery/pkg/version -p github.com/kubernetes-incubator/metrics-server/pkg/generated/openapi/ -O zz_generated.openapi -h $(REPO_DIR)/hack/boilerplate.go.txt -r /dev/null | 
|  | 53 |  | 
|  | 54 | # building depends on all go files (this is mostly redundant in the face of go 1.10's incremental builds, | 
|  | 55 | # but it allows us to safely write actual dependency rules in our makefile) | 
|  | 56 | src_deps=$(shell find pkg cmd -type f -name "*.go" -and ! -name "zz_generated.*.go") | 
|  | 57 | _output/%/metrics-server: $(src_deps) pkg/generated/openapi/zz_generated.openapi.go | 
|  | 58 | GOARCH=$* CGO_ENABLED=0 go build -ldflags "$(LDFLAGS)" -o _output/$*/metrics-server github.com/kubernetes-incubator/metrics-server/cmd/metrics-server | 
|  | 59 |  | 
|  | 60 | # Image Rules | 
|  | 61 | # ----------- | 
|  | 62 |  | 
|  | 63 | # build a container using containerized build (the current arch by default) | 
|  | 64 | container: container-$(ARCH) | 
|  | 65 |  | 
|  | 66 | container-%: pkg/generated/openapi/zz_generated.openapi.go tmpdir | 
|  | 67 | # Run the build in a container in order to have reproducible builds | 
|  | 68 | docker run --rm -v $(TEMP_DIR):/build -v $(REPO_DIR):/go/src/github.com/kubernetes-incubator/metrics-server -w /go/src/github.com/kubernetes-incubator/metrics-server golang:$(GOLANG_VERSION) /bin/bash -c "\ | 
|  | 69 | GOARCH=$(ARCH) CGO_ENABLED=0 go build -ldflags \"$(LDFLAGS)\" -o /build/metrics-server github.com/kubernetes-incubator/metrics-server/cmd/metrics-server" | 
|  | 70 |  | 
|  | 71 | # copy the base Dockerfile into the temp dir, and set the base image | 
|  | 72 | cp deploy/docker/Dockerfile $(TEMP_DIR) | 
|  | 73 | sed -i -e "s|BASEIMAGE|$(BASEIMAGE)|g" $(TEMP_DIR)/Dockerfile | 
|  | 74 |  | 
|  | 75 | # run the actual build | 
|  | 76 | docker build --pull -t $(IMAGE_NAME) $(TEMP_DIR) | 
|  | 77 |  | 
|  | 78 | # remove our TEMP_DIR, as needed | 
|  | 79 | rm -rf $(TEMP_DIR) | 
|  | 80 |  | 
|  | 81 | # build a container using a locally-built binary (the current arch by default) | 
|  | 82 | container-only: container-only-$(ARCH) | 
|  | 83 |  | 
|  | 84 | container-only-%: _output/$(ARCH)/metrics-server tmpdir | 
|  | 85 | # copy the base Dockerfile and binary into the temp dir, and set the base image | 
|  | 86 | cp deploy/docker/Dockerfile $(TEMP_DIR) | 
|  | 87 | cp _output/$(ARCH)/metrics-server $(TEMP_DIR) | 
|  | 88 | sed -i -e "s|BASEIMAGE|$(BASEIMAGE)|g" $(TEMP_DIR)/Dockerfile | 
|  | 89 |  | 
|  | 90 | # run the actual build | 
|  | 91 | docker build --pull -t $(IMAGE_NAME) $(TEMP_DIR) | 
|  | 92 |  | 
|  | 93 | # remove our TEMP_DIR, as needed | 
|  | 94 | rm -rf $(TEMP_DIR) | 
|  | 95 |  | 
|  | 96 | # Official Container Push Rules | 
|  | 97 | # ----------------------------- | 
|  | 98 |  | 
|  | 99 | # do the actual push for official images | 
|  | 100 | do-push-%: | 
|  | 101 | # push with main tag | 
|  | 102 | docker push $(PREFIX)/metrics-server-$*:$(VERSION) | 
|  | 103 |  | 
|  | 104 | # push alternate tags | 
|  | 105 | ifeq ($(ARCH),amd64) | 
|  | 106 | # TODO: Remove this and push the manifest list as soon as it's working | 
|  | 107 | docker tag $(PREFIX)/metrics-server-$*:$(VERSION) $(PREFIX)/metrics-server:$(VERSION) | 
|  | 108 | docker push $(PREFIX)/metrics-server:$(VERSION) | 
|  | 109 | endif | 
|  | 110 |  | 
|  | 111 | # do build and then push a given official image | 
|  | 112 | sub-push-%: container-% do-push-% ; | 
|  | 113 |  | 
|  | 114 | # do build and then push all official images | 
|  | 115 | push: gcr-login $(addprefix sub-push-,$(ALL_ARCHITECTURES)) ; | 
|  | 116 | # TODO: push with manifest-tool? | 
|  | 117 | # Should depend on target: ./manifest-tool | 
|  | 118 |  | 
|  | 119 | # log in to the official container registry | 
|  | 120 | gcr-login: | 
|  | 121 | ifeq ($(findstring gcr.io,$(PREFIX)),gcr.io) | 
|  | 122 | @echo "If you are pushing to a gcr.io registry, you have to be logged in via 'docker login'; 'gcloud docker push' can't push manifest lists yet." | 
|  | 123 | @echo "This script is automatically logging you in now with 'gcloud docker -a'" | 
|  | 124 | gcloud docker -a | 
|  | 125 | endif | 
|  | 126 |  | 
|  | 127 | # Utility Rules | 
|  | 128 | # ------------- | 
|  | 129 |  | 
|  | 130 | clean: | 
|  | 131 | rm -rf _output | 
|  | 132 | rm pkg/generated/openapi/zz_generated.openapi.go | 
|  | 133 |  | 
|  | 134 | fmt: | 
|  | 135 | find pkg cmd -type f -name "*.go" | xargs gofmt -s -w | 
|  | 136 |  | 
|  | 137 | test-unit: pkg/generated/openapi/zz_generated.openapi.go | 
|  | 138 | ifeq ($(ARCH),amd64) | 
|  | 139 | GOARCH=$(ARCH) go test --test.short -race ./pkg/... $(FLAGS) | 
|  | 140 | else | 
|  | 141 | GOARCH=$(ARCH) go test --test.short ./pkg/... $(FLAGS) | 
|  | 142 | endif | 
|  | 143 |  | 
|  | 144 | # set up a temporary director when we need it | 
|  | 145 | # it's the caller's responsibility to clean it up | 
|  | 146 | tmpdir: | 
|  | 147 | $(eval TEMP_DIR:=$(shell mktemp -d /tmp/metrics-server.XXXXXX)) |