From f2eccd62cc55f3e0df64cc43050bd8b59bb72aeb Mon Sep 17 00:00:00 2001 From: Tom Wieczorek Date: Thu, 26 Oct 2023 16:09:34 +0200 Subject: [PATCH] Introduce K0S_BUILDMODE Makefile variable Allow to select how to build k0s, similar to what EMBEDDED_BIND_BUILDMODE is doing for the embedded binaries. The two supported values are "docker", which is the default and builds k0s the usual way, and "host", which bypasses Docker and uses the toolchains present on the host system. Doing this was already partly possible via `make GO_ENV=''`, but this still called out to Docker to build the k0s build image and for the codegen tasks. The K0S_BUILDMODE variable bypasses Docker correctly for all make requisites of k0s, including codegen. Note that this doesn't impact the embedded binaries, so they need to be skipped via EMBEDDED_BIND_BUILDMODE=none to have a completely Docker-free build. This allows for building and testing k0s on systems that don't have Docker, or on architectures for which there are no appropriate Docker images available. Signed-off-by: Tom Wieczorek --- .github/workflows/go.yml | 6 ++-- Makefile | 67 ++++++++++++++++++++++++++-------------- 2 files changed, 45 insertions(+), 28 deletions(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index ff7c8220500d..ac6e3ce49f18 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -189,12 +189,10 @@ jobs: - name: Run unit tests env: - EMBEDDED_BINS_BUILDMODE: none TARGET_OS: windows - GO: go - GO_ENV: '' + K0S_BUILDMODE: host + EMBEDDED_BINS_BUILDMODE: none run: | - make --touch .k0sbuild.docker-image.k0s go.sum make bindata make --touch codegen make check-unit diff --git a/Makefile b/Makefile index 073043c6f274..6e9be041aa24 100644 --- a/Makefile +++ b/Makefile @@ -21,11 +21,16 @@ K0S_GO_BUILD_CACHE ?= build/cache GO_SRCS := $(shell find . -type f -name '*.go' -not -path './$(K0S_GO_BUILD_CACHE)/*' -not -path './inttest/*' -not -name '*_test.go' -not -name 'zz_generated*') GO_CHECK_UNIT_DIRS := . ./cmd/... ./pkg/... ./internal/... ./static/... ./hack/... -# EMBEDDED_BINS_BUILDMODE can be either: -# docker builds the binaries in docker -# none does not embed any binaries +# K0S_BUILDMODE can be either: +# docker builds k0s in Docker +# host builds k0s using the host's toolchains +K0S_BUILDMODE ?= docker +# EMBEDDED_BINS_BUILDMODE can be either: +# docker builds the embedded binaries in Docker +# none does not embed any binaries EMBEDDED_BINS_BUILDMODE ?= docker + # k0s runs on linux even if it's built on mac or windows TARGET_OS ?= linux BUILD_UID ?= $(shell id -u) @@ -49,6 +54,33 @@ else BUILD_DATE ?= $(shell TZ=UTC git log -1 --pretty=%cd --date='format-local:$(BUILD_DATE_FMT)' || date -u +$(BUILD_DATE_FMT)) endif +GOLANG_IMAGE ?= $(golang_buildimage) + +ifeq ($(K0S_BUILDMODE), docker) +k0s_buildmode_requisites = .k0sbuild.docker-image.k0s +K0S_GO_BUILD_CACHE_VOLUME_PATH ?= $(realpath $(K0S_GO_BUILD_CACHE)) +GO_ENV ?= docker run --rm \ + -v '$(K0S_GO_BUILD_CACHE_VOLUME_PATH)':/run/k0s-build \ + -v '$(CURDIR)':/go/src/github.com/k0sproject/k0s \ + -w /go/src/github.com/k0sproject/k0s \ + -e GOOS \ + -e CGO_ENABLED \ + -e GOARCH \ + --user $(BUILD_UID):$(BUILD_GID) \ + -- '$(shell cat .k0sbuild.docker-image.k0s)' +else ifeq ($(K0S_BUILDMODE), host) +k0s_buildmode_requisites = +GO_ENV ?= +else +$(error Unknown value for K0S_BUILDMODE: $(K0S_BUILDMODE)) +endif + +ifeq (, $(filter $(EMBEDDED_BINS_BUILDMODE), docker none)) +$(error Unknown value for EMBEDDED_BINS_BUILDMODE: $(EMBEDDED_BINS_BUILDMODE)) +endif + +GO ?= $(GO_ENV) go + LD_FLAGS += -X github.com/k0sproject/k0s/pkg/build.Version=$(VERSION) LD_FLAGS += -X github.com/k0sproject/k0s/pkg/build.RuncVersion=$(runc_version) LD_FLAGS += -X github.com/k0sproject/k0s/pkg/build.ContainerdVersion=$(containerd_version) @@ -71,19 +103,6 @@ endif endif LD_FLAGS += $(BUILD_GO_LDFLAGS_EXTRA) -GOLANG_IMAGE ?= $(golang_buildimage) -K0S_GO_BUILD_CACHE_VOLUME_PATH=$(realpath $(K0S_GO_BUILD_CACHE)) -GO_ENV ?= docker run --rm \ - -v '$(K0S_GO_BUILD_CACHE_VOLUME_PATH)':/run/k0s-build \ - -v '$(CURDIR)':/go/src/github.com/k0sproject/k0s \ - -w /go/src/github.com/k0sproject/k0s \ - -e GOOS \ - -e CGO_ENABLED \ - -e GOARCH \ - --user $(BUILD_UID):$(BUILD_GID) \ - -- '$(shell cat .k0sbuild.docker-image.k0s)' -GO ?= $(GO_ENV) go - # https://www.gnu.org/software/make/manual/make.html#index-spaces_002c-in-variable-values nullstring := space := $(nullstring) # space now holds a single space @@ -110,7 +129,7 @@ $(K0S_GO_BUILD_CACHE): --build-arg BUILDIMAGE=$(GOLANG_IMAGE) \ -t k0sbuild.docker-image.k0s - '$@' airgap-image-bundle-linux-amd64.tar: TARGET_PLATFORM := linux/amd64