Skip to content

Commit 3454f57

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 3454f57

File tree

1 file changed

+68
-11
lines changed

1 file changed

+68
-11
lines changed

hack/components/bump-linux-bridge.sh

Lines changed: 68 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,31 @@ 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+
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
33-
FROM registry.access.redhat.com/ubi8/ubi-minimal AS builder
43+
FROM --platform=\$BUILDPLATFORM registry.access.redhat.com/ubi8/ubi-minimal AS builder
44+
ARG TARGETOS
45+
ARG TARGETARCH
3446
RUN microdnf install -y golang git
3547
RUN \
3648
git clone https://${LINUX_BRIDGE_REPO} ${LINUX_BRIDGE_PATH} && \
3749
cd ${LINUX_BRIDGE_PATH} && \
3850
git checkout ${LINUX_BRIDGE_TAG}
3951
WORKDIR ${LINUX_BRIDGE_PATH}
40-
RUN GOFLAGS=-mod=vendor ./build_linux.sh
52+
RUN GOFLAGS=-mod=vendor GOARCH=\${TARGETARCH} GOOS=\${TARGETOS} ./build_linux.sh
4153
42-
FROM registry.access.redhat.com/ubi8/ubi-minimal
54+
FROM --platform=linux/\$TARGETARCH registry.access.redhat.com/ubi8/ubi-minimal
4355
LABEL org.opencontainers.image.authors="[email protected]"
4456
ENV SOURCE_DIR=${REMOTE_SOURCE_DIR}/app
4557
RUN mkdir -p ${LINUX_BRIDGE_TAR_CONTAINER_DIR}
@@ -49,14 +61,59 @@ COPY --from=builder ${LINUX_BRIDGE_PATH}/bin/tuning ${LINUX_BRIDGE_TAR_CONTAINER
4961
RUN sha256sum ${LINUX_BRIDGE_TAR_CONTAINER_DIR}/bridge >${LINUX_BRIDGE_TAR_CONTAINER_DIR}/bridge.checksum
5062
RUN sha256sum ${LINUX_BRIDGE_TAR_CONTAINER_DIR}/tuning >${LINUX_BRIDGE_TAR_CONTAINER_DIR}/tuning.checksum
5163
EOF
52-
${OCI_BIN} build -t ${LINUX_BRIDGE_IMAGE_TAGGED} .
53-
)
64+
}
5465

55-
echo 'Push the image to KubeVirt repo'
56-
(
57-
if [ ! -z ${PUSH_IMAGES} ]; then
58-
${OCI_BIN} push "${LINUX_BRIDGE_IMAGE_TAGGED}"
66+
check_and_create_docker_builder() {
67+
existing_builder=$(docker buildx ls | grep -w "$DOCKER_BUILDER" | awk '{print $1}' || true)
68+
if [ -n "$existing_builder" ]; then
69+
echo "Builder '$DOCKER_BUILDER' already exists. Using existing builder."
70+
docker buildx use "$DOCKER_BUILDER"
71+
else
72+
echo "Creating a new Docker Buildx builder: $DOCKER_BUILDER"
73+
docker buildx create --driver-opt network=host --use --name "$DOCKER_BUILDER"
74+
fi
75+
}
76+
77+
build_docker_image() {
78+
docker buildx build --platform "${PLATFORMS}" -t "${LINUX_BRIDGE_IMAGE_TAGGED}" . --push
79+
docker buildx rm "$DOCKER_BUILDER"
80+
}
81+
82+
build_podman_image() {
83+
podman manifest rm "${LINUX_BRIDGE_IMAGE_TAGGED}" 2>/dev/null || true
84+
podman rmi "${LINUX_BRIDGE_IMAGE_TAGGED}" 2>/dev/null || true
85+
podman manifest create "${LINUX_BRIDGE_IMAGE_TAGGED}"
86+
87+
for platform in "${PLATFORM_LIST[@]}"; do
88+
podman build --platform "$platform" --manifest "${LINUX_BRIDGE_IMAGE_TAGGED}" .
89+
done
90+
}
91+
92+
push_image_to_kubevirt_repo() {
93+
echo 'Push the image to KubeVirt repo'
94+
if [ "${OCI_BIN}" == "podman" ]; then
95+
if [ ! -z "${PUSH_IMAGES}" ]; then
96+
podman manifest push "${LINUX_BRIDGE_IMAGE_TAGGED}"
97+
fi
5998
fi
99+
}
100+
101+
102+
(
103+
cd ${LINUX_BRIDGE_PATH}
104+
create_dockerfile
105+
(
106+
if [[ "${OCI_BIN}" == "docker" ]]; then
107+
check_and_create_docker_builder
108+
build_docker_image
109+
elif [[ "${OCI_BIN}" == "podman" ]]; then
110+
build_podman_image
111+
push_image_to_kubevirt_repo
112+
else
113+
echo "Invalid OCI_BIN value. It must be either 'docker' or 'podman'."
114+
exit 1
115+
fi
116+
)
60117
)
61118

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

0 commit comments

Comments
 (0)