Skip to content

Commit

Permalink
Use Different Images for Agent and Controller
Browse files Browse the repository at this point in the history
Fixes antrea-io#5691.

Modified the code to build separate images for antrea-agent and
antrea-controller, because there are many resources that are not
required by controller and are required by agent only, and unified
image for both creates a burden when starting antrea-controller and
thus it takes time to start. For this reason I have create separate
images for antrea-agent and antrea-controller.

Signed-off-by: Pulkit Jain <jainpu@vmware.com>
  • Loading branch information
Pulkit Jain committed Dec 13, 2023
1 parent f05c696 commit 8f8bd2c
Show file tree
Hide file tree
Showing 8 changed files with 220 additions and 30 deletions.
20 changes: 20 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,26 @@ else
endif
docker tag antrea/antrea-ubuntu:$(DOCKER_IMG_VERSION) antrea/antrea-ubuntu

.PHONY: build-ubuntu-controller
build-ubuntu-controller:
@echo "===> Building antrea/antrea-ubuntu Docker image <==="
ifneq ($(NO_PULL),)
docker build -t antrea/antrea-controller-ubuntu:$(DOCKER_IMG_VERSION) -f build/images/Dockerfile.build.ubuntu.controller $(DOCKER_BUILD_ARGS) .
else
docker build --pull -t antrea/antrea-controller-ubuntu:$(DOCKER_IMG_VERSION) -f build/images/Dockerfile.build.ubuntu.controller $(DOCKER_BUILD_ARGS) .
endif
docker tag antrea/antrea-controller-ubuntu:$(DOCKER_IMG_VERSION) antrea/antrea-controller-ubuntu

.PHONY: build-ubuntu-agent
build-ubuntu-agent:
@echo "===> Building antrea/antrea-ubuntu Docker image <==="
ifneq ($(NO_PULL),)
docker build -t antrea/antrea-agent-ubuntu:$(DOCKER_IMG_VERSION) -f build/images/Dockerfile.build.ubuntu.agent $(DOCKER_BUILD_ARGS) .
else
docker build --pull -t antrea/antrea-agent-ubuntu:$(DOCKER_IMG_VERSION) -f build/images/Dockerfile.build.ubuntu.agent $(DOCKER_BUILD_ARGS) .
endif
docker tag antrea/antrea-agent-ubuntu:$(DOCKER_IMG_VERSION) antrea/antrea-agent-ubuntu

# Build bins in a golang container, and build the antrea-ubuntu Docker image.
.PHONY: build-ubuntu
build-ubuntu:
Expand Down
2 changes: 1 addition & 1 deletion build/images/Dockerfile.build.ubuntu
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ RUN make antrea-agent antrea-controller antrea-cni
RUN CGO_ENABLED=0 make antctl-linux
RUN mv bin/antctl-linux bin/antctl

FROM antrea/base-ubuntu:${BUILD_TAG}
FROM antrea/unified-base-ubuntu:${BUILD_TAG}

LABEL maintainer="Antrea <projectantrea-dev@googlegroups.com>"
LABEL description="The Docker image to deploy the Antrea CNI."
Expand Down
42 changes: 42 additions & 0 deletions build/images/Dockerfile.build.ubuntu.agent
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Copyright 2023 Antrea Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

ARG GO_VERSION
ARG BUILD_TAG
FROM golang:${GO_VERSION} as antrea-build

WORKDIR /antrea

COPY go.mod /antrea/go.mod

RUN go mod download

COPY . /antrea

RUN make antrea-agent antrea-cni
# Disable CGO for antctl in case it is copied outside of the container image. It
# also reduces the size of the binary and aligns with how we distribute antctl
# in release assets.
RUN CGO_ENABLED=0 make antctl-linux
RUN mv bin/antctl-linux bin/antctl

FROM antrea/agent-base-ubuntu:${BUILD_TAG}

LABEL maintainer="Antrea <projectantrea-dev@googlegroups.com>"
LABEL description="The Docker image to deploy the Antrea CNI."

USER root

