blob: 1998650c0e97d106fa0ae4b0661f633361f3d0af [file] [log] [blame]
Matthias Andreas Benkard832a54e2019-01-29 09:27:38 +01001# Common User-Settable Flags
2# ==========================
3PREFIX?=gcr.io/google_containers
4FLAGS=
5ARCH?=amd64
6GOLANG_VERSION?=1.10
7# You can set this variable for testing and the built image will also be tagged with this name
8IMAGE_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)
12all: _output/$(ARCH)/metrics-server
13
14# Constants
15# =========
16ALL_ARCHITECTURES=amd64 arm arm64 ppc64le s390x
17ML_PLATFORMS=linux/amd64,linux/arm,linux/arm64,linux/ppc64le,linux/s390x
18
19# Calculated Variables
20# ====================
21REPO_DIR:=$(shell pwd)
22LDFLAGS=-w $(VERSION_LDFLAGS)
23# get the appropriate version information
24include hack/Makefile.buildinfo
25# Set default base image dynamically for each arch
26ifeq ($(ARCH),amd64)
27 BASEIMAGE?=busybox
28endif
29ifeq ($(ARCH),arm)
30 BASEIMAGE?=arm32v7/busybox
31endif
32ifeq ($(ARCH),arm64)
33 BASEIMAGE?=arm64v8/busybox
34endif
35ifeq ($(ARCH),ppc64le)
36 BASEIMAGE?=ppc64le/busybox
37endif
38ifeq ($(ARCH),s390x)
39 BASEIMAGE?=s390x/busybox
40endif
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
51pkg/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)
56src_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)
64container: container-$(ARCH)
65
66container-%: 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)
82container-only: container-only-$(ARCH)
83
84container-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
100do-push-%:
101 # push with main tag
102 docker push $(PREFIX)/metrics-server-$*:$(VERSION)
103
104 # push alternate tags
105ifeq ($(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)
109endif
110
111# do build and then push a given official image
112sub-push-%: container-% do-push-% ;
113
114# do build and then push all official images
115push: 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
120gcr-login:
121ifeq ($(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
125endif
126
127# Utility Rules
128# -------------
129
130clean:
131 rm -rf _output
132 rm pkg/generated/openapi/zz_generated.openapi.go
133
134fmt:
135 find pkg cmd -type f -name "*.go" | xargs gofmt -s -w
136
137test-unit: pkg/generated/openapi/zz_generated.openapi.go
138ifeq ($(ARCH),amd64)
139 GOARCH=$(ARCH) go test --test.short -race ./pkg/... $(FLAGS)
140else
141 GOARCH=$(ARCH) go test --test.short ./pkg/... $(FLAGS)
142endif
143
144# set up a temporary director when we need it
145# it's the caller's responsibility to clean it up
146tmpdir:
147 $(eval TEMP_DIR:=$(shell mktemp -d /tmp/metrics-server.XXXXXX))