diff --git a/README.md b/README.md index 4c736c5..da98710 100644 --- a/README.md +++ b/README.md @@ -55,8 +55,6 @@ Prerequisites * make * Docker * curl (downloading dependency packages, kubelet, crio, etc) -* go (building clitools, control-agent) - * controller-gen (see [clitools readme](clitools/README.md)) * git (cloning uboot repo because uboot does not provide direct downloads) ``` diff --git a/clitools/docker/crdgen.Dockerfile b/clitools/docker/crdgen.Dockerfile new file mode 100644 index 0000000..f546d2b --- /dev/null +++ b/clitools/docker/crdgen.Dockerfile @@ -0,0 +1,14 @@ +ARG BASE_IMAGE=localhost/monok8s/ctl-build-base:dev +FROM ${BASE_IMAGE} AS build + +WORKDIR /src + +RUN GOBIN=/usr/local/bin go install sigs.k8s.io/controller-tools/cmd/controller-gen@v0.20.1 + +COPY . . + +RUN mkdir -p /out && \ + controller-gen crd paths=./pkg/apis/... output:crd:dir=/out + +FROM scratch +COPY --from=build /out/ / diff --git a/clitools/docker/ctl-build-base.Dockerfile b/clitools/docker/ctl-build-base.Dockerfile new file mode 100644 index 0000000..e07dc41 --- /dev/null +++ b/clitools/docker/ctl-build-base.Dockerfile @@ -0,0 +1,8 @@ +FROM golang:1.26-alpine + +WORKDIR /src + +RUN apk add --no-cache git build-base + +COPY go.mod go.sum ./ +RUN go mod download diff --git a/clitools/docker/ctl-builder-local.Dockerfile b/clitools/docker/ctl-builder-local.Dockerfile new file mode 100644 index 0000000..e0be383 --- /dev/null +++ b/clitools/docker/ctl-builder-local.Dockerfile @@ -0,0 +1,24 @@ +FROM golang:1.26-alpine AS build + +ARG VERSION +ARG KUBE_VERSION +ARG GIT_REV=unknown + +WORKDIR /src + +RUN apk add --no-cache git build-base + +COPY go.mod go.sum ./ +RUN go mod download + +COPY . . + +RUN test -f pkg/buildinfo/buildinfo_gen.go + +RUN mkdir -p /out && \ + GOOS=darwin GOARCH=arm64 CGO_ENABLED=0 \ + go build -trimpath -ldflags="-s -w" \ + -o /out/ctl-${VERSION} ./cmd/ctl + +FROM scratch +COPY --from=build /out/ / diff --git a/clitools/docker/ctl-builder.Dockerfile b/clitools/docker/ctl-builder.Dockerfile new file mode 100644 index 0000000..3e171c6 --- /dev/null +++ b/clitools/docker/ctl-builder.Dockerfile @@ -0,0 +1,20 @@ +ARG BASE_IMAGE=localhost/monok8s/ctl-build-base:dev +FROM ${BASE_IMAGE} AS build + +ARG VERSION=dev +ARG TARGETOS=linux +ARG TARGETARCH=arm64 + +WORKDIR /src + +COPY . . + +RUN test -f pkg/buildinfo/buildinfo_gen.go + +RUN mkdir -p /out && \ + GOOS=${TARGETOS} GOARCH=${TARGETARCH} CGO_ENABLED=0 \ + go build -trimpath -ldflags="-s -w" \ + -o /out/ctl-linux-aarch64-${VERSION} ./cmd/ctl + +FROM scratch +COPY --from=build /out/ / diff --git a/clitools/go.mod b/clitools/go.mod index aec328a..97076d2 100644 --- a/clitools/go.mod +++ b/clitools/go.mod @@ -1,6 +1,6 @@ module example.com/monok8s -go 1.24.0 +go 1.26.0 require ( github.com/klauspost/compress v1.18.5 diff --git a/clitools/makefile b/clitools/makefile index 05a5f52..149edf2 100644 --- a/clitools/makefile +++ b/clitools/makefile @@ -1,8 +1,10 @@ include ../build.env +BUILD_PLATFORM ?= linux/amd64 + # Should be the same as upstream version in production VERSION ?= dev -UBOOT_VERSION=v2026.01 +UBOOT_VERSION ?= v2026.01 # Target kube version KUBE_VERSION ?= v1.33.3 @@ -13,15 +15,28 @@ PACKAGES_DIR := packages BIN_DIR := bin OUT_DIR := out -UBOOT_TAR := $(PACKAGES_DIR)/uboot-$(UBOOT_VERSION).tar.gz - +UBOOT_TAR := $(PACKAGES_DIR)/uboot-$(UBOOT_VERSION).tar.gz BUILDINFO_FILE := pkg/buildinfo/buildinfo_gen.go -CRD_PATHS := ./pkg/apis/... +CRD_PATHS := ./pkg/apis/... + + +BUILDX_BUILDER := container-builder +LOCAL_REGISTRY := registry +LOCAL_REGISTRY_PORT := 5000 + +CTL_BUILD_BASE_IMAGE := localhost:5000/monok8s/ctl-build-base:$(VERSION) +CTL_BINARY := ctl-linux-aarch64-$(VERSION) $(PACKAGES_DIR): mkdir -p $@ -# Never cache this +$(BIN_DIR): + mkdir -p $@ + +$(OUT_DIR): + mkdir -p $@ + +# Keep buildinfo host-side since it is just generated source and lets Docker see it in build context. .buildinfo: @mkdir -p $(dir $(BUILDINFO_FILE)) @printf '%s\n' \ @@ -35,11 +50,41 @@ $(PACKAGES_DIR): ')' \ > $(BUILDINFO_FILE) -$(UBOOT_TAR): | $(PACKAGES_DIR) - git clone --depth 1 --branch v2026.01 --filter=blob:none https://github.com/u-boot/u-boot.git $(OUT_DIR)/u-boot-$(UBOOT_VERSION) +ensure-buildx: + @if ! docker buildx inspect $(BUILDX_BUILDER) >/dev/null 2>&1; then \ + echo "Creating buildx builder $(BUILDX_BUILDER)..."; \ + docker buildx create \ + --name $(BUILDX_BUILDER) \ + --driver docker-container \ + --driver-opt network=host \ + --bootstrap --use; \ + else \ + echo "Using existing buildx builder $(BUILDX_BUILDER)"; \ + docker buildx use $(BUILDX_BUILDER); \ + fi + +ensure-registry: + @if ! docker container inspect $(LOCAL_REGISTRY) >/dev/null 2>&1; then \ + echo "Creating local registry..."; \ + docker run -d \ + --restart=always \ + -p $(LOCAL_REGISTRY_PORT):5000 \ + --name $(LOCAL_REGISTRY) \ + registry:2; \ + else \ + if [ "$$(docker inspect -f '{{.State.Running}}' $(LOCAL_REGISTRY))" != "true" ]; then \ + echo "Starting existing local registry..."; \ + docker start $(LOCAL_REGISTRY); \ + fi; \ + fi + +$(UBOOT_TAR): | $(PACKAGES_DIR) $(OUT_DIR) + rm -rf "$(OUT_DIR)/u-boot-$(UBOOT_VERSION)" + git clone --depth 1 --branch "$(UBOOT_VERSION)" --filter=blob:none \ + https://github.com/u-boot/u-boot.git "$(OUT_DIR)/u-boot-$(UBOOT_VERSION)" tar -C "$(OUT_DIR)/u-boot-$(UBOOT_VERSION)" -zcf "$@" . - rm -rf $(OUT_DIR)/u-boot-$(UBOOT_VERSION) - test -f $@ + rm -rf "$(OUT_DIR)/u-boot-$(UBOOT_VERSION)" + test -f "$@" uboot-tools: $(UBOOT_TAR) docker buildx build --platform linux/arm64 \ @@ -48,40 +93,78 @@ uboot-tools: $(UBOOT_TAR) --build-arg UBOOT_TAR=$(UBOOT_TAR) \ --output type=local,dest=./$(OUT_DIR) . -build: .buildinfo - mkdir -p $(BIN_DIR) $(OUT_DIR)/crds - controller-gen crd paths=$(CRD_PATHS) output:crd:dir=$(OUT_DIR)/crds - GOOS=linux GOARCH=arm64 go build -o $(BIN_DIR)/ctl-linux-aarch64-$(VERSION) ./cmd/ctl +ctl-build-base: ensure-buildx ensure-registry + docker buildx build \ + --platform linux/amd64,linux/arm64 \ + -f docker/ctl-build-base.Dockerfile \ + -t $(CTL_BUILD_BASE_IMAGE) \ + --output type=image,push=true,registry.insecure=true . + +build-bin: ctl-build-base | $(BIN_DIR) + docker buildx build \ + --platform $(BUILD_PLATFORM) \ + -f docker/ctl-builder.Dockerfile \ + --build-arg BASE_IMAGE=$(CTL_BUILD_BASE_IMAGE) \ + --build-arg VERSION=$(VERSION) \ + --build-arg TARGETOS=linux \ + --build-arg TARGETARCH=arm64 \ + --output type=local,dest=./$(BIN_DIR) . + +build-crds: ctl-build-base | $(OUT_DIR) + mkdir -p "$(OUT_DIR)/crds" + docker buildx build \ + --platform $(BUILD_PLATFORM) \ + -f docker/crdgen.Dockerfile \ + --build-arg BASE_IMAGE=$(CTL_BUILD_BASE_IMAGE) \ + --output type=local,dest=./$(OUT_DIR)/crds . build-agent: build uboot-tools - docker build \ + docker buildx build \ + --platform linux/arm64 \ -f docker/ctl-agent.Dockerfile \ - --platform=linux/arm64 \ --build-arg VERSION=$(VERSION) \ + --load \ -t localhost/monok8s/control-agent:$(VERSION) . -build-local: .buildinfo - mkdir -p $(BIN_DIR) - go build -o $(BIN_DIR)/ctl-$(VERSION) ./cmd/ctl +build-local: .buildinfo | $(BIN_DIR) + docker buildx build \ + -f docker/ctl-builder-local.Dockerfile \ + --build-arg VERSION=$(VERSION) \ + --build-arg KUBE_VERSION=$(KUBE_VERSION) \ + --build-arg GIT_REV=$(GIT_REV) \ + --output type=local,dest=./$(BIN_DIR) . run-agent: - go run -tags dev ./cmd/ctl agent --env-file ./out/cluster.env + docker run --rm \ + -v "$$(pwd)/out:/work/out" \ + localhost/monok8s/control-agent:$(VERSION) \ + agent --env-file /work/out/cluster.env + +build: build-bin build-crds clean: - -docker image rm localhost/monok8s/control-agent:$(VERSION) - rm -rf $(BIN_DIR) \ - $(BUILDINFO_FILE) \ - $(OUT_DIR)/crds + -docker image rm localhost/monok8s/control-agent:$(VERSION) >/dev/null 2>&1 || true + rm -rf \ + $(BIN_DIR) \ + $(OUT_DIR)/crds \ + $(BUILDINFO_FILE) dockerclean: @echo "Removing tagged images..." - docker rmi \ localhost/monok8s/control-agent:$(VERSION) \ + localhost/monok8s/ctl-builder:$(VERSION) \ + localhost/monok8s/crdgen:$(VERSION) \ 2>/dev/null || true - @echo "Removing dangling images..." + @echo "Removing dangling build cache/images..." - docker image prune -f + - docker builder prune -f all: build build-agent build-local -.PHONY: clean all run .buildinfo build build-local build-agent uboot-tools +.PHONY: \ + all clean dockerclean \ + .buildinfo ensure-buildx ensure-registry \ + build build-bin build-crds build-local build-agent \ + uboot-tools run-agent