Skip to content

Commit

Permalink
Merge pull request #783 from akutz/feature/cross-arch-images
Browse files Browse the repository at this point in the history
✨ Support building cross-arch images
  • Loading branch information
akutz authored Oct 30, 2024
2 parents 22a12c1 + c138f8b commit 5f28e3b
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 18 deletions.
16 changes: 16 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,22 @@ jobs:
- name: Vulncheck Go
run: make vulncheck-go

build-image:
needs:
- verify-go-modules
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v4
- name: Install Go
uses: actions/setup-go@v5
with:
go-version: ${{ env.GO_VERSION }}
cache: true
cache-dependency-path: '**/go.sum'
- name: Build Image
run: GOOS=linux GOARCH=amd64 make image-build

build-manager:
needs:
- verify-go-modules
Expand Down
38 changes: 28 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ prereqs:
## --------------------------------------

help: ## Display this help
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z0-9_-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)

## --------------------------------------
## Testing
Expand Down Expand Up @@ -606,7 +606,7 @@ kind-down: delete-kind-cluster
kind-down: ## Delete kind cluster

.PHONY: deploy-local-kind
deploy-local-kind: docker-build load-kind
deploy-local-kind: image-build load-kind

.PHONY: kind-deploy
kind-deploy: deploy-local-kind
Expand Down Expand Up @@ -707,21 +707,39 @@ docs-serve-docker: ## Serve docs w container
## Docker
## --------------------------------------

.PHONY: docker-build
docker-build: ## Build the container image
hack/build-container.sh -i $(IMAGE) -t $(IMAGE_TAG) -v $(BUILD_VERSION) -n $(BUILD_NUMBER)
.PHONY: image-build
image-build: GOOS=linux
image-build: ## Build container image
GOOS="$(GOOS)" GOARCH="$(GOARCH)" hack/build-container.sh \
-i "$(IMAGE)" \
-t "$(IMAGE_TAG)" \
-v "$(BUILD_VERSION)" \
-n "$(BUILD_NUMBER)" \
-o "$(abspath $(ARTIFACTS_DIR))/$(IMAGE)-$(GOOS)_$(GOARCH).tar"

.PHONY: docker-push
docker-push: prereqs ## Push the container image
.PHONY: image-build-amd64
image-build-amd64: ## Build amd64 container image
GOARCH=amd64 $(MAKE) image-build

.PHONY: image-build-arm64
image-build-arm64: ## Build arm64 container image
GOARCH=arm64 $(MAKE) image-build

.PHONY: image-push
image-push: ## Push container image
$(CRI_BIN) push ${IMG}

.PHONY: docker-remove
docker-remove: ## Remove the container image
.PHONY: image-remove
image-remove: ## Remove container image
@if [[ "`$(CRI_BIN) images -q ${IMG} 2>/dev/null`" != "" ]]; then \
echo "Remove container ${IMG}"; \
$(CRI_BIN) rmi ${IMG}; \
fi

# The following are for backwards compatibility.
docker-build: image-build
docker-push: image-push
docker-remove: image-remove

## --------------------------------------
## Vulnerability Checks
Expand All @@ -737,7 +755,7 @@ vulncheck-go: $(GOVULNCHECK)
## --------------------------------------

.PHONY: clean
clean: docker-remove ## Remove all generated files
clean: image-remove ## Remove all generated files
rm -rf bin *.out $(ARTIFACTS_DIR)

.PHONY: verify
Expand Down
77 changes: 69 additions & 8 deletions hack/build-container.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,95 @@ IMAGE=
IMAGE_TAG=
BUILD_NUMBER=

if [ -z "${GOHOSTOS:-}" ] || [ -z "${GOHOSTARCH:-}" ]; then
if ! command -v go >/dev/null 2>&1; then
echo "GOHOSTOS and/or GOHOSTARCH are unset and golang is not detected" 1>&2
exit 1
fi
fi

GOHOSTOS="${GOHOSTOS:-$(go env GOHOSTOS)}"
GOHOSTARCH="${GOHOSTARCH:-$(go env GOHOSTARCH)}"

