Skip to content

Commit

Permalink
Merge pull request #95 from frobware/speed-up-build
Browse files Browse the repository at this point in the history
Speed Up `bpfman-agent` and `bpfman-operator` Build
  • Loading branch information
anfredette authored Aug 9, 2024
2 parents 2b97bf4 + dfda635 commit 3a5dec9
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 28 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bin
19 changes: 4 additions & 15 deletions Containerfile.bpfman-agent
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,15 @@ RUN echo "TARGETOS=${TARGETOS} TARGETARCH=${TARGETARCH} BUILDPLATFORM=${BUILDP

WORKDIR /usr/src/bpfman-operator

# Copy the Go Modules manifests
COPY go.mod go.mod
COPY go.sum go.sum

COPY vendor/ vendor/

# cache deps before building and copying source so that we don't need to re-download as much
# and so that source changes don't invalidate our downloaded layer
RUN go mod download

# Copy the go source
# Copy everything except what's excluded by the .dockerignore file.
COPY . .

# Build
# the GOARCH has not a default value to allow the binary be built according to the host where the command
# was called. For example, if we call make docker-build in a local env which has the Apple Silicon M1 SO
# the docker BUILDPLATFORM arg will be linux/arm64 when for Apple x86 it will be linux/amd64. Therefore,
# by leaving it empty we can ensure that the container and binary shipped on it will have the same platform.
WORKDIR /usr/src/bpfman-operator
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -mod vendor -a -o bpfman-agent ./cmd/bpfman-agent/main.go
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -mod vendor -o bpfman-agent ./cmd/bpfman-agent/main.go

# Use the fedora minimal image to reduce the size of the final image but still
# be able to easily install extra packages.
Expand All @@ -50,9 +39,9 @@ WORKDIR /
COPY --from=bpfman-agent-build /usr/src/bpfman-operator/bpfman-agent .

# Install crictl
RUN ${DNF_CMD} -y install wget tar gzip
RUN ${DNF_CMD} -y install wget tar gzip ca-certificates
ARG VERSION="v1.28.0"
RUN wget --no-check-certificate https://github.com/kubernetes-sigs/cri-tools/releases/download/${VERSION}/crictl-${VERSION}-linux-${TARGETARCH}.tar.gz
RUN wget https://github.com/kubernetes-sigs/cri-tools/releases/download/${VERSION}/crictl-${VERSION}-linux-${TARGETARCH}.tar.gz
RUN tar zxvf crictl-${VERSION}-linux-${TARGETARCH}.tar.gz -C /usr/local/bin
RUN rm -f crictl-${VERSION}-linux-${TARGETARCH}.tar.gz
RUN ${DNF_CMD} -y clean all
Expand Down
15 changes: 2 additions & 13 deletions Containerfile.bpfman-operator
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,15 @@ RUN echo "TARGETOS=${TARGETOS} TARGETARCH=${TARGETARCH} BUILDPLATFORM=${BUILDP

WORKDIR /usr/src/bpfman-operator

# Copy the Go Modules manifests
COPY go.mod go.mod
COPY go.sum go.sum

COPY vendor/ vendor/

# cache deps before building and copying source so that we don't need to re-download as much
# and so that source changes don't invalidate our downloaded layer
RUN go mod download

# Copy the go source
# Copy everything except what's excluded by the .dockerignore file.
COPY . .

# Build
# the GOARCH has not a default value to allow the binary be built according to the host where the command
# was called. For example, if we call make docker-build in a local env which has the Apple Silicon M1 SO
# the docker BUILDPLATFORM arg will be linux/arm64 when for Apple x86 it will be linux/amd64. Therefore,
# by leaving it empty we can ensure that the container and binary shipped on it will have the same platform.
WORKDIR /usr/src/bpfman-operator
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -mod vendor -a -o bpfman-operator ./cmd/bpfman-operator/main.go
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -mod vendor -o bpfman-operator ./cmd/bpfman-operator/main.go

# Use the fedora minimal image to reduce the size of the final image but still
# be able to easily install extra packages.
Expand Down
16 changes: 16 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -318,17 +318,33 @@ build: fmt ## Build bpfman-operator and bpfman-agent binaries.
CGO_ENABLED=0 GOOS=linux GOARCH=$(GOARCH) go build -mod vendor -o bin/bpfman-operator cmd/bpfman-operator/main.go
CGO_ENABLED=0 GOOS=linux GOARCH=$(GOARCH) go build -mod vendor -o bin/bpfman-agent cmd/bpfman-agent/main.go

# These paths map the host's GOCACHE location to the container's
# location. We want to mount the host's Go cache in the container to
# speed up builds, particularly during development. Only podman (i.e.,
# not Docker) permits volumes to be mapped for builds so we do this
# conditionally.
ifeq ($(OCI_BIN),podman)
LOCAL_GOCACHE_PATH ?= $(shell go env GOCACHE)
CONTAINER_GOCACHE_PATH ?= /root/.cache/go-build
$(shell mkdir -p $(LOCAL_GOCACHE_PATH))
endif

.PHONY: build-images
build-images: ## Build bpfman-agent and bpfman-operator images.
$(if $(filter $(OCI_BIN),podman), \
@echo "Adding GOCACHE volume mount $(LOCAL_GOCACHE_PATH):$(CONTAINER_GOCACHE_PATH).")
$(OCI_BIN) version
$(OCI_BIN) buildx build --load -t ${BPFMAN_OPERATOR_IMG} \
--build-arg TARGETPLATFORM=linux/$(GOARCH) \
--build-arg TARGETARCH=$(GOARCH) \
--build-arg BUILDPLATFORM=linux/amd64 \
$(if $(filter $(OCI_BIN),podman),--volume "$(LOCAL_GOCACHE_PATH):$(CONTAINER_GOCACHE_PATH):z") \
-f Containerfile.bpfman-operator .
$(OCI_BIN) buildx build --load -t ${BPFMAN_AGENT_IMG} \
--build-arg TARGETPLATFORM=linux/$(GOARCH) \
--build-arg TARGETARCH=$(GOARCH) \
--build-arg BUILDPLATFORM=linux/amd64 \
$(if $(filter $(OCI_BIN),podman),--volume "$(LOCAL_GOCACHE_PATH):$(CONTAINER_GOCACHE_PATH):z") \
-f Containerfile.bpfman-agent .

.PHONY: push-images
Expand Down

0 comments on commit 3a5dec9

Please sign in to comment.