Skip to content

Commit

Permalink
Disable cgo for all Antrea binaries
Browse files Browse the repository at this point in the history
Instead of selectively disabling cgo for some binaries (e.g., release
assets), we now unconditionally disable cgo for all binaries, even those
that only run inside the container image for which they were built
(e.g., antrea-controller). After some analysis, there seems to be no
downside in doing this. We also get some benefits such as reduced build
time for the default make command.

Fixes antrea-io#5724

Signed-off-by: Antonin Bas <antonin.bas@broadcom.com>
  • Loading branch information
antoninbas committed Feb 14, 2024
1 parent 1ba2342 commit 54cec86
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 53 deletions.
20 changes: 11 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ SHELL := /bin/bash
GO ?= go
LDFLAGS :=
GOFLAGS :=
# By default, disable cgo for all Go binaries.
# For binaries meant to be published as release assets or copied to a different host, cgo should
# always be disabled.
CGO_ENABLED ?= 0
BINDIR ?= $(CURDIR)/bin
GO_FILES := $(shell find . -type d -name '.cache' -prune -o -type f -name '*.go' -print)
GOPATH ?= $$($(GO) env GOPATH)
Expand Down Expand Up @@ -33,6 +37,8 @@ WIN_BUILD_ARGS += --build-arg NANOSERVER_VERSION=$(NANOSERVER_VERSION)
WIN_BUILD_ARGS += --build-arg WIN_BUILD_TAG=$(WIN_BUILD_TAG)
WIN_BUILD_ARGS += --build-arg WIN_BUILD_OVS_TAG=$(WIN_BUILD_OVS_TAG)

export CGO_ENABLED

.PHONY: all
all: build

Expand Down Expand Up @@ -78,7 +84,7 @@ antrea-agent:
.PHONY: antrea-agent-release
antrea-agent-release:
@mkdir -p $(BINDIR)
@CGO_ENABLED=0 $(GO) build -o $(BINDIR)/$(ANTREA_AGENT_BINARY_NAME) $(GOFLAGS) -ldflags '$(LDFLAGS)' antrea.io/antrea/cmd/antrea-agent
$(GO) build -o $(BINDIR)/$(ANTREA_AGENT_BINARY_NAME) $(GOFLAGS) -ldflags '$(LDFLAGS)' antrea.io/antrea/cmd/antrea-agent

.PHONY: antrea-agent-simulator
antrea-agent-simulator:
Expand All @@ -104,29 +110,25 @@ antrea-controller-instr-binary:
@mkdir -p $(BINDIR)
GOOS=linux $(GO) test -tags testbincover -covermode count -coverpkg=antrea.io/antrea/pkg/... -c -o $(BINDIR)/antrea-controller-coverage $(GOFLAGS) -ldflags '$(LDFLAGS)' antrea.io/antrea/cmd/antrea-controller

# diable cgo for antrea-cni since it can be installed on some systems with
# incompatible or missing system libraries.
.PHONY: antrea-cni
antrea-cni:
@mkdir -p $(BINDIR)
GOOS=linux CGO_ENABLED=0 $(GO) build -o $(BINDIR) $(GOFLAGS) -ldflags '$(LDFLAGS)' antrea.io/antrea/cmd/antrea-cni
GOOS=linux $(GO) build -o $(BINDIR) $(GOFLAGS) -ldflags '$(LDFLAGS)' antrea.io/antrea/cmd/antrea-cni

.PHONY: antrea-cni
antrea-cni-release:
@mkdir -p $(BINDIR)
@CGO_ENABLED=0 $(GO) build -o $(BINDIR)/$(ANTREA_CNI_BINARY_NAME) $(GOFLAGS) -ldflags '$(LDFLAGS)' antrea.io/antrea/cmd/antrea-cni
$(GO) build -o $(BINDIR)/$(ANTREA_CNI_BINARY_NAME) $(GOFLAGS) -ldflags '$(LDFLAGS)' antrea.io/antrea/cmd/antrea-cni

.PHONY: antctl-instr-binary
antctl-instr-binary:
@mkdir -p $(BINDIR)
GOOS=linux $(GO) test -tags testbincover -covermode count -coverpkg=antrea.io/antrea/pkg/... -c -o $(BINDIR)/antctl-coverage $(GOFLAGS) -ldflags '$(LDFLAGS)' antrea.io/antrea/cmd/antctl