GOOS=linux
GOARCH="${GOARCH:-${GOHOSTARCH}}"

usage() {
echo "Usage: $(basename "${0}") -i image -t imageTag [-n buildNumber] [-c commit] [-b branch] [-v version]"
echo "Usage: $(basename "${0}") -i image -t imageTag [-n buildNumber] [-c commit] [-b branch] [-v version] [-o outFile]"
exit 1
}

build() {
GOOS=linux
echo "GOOS=${GOOS} GOARCH=${GOARCH}"
build_docker() {
if [ "${GOOS}" = "${GOHOSTOS}" ] && [ "${GOARCH}" = "${GOHOSTARCH}" ]; then

"${CRI_BIN}" build . \
-t "${IMAGE}":"${IMAGE_TAG}" \
-t "${IMAGE}":"${BUILD_NUMBER}" \
-t "${IMAGE}":"${BUILD_VERSION}" \
--build-arg BUILD_BRANCH="${BUILD_BRANCH}" \
--build-arg BUILD_COMMIT="${BUILD_COMMIT}" \
--build-arg BUILD_NUMBER="${BUILD_NUMBER}" \
--build-arg BUILD_VERSION="${BUILD_VERSION}"

elif docker buildx version >/dev/null 2>&1; then

"${CRI_BIN}" buildx build . \
-t "${IMAGE}":"${IMAGE_TAG}" \
-t "${IMAGE}":"${BUILD_NUMBER}" \
-t "${IMAGE}":"${BUILD_VERSION}" \
--platform "${GOOS}/${GOARCH}" \
--build-arg BUILD_BRANCH="${BUILD_BRANCH}" \
--build-arg BUILD_COMMIT="${BUILD_COMMIT}" \
--build-arg BUILD_NUMBER="${BUILD_NUMBER}" \
--build-arg BUILD_VERSION="${BUILD_VERSION}"

else

echo "docker buildx is not installed" 1>&2
exit 1

fi
}

build_podman() {
"${CRI_BIN}" build . \
-t "${IMAGE}":"${IMAGE_TAG}" \
-t "${IMAGE}":"${BUILD_NUMBER}" \
-t "${IMAGE}":"${BUILD_VERSION}" \
--os "${GOOS}" --arch "${GOARCH}" \
--build-arg BUILD_BRANCH="${BUILD_BRANCH}" \
--build-arg BUILD_COMMIT="${BUILD_COMMIT}" \
--build-arg BUILD_NUMBER="${BUILD_NUMBER}" \
--build-arg BUILD_VERSION="${BUILD_VERSION}" \
--build-arg TARGETOS="${GOOS}" \
--build-arg TARGETARCH="${GOARCH}"
--build-arg BUILD_VERSION="${BUILD_VERSION}"
}

build() {
echo "GOOS=${GOOS} GOARCH=${GOARCH}"

if [[ "${CRI_BIN}" == *"podman"* ]]; then
build_podman
elif [[ "${CRI_BIN}" == *"docker"* ]]; then
build_docker
else
echo "unsupported cri: '${CRI_BIN}'" 1>&2
exit 1
fi

if [ -n "${OUT_FILE:-}" ]; then
mkdir -p "$(dirname "${OUT_FILE}")"
"${CRI_BIN}" save "${IMAGE}":"${IMAGE_TAG}" -o "${OUT_FILE}"
fi
}

while getopts ":i:t:n:c:b:v:" opt ; do
while getopts ":i:t:n:c:b:v:o:" opt ; do
case $opt in
"i" ) IMAGE="${OPTARG}" ;;
"t" ) IMAGE_TAG="${OPTARG}" ;;
"b" ) BUILD_BRANCH="${OPTARG}" ;;
"c" ) BUILD_COMMIT="${OPTARG}" ;;
"n" ) BUILD_NUMBER="${OPTARG}" ;;
"v" ) BUILD_VERSION="${OPTARG}" ;;
"o" ) OUT_FILE="${OPTARG}" ;;
\? ) usage ;;
esac
done
Expand Down

0 comments on commit 5f28e3b

Please sign in to comment.