Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add initial support for multi architecture builds #65

Merged
merged 8 commits into from
Jul 26, 2024
12 changes: 8 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
FROM golang:1.22-alpine AS builder
FROM --platform=${BUILDPLATFORM:-linux/amd64} golang:1.22-alpine AS builder

ARG BUILDPLATFORM
ARG TARGETOS
ARG TARGETARCH

RUN apk --no-cache add ca-certificates make
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN make build

RUN make build TARGETOS=${TARGETOS} TARGETARCH=${TARGETARCH}

FROM scratch
FROM --platform=${BUILDPLATFORM:-linux/amd64} scratch
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
WORKDIR /root/
COPY --from=builder /app/bin .
Expand Down
71 changes: 58 additions & 13 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,39 +1,86 @@
VERSION ?= v0.0.1
REGISTRY ?= ghcr.io
# Variables that are used in `defined<>endef` blocks need to be exported
export VERSION ?= v0.0.1
export REGISTRY ?= ghcr.io
IMAGE_BUILDER ?= docker
IMAGE_BUILD_CMD ?= build
IMAGE_NAME ?= woehrl01/pod-pacemaker
IMAGE_BUILD_CMD ?= buildx build
export IMAGE_NAME ?= woehrl01/pod-pacemaker

export IMG = $(REGISTRY)/$(IMAGE_NAME):$(VERSION)
export CGO_ENABLED=0
export GOOS=linux

.PHONY: proto
.ONESHELL:

define kind_load =
# The architecture to load depends on the current environment (gitlab runner/workstation)
arch=$(uname -m)
case ${arch} in
x86_64)
kind load docker-image ${IMG}-amd64
;;
aarch64)
kind load docker-image ${IMG}-arm64
;;
*)
echo "Architecture ${arch} not supported"
exit 1
;;
esac
endef

define kind_deploy =
# The architecture to load depends on the current environment (gitlab runner/workstation)
arch=$(uname -m)
case ${arch} in
x86_64)
tag=${VERSION}-amd64
;;
aarch64)
tag=${VERSION}-arm64
;;
*)
echo "Architecture ${arch} not supported"
exit 1
;;
esac
helm upgrade --install pod-pacemaker charts/pod-pacemaker \
--set image.repository=${REGISTRY}/${IMAGE_NAME} \
--set image.tag=${tag} \
--set debugLogging=true \
--set defaultThrottleConfig.config.maxConcurrent.value=1
endef

.SILENT: kind-deploy kind-load

proto:
protoc --go_out=. --go_opt=paths=source_relative \
--go-grpc_out=. --go-grpc_opt=paths=source_relative \
proto/pod_limiter.proto

cni:
cd cmd/cni-plugin && go build -o ../../bin/cni-plugin
cd cmd/cni-plugin && CGO_ENABLED=${CGO_ENABLED} GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -o ../../bin/cni-plugin

make-init:
cd cmd/init && go build -o ../../bin/cni-init
cd cmd/init && CGO_ENABLED=${CGO_ENABLED} GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -o ../../bin/cni-init

daemonset:
cd cmd/node-daemon && go build -o ../../bin/node-daemon
cd cmd/node-daemon && CGO_ENABLED=${CGO_ENABLED} GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -o ../../bin/node-daemon

build: cni make-init daemonset

clean:
rm -rf bin/*

docker-build:
$(IMAGE_BUILDER) $(IMAGE_BUILD_CMD) -t $(IMG) .
# Build each supported architecture and load the resulting image into docker for use with kind
docker buildx create --use
$(IMAGE_BUILDER) $(IMAGE_BUILD_CMD) --load --platform linux/amd64 --tag $(IMG)-amd64 .
$(IMAGE_BUILDER) $(IMAGE_BUILD_CMD) --load --platform linux/arm64/v8 --tag $(IMG)-arm64 .

docker-push:
$(IMAGE_BUILDER) push $(IMG)
# Build (again) for all supported architectures and pushlish the result
$(IMAGE_BUILDER) $(IMAGE_BUILD_CMD) --push --platform linux/arm64/v8,linux/amd64 --tag $(IMG) .

helm-render:
helm template charts/pod-pacemaker --set image.repository=$(REGISTRY)/$(IMAGE_NAME) --set image.tag=$(VERSION)
Expand All @@ -45,9 +92,7 @@ helm-push:
helm package charts/pod-pacemaker --app-version $(VERSION) --version $(VERSION)
helm push pod-pacemaker-*.tgz oci://ghcr.io/woehrl01/pod-pacemaker

kind-load:
kind load docker-image $(IMG)
kind-load: ; $(value kind_load)

kind-deploy:
helm install pod-pacemaker charts/pod-pacemaker --set image.repository=$(REGISTRY)/$(IMAGE_NAME) --set image.tag=$(VERSION) --set debugLogging=true --set defaultThrottleConfig.config.maxConcurrent.value=1
kind-deploy: ; $(value kind_deploy)