From 993a53fd3780128280970eaf577c0dfe93924c1c Mon Sep 17 00:00:00 2001 From: Matt Rickard Date: Mon, 26 Jun 2017 13:20:16 -0700 Subject: [PATCH] Add option to build minikube, localkube in docker BUILD_IN_DOCKER=y will build both localkube and minikube in docker LOCALKUBE_BUILD_IN_DOCKER=y, MINIKUBE_BUILD_IN_DOCKER=y will toggle docker builds for each respective binary Regardless of the options provided, if you attempt to build localkube on a non-linux platform, it will run in docker. This ensures that `make` still works as expected on darwin. --- Makefile | 66 +++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 42 insertions(+), 24 deletions(-) diff --git a/Makefile b/Makefile index 5dc6776ec6d2..19394da8189e 100644 --- a/Makefile +++ b/Makefile @@ -21,10 +21,11 @@ DEB_VERSION ?= $(VERSION_MAJOR).$(VERSION_MINOR)-$(VERSION_BUILD) INSTALL_SIZE ?= $(shell du out/minikube-windows-amd64.exe | cut -f1) BUILDROOT_BRANCH ?= 2017.02 REGISTRY?=gcr.io/k8s-minikube -DARWIN_BUILD_IMAGE ?= karalabe/xgo-1.8.3 -ISO_BUILD_IMAGE ?= $(REGISTRY)/buildroot-image -# The iso will be versioned the same as minikube +MINIKUBE_BUILD_IMAGE ?= karalabe/xgo-1.8.3 +LOCALKUBE_BUILD_IMAGE ?= gcr.io/google_containers/kube-cross:v1.7.1-0 +ISO_BUILD_IMAGE ?= $(REGISTRY)/buildroot-image + ISO_VERSION ?= v0.20.0 ISO_BUCKET ?= minikube/iso @@ -33,13 +34,6 @@ GOARCH ?= $(shell go env GOARCH) BUILD_DIR ?= ./out ORG := k8s.io REPOPATH ?= $(ORG)/minikube -BUILD_IMAGE ?= gcr.io/google_containers/kube-cross:v1.7.1-0 -IS_EXE ?= - -ifeq ($(IN_DOCKER),1) - GOPATH := /go - export GOPATH -endif # Use system python if it exists, otherwise use Docker. PYTHON := $(shell command -v python || echo "docker run --rm -it -v $(shell pwd):/minikube -w /minikube python python") @@ -58,32 +52,56 @@ LOCALKUBE_LDFLAGS := "$(K8S_VERSION_LDFLAGS) $(MINIKUBE_LDFLAGS) -s -w -extldfla LOCALKUBEFILES := GOPATH=$(GOPATH) go list -f '{{join .Deps "\n"}}' ./cmd/localkube/ | grep k8s.io | GOPATH=$(GOPATH) xargs go list -f '{{ range $$file := .GoFiles }} {{$$.Dir}}/{{$$file}}{{"\n"}}{{end}}' MINIKUBEFILES := GOPATH=$(GOPATH) go list -f '{{join .Deps "\n"}}' ./cmd/minikube/ | grep k8s.io | GOPATH=$(GOPATH) xargs go list -f '{{ range $$file := .GoFiles }} {{$$.Dir}}/{{$$file}}{{"\n"}}{{end}}' +MINIKUBE_ENV_LINUX := CGO_ENABLED=1 GOARCH=amd64 GOOS=linux +MINIKUBE_ENV_DARWIN := CC=o64-clang CXX=o64-clang++ CGO_ENABLED=1 GOARCH=amd64 GOOS=darwin +MINIKUBE_ENV_WINDOWS := CGO_ENABLED=1 GOARCH=amd64 GOOS=linux + +MINIKUBE_DOCKER_CMD := docker run -e IN_DOCKER=1 --user $(shell id -u):$(shell id -g) --workdir /go/src/$(REPOPATH) --entrypoint /bin/bash -v $(PWD):/go/src/$(REPOPATH) $(MINIKUBE_BUILD_IMAGE) -c +KUBE_CROSS_DOCKER_CMD := docker run -w /go/src/$(REPOPATH) --user $(shell id -u):$(shell id -g) -e IN_DOCKER=1 -v $(shell pwd):/go/src/$(REPOPATH) $(LOCALKUBE_BUILD_IMAGE) + +# $(call MINIKUBE_GO_BUILD_CMD, output file, OS) +define MINIKUBE_GO_BUILD_CMD + $(call uc,MINIKUBE_ENV_$(2)) go build --installsuffix cgo -ldflags="$(MINIKUBE_LDFLAGS) $(K8S_VERSION_LDFLAGS)" -o $(1) k8s.io/minikube/cmd/minikube +endef + +ifeq ($(BUILD_IN_DOCKER),y) + MINIKUBE_BUILD_IN_DOCKER=y + LOCALKUBE_BUILD_IN_DOCKER=y +endif + +# If not on linux, localkube must be built in docker +ifneq ($(BUILD_OS),Linux) + LOCALKUBE_BUILD_IN_DOCKER=y +endif + +ifeq ($(IN_DOCKER),1) + MINIKUBE_BUILD_IN_DOCKER=n + LOCALKUBE_BUILD_IN_DOCKER=n +endif + ifeq ($(GOOS),windows) IS_EXE = ".exe" endif out/minikube$(IS_EXE): out/minikube-$(GOOS)-$(GOARCH)$(IS_EXE) - cp $(BUILD_DIR)/minikube-$(GOOS)-$(GOARCH)$(IS_EXE) $(BUILD_DIR)/minikube$(IS_EXE) + cp $(BUILD_DIR)/minikube-$(GOOS)-$(GOARCH) $(BUILD_DIR)/minikube$(IS_EXE) out/localkube: $(shell $(LOCALKUBEFILES)) -ifeq ($(BUILD_OS),Linux) - CGO_ENABLED=1 go build -tags static_build -ldflags=$(LOCALKUBE_LDFLAGS) -o $(BUILD_DIR)/localkube ./cmd/localkube +ifeq ($(LOCALKUBE_BUILD_IN_DOCKER),y) + $(KUBE_CROSS_DOCKER_CMD) make $@ else - docker run -w /go/src/$(REPOPATH) -e IN_DOCKER=1 -v $(shell pwd):/go/src/$(REPOPATH) $(BUILD_IMAGE) make out/localkube + CGO_ENABLED=1 go build -tags static_build -ldflags=$(LOCALKUBE_LDFLAGS) -o $(BUILD_DIR)/localkube ./cmd/localkube endif -out/minikube-darwin-amd64: pkg/minikube/assets/assets.go $(shell $(MINIKUBEFILES)) -ifeq ($(IN_DOCKER),1) - CC=o64-clang CXX=o64-clang++ CGO_ENABLED=1 GOARCH=amd64 GOOS=darwin go build --installsuffix cgo -ldflags="$(MINIKUBE_LDFLAGS) $(K8S_VERSION_LDFLAGS)" -a -o $(BUILD_DIR)/minikube-darwin-amd64 k8s.io/minikube/cmd/minikube +out/minikube-windows-amd64.exe: out/minikube-windows-amd64 + mv out/minikube-windows-amd64 out/minikube-windows-amd64.exe + +out/minikube-%-amd64: pkg/minikube/assets/assets.go $(shell $(MINIKUBEFILES)) +ifeq ($(MINIKUBE_BUILD_IN_DOCKER),y) + $(MINIKUBE_DOCKER_CMD) '$(call MINIKUBE_GO_BUILD_CMD, $@, $*)' else - docker run -e IN_DOCKER=1 --workdir /go/src/$(REPOPATH) --entrypoint /usr/bin/make -v $(PWD):/go/src/$(REPOPATH) $(DARWIN_BUILD_IMAGE) out/minikube-darwin-amd64 + $(call MINIKUBE_GO_BUILD_CMD, $@, $*) endif -out/minikube-linux-amd64: pkg/minikube/assets/assets.go $(shell $(MINIKUBEFILES)) - CGO_ENABLED=1 GOARCH=amd64 GOOS=linux go build --installsuffix cgo -ldflags="$(MINIKUBE_LDFLAGS) $(K8S_VERSION_LDFLAGS)" -a -o $(BUILD_DIR)/minikube-linux-amd64 k8s.io/minikube/cmd/minikube - -out/minikube-windows-amd64.exe: pkg/minikube/assets/assets.go $(shell $(MINIKUBEFILES)) - CGO_ENABLED=0 GOARCH=amd64 GOOS=windows go build --installsuffix cgo -ldflags="$(MINIKUBE_LDFLAGS) $(K8S_VERSION_LDFLAGS)" -a -o $(BUILD_DIR)/minikube-windows-amd64.exe k8s.io/minikube/cmd/minikube - minikube_iso: # old target kept for making tests happy echo $(ISO_VERSION) > deploy/iso/minikube-iso/board/coreos/minikube/rootfs-overlay/etc/VERSION if [ ! -d $(BUILD_DIR)/buildroot ]; then \