diff --git a/Makefile b/Makefile index 01e5c474345..9ae52104bec 100644 --- a/Makefile +++ b/Makefile @@ -52,6 +52,9 @@ endif ifneq ($(NO_CACHE),) DOCKER_BUILD_ARGS += --no-cache endif +ifneq ($(DOCKER_TARGETPLATFORM),) + DOCKER_BUILD_ARGS += --platform $(DOCKER_TARGETPLATFORM) +endif DOCKER_BUILD_ARGS += --build-arg OVS_VERSION=$(OVS_VERSION) DOCKER_BUILD_ARGS += --build-arg GO_VERSION=$(GO_VERSION) DOCKER_BUILD_ARGS += --build-arg BUILD_TAG=$(BUILD_TAG) diff --git a/build/images/.gitignore b/build/images/.gitignore new file mode 100644 index 00000000000..398eaf9522d --- /dev/null +++ b/build/images/.gitignore @@ -0,0 +1 @@ +.targetarch diff --git a/build/images/Dockerfile.agent.ubuntu b/build/images/Dockerfile.agent.ubuntu index 1b4bf4b0dcb..b345772488e 100644 --- a/build/images/Dockerfile.agent.ubuntu +++ b/build/images/Dockerfile.agent.ubuntu @@ -13,7 +13,7 @@ # limitations under the License. ARG BUILD_TAG -FROM antrea/base-ubuntu:${BUILD_TAG} +FROM antrea/base-ubuntu-${TARGETPLATFORM}:${BUILD_TAG} LABEL maintainer="Antrea " LABEL description="The development Docker image to deploy the antrea-agent." diff --git a/build/images/Dockerfile.build.agent.coverage b/build/images/Dockerfile.build.agent.coverage index 9a1d3735035..d77d12cece7 100644 --- a/build/images/Dockerfile.build.agent.coverage +++ b/build/images/Dockerfile.build.agent.coverage @@ -28,7 +28,7 @@ RUN make antctl-instr-binary RUN make antrea-cni antrea-agent-instr-binary -FROM antrea/base-ubuntu:${BUILD_TAG} +FROM antrea/base-ubuntu-${TARGETARCH}:${BUILD_TAG} LABEL maintainer="Antrea " LABEL description="The Docker image to deploy the antrea-agent with code coverage measurement enabled (used for testing)." diff --git a/build/images/Dockerfile.build.agent.ubi b/build/images/Dockerfile.build.agent.ubi index 85360c94e43..2da71bf4fa8 100644 --- a/build/images/Dockerfile.build.agent.ubi +++ b/build/images/Dockerfile.build.agent.ubi @@ -33,7 +33,7 @@ RUN --mount=type=cache,target=/go/pkg/mod/ \ --mount=type=cache,target=/root/.cache/go-build/ \ make antrea-agent antrea-cni -FROM antrea/base-ubi:${BUILD_TAG} +FROM antrea/base-ubi-${TARGETARCH}:${BUILD_TAG} LABEL maintainer="Antrea " LABEL description="The Docker image to deploy the antrea-agent." diff --git a/build/images/Dockerfile.build.agent.ubuntu b/build/images/Dockerfile.build.agent.ubuntu index bebe826f818..ebe8ec0e231 100644 --- a/build/images/Dockerfile.build.agent.ubuntu +++ b/build/images/Dockerfile.build.agent.ubuntu @@ -33,7 +33,7 @@ RUN --mount=type=cache,target=/go/pkg/mod/ \ --mount=type=cache,target=/root/.cache/go-build/ \ make antrea-agent antrea-cni -FROM antrea/base-ubuntu:${BUILD_TAG} +FROM antrea/base-ubuntu-${TARGETARCH}:${BUILD_TAG} LABEL maintainer="Antrea " LABEL description="The Docker image to deploy the antrea-agent." diff --git a/build/images/Dockerfile.build.controller.coverage b/build/images/Dockerfile.build.controller.coverage index 9980548ac81..b90f8e3cd22 100644 --- a/build/images/Dockerfile.build.controller.coverage +++ b/build/images/Dockerfile.build.controller.coverage @@ -13,7 +13,6 @@ # limitations under the License. ARG GO_VERSION -ARG BUILD_TAG FROM golang:${GO_VERSION} AS antrea-build WORKDIR /antrea diff --git a/build/images/Dockerfile.build.controller.ubi b/build/images/Dockerfile.build.controller.ubi index 73694bbc570..8d73b5e4e30 100644 --- a/build/images/Dockerfile.build.controller.ubi +++ b/build/images/Dockerfile.build.controller.ubi @@ -13,7 +13,6 @@ # limitations under the License. ARG GO_VERSION -ARG BUILD_TAG FROM golang:${GO_VERSION} AS antrea-build WORKDIR /antrea diff --git a/build/images/Dockerfile.build.controller.ubuntu b/build/images/Dockerfile.build.controller.ubuntu index 92be5b0bdf7..b46e430950d 100644 --- a/build/images/Dockerfile.build.controller.ubuntu +++ b/build/images/Dockerfile.build.controller.ubuntu @@ -13,7 +13,6 @@ # limitations under the License. ARG GO_VERSION -ARG BUILD_TAG FROM golang:${GO_VERSION} AS antrea-build WORKDIR /antrea diff --git a/build/images/base/Dockerfile b/build/images/base/Dockerfile index 8221325e9a2..e197c03acda 100644 --- a/build/images/base/Dockerfile +++ b/build/images/base/Dockerfile @@ -12,7 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -ARG BUILD_TAG FROM ubuntu:24.04 AS cni-binaries ARG CNI_BINARIES_VERSION @@ -35,7 +34,7 @@ RUN set -eux; \ mkdir -p /opt/cni/bin; \ wget -q -O - https://github.com/containernetworking/plugins/releases/download/$CNI_BINARIES_VERSION/cni-plugins-linux-${pluginsArch}-$CNI_BINARIES_VERSION.tgz | tar xz -C /opt/cni/bin $CNI_PLUGINS -FROM antrea/openvswitch:${BUILD_TAG} +FROM antrea-openvswitch ARG SURICATA_VERSION diff --git a/build/images/base/Dockerfile.ubi b/build/images/base/Dockerfile.ubi index c6134bc911c..c69f343139c 100644 --- a/build/images/base/Dockerfile.ubi +++ b/build/images/base/Dockerfile.ubi @@ -12,7 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -ARG BUILD_TAG FROM ubuntu:24.04 AS cni-binaries ARG CNI_BINARIES_VERSION @@ -35,7 +34,7 @@ RUN set -eux; \ mkdir -p /opt/cni/bin; \ wget -q -O - https://github.com/containernetworking/plugins/releases/download/$CNI_BINARIES_VERSION/cni-plugins-linux-${pluginsArch}-$CNI_BINARIES_VERSION.tgz | tar xz -C /opt/cni/bin $CNI_PLUGINS -FROM antrea/openvswitch-ubi:${BUILD_TAG} +FROM antrea-openvswitch ARG SURICATA_VERSION diff --git a/build/images/base/build.sh b/build/images/base/build.sh index f5355e25b3c..96c0515592e 100755 --- a/build/images/base/build.sh +++ b/build/images/base/build.sh @@ -92,10 +92,8 @@ if $PUSH && ! check_docker_build_driver "docker-container"; then exit 1 fi -if [ "$PLATFORM" != "" ] && $PUSH; then - echoerr "Cannot use --platform with --push" - exit 1 -fi +TARGETARCH=$(set -e; get_target_arch "$PLATFORM" "$THIS_DIR/../.targetarch") +echo "Target arch: $TARGETARCH" PLATFORM_ARG="" if [ "$PLATFORM" != "" ]; then @@ -118,24 +116,23 @@ if [[ $BUILD_TAG == "" ]]; then BUILD_TAG=$BUILD_CACHE_TAG fi +ANTREA_OPENVSWITCH_IMAGE="" +if [ "$DISTRO" == "ubuntu" ]; then + ANTREA_OPENVSWITCH_IMAGE="antrea/openvswitch-$TARGETARCH:$BUILD_TAG" +elif [ "$DISTRO" == "ubi" ]; then + ANTREA_OPENVSWITCH_IMAGE="antrea/openvswitch-ubi-$TARGETARCH:$BUILD_TAG" +fi + if $PULL; then # The ubuntu image is also used for the UBI build (for the cni-binaries intermediate image). if [[ ${DOCKER_REGISTRY} == "" ]]; then docker pull $PLATFORM_ARG ubuntu:24.04 else - docker pull ${DOCKER_REGISTRY}/antrea/ubuntu:24.04 + docker pull $PLATFORM_ARG ${DOCKER_REGISTRY}/antrea/ubuntu:24.04 docker tag ${DOCKER_REGISTRY}/antrea/ubuntu:24.04 ubuntu:24.04 fi - if [ "$DISTRO" == "ubuntu" ]; then - IMAGES_LIST=( - "antrea/openvswitch:$BUILD_TAG" - ) - elif [ "$DISTRO" == "ubi" ]; then - IMAGES_LIST=( - "antrea/openvswitch-ubi:$BUILD_TAG" - ) - fi + IMAGES_LIST=("$ANTREA_OPENVSWITCH_IMAGE") for image in "${IMAGES_LIST[@]}"; do if [[ ${DOCKER_REGISTRY} == "" ]]; then docker pull $PLATFORM_ARG "${image}" || true @@ -152,7 +149,8 @@ fi function docker_build_and_push() { local image="$1" local dockerfile="$2" - local build_args="--build-arg CNI_BINARIES_VERSION=$CNI_BINARIES_VERSION --build-arg SURICATA_VERSION=$SURICATA_VERSION --build-arg BUILD_TAG=$BUILD_TAG" + local build_args="--build-arg CNI_BINARIES_VERSION=$CNI_BINARIES_VERSION --build-arg SURICATA_VERSION=$SURICATA_VERSION" + local build_context="--build-context antrea-openvswitch=docker-image://$ANTREA_OPENVSWITCH_IMAGE" local cache_args="" if $PUSH; then cache_args="$cache_args --cache-to type=registry,ref=$image-cache:$BUILD_CACHE_TAG,mode=max" @@ -162,7 +160,7 @@ function docker_build_and_push() { else cache_args="$cache_args --cache-from type=registry,ref=$image-cache:$BUILD_CACHE_TAG,mode=max" fi - docker buildx build $PLATFORM_ARG -o type=docker -t $image:$BUILD_TAG $cache_args $build_args -f $dockerfile . + docker buildx build $PLATFORM_ARG -o type=docker -t $image:$BUILD_TAG $cache_args $build_args $build_context -f $dockerfile . if $PUSH; then docker push $image:$BUILD_TAG @@ -171,9 +169,9 @@ function docker_build_and_push() { if [ "$DISTRO" == "ubuntu" ]; then - docker_build_and_push "antrea/base-ubuntu" Dockerfile + docker_build_and_push "antrea/base-ubuntu-$TARGETARCH" Dockerfile elif [ "$DISTRO" == "ubi" ]; then - docker_build_and_push "antrea/base-ubi" Dockerfile.ubi + docker_build_and_push "antrea/base-ubi-$TARGETARCH" Dockerfile.ubi fi popd > /dev/null diff --git a/build/images/build-utils.sh b/build/images/build-utils.sh index 414211debbc..0ad6359cba8 100644 --- a/build/images/build-utils.sh +++ b/build/images/build-utils.sh @@ -77,3 +77,24 @@ function docker_build_and_push_windows() { docker buildx build --platform windows/amd64 -o ${output} -t ${image}:${build_tag} ${pull_option} ${build_args} -f $dockerfile . } + +function get_target_arch() { + local platform="$1" + local output_cache_path="$2" + local arch="" + if [ -n "$output_cache_path" ] && [ -f "$output_cache_path" ]; then + arch=$(head -n 1 "$output_cache_path") + fi + if [ -z "$arch" ]; then + local platform_arg="" + if [ -n "$platform" ]; then + platform_arg="--platform $platform" + fi + echo "FROM scratch" | docker buildx build $platform_arg -t antrea-test-arch - + arch=$(docker inspect antrea-test-arch --format '{{ .Architecture }}') + if [ -n "$output_cache_path" ]; then + echo "$arch" > "$output_cache_path" + fi + fi + echo "$arch" +} diff --git a/build/images/ovs/build.sh b/build/images/ovs/build.sh index a66dcb80ad2..1ad137e454a 100755 --- a/build/images/ovs/build.sh +++ b/build/images/ovs/build.sh @@ -92,16 +92,14 @@ if $PUSH && [ "$DISTRO" != "windows" ] && ! check_docker_build_driver "docker-co exit 1 fi -if [ "$PLATFORM" != "" ] && $PUSH; then - echoerr "Cannot use --platform with --push" - exit 1 -fi - if [ "$DISTRO" != "ubuntu" ] && [ "$DISTRO" != "ubi" ] && [ "$DISTRO" != "windows" ]; then echoerr "Invalid distribution $DISTRO" exit 1 fi +TARGETARCH=$(set -e; get_target_arch "$PLATFORM" "$THIS_DIR/../.targetarch") +echo "Target arch: $TARGETARCH" + OVS_VERSION_FILE="../deps/ovs-version" if [ "$DISTRO" == "windows" ]; then OVS_VERSION_FILE="../deps/ovs-version-windows" @@ -127,7 +125,7 @@ if $PULL; then if [[ ${DOCKER_REGISTRY} == "" ]]; then docker pull $PLATFORM_ARG ubuntu:24.04 else - docker pull ${DOCKER_REGISTRY}/antrea/ubuntu:24.04 + docker pull $PLATFORM_ARG ${DOCKER_REGISTRY}/antrea/ubuntu:24.04 docker tag ${DOCKER_REGISTRY}/antrea/ubuntu:24.04 ubuntu:24.04 fi elif [ "$DISTRO" == "ubi" ]; then @@ -159,9 +157,9 @@ function docker_build_and_push() { } if [ "$DISTRO" == "ubuntu" ]; then - docker_build_and_push "antrea/openvswitch" "Dockerfile" + docker_build_and_push "antrea/openvswitch-$TARGETARCH" "Dockerfile" elif [ "$DISTRO" == "ubi" ]; then - docker_build_and_push "antrea/openvswitch-ubi" "Dockerfile.ubi" + docker_build_and_push "antrea/openvswitch-ubi-$TARGETARCH" "Dockerfile.ubi" elif [ "$DISTRO" == "windows" ]; then image="antrea/windows-ovs" build_args="--build-arg OVS_VERSION=$OVS_VERSION" diff --git a/build/images/test/Dockerfile b/build/images/test/Dockerfile index 4b4ae0899a0..104dc7b2a7d 100644 --- a/build/images/test/Dockerfile +++ b/build/images/test/Dockerfile @@ -13,7 +13,7 @@ # limitations under the License. ARG BUILD_TAG -FROM antrea/openvswitch:${BUILD_TAG} +FROM antrea/openvswitch-$TARGETARCH:$BUILD_TAG LABEL maintainer="Antrea " LABEL description="A Docker image for Antrea integration tests." diff --git a/hack/build-antrea-linux-all.sh b/hack/build-antrea-linux-all.sh index be5176ceca6..b6eb72fc508 100755 --- a/hack/build-antrea-linux-all.sh +++ b/hack/build-antrea-linux-all.sh @@ -132,17 +132,15 @@ if [ "$BUILD_TAG" != "" ]; then fi # We pull all images ahead of time, instead of calling the independent build.sh -# scripts with "--pull". We do not want to overwrite the antrea/openvswitch -# image we just built when calling build.sh to build the antrea/base-ubuntu -# image! +# scripts with "--pull". if $PULL; then if [[ ${DOCKER_REGISTRY} == "" ]]; then docker pull $PLATFORM_ARG ubuntu:24.04 docker pull $PLATFORM_ARG golang:$GO_VERSION else - docker pull ${DOCKER_REGISTRY}/antrea/ubuntu:24.04 + docker pull $PLATFORM_ARG ${DOCKER_REGISTRY}/antrea/ubuntu:24.04 docker tag ${DOCKER_REGISTRY}/antrea/ubuntu:24.04 ubuntu:24.04 - docker pull ${DOCKER_REGISTRY}/antrea/golang:$GO_VERSION + docker pull $PLATFORM_ARG ${DOCKER_REGISTRY}/antrea/golang:$GO_VERSION docker tag ${DOCKER_REGISTRY}/antrea/golang:$GO_VERSION golang:$GO_VERSION fi if [ "$DISTRO" == "ubi" ]; then @@ -169,6 +167,9 @@ export NO_PULL=1 # explicitly (note that we already set DOCKER_CLI_EXPERIMENTAL=enabled at the # beginning of the script). export DOCKER_BUILDKIT=1 +if [ "$PLATFORM" != "" ]; then + export DOCKER_TARGETPLATFORM="$PLATFORM" +fi if [ "$DISTRO" == "ubuntu" ]; then if $COVERAGE; then make build-controller-ubuntu-coverage