Skip to content

Commit

Permalink
Enable support for multiple platforms in the Linux Bridge CNI.
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
ashokpariya0 committed Nov 28, 2024
1 parent 302ad3e commit 8f57b9d
Showing 1 changed file with 65 additions and 6 deletions.
71 changes: 65 additions & 6 deletions hack/components/bump-linux-bridge.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,17 @@ echo 'Build container image with linux-bridge binaries'
LINUX_BRIDGE_TAR_CONTAINER_DIR=/usr/src/github.com/containernetworking/plugins/bin
LINUX_BRIDGE_IMAGE=quay.io/kubevirt/cni-default-plugins
LINUX_BRIDGE_IMAGE_TAGGED=${LINUX_BRIDGE_IMAGE}:${LINUX_BRIDGE_TAG}
(
cd ${LINUX_BRIDGE_PATH}
ARCH=$(uname -m | sed 's/x86_64/amd64/')
PLATFORMS="linux/amd64,linux/s390x"
DOCKER_BUILDER="${DOCKER_BUILDER:-linux-bridge-docker-builder}"
IFS=',' read -r -a PLATFORM_LIST <<< "$PLATFORMS"

create_dockerfile() {
cat <<EOF > Dockerfile
ARG BUILD_ARCH=amd64
FROM registry.access.redhat.com/ubi8/ubi-minimal AS builder
ARG TARGETOS
ARG TARGETARCH
RUN microdnf install -y golang git
RUN \
git clone https://${LINUX_BRIDGE_REPO} ${LINUX_BRIDGE_PATH} && \
Expand All @@ -39,7 +46,7 @@ RUN \
WORKDIR ${LINUX_BRIDGE_PATH}
RUN GOFLAGS=-mod=vendor ./build_linux.sh
FROM registry.access.redhat.com/ubi8/ubi-minimal
FROM registry.access.redhat.com/ubi8/ubi-minimal AS final
LABEL org.opencontainers.image.authors="[email protected]"
ENV SOURCE_DIR=${REMOTE_SOURCE_DIR}/app
RUN mkdir -p ${LINUX_BRIDGE_TAR_CONTAINER_DIR}
Expand All @@ -49,13 +56,65 @@ COPY --from=builder ${LINUX_BRIDGE_PATH}/bin/tuning ${LINUX_BRIDGE_TAR_CONTAINER
RUN sha256sum ${LINUX_BRIDGE_TAR_CONTAINER_DIR}/bridge >${LINUX_BRIDGE_TAR_CONTAINER_DIR}/bridge.checksum
RUN sha256sum ${LINUX_BRIDGE_TAR_CONTAINER_DIR}/tuning >${LINUX_BRIDGE_TAR_CONTAINER_DIR}/tuning.checksum
EOF
${OCI_BIN} build -t ${LINUX_BRIDGE_IMAGE_TAGGED} .
}

check_and_create_docker_builder() {
existing_builder=$(docker buildx ls | grep -w "$DOCKER_BUILDER" | awk '{print $1}' || true)
if [ -n "$existing_builder" ]; then
echo "Builder '$DOCKER_BUILDER' already exists. Using existing builder."
docker buildx use "$DOCKER_BUILDER"
else
echo "Creating a new Docker Buildx builder: $DOCKER_BUILDER"
docker buildx create --driver-opt network=host --use --name "$DOCKER_BUILDER"
fi
}

build_docker_image() {
docker buildx build --platform "${PLATFORMS}" --build-arg BUILD_ARCH="$ARCH" -t "${LINUX_BRIDGE_IMAGE_TAGGED}" . --push
docker buildx rm "$DOCKER_BUILDER"
}

build_podman_image() {
podman manifest rm "${LINUX_BRIDGE_IMAGE_TAGGED}" || true
podman rmi "${LINUX_BRIDGE_IMAGE_TAGGED}" || true
podman manifest create "${LINUX_BRIDGE_IMAGE_TAGGED}"

for platform in "${PLATFORM_LIST[@]}"; do
podman build --no-cache --build-arg BUILD_ARCH="$ARCH" --platform "$platform" --manifest "${LINUX_BRIDGE_IMAGE_TAGGED}" .
done
}

modify_dockerfile_for_platform_and_architecture() {
local dockerfile="$1"
# Modify Dockerfile to set platform and architecture
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"
sed -i 's|RUN GOFLAGS=-mod=vendor ./build_linux.sh|RUN GOFLAGS=-mod=vendor GOARCH=${TARGETARCH} GOOS=${TARGETOS} ./build_linux.sh|' "$dockerfile"
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"
}

(
cd ${LINUX_BRIDGE_PATH}
create_dockerfile
modify_dockerfile_for_platform_and_architecture "Dockerfile"
(
if [[ "${OCI_BIN}" == "docker" ]]; then
check_and_create_docker_builder
build_docker_image
elif [[ "${OCI_BIN}" == "podman" ]]; then
build_podman_image
else
echo "Invalid OCI_BIN value. It must be either 'docker' or 'podman'."
exit 1
fi
)
)

echo 'Push the image to KubeVirt repo'
(
if [ ! -z ${PUSH_IMAGES} ]; then
${OCI_BIN} push "${LINUX_BRIDGE_IMAGE_TAGGED}"
if [ "${OCI_BIN}" == "podman" ]; then
if [ ! -z ${PUSH_IMAGES} ]; then
podman manifest push "${LINUX_BRIDGE_IMAGE_TAGGED}"
fi
fi
)

Expand Down

0 comments on commit 8f57b9d

Please sign in to comment.