Skip to content

Commit 6a4f404

Browse files
committed
Enable support for multiple platforms in the Linux Bridge CNI.
These updates allow the building and pushing of Linux Bridge container images for multiple platforms (e.g., amd64, s390x) using a single Dockerfile. Multi-platform build support is provided for both Docker and Podman container runtimes. Signed-off-by: Ashok Pariya <[email protected]>
1 parent 302ad3e commit 6a4f404

File tree

1 file changed

+74
-8
lines changed

1 file changed

+74
-8
lines changed

hack/components/bump-linux-bridge.sh

Lines changed: 74 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,23 @@ echo 'Build container image with linux-bridge binaries'
2727
LINUX_BRIDGE_TAR_CONTAINER_DIR=/usr/src/github.com/containernetworking/plugins/bin
2828
LINUX_BRIDGE_IMAGE=quay.io/kubevirt/cni-default-plugins
2929
LINUX_BRIDGE_IMAGE_TAGGED=${LINUX_BRIDGE_IMAGE}:${LINUX_BRIDGE_TAG}
30-
(
31-
cd ${LINUX_BRIDGE_PATH}
30+
DOCKER_BUILDER="${DOCKER_BUILDER:-linux-bridge-docker-builder}"
31+
# By default, the build will be based on the host architecture.
32+
# To build for other platforms, you can:
33+
# 1. Export all supported platforms: export PLATFORMS=all
34+
# 2. Or specify specific platforms: export PLATFORMS=linux/amd64,linux/arm64
35+
PLATFORM_LIST="linux/amd64,linux/s390x,linux/arm64"
36+
ARCH=$(uname -m | sed 's/x86_64/amd64/;s/aarch64/arm64/')
37+
[ -z "$PLATFORMS" ] && PLATFORMS="linux/${ARCH}"
38+
[ "$PLATFORMS" == "all" ] && PLATFORMS="${PLATFORM_LIST}"
39+
IFS=',' read -r -a PLATFORM_LIST <<< "$PLATFORMS"
40+
41+
create_dockerfile() {
3242
cat <<EOF > Dockerfile
43+
ARG BUILD_ARCH=amd64
3344
FROM registry.access.redhat.com/ubi8/ubi-minimal AS builder
45+
ARG TARGETOS
46+
ARG TARGETARCH
3447
RUN microdnf install -y golang git
3548
RUN \
3649
git clone https://${LINUX_BRIDGE_REPO} ${LINUX_BRIDGE_PATH} && \
@@ -49,14 +62,67 @@ COPY --from=builder ${LINUX_BRIDGE_PATH}/bin/tuning ${LINUX_BRIDGE_TAR_CONTAINER
4962
RUN sha256sum ${LINUX_BRIDGE_TAR_CONTAINER_DIR}/bridge >${LINUX_BRIDGE_TAR_CONTAINER_DIR}/bridge.checksum
5063
RUN sha256sum ${LINUX_BRIDGE_TAR_CONTAINER_DIR}/tuning >${LINUX_BRIDGE_TAR_CONTAINER_DIR}/tuning.checksum
5164
EOF
52-
${OCI_BIN} build -t ${LINUX_BRIDGE_IMAGE_TAGGED} .
53-
)
65+
}
5466

55-
echo 'Push the image to KubeVirt repo'
56-
(
57-
if [ ! -z ${PUSH_IMAGES} ]; then
58-
${OCI_BIN} push "${LINUX_BRIDGE_IMAGE_TAGGED}"
67+
check_and_create_docker_builder() {
68+
existing_builder=$(docker buildx ls | grep -w "$DOCKER_BUILDER" | awk '{print $1}' || true)
69+
if [ -n "$existing_builder" ]; then
70+
echo "Builder '$DOCKER_BUILDER' already exists. Using existing builder."
71+
docker buildx use "$DOCKER_BUILDER"
72+
else
73+
echo "Creating a new Docker Buildx builder: $DOCKER_BUILDER"
74+
docker buildx create --driver-opt network=host --use --name "$DOCKER_BUILDER"
75+
fi
76+
}
77+
78+
build_docker_image() {
79+
docker buildx build --platform "${PLATFORMS}" -t "${LINUX_BRIDGE_IMAGE_TAGGED}" . --push
80+
docker buildx rm "$DOCKER_BUILDER"
81+
}
82+
83+
build_podman_image() {
84+
podman manifest rm "${LINUX_BRIDGE_IMAGE_TAGGED}" 2>/dev/null || true
85+
podman rmi "${LINUX_BRIDGE_IMAGE_TAGGED}" 2>/dev/null || true
86+
podman manifest create "${LINUX_BRIDGE_IMAGE_TAGGED}"
87+
88+
for platform in "${PLATFORM_LIST[@]}"; do
89+
podman build --platform "$platform" --manifest "${LINUX_BRIDGE_IMAGE_TAGGED}" .
90+
done
91+
}
92+
93+
push_image_to_kubevirt_repo() {
94+
echo 'Push the image to KubeVirt repo'
95+
if [ "${OCI_BIN}" == "podman" ]; then
96+
if [ ! -z "${PUSH_IMAGES}" ]; then
97+
podman manifest push "${LINUX_BRIDGE_IMAGE_TAGGED}"
98+
fi
5999
fi
100+
}
101+
102+
modify_dockerfile_for_platform_and_architecture() {
103+
local dockerfile="$1"
104+
# Modify Dockerfile to set platform and architecture
105+
sed -i 's|^FROM registry.access.redhat.com/ubi8/ubi-minimal AS builder$|FROM --platform=$BUILDPLATFORM registry.access.redhat.com/ubi8/ubi-minimal AS builder|' "$dockerfile"
106+
sed -i 's|RUN GOFLAGS=-mod=vendor ./build_linux.sh|RUN GOFLAGS=-mod=vendor GOARCH=${TARGETARCH} GOOS=${TARGETOS} ./build_linux.sh|' "$dockerfile"
107+
sed -i 's/^FROM registry.access.redhat.com\/ubi8\/ubi-minimal$/FROM --platform=linux\/${TARGETARCH} registry.access.redhat.com\/ubi8\/ubi-minimal AS final/' "$dockerfile"
108+
}
109+
110+
(
111+
cd ${LINUX_BRIDGE_PATH}
112+
create_dockerfile
113+
modify_dockerfile_for_platform_and_architecture "Dockerfile"
114+
(
115+
if [[ "${OCI_BIN}" == "docker" ]]; then
116+
check_and_create_docker_builder
117+
build_docker_image
118+
elif [[ "${OCI_BIN}" == "podman" ]]; then
119+
build_podman_image
120+
push_image_to_kubevirt_repo
121+
else
122+
echo "Invalid OCI_BIN value. It must be either 'docker' or 'podman'."
123+
exit 1
124+
fi
125+
)
60126
)
61127

62128
if [[ -n "$(docker-utils::check_image_exists "${LINUX_BRIDGE_IMAGE}" "${LINUX_BRIDGE_TAG}")" ]]; then

0 commit comments

Comments
 (0)