COPY build/images/scripts/* /usr/local/bin/
COPY --from=antrea-build /antrea/bin/* /usr/local/bin/
42 changes: 42 additions & 0 deletions build/images/Dockerfile.build.ubuntu.controller
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Copyright 2023 Antrea Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

ARG GO_VERSION
ARG BUILD_TAG
FROM golang:${GO_VERSION} as antrea-build

WORKDIR /antrea

COPY go.mod /antrea/go.mod

RUN go mod download

COPY . /antrea

RUN make antrea-controller
# Disable CGO for antctl in case it is copied outside of the container image. It
# also reduces the size of the binary and aligns with how we distribute antctl
# in release assets.
RUN CGO_ENABLED=0 make antctl-linux
RUN mv bin/antctl-linux bin/antctl

FROM antrea/controller-base-ubuntu:${BUILD_TAG}

LABEL maintainer="Antrea <projectantrea-dev@googlegroups.com>"
LABEL description="The Docker image to deploy the Antrea CNI."

USER root

COPY build/images/scripts/* /usr/local/bin/
COPY --from=antrea-build /antrea/bin/* /usr/local/bin/
29 changes: 29 additions & 0 deletions build/images/base/Dockerfile.controller
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Copyright 2023 Antrea Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

ARG BUILD_TAG
FROM ubuntu:22.04 as cni-binaries

RUN apt-get update && \
apt-get install -y --no-install-recommends wget ca-certificates

# Download containernetworking plugin binaries for the correct architecture
RUN set -eux; \
dpkgArch="$(dpkg --print-architecture)"; \
case "${dpkgArch##*-}" in \
amd64) pluginsArch='amd64' ;; \
armhf) pluginsArch='arm' ;; \
arm64) pluginsArch='arm64' ;; \
*) pluginsArch=''; echo >&2; echo >&2 "unsupported architecture '$dpkgArch'"; echo >&2 ; exit 1 ;; \
esac;
98 changes: 74 additions & 24 deletions build/images/base/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ Build the antrea base image.
--pull Always attempt to pull a newer version of the base images
--push Push the built image to the registry
--platform <PLATFORM> Target platform for the image if server is multi-platform capable
--distro <distro> Target Linux distribution"
--distro <distro> Target Linux distribution
--type Type of image, whether it is for agent, ontroller or unified"

function print_usage {
echoerr "$_usage"
Expand All @@ -38,6 +39,7 @@ PULL=false
PUSH=false
PLATFORM=""
DISTRO="ubuntu"
TYPE="unified"

while [[ $# -gt 0 ]]
do
Expand All @@ -60,6 +62,10 @@ case $key in
DISTRO="$2"
shift 2
;;
--type)
TYPE="$2"
shift 2
;;
-h|--help)
print_usage
exit 0
Expand Down Expand Up @@ -129,29 +135,73 @@ if $PULL; then
done
fi

docker build $PLATFORM_ARG --target cni-binaries \
--cache-from antrea/cni-binaries:$CNI_BINARIES_VERSION \
-t antrea/cni-binaries:$CNI_BINARIES_VERSION \
--build-arg CNI_BINARIES_VERSION=$CNI_BINARIES_VERSION \
--build-arg BUILD_TAG=$BUILD_TAG .

if [ "$DISTRO" == "ubuntu" ]; then
docker build $PLATFORM_ARG \
--cache-from antrea/cni-binaries:$CNI_BINARIES_VERSION \
--cache-from antrea/base-ubuntu:$BUILD_TAG \
-t antrea/base-ubuntu:$BUILD_TAG \
--build-arg CNI_BINARIES_VERSION=$CNI_BINARIES_VERSION \
--build-arg SURICATA_VERSION=$SURICATA_VERSION \
--build-arg BUILD_TAG=$BUILD_TAG .
elif [ "$DISTRO" == "ubi" ]; then
docker build $PLATFORM_ARG \
--cache-from antrea/cni-binaries:$CNI_BINARIES_VERSION \
--cache-from antrea/base-ubi:$BUILD_TAG \
-t antrea/base-ubi:$BUILD_TAG \
-f Dockerfile.ubi \
--build-arg CNI_BINARIES_VERSION=$CNI_BINARIES_VERSION \
--build-arg SURICATA_VERSION=$SURICATA_VERSION \
--build-arg BUILD_TAG=$BUILD_TAG .
if [ "$TYPE" == "unified" ]; then
docker build $PLATFORM_ARG --target cni-binaries \
--cache-from antrea/cni-binaries:$CNI_BINARIES_VERSION \
-t antrea/cni-binaries:$CNI_BINARIES_VERSION \
--build-arg CNI_BINARIES_VERSION=$CNI_BINARIES_VERSION \
--build-arg BUILD_TAG=$BUILD_TAG .

if [ "$DISTRO" == "ubuntu" ]; then
docker build $PLATFORM_ARG \
--cache-from antrea/cni-binaries:$CNI_BINARIES_VERSION \
--cache-from antrea/base-ubuntu:$BUILD_TAG \
-t antrea/unified-base-ubuntu:$BUILD_TAG \
--build-arg CNI_BINARIES_VERSION=$CNI_BINARIES_VERSION \
--build-arg SURICATA_VERSION=$SURICATA_VERSION \
--build-arg BUILD_TAG=$BUILD_TAG .
elif [ "$DISTRO" == "ubi" ]; then
docker build $PLATFORM_ARG \
--cache-from antrea/cni-binaries:$CNI_BINARIES_VERSION \
--cache-from antrea/base-ubi:$BUILD_TAG \
-t antrea/base-ubi:$BUILD_TAG \
-f Dockerfile.ubi \
--build-arg CNI_BINARIES_VERSION=$CNI_BINARIES_VERSION \
--build-arg SURICATA_VERSION=$SURICATA_VERSION \
--build-arg BUILD_TAG=$BUILD_TAG .
fi
elif [ "$TYPE" == "agent" ]; then
docker build $PLATFORM_ARG --target cni-binaries \
--cache-from antrea/cni-binaries:$CNI_BINARIES_VERSION \
-t antrea/cni-binaries:$CNI_BINARIES_VERSION \
--build-arg CNI_BINARIES_VERSION=$CNI_BINARIES_VERSION \
--build-arg BUILD_TAG=$BUILD_TAG .

if [ "$DISTRO" == "ubuntu" ]; then
docker build $PLATFORM_ARG \
--cache-from antrea/cni-binaries:$CNI_BINARIES_VERSION \
--cache-from antrea/base-ubuntu:$BUILD_TAG \
-t antrea/agent-base-ubuntu:$BUILD_TAG \
--build-arg CNI_BINARIES_VERSION=$CNI_BINARIES_VERSION \
--build-arg SURICATA_VERSION=$SURICATA_VERSION \
--build-arg BUILD_TAG=$BUILD_TAG .
elif [ "$DISTRO" == "ubi" ]; then
docker build $PLATFORM_ARG \
--cache-from antrea/cni-binaries:$CNI_BINARIES_VERSION \
--cache-from antrea/base-ubi:$BUILD_TAG \
-t antrea/base-ubi:$BUILD_TAG \
-f Dockerfile.ubi \
--build-arg CNI_BINARIES_VERSION=$CNI_BINARIES_VERSION \
--build-arg SURICATA_VERSION=$SURICATA_VERSION \
--build-arg BUILD_TAG=$BUILD_TAG .
fi
else
if [ "$DISTRO" == "ubuntu" ]; then
docker build $PLATFORM_ARG \
--cache-from antrea/base-ubuntu:$BUILD_TAG \
-t antrea/controller-base-ubuntu:$BUILD_TAG \
-f Dockerfile.controller \
--build-arg BUILD_TAG=$BUILD_TAG .
elif [ "$DISTRO" == "ubi" ]; then
docker build $PLATFORM_ARG \
--cache-from antrea/cni-binaries:$CNI_BINARIES_VERSION \
--cache-from antrea/base-ubi:$BUILD_TAG \
-t antrea/base-ubi:$BUILD_TAG \
-f Dockerfile.ubi \
--build-arg CNI_BINARIES_VERSION=$CNI_BINARIES_VERSION \
--build-arg SURICATA_VERSION=$SURICATA_VERSION \
--build-arg BUILD_TAG=$BUILD_TAG .
fi
fi

if $PUSH; then
Expand Down
8 changes: 4 additions & 4 deletions build/yamls/antrea.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6887,7 +6887,7 @@ spec:
serviceAccountName: antrea-agent
initContainers:
- name: install-cni
image: "antrea/antrea-ubuntu:latest"
image: "antrea/antrea-agent-ubuntu:latest"
imagePullPolicy: IfNotPresent
resources:
requests:
Expand Down Expand Up @@ -6920,7 +6920,7 @@ spec:
mountPath: /var/run/antrea
containers:
- name: antrea-agent
image: "antrea/antrea-ubuntu:latest"
image: "antrea/antrea-agent-ubuntu:latest"
imagePullPolicy: IfNotPresent
command: ["antrea-agent"]
# Log to both "/var/log/antrea/" and stderr (so "kubectl logs" can work).-
Expand Down Expand Up @@ -7011,7 +7011,7 @@ spec:
- name: xtables-lock
mountPath: /run/xtables.lock
- name: antrea-ovs
image: "antrea/antrea-ubuntu:latest"
image: "antrea/antrea-agent-ubuntu:latest"
imagePullPolicy: IfNotPresent
resources:
requests:
Expand Down Expand Up @@ -7127,7 +7127,7 @@ spec:
serviceAccountName: antrea-controller
containers:
- name: antrea-controller
image: "antrea/antrea-ubuntu:latest"
image: "antrea/antrea-controller-ubuntu:latest"
imagePullPolicy: IfNotPresent
resources:
requests:
Expand Down
9 changes: 8 additions & 1 deletion hack/build-antrea-linux-all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,21 @@ cd build/images/ovs
cd -

cd build/images/base
./build.sh $ARGS
ARGS1="$ARGS --type controller"
ARGS2="$ARGS --type agent"
ARGS3="$ARGS --type unified"
./build.sh $ARGS1
./build.sh $ARGS2
./build.sh $ARGS3
cd -

export NO_PULL=1
if [ "$DISTRO" == "ubuntu" ]; then
if $COVERAGE; then
make build-ubuntu-coverage
else
make build-ubuntu-controller
make build-ubuntu-agent
make
fi
elif [ "$DISTRO" == "ubi" ]; then
Expand Down

0 comments on commit 8f8bd2c

Please sign in to comment.