Skip to content

Commit

Permalink
Add operator multiarch builds
Browse files Browse the repository at this point in the history
  • Loading branch information
iamjoemccormick committed Feb 26, 2024
1 parent e31a88b commit 71fb1b8
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 11 deletions.
23 changes: 21 additions & 2 deletions operator/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,31 @@

# Use distroless as minimal base image to package the manager binary. Refer to
# https://github.com/GoogleContainerTools/distroless for more details.
FROM gcr.io/distroless/static:nonroot
FROM --platform=$BUILDPLATFORM gcr.io/distroless/static:nonroot
LABEL maintainers="ThinkParQ"
LABEL description="BeeGFS CSI Driver Operator"
LABEL org.opencontainers.image.description="BeeGFS CSI Driver Operator"
LABEL org.opencontainers.image.source="https://github.com/ThinkParQ/beegfs-csi-driver/operator"
LABEL org.opencontainers.image.licenses="Apache-2.0"

# Multi-arch images can be built from this Dockerfile. When the container image is built it is
# expected the controller binary was already created and exists bin/ using Make. By default calling
# Make with no arguments builds these files for the current architecture with no suffix allowing the
# container image to be built without multiarch support by default.
#
# If Make is called with the `BUILD_PLATFORMS` build argument, a controller binary will be
# compiled for each platform with an architecture suffix. These can then be used to build a
# multiarch container image using `docker buildx build` by specifying the same list of platforms
# using the `--platform` flag. Note the buildx flag and BUILD_PLATFORMS argument accept slightly
# different values, for example to build for both amd64 and arm64:
#
# `make BUILD_PLATFORMS="linux amd64 amd64 amd64;linux arm64 arm64 arm64" all`
# `docker buildx build --platform=linux/amd64,linux/arm64`
ARG TARGETARCH
WORKDIR /
COPY bin/manager .

# Copy architecture specific manager to the image.
COPY bin/manager$TARGETARCH /manager
USER 65532:65532

ENTRYPOINT ["/manager"]
36 changes: 28 additions & 8 deletions operator/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
# - use environment variables to overwrite this value (e.g export VERSION=0.0.2)
VERSION ?= 1.5.0

# BUILD_PLATFORMS contains a set of tuples [os arch buildx_platform suffix base_image addon_image]
# separated by semicolon. An empty variable or empty entry (= just a
# semicolon) builds for the default platform of the current Go
# toolchain. This approach was adapted from the CSI driver release-tools.1
BUILD_PLATFORMS =

# CHANNELS define the bundle channels used in the bundle.
# Add a new line here if you would like to change its default config. (E.g CHANNELS = "candidate,fast,stable")
# To re-generate a bundle for other specific channels without changing the standard setup, you can:
Expand Down Expand Up @@ -110,15 +116,24 @@ test: manifests generate fmt vet envtest ## Run tests.
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test -v ./... -coverprofile cover.out

##@ Build

.PHONY: build
build: generate fmt vet ## Build manager binary.
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o bin/manager main.go
mkdir -p bin
echo '$(BUILD_PLATFORMS)' | tr ';' '\n' | while read -r os arch buildx_platform suffix base_image addon_image; do \
if ! (set -x; CGO_ENABLED=0 GOOS="$$os" GOARCH="$$arch" go build $(GOFLAGS_VENDOR) -a -ldflags \
'$(FULL_LDFLAGS)' -o "./bin/manager$$suffix" main.go); then \
echo "Building manager for GOOS=$$os GOARCH=$$arch failed, see error(s) above."; \
exit 1; \
fi; \
done

.PHONY: run
run: manifests generate fmt vet ## Run a controller from your host.
go run ./main.go --zap-devel=true --zap-log-level=5

# Note the Makefile doesn't build multiarch images (only the current architecture).
# Multiarch images are currently built/published using GitHub actions or manually.

.PHONY: docker-build
docker-build: test ## Build docker image with the manager.
docker build -t ${IMG} .
Expand Down Expand Up @@ -249,9 +264,14 @@ PLATFORMS ?= linux/arm64,linux/amd64,linux/s390x,linux/ppc64le
.PHONY: docker-buildx
docker-buildx: test ## Build and push docker image for the manager for cross-platform support
# copy existing Dockerfile and insert --platform=${BUILDPLATFORM} into Dockerfile.cross, and preserve the original Dockerfile
sed -e '1 s/\(^FROM\)/FROM --platform=\$$\{BUILDPLATFORM\}/; t' -e ' 1,// s//FROM --platform=\$$\{BUILDPLATFORM\}/' Dockerfile > Dockerfile.cross
- docker buildx create --name project-v3-builder
docker buildx use project-v3-builder
- docker buildx build --push --platform=$(PLATFORMS) --tag ${IMG} -f Dockerfile.cross
- docker buildx rm project-v3-builder
rm Dockerfile.cross
# sed -e '1 s/\(^FROM\)/FROM --platform=\$$\{BUILDPLATFORM\}/; t' -e ' 1,// s//FROM --platform=\$$\{BUILDPLATFORM\}/' Dockerfile > Dockerfile.cross
# - docker buildx create --name project-v3-builder
# docker buildx use project-v3-builder
# - docker buildx build --push --platform=$(PLATFORMS) --tag ${IMG} -f Dockerfile.cross
# - docker buildx rm project-v3-builder
# rm Dockerfile.cross
# This has not been updated to work with how we build multiarch images using GitHub actions.
# Fail the target to prevent accidental usage.
@echo "Using the docker-buildx target is not currently supported"
@exit 1

3 changes: 3 additions & 0 deletions operator/docs/developer-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,9 @@ chmod +x install.sh
* All prerequisites for the BeeGFS CSI driver must be installed on your
Kubernetes nodes. If you are using Minikube there is a script to do this at
`hack/minikube_install_driver_prerequisites.sh`.
* You must also provide Minikube its own base client configuration file. For example you might
bind mount /etc/beegfs from the host OS into the Minikube container using `minikube mount
/etc/beegfs:/etc/beegfs` (note the command must stay running for the mount to stay active).

Steps:

Expand Down
2 changes: 1 addition & 1 deletion operator/hack/minikube_install_driver_prerequisites.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash
set -euo pipefail

export BEEGFS_VERSION=7.3.4
export BEEGFS_VERSION=7.4.2

# Install the BeeGFS beegfs-ctl tool into the Minikube container:
minikube ssh "sudo apt-get update"
Expand Down

0 comments on commit 71fb1b8

Please sign in to comment.