Skip to content

Commit 8f57b9d

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 8f57b9d

File tree

1 file changed

+65
-6
lines changed

1 file changed

+65
-6
lines changed

hack/components/bump-linux-bridge.sh

Lines changed: 65 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,17 @@ 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+
ARCH=$(uname -m | sed 's/x86_64/amd64/')
31+
PLATFORMS="linux/amd64,linux/s390x"
32+
DOCKER_BUILDER="${DOCKER_BUILDER:-linux-bridge-docker-builder}"
33+
IFS=',' read -r -a PLATFORM_LIST <<< "$PLATFORMS"
34+
35+
create_dockerfile() {
3236
cat <<EOF > Dockerfile
37+
ARG BUILD_ARCH=amd64
3338
FROM registry.access.redhat.com/ubi8/ubi-minimal AS builder
39+
ARG TARGETOS
40+
ARG TARGETARCH
3441
RUN microdnf install -y golang git
3542
RUN \
3643
git clone https://${LINUX_BRIDGE_REPO} ${LINUX_BRIDGE_PATH} && \
@@ -39,7 +46,7 @@ RUN \
3946
WORKDIR ${LINUX_BRIDGE_PATH}
4047
RUN GOFLAGS=-mod=vendor ./build_linux.sh
4148
42-
FROM registry.access.redhat.com/ubi8/ubi-minimal
49+
FROM registry.access.redhat.com/ubi8/ubi-minimal AS final
4350
LABEL org.opencontainers.image.authors="[email protected]"
4451
ENV SOURCE_DIR=${REMOTE_SOURCE_DIR}/app
4552
RUN mkdir -p ${LINUX_BRIDGE_TAR_CONTAINER_DIR}
@@ -49,13 +56,65 @@ COPY --from=builder ${LINUX_BRIDGE_PATH}/bin/tuning ${LINUX_BRIDGE_TAR_CONTAINER
4956
RUN sha256sum ${LINUX_BRIDGE_TAR_CONTAINER_DIR}/bridge >${LINUX_BRIDGE_TAR_CONTAINER_DIR}/bridge.checksum
5057
RUN sha256sum ${LINUX_BRIDGE_TAR_CONTAINER_DIR}/tuning >${LINUX_BRIDGE_TAR_CONTAINER_DIR}/tuning.checksum
5158
EOF
52-
${OCI_BIN} build -t ${LINUX_BRIDGE_IMAGE_TAGGED} .
59+
}
60+
61+
check_and_create_docker_builder() {
62+
existing_builder=$(docker buildx ls | grep -w "$DOCKER_BUILDER" | awk '{print $1}' || true)
63+
if [ -n "$existing_builder" ]; then
64+
echo "Builder '$DOCKER_BUILDER' already exists. Using existing builder."
65+
docker buildx use "$DOCKER_BUILDER"
66+
else
67+
echo "Creating a new Docker Buildx builder: $DOCKER_BUILDER"
68+
docker buildx create --driver-opt network=host --use --name "$DOCKER_BUILDER"
69+
fi
70+
}
71+
72+
build_docker_image() {
73+
docker buildx build --platform "${PLATFORMS}" --build-arg BUILD_ARCH="$ARCH" -t "${LINUX_BRIDGE_IMAGE_TAGGED}" . --push
74+
docker buildx rm "$DOCKER_BUILDER"
75+
}
76+
77+
build_podman_image() {
78+
podman manifest rm "${LINUX_BRIDGE_IMAGE_TAGGED}" || true
79+
podman rmi "${LINUX_BRIDGE_IMAGE_TAGGED}" || true
80+
podman manifest create "${LINUX_BRIDGE_IMAGE_TAGGED}"
81+
82+
for platform in "${PLATFORM_LIST[@]}"; do
83+
podman build --no-cache --build-arg BUILD_ARCH="$ARCH" --platform "$platform" --manifest "${LINUX_BRIDGE_IMAGE_TAGGED}" .
84+
done
85+
}
86+
87+
modify_dockerfile_for_platform_and_architecture() {
88+
local dockerfile="$1"
89+
# Modify Dockerfile to set platform and architecture
90+
sed -i 's|^FROM registry.access.redhat.com/ubi8/ubi-minimal AS builder$|FROM --platform=linux/${BUILD_ARCH} registry.access.redhat.com/ubi8/ubi-minimal AS builder|' "$dockerfile"
91+
sed -i 's|RUN GOFLAGS=-mod=vendor ./build_linux.sh|RUN GOFLAGS=-mod=vendor GOARCH=${TARGETARCH} GOOS=${TARGETOS} ./build_linux.sh|' "$dockerfile"
92+
sed -i 's/^FROM registry.access.redhat.com\/ubi8\/ubi-minimal AS final$/FROM --platform=linux\/${TARGETARCH} registry.access.redhat.com\/ubi8\/ubi-minimal AS final/' "$dockerfile"
93+
}
94+
95+
(
96+
cd ${LINUX_BRIDGE_PATH}
97+
create_dockerfile
98+
modify_dockerfile_for_platform_and_architecture "Dockerfile"
99+
(
100+
if [[ "${OCI_BIN}" == "docker" ]]; then
101+
check_and_create_docker_builder
102+
build_docker_image
103+
elif [[ "${OCI_BIN}" == "podman" ]]; then
104+
build_podman_image
105+
else
106+
echo "Invalid OCI_BIN value. It must be either 'docker' or 'podman'."
107+
exit 1
108+
fi
109+
)
53110
)
54111

55112
echo 'Push the image to KubeVirt repo'
56113
(
57-
if [ ! -z ${PUSH_IMAGES} ]; then
58-
${OCI_BIN} push "${LINUX_BRIDGE_IMAGE_TAGGED}"
114+
if [ "${OCI_BIN}" == "podman" ]; then
115+
if [ ! -z ${PUSH_IMAGES} ]; then
116+
podman manifest push "${LINUX_BRIDGE_IMAGE_TAGGED}"
117+
fi
59118
fi
60119
)
61120

0 commit comments

Comments
 (0)