Skip to content

Commit

Permalink
refactor containerized build
Browse files Browse the repository at this point in the history
  • Loading branch information
BenTheElder committed Jun 19, 2019
1 parent 07d7846 commit a03ada7
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 66 deletions.
88 changes: 23 additions & 65 deletions Makefile
Expand Up @@ -12,89 +12,47 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# Simple makefile to build kind quickly and reproducibly in a container
# Old-skool build tools.
# Simple makefile to build kind quickly and reproducibly in a container..
# Only requires docker on the host
#
# Common uses:
# - installing kind: `make install INSTALL_DIR=$HOME/go/bin`
# - building: `make build`
# - cleaning up and starting over: `make clean`

# settings
# get the repo root and output path, go_container.sh respects these
REPO_ROOT:=${CURDIR}
# autodetect host GOOS and GOARCH by default, even if go is not installed
GOOS?=$(shell hack/util/goos.sh)
GOARCH?=$(shell hack/util/goarch.sh)
OUT_DIR=$(REPO_ROOT)/bin
# make install will place binaries here
# the default path attempst to mimic go install
INSTALL_DIR?=$(shell hack/util/goinstalldir.sh)
# the default path attempts to mimic go install
INSTALL_DIR?=$(shell hack/build/goinstalldir.sh)
# the output binary name, overridden when cross compiling
KIND_BINARY_NAME?=kind
# use the official module proxy by default
GOPROXY?=https://proxy.golang.org
# default build image
GO_VERSION?=1.12.6
GO_IMAGE?=golang:$(GO_VERSION)
# docker volume name, used as a go module / build cache
CACHE_VOLUME?=kind-build-cache

# variables for consistent logic, don't override these
CONTAINER_REPO_DIR=/src/kind
CONTAINER_OUT_DIR=$(CONTAINER_REPO_DIR)/bin
OUT_DIR=$(REPO_ROOT)/bin
UID:=$(shell id -u)
GID:=$(shell id -g)

# standard "make" target -> builds
all: build

# creates the cache volume
make-cache:
@echo + Ensuring build cache volume exists
docker volume create $(CACHE_VOLUME)

# cleans the cache volume
clean-cache:
@echo + Removing build cache volume
docker volume rm $(CACHE_VOLUME)

# creates the output directory
out-dir:
@echo + Ensuring build output directory exists
mkdir -p $(OUT_DIR)

# cleans the output directory
clean-output:
@echo + Removing build output directory
rm -rf $(OUT_DIR)/

# builds kind in a container, outputs to $(OUT_DIR)
kind: make-cache out-dir
@echo + Building kind binary
docker run \
--rm \
-v $(CACHE_VOLUME):/go \
-e GOCACHE=/go/cache \
-v $(OUT_DIR):/out \
-v $(REPO_ROOT):$(CONTAINER_REPO_DIR) \
-w $(CONTAINER_REPO_DIR) \
-e GO111MODULE=on \
-e GOPROXY=$(GOPROXY) \
-e CGO_ENABLED=0 \
-e GOOS=$(GOOS) \
-e GOARCH=$(GOARCH) \
-e HTTP_PROXY=$(HTTP_PROXY) \
-e HTTPS_PROXY=$(HTTPS_PROXY) \
-e NO_PROXY=$(NO_PROXY) \
--user $(UID):$(GID) \
$(GO_IMAGE) \
go build -v -o /out/$(KIND_BINARY_NAME) .
@echo + Built kind binary to $(OUT_DIR)/$(KIND_BINARY_NAME)
kind:
hack/build/go_container.sh go build -v -o /out/$(KIND_BINARY_NAME)

# alias for building kind
build: kind

# use: make install INSTALL_DIR=/usr/local/bin
install: build
@echo + Copying kind binary to INSTALL_DIR
install $(OUT_DIR)/$(KIND_BINARY_NAME) $(INSTALL_DIR)/$(KIND_BINARY_NAME)

# cleans the cache volume
clean-cache:
docker volume rm -f kind-build-cache >/dev/null

# cleans the output directory
clean-output:
rm -rf $(OUT_DIR)/

# standard cleanup target
clean: clean-cache clean-output
clean: clean-output clean-cache

.PHONY: all make-cache clean-cache out-dir clean-output kind build install clean
.PHONY: all kind build install clean-cache clean-output clean
1 change: 1 addition & 0 deletions hack/build/README.md
@@ -0,0 +1 @@
This directory contains tooling used for building
90 changes: 90 additions & 0 deletions hack/build/go_container.sh
@@ -0,0 +1,90 @@
#!/bin/sh
# Copyright 2019 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Simple posix sh reproducible build container script with (go) caching
# Only requires docker on the host
set -o nounset
set -o errexit
set -o pipefail

# get and go to the repo root
REPO_ROOT="${REPO_ROOT:-$(git rev-parse --show-toplevel)}"

# autodetect host GOOS and GOARCH if not set, even if go is not installed
GOOS="${GOOS:-$("${REPO_ROOT}/hack/build/goos.sh")}"
GOARCH="${GOARCH:-$("${REPO_ROOT}/hack/build/goarch.sh")}"

# use the official module proxy by default
GOPROXY="${GOPROXY:-https://proxy.golang.org}"

# default build image
GO_VERSION="1.12.6"
GO_IMAGE="golang:${GO_VERSION}"

# docker volume name, used as a go module / build cache
CACHE_VOLUME="kind-build-cache"

# output directory
OUT_DIR="${OUT_DIR:-${REPO_ROOT}/bin}"
# source directory
SOURCE_DIR="${SOURCE_DIR:-${REPO_ROOT}}"

# creates the output directory
make_out_dir() {
mkdir -p "${OUT_DIR}"
}

# creates the cache volume
make_cache_volume() {
docker volume create "${CACHE_VOLUME}" >/dev/null
}

# runs $@ in a go container with caching etc. and the repo mount to /src
run_in_go_container() {
# get user id and group id so we can run the container
_UID=$(id -u)
_GID=$(id -g)
# run in the container
docker run \
`# ensure the container is removed on exit` \
--rm \
`# use the cache volume for go` \
-v "${CACHE_VOLUME}:/go" \
-e GOCACHE=/go/cache \
`# mount the output & repo dir, set working directory to the repo` \
-v "${OUT_DIR}:/out" \
-v "${SOURCE_DIR}:/src" \
-w "/src" \
`# go settings: use modules and proxy, disable cgo, set OS/Arch` \
-e GO111MODULE=on \
-e GOPROXY="${GOPROXY}" \
-e CGO_ENABLED=0 \
-e GOOS="${GOOS}" \
-e GOARCH="${GOARCH}" \
`# pass through proxy settings from the host` \
-e HTTP_PROXY="${HTTP_PROXY:+}" \
-e HTTPS_PROXY="${HTTPS_PROX:+}" \
-e NO_PROXY="${NO_PROXY:+}" \
`# run as if the host user for consistent file permissions` \
--user "${_UID}:${_GID}" \
`# use the golang image for the container` \
"${GO_IMAGE}" \
`# and finally, pass through args` \
"$@"
}

make_out_dir
make_cache_volume
run_in_go_container "$@"
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 0 additions & 1 deletion hack/util/README.md

This file was deleted.

0 comments on commit a03ada7

Please sign in to comment.