Skip to content

Commit 8bdee92

Browse files
bk201Vicente-Cheng
authored andcommitted
build: migrate from Dapper to single-Dockerfile build system
Replace the Dapper-based build system (Dockerfile.dapper + scripts/entry + .dapper download) with a single root Dockerfile driven directly from the Makefile via `docker build --target <stage>`. Dockerfile: - Add builder stage from registry.suse.com/bci/golang:1.26 with all tooling (git, docker, golangci-lint v2.12.2, controller-gen v0.17.1, openapi-gen v0.29.13) - Add base stage (COPY . .) with WORKDIR/HOME set to the Go module path - Add build stage + build-output scratch stage (cross-compiles amd64/arm64) - Add validate stage (golangci-lint + go fmt) - Add validate-ci stage with git-init trick for dirty-check after go generate - Add test stage (go test) - Add generate-manifest stage + generate-manifest-output scratch stage (controller-gen, no Go cache mounts needed) Makefile: - Replace dapper download/invocation with docker build --target <stage> - Add standard MK_* variables (MK_HOST_ARCH, MK_REPO_ID, MK_DOCKER_PROGRESS, MK_DOCKER_PULL, MK_DOCKER_RUN_OPTS_TTY pattern via DOCKER_BUILD) - Add gen-version-env prerequisite to all DOCKER_BUILD targets - Set .DEFAULT_GOAL := ci (matching the old Dapper CMD) - package target remains host-side: ARCH=$(MK_HOST_ARCH) ./scripts/package - Add clean / clean-all targets scripts/version: - Add TOP_DIR + git availability guard with .version_env fallback for container builds and git worktree checkouts - Replace `go env GOHOSTARCH` with `uname -m | sed ...` (host may lack Go) - Write scripts/.version_env after all variables are computed - Move BUILD_FOR_CI early-return after the env file write .dockerignore: - Add .git (stages needing git use git-init trick inside the stage) - Preserve ./.dapper and /.cache entries - Document that /bin is intentionally not excluded (package/Dockerfile uses COPY bin/... with repo root as build context) .gitignore: - Add scripts/.version_env (generated artifact, must not be committed) Delete orchestration scripts (replaced by Makefile targets): - scripts/ci, scripts/default, scripts/entry, scripts/release Signed-off-by: Kiefer Chang <kiefer.chang@suse.com>
1 parent 48ddda1 commit 8bdee92

10 files changed

Lines changed: 204 additions & 89 deletions

File tree

.dockerignore

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,11 @@
1+
# git dir is excluded to avoid passing the entire git object database through
2+
# the build context; stages that need git use git-init inside the stage.
3+
.git
4+
15
./.dapper
2-
./.cache
6+
/.cache
7+
*.swp
8+
.idea/
9+
10+
# /bin is intentionally NOT excluded because `make package` uses the project root
11+
# as docker build context and package/Dockerfile does COPY bin/...

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
/bin
44
*.swp
55
.idea
6+
scripts/.version_env

Dockerfile

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
FROM registry.suse.com/bci/golang:1.26 AS builder
2+
ARG MK_HOST_ARCH
3+
ENV ARCH=$MK_HOST_ARCH
4+
5+
RUN zypper -n rm container-suseconnect && \
6+
zypper -n install git curl docker gzip tar wget awk docker-buildx
7+
8+
COPY --from=golangci/golangci-lint:v2.12.2-alpine@sha256:91b27804074a0bacea298707f016911e60cf0cdbc6c7bf5ccacb5f0606d18d60 /usr/bin/golangci-lint /usr/local/bin/golangci-lint
9+
10+
RUN go install sigs.k8s.io/controller-tools/cmd/controller-gen@v0.17.1
11+
12+
RUN go install k8s.io/code-generator/cmd/openapi-gen@v0.29.13
13+
14+
ENV HOME=/go/src/github.com/harvester/networkfs-manager
15+
16+
# ---- base ----
17+
FROM builder AS base
18+
WORKDIR /go/src/github.com/harvester/networkfs-manager
19+
COPY . .
20+
21+
# ---- build ----
22+
FROM base AS build
23+
ARG MK_REPO_ID
24+
25+
RUN --mount=type=cache,target=/go/pkg/mod,id=networkfs-manager-go-mod-${MK_REPO_ID} \
26+
--mount=type=cache,target=/go/src/github.com/harvester/networkfs-manager/.cache/go-build,id=networkfs-manager-go-build-${MK_REPO_ID} \
27+
./scripts/build
28+
29+
FROM scratch AS build-output
30+
COPY --from=build /go/src/github.com/harvester/networkfs-manager/bin/ /bin/
31+
32+
# ---- validate ----
33+
FROM base AS validate
34+
ARG MK_REPO_ID
35+
36+
RUN --mount=type=cache,target=/go/pkg/mod,id=networkfs-manager-go-mod-${MK_REPO_ID} \
37+
--mount=type=cache,target=/go/src/github.com/harvester/networkfs-manager/.cache/go-build,id=networkfs-manager-go-build-${MK_REPO_ID} \
38+
./scripts/validate
39+
40+
# ---- validate-ci ----
41+
FROM base AS validate-ci
42+
ARG MK_REPO_ID
43+
44+
RUN git config --global user.email "ci@example.com" && \
45+
git config --global user.name "ci" && \
46+
git init 2>/dev/null && git add . && git commit -q -m "commit for validate-ci"
47+
48+
RUN --mount=type=cache,target=/go/pkg/mod,id=networkfs-manager-go-mod-${MK_REPO_ID} \
49+
--mount=type=cache,target=/go/src/github.com/harvester/networkfs-manager/.cache/go-build,id=networkfs-manager-go-build-${MK_REPO_ID} \
50+
./scripts/validate-ci
51+
52+
# ---- test ----
53+
FROM base AS test
54+
ARG MK_REPO_ID
55+
56+
RUN --mount=type=cache,target=/go/pkg/mod,id=networkfs-manager-go-mod-${MK_REPO_ID} \
57+
--mount=type=cache,target=/go/src/github.com/harvester/networkfs-manager/.cache/go-build,id=networkfs-manager-go-build-${MK_REPO_ID} \
58+
./scripts/test
59+
60+
# ---- generate-manifest ----
61+
FROM base AS generate-manifest
62+
63+
RUN ./scripts/generate-manifest
64+
65+
FROM scratch AS generate-manifest-output
66+
COPY --from=generate-manifest /go/src/github.com/harvester/networkfs-manager/manifests/ /manifests/

