Skip to content

Commit

Permalink
Forward SOURCE_DATE_EPOCH to embedded k8s binaries
Browse files Browse the repository at this point in the history
Make it possible for embedded binaries to pick up the SOURCE_DATE_EPOCH
with which k0s is built. If supported, the embedded binaries will
display the same build date as the k0s binary.

Currently, only Kubernetes binaries will get the timestamp. It still
needs to be checked what other binaries embed the build date.

Signed-off-by: Tom Wieczorek <[email protected]>
  • Loading branch information
twz123 committed Oct 2, 2023
1 parent e3427c9 commit 615ddc8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
12 changes: 5 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,9 @@ endif

# https://reproducible-builds.org/docs/source-date-epoch/#makefile
# https://reproducible-builds.org/docs/source-date-epoch/#git
# https://stackoverflow.com/a/15103333
SOURCE_DATE_EPOCH ?= $(shell git log -1 --pretty=%ct || date -u +%s)
BUILD_DATE_FMT = %Y-%m-%dT%H:%M:%SZ
ifdef SOURCE_DATE_EPOCH
BUILD_DATE ?= $(shell date -u -d "@$(SOURCE_DATE_EPOCH)" "+$(BUILD_DATE_FMT)" 2>/dev/null || date -u -r "$(SOURCE_DATE_EPOCH)" "+$(BUILD_DATE_FMT)" 2>/dev/null || date -u "+$(BUILD_DATE_FMT)")
else
BUILD_DATE ?= $(shell TZ=UTC git log -1 --pretty=%cd --date='format-local:$(BUILD_DATE_FMT)' || date -u +$(BUILD_DATE_FMT))
endif
BUILD_DATE ?= $(shell date -u -d "@$(SOURCE_DATE_EPOCH)" "+$(BUILD_DATE_FMT)" 2>/dev/null || date -u -r "$(SOURCE_DATE_EPOCH)" "+$(BUILD_DATE_FMT)" 2>/dev/null || date -u "+$(BUILD_DATE_FMT)")

LD_FLAGS += -X github.com/k0sproject/k0s/pkg/build.Version=$(VERSION)
LD_FLAGS += -X github.com/k0sproject/k0s/pkg/build.RuncVersion=$(runc_version)
Expand Down Expand Up @@ -197,7 +193,9 @@ k0s.exe k0s: $(GO_SRCS) $(codegen_targets) go.sum
&& mv $@.tmp $@

.bins.windows.stamp .bins.linux.stamp: embedded-bins/Makefile.variables
$(MAKE) -C embedded-bins buildmode=$(EMBEDDED_BINS_BUILDMODE) TARGET_OS=$(patsubst .bins.%.stamp,%,$@)
$(MAKE) -C embedded-bins \
TARGET_OS=$(patsubst .bins.%.stamp,%,$@) \
SOURCE_DATE_EPOCH=$(SOURCE_DATE_EPOCH)
touch $@

.PHONY: codegen
Expand Down
5 changes: 5 additions & 0 deletions embedded-bins/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ export GOOS
TARGET_OS ?= linux
export TARGET_OS

# https://reproducible-builds.org/docs/source-date-epoch/#makefile
# https://reproducible-builds.org/docs/source-date-epoch/#git
SOURCE_DATE_EPOCH ?= $(shell git log -1 --pretty=%ct || date -u +%s)

bindir = staging/${TARGET_OS}/bin
posix_bins = runc kubelet containerd containerd-shim containerd-shim-runc-v1 containerd-shim-runc-v2 kube-apiserver kube-scheduler kube-controller-manager etcd kine konnectivity-server xtables-legacy-multi xtables-nft-multi
windows_bins = kubelet.exe kube-proxy.exe containerd.exe containerd-shim-runhcs-v1.exe
Expand Down Expand Up @@ -79,6 +83,7 @@ build_docker_image = \
--build-arg CONTAINERD_BINS="$(containerd_bins)" \
--build-arg KUBERNETES_BINS="$(kubernetes_bins)" \
--build-arg VERSION=$($(patsubst %/Dockerfile,%,$<)_version) \
--build-arg SOURCE_DATE_EPOCH=$(SOURCE_DATE_EPOCH) \
--build-arg BUILDIMAGE=$($(patsubst %/Dockerfile,%,$<)_buildimage) \
--build-arg BUILD_GO_TAGS=$($(patsubst %/Dockerfile,%,$<)_build_go_tags) \
--build-arg BUILD_GO_CGO_ENABLED=$($(patsubst %/Dockerfile,%,$<)_build_go_cgo_enabled) \
Expand Down
5 changes: 4 additions & 1 deletion embedded-bins/kubernetes/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ RUN git -c advice.detachedHead=false clone -b v$VERSION --depth=1 https://github
WORKDIR /go/src/github.com/kubernetes/kubernetes

ARG TARGET_OS \
SOURCE_DATE_EPOCH \
BUILD_GO_TAGS \
BUILD_GO_CGO_ENABLED \
BUILD_GO_FLAGS \
Expand All @@ -17,7 +18,7 @@ ARG TARGET_OS \
KUBERNETES_BINS

RUN \
set -e; \
set -ex; \
export GOPATH=/go; \
if [ "${TARGET_OS}" = windows ]; then \
commands="${KUBERNETES_BINS}"; \
Expand All @@ -34,7 +35,9 @@ RUN \
export KUBE_STATIC_OVERRIDES=$commands; \
fi; \
mkdir /out; \
export SOURCE_DATE_EPOCH=$SOURCE_DATE_EPOCH; \
export FORCE_HOST_GO=y; \
export KUBE_VERBOSE=9; \
export KUBE_GIT_VERSION="v$VERSION+k0s"; \
for cmd in $commands; do \
make GOFLAGS="${BUILD_GO_FLAGS} -tags=${BUILD_GO_TAGS}" GOLDFLAGS="${BUILD_GO_LDFLAGS_EXTRA}" WHAT=cmd/$cmd; \
Expand Down

0 comments on commit 615ddc8

Please sign in to comment.