# diable cgo for antrea-cni and antrea-agent: antrea-cni is meant to be
# installed on the host and the antrea-agent is run as a process on Windows.
.PHONY: windows-bin
windows-bin:
@mkdir -p $(BINDIR)
GOOS=windows CGO_ENABLED=0 $(GO) build -o $(BINDIR) $(GOFLAGS) -ldflags '$(LDFLAGS)' antrea.io/antrea/cmd/antrea-cni antrea.io/antrea/cmd/antrea-agent antrea.io/antrea/cmd/antctl
GOOS=windows $(GO) build -o $(BINDIR) $(GOFLAGS) -ldflags '$(LDFLAGS)' antrea.io/antrea/cmd/antrea-cni antrea.io/antrea/cmd/antrea-agent antrea.io/antrea/cmd/antctl

.PHONY: flow-aggregator
flow-aggregator:
Expand Down Expand Up @@ -235,7 +237,7 @@ antctl: $(ANTCTL_BINARIES)

.PHONY: antctl-release
antctl-release:
@CGO_ENABLED=0 $(GO) build -o $(BINDIR)/$(ANTCTL_BINARY_NAME) $(GOFLAGS) -ldflags '-s -w $(LDFLAGS)' antrea.io/antrea/cmd/antctl
$(GO) build -o $(BINDIR)/$(ANTCTL_BINARY_NAME) $(GOFLAGS) -ldflags '-s -w $(LDFLAGS)' antrea.io/antrea/cmd/antctl

.PHONY: check-copyright
check-copyright:
Expand Down
6 changes: 1 addition & 5 deletions build/images/Dockerfile.build.agent.coverage
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,7 @@ RUN go mod download

COPY . /antrea

# 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 antctl-instr-binary
RUN mv bin/antctl-linux bin/antctl
RUN make antctl-linux antctl-instr-binary && mv bin/antctl-linux bin/antctl

RUN make antrea-agent antrea-cni antrea-agent-instr-binary

Expand Down
5 changes: 1 addition & 4 deletions build/images/Dockerfile.build.agent.ubi
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,9 @@ RUN --mount=type=cache,target=/go/pkg/mod/ \

COPY . /antrea

# 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 --mount=type=cache,target=/go/pkg/mod/ \
--mount=type=cache,target=/root/.cache/go-build/ \
CGO_ENABLED=0 make antctl-linux && mv bin/antctl-linux bin/antctl
make antctl-linux && mv bin/antctl-linux bin/antctl

RUN --mount=type=cache,target=/go/pkg/mod/ \
--mount=type=cache,target=/root/.cache/go-build/ \
Expand Down
5 changes: 1 addition & 4 deletions build/images/Dockerfile.build.agent.ubuntu
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,9 @@ RUN --mount=type=cache,target=/go/pkg/mod/ \

COPY . /antrea

# 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 --mount=type=cache,target=/go/pkg/mod/ \
--mount=type=cache,target=/root/.cache/go-build/ \
CGO_ENABLED=0 make antctl-linux && mv bin/antctl-linux bin/antctl
make antctl-linux && mv bin/antctl-linux bin/antctl

RUN --mount=type=cache,target=/go/pkg/mod/ \
--mount=type=cache,target=/root/.cache/go-build/ \
Expand Down
6 changes: 1 addition & 5 deletions build/images/Dockerfile.build.controller.coverage
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,7 @@ RUN go mod download

COPY . /antrea

# 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 antctl-instr-binary
RUN mv bin/antctl-linux bin/antctl
RUN make antctl-linux antctl-instr-binary && mv bin/antctl-linux bin/antctl

RUN make antrea-controller antrea-controller-instr-binary

Expand Down
5 changes: 1 addition & 4 deletions build/images/Dockerfile.build.controller.ubi
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,9 @@ RUN --mount=type=cache,target=/go/pkg/mod/ \

COPY . /antrea

# 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 --mount=type=cache,target=/go/pkg/mod/ \
--mount=type=cache,target=/root/.cache/go-build/ \
CGO_ENABLED=0 make antctl-linux && mv bin/antctl-linux bin/antctl
make antctl-linux && mv bin/antctl-linux bin/antctl