Dockerfile.dapper

Lines changed: 0 additions & 26 deletions
This file was deleted.

Makefile

Lines changed: 89 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,89 @@
1-
TARGETS := $(shell ls scripts)
2-
3-
SHA512SUM_Linux_aarch64 := 781951b31e5ff018a04e755c6da7163b31a81edda61f1bed4def8d0e24229865c58a3d26aa0cc4184058d91ebcae300ead2cad16d3c46ccb1098419e3e41a016
4-
SHA512SUM_Linux_x86_64 := d2ec27ecf9362e2fafd27d76d85a5c5b92b53aefe07cffa76bf9887db6bee07b1023cca8fc32a2c9bdd2ecfadaee71397066b41bd37c9ebbbbce09913f0884d4
5-
SHA512SUM_Darwin_arm64 := 8a356c89ad32af1698ae8615a6e303773a8ac58b114368454d59965ec2aa8282e780d1e228d37c301ce6f87596f68bfe7f204eb5f4c019c386a58dd94153ddcf
6-
SHA512SUM_Darwin_x86_64 := dbab05de04dda26793f4ae7875d0fba96ee54b0228e192fd40c0b2116ed345b5444047fc2e0c90cb481f28cbe0e0452bcecb268c8d074cd8615eb2f5463c30b6
7-
SHA512SUM_Windows_x86_64 := 807aee2f68b6da35cb0885558f5cbc9a6c8747a56c7a200f0e1fcac9e2fd0da570cbb39e48b3192bd1a71805f2ab38fd19d77faebba97a89e5d9a8b430ee429e
8-
9-
.dapper:
10-
@echo Downloading dapper
11-
@curl -sL https://releases.rancher.com/dapper/v0.6.0/dapper-`uname -s`-`uname -m` > .dapper.tmp
12-
@CHECKSUM=$$(shasum -a 512 .dapper.tmp | awk '{print $$1}'); \
13-
if [ "$$CHECKSUM" != "$(SHA512SUM_$(shell uname -s)_$(shell uname -m))" ]; then \
14-
echo "Checksum verification failed!"; \
15-
exit 1; \
16-
fi
17-
@@chmod +x .dapper.tmp
18-
@./.dapper.tmp -v
19-
@mv .dapper.tmp .dapper
20-
21-
$(TARGETS): .dapper
22-
./.dapper $@
23-
24-
.DEFAULT_GOAL := default
25-
26-
.PHONY: $(TARGETS)
1+
ROOT := $(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))
2+
3+
DOCKER_BUILDKIT := 1
4+
export DOCKER_BUILDKIT
5+
6+
MK_HOST_ARCH ?= $(shell uname -m | sed 's/x86_64/amd64/;s/aarch64/arm64/')
7+
export MK_HOST_ARCH
8+
9+
MK_REPO_ID := $(shell echo -n "$(ROOT)$$(cat /etc/machine-id 2>/dev/null)" | sha256sum | cut -c1-8)
10+
export MK_REPO_ID
11+
12+
MK_DOCKER_PROGRESS ?= plain
13+
export MK_DOCKER_PROGRESS
14+
15+
MK_DOCKER_PULL ?= --pull
16+
export MK_DOCKER_PULL
17+
18+
ifdef CI
19+
BOLD :=
20+
CYAN :=
21+
RESET :=
22+
else
23+
BOLD := \033[1m
24+
CYAN := \033[36m
25+
RESET := \033[0m
26+
endif
27+
28+
BANNER = @printf "$(BOLD)$(CYAN)[target: $@]$(RESET)\n"
29+
30+
DOCKER_BUILD = docker build $(MK_DOCKER_PULL) \
31+
--progress=$(MK_DOCKER_PROGRESS) \
32+
--build-arg MK_REPO_ID \
33+
--build-arg MK_HOST_ARCH \
34+
-f $(ROOT)/Dockerfile $(ROOT)
35+
36+
.DEFAULT_GOAL := ci
37+
38+
.PHONY: ci build test validate validate-ci generate-manifest package gen-version-env clean clean-all
39+
40+
# ---- gen-version-env ----
41+
# Pre-generate version env for container builds (no .git needed inside Docker).
42+
# Also handles git worktree checkouts where .git is a pointer file to an external directory.
43+
gen-version-env:
44+
@bash $(ROOT)/scripts/version > /dev/null
45+
46+
# ---- ci ----
47+
ci: build test validate validate-ci package
48+
49+
# ---- build ----
50+
build: gen-version-env | $(ROOT)/bin
51+
$(BANNER)
52+
$(DOCKER_BUILD) --target build-output \
53+
--output type=local,dest=$(ROOT)
54+
55+
# ---- test ----
56+
test: gen-version-env
57+
$(BANNER)
58+
$(DOCKER_BUILD) --target test
59+
60+
# ---- validate ----
61+
validate: gen-version-env
62+
$(BANNER)
63+
$(DOCKER_BUILD) --target validate
64+
65+
# ---- validate-ci ----
66+
validate-ci: gen-version-env
67+
$(BANNER)
68+
$(DOCKER_BUILD) --target validate-ci
69+
70+
# ---- generate-manifest ----
71+
generate-manifest: gen-version-env
72+
$(BANNER)
73+
$(DOCKER_BUILD) --target generate-manifest-output \
74+
--output type=local,dest=$(ROOT)
75+
76+
# ---- package ----
77+
package: build
78+
$(BANNER)
79+
ARCH=$(MK_HOST_ARCH) $(ROOT)/scripts/package
80+
81+
$(ROOT)/bin:
82+
mkdir -p $@
83+
84+
# ---- clean ----
85+
clean:
86+
@rm -rf $(ROOT)/bin
87+
@rm -f $(ROOT)/scripts/.version_env
88+
89+
clean-all: clean

scripts/ci

Lines changed: 0 additions & 10 deletions
This file was deleted.

scripts/default

Lines changed: 0 additions & 8 deletions
This file was deleted.

scripts/entry

Lines changed: 0 additions & 11 deletions
This file was deleted.

scripts/release

Lines changed: 0 additions & 3 deletions
This file was deleted.

scripts/version

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
#!/bin/bash
22

3+
TOP_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)
4+
_VERSION_ENV="${TOP_DIR}/scripts/.version_env"
5+
6+
# When .git is not available (container build or git worktree checkout where the
7+
# linked git dir is outside the Docker context), fall back to a pre-generated env
8+
# file. Run 'scripts/version' or 'make gen-version-env' on a host with git access.
9+
if ! git -C "${TOP_DIR}" rev-parse HEAD &>/dev/null 2>&1; then
10+
if [[ -f "${_VERSION_ENV}" ]]; then
11+
# shellcheck source=/dev/null
12+
source "${_VERSION_ENV}"
13+
return 0 2>/dev/null || true
14+
fi
15+
echo "ERROR: git is unavailable and no pre-generated scripts/.version_env was found." >&2
16+
echo "Run 'scripts/version' (or 'make gen-version-env') on a host with git access first." >&2
17+
return 1 2>/dev/null || exit 1
18+
fi
19+
320
if [ -n "$(git status --porcelain --untracked-files=no)" ]; then
421
DIRTY="-dirty"
522
fi
@@ -14,18 +31,35 @@ else
1431
fi
1532

1633
if [ -z "$ARCH" ]; then
17-
ARCH=$(go env GOHOSTARCH)
34+
ARCH=$(uname -m | sed 's/x86_64/amd64/;s/aarch64/arm64/')
1835
fi
1936

2037
SUFFIX="-${ARCH}"
2138

2239
TAG=${TAG:-${VERSION}${SUFFIX}}
2340
REPO=${REPO:-harvester}
41+
2442
if [[ -n ${BUILD_FOR_CI} ]]; then
25-
TAG=${COMMIT}
26-
return
43+
TAG=${COMMIT}
2744
fi
2845

29-
if echo $TAG | grep -q dirty; then
46+
if echo "$TAG" | grep -q dirty; then
3047
TAG=dev
3148
fi
49+
50+
cat > "${_VERSION_ENV}" <<__EOF__
51+
# Auto-generated by scripts/version — do not edit manually.
52+
# Refresh by running scripts/version (or 'make gen-version-env') on a host with git access.
53+
DIRTY="${DIRTY}"
54+
COMMIT="${COMMIT}"
55+
GIT_TAG="${GIT_TAG}"
56+
VERSION="${VERSION}"
57+
ARCH="${ARCH}"
58+
SUFFIX="${SUFFIX}"
59+
TAG="${TAG}"
60+
REPO="${REPO}"
61+
__EOF__
62+
63+
if [[ -n ${BUILD_FOR_CI} ]]; then
64+
return 0 2>/dev/null || true
65+
fi

0 commit comments

Comments
 (0)