RUN --mount=type=cache,target=/go/pkg/mod/ \
--mount=type=cache,target=/root/.cache/go-build/ \
Expand Down
5 changes: 1 addition & 4 deletions build/images/Dockerfile.build.controller.ubuntu
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,9 @@ RUN --mount=type=cache,target=/go/pkg/mod/ \

COPY . /antrea

# 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 --mount=type=cache,target=/go/pkg/mod/ \
--mount=type=cache,target=/root/.cache/go-build/ \
CGO_ENABLED=0 make antctl-linux && mv bin/antctl-linux bin/antctl
make antctl-linux && mv bin/antctl-linux bin/antctl

RUN --mount=type=cache,target=/go/pkg/mod/ \
--mount=type=cache,target=/root/.cache/go-build/ \
Expand Down
10 changes: 4 additions & 6 deletions build/images/Dockerfile.build.coverage
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,11 @@ RUN go mod download

COPY . /antrea

# 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 antctl-instr-binary
RUN mv bin/antctl-linux bin/antctl
# Build antctl first in order to share an extra layer with
# build/images/Dockerfile.build.agent.coverage and build/images/Dockerfile.build.controller.coverage.
RUN make antctl-linux antctl-instr-binary && mv bin/antctl-linux bin/antctl

# Build antrea-agent and antrea-cni first in order to share an extra layer with
# Then build antrea-agent and antrea-cni, in order to share an extra layer with
# build/images/Dockerfile.build.agent.coverage.
RUN make antrea-agent antrea-cni antrea-agent-instr-binary

Expand Down
9 changes: 4 additions & 5 deletions build/images/Dockerfile.build.ubi
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,13 @@ RUN --mount=type=cache,target=/go/pkg/mod/ \

COPY . /antrea

# 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.
# Build antctl first in order to share an extra layer with
# build/images/Dockerfile.build.agent.ubi and build/images/Dockerfile.build.controller.ubi.
RUN --mount=type=cache,target=/go/pkg/mod/ \
--mount=type=cache,target=/root/.cache/go-build/ \
CGO_ENABLED=0 make antctl-linux && mv bin/antctl-linux bin/antctl
make antctl-linux && mv bin/antctl-linux bin/antctl

# Build antrea-agent and antrea-cni first in order to share an extra layer with
# Then build antrea-agent and antrea-cni, in order to share an extra layer with
# build/images/Dockerfile.build.agent.ubi.
RUN --mount=type=cache,target=/go/pkg/mod/ \
--mount=type=cache,target=/root/.cache/go-build/ \
Expand Down
9 changes: 4 additions & 5 deletions build/images/Dockerfile.build.ubuntu
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,13 @@ RUN --mount=type=cache,target=/go/pkg/mod/ \

COPY . /antrea

# 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.
# Build antctl first in order to share an extra layer with
# build/images/Dockerfile.build.agent.ubuntu and build/images/Dockerfile.build.controller.ubuntu.
RUN --mount=type=cache,target=/go/pkg/mod/ \
--mount=type=cache,target=/root/.cache/go-build/ \
CGO_ENABLED=0 make antctl-linux && mv bin/antctl-linux bin/antctl
make antctl-linux && mv bin/antctl-linux bin/antctl

# Build antrea-agent and antrea-cni first in order to share an extra layer with
# Then build antrea-agent and antrea-cni, in order to share an extra layer with
# build/images/Dockerfile.build.agent.ubuntu.
RUN --mount=type=cache,target=/go/pkg/mod/ \
--mount=type=cache,target=/root/.cache/go-build/ \
Expand Down
5 changes: 3 additions & 2 deletions hack/release/prepare-assets.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ pushd $THIS_DIR/../.. > /dev/null
mkdir -p "$1"
OUTPUT_DIR=$(cd "$1" && pwd)

# Cgo should always be disabled for release assets.
export CGO_ENABLED=0

ANTREA_BUILDS=(
"linux amd64 linux-x86_64"
"linux arm64 linux-arm64"
Expand All @@ -63,8 +66,6 @@ for build in "${ANTREA_BUILDS[@]}"; do
arch="${args[1]}"
suffix="${args[2]}"

# all "*-release" targets disable cgo, which is appropriate when
# distributing release assets, for portability.
GOOS=$os GOARCH=$arch ANTCTL_BINARY_NAME="antctl-$suffix" BINDIR="$OUTPUT_DIR" make antctl-release
done

Expand Down

0 comments on commit 54cec86

Please sign in to comment.