Skip to content

Commit 9b74126

Browse files
committed
fix: make targets run on arm64 architecture
Signed-off-by: Oleksii Kurinnyi <[email protected]>
1 parent c390272 commit 9b74126

File tree

8 files changed

+145
-23
lines changed

8 files changed

+145
-23
lines changed

Makefile

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ OPM_VERSION = v1.19.5
5252

5353
CRD_OPTIONS ?= "crd:crdVersions=v1"
5454

55+
GOOS = linux
56+
GOARCH = amd64
57+
5558
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
5659
ifeq (,$(shell go env GOBIN))
5760
GOBIN=$(shell go env GOPATH)/bin
@@ -185,23 +188,48 @@ generate: controller-gen
185188
### docker: Builds and pushes controller image
186189
docker: _print_vars docker-build docker-push
187190

188-
### docker-build: Builds the controller image
189-
docker-build:
190-
$(DOCKER) build . -t ${DWO_IMG} -f build/Dockerfile
191+
### docker-build: Builds the images and the local manifest list
192+
docker-build: _docker-build-amd64 _docker-build-arm64
193+
# clean up any old local manifest list to avoid conflicts
194+
$(DOCKER) manifest rm ${DWO_IMG} || true
195+
# create a new local manifest list from the freshly built images
196+
$(DOCKER) manifest create ${DWO_IMG} ${DWO_IMG}-amd64 ${DWO_IMG}-arm64
197+
198+
199+
### _docker-build-amd64: Builds the amd64 image
200+
_docker-build-amd64:
201+
$(DOCKER) build . --platform linux/amd64 -t ${DWO_IMG}-amd64 -f build/Dockerfile
202+
203+
### _docker-build-arm64: Builds the arm64 image
204+
_docker-build-arm64:
205+
$(DOCKER) build . --platform linux/arm64 -t ${DWO_IMG}-arm64 -f build/Dockerfile
206+
207+
208+
### docker-push: Pushes the images and manifest to the registry
209+
docker-push: _docker-check-push
210+
# pushing architecture-specific images to the registry
211+
$(DOCKER) push ${DWO_IMG}-amd64
212+
$(DOCKER) push ${DWO_IMG}-arm64
213+
214+
# clean up any old local manifest list to avoid conflicts
215+
$(DOCKER) manifest rm ${DWO_IMG} || true
216+
# re-create the manifest right before pushing to ensure its state is clean
217+
$(DOCKER) manifest create ${DWO_IMG} ${DWO_IMG}-amd64 ${DWO_IMG}-arm64
218+
# push the manifest list and its images
219+
$(DOCKER) manifest push --all ${DWO_IMG}
191220

192-
### docker-push: Pushes the controller image
193-
docker-push:
221+
### _docker-check-push: Asks for confirmation before pushing the image, unless running in CI
222+
_docker-check-push:
194223
ifneq ($(INITIATOR),CI)
195224
ifeq ($(DWO_IMG),quay.io/devfile/devworkspace-controller:next)
196225
@echo -n "Are you sure you want to push $(DWO_IMG)? [y/N] " && read ans && [ $${ans:-N} = y ]
197226
endif
198227
endif
199-
$(DOCKER) push ${DWO_IMG}
200228

201229
### compile-devworkspace-controller: Compiles the devworkspace-controller binary
202230
.PHONY: compile-devworkspace-controller
203231
compile-devworkspace-controller:
204-
CGO_ENABLED=0 GOOS=linux GOARCH=$(ARCH) GO111MODULE=on go build \
232+
CGO_ENABLED=0 GOOS=linux GOARCH=$(GOARCH) GO111MODULE=on go build \
205233
-a -o _output/bin/devworkspace-controller \
206234
-gcflags all=-trimpath=/ \
207235
-asmflags all=-trimpath=/ \
@@ -212,7 +240,7 @@ compile-devworkspace-controller:
212240
### compile-webhook-server: Compiles the webhook-server
213241
.PHONY: compile-webhook-server
214242
compile-webhook-server:
215-
CGO_ENABLED=0 GOOS=linux GOARCH=$(ARCH) GO111MODULE=on go build \
243+
CGO_ENABLED=0 GOOS=linux GOARCH=$(GOARCH) GO111MODULE=on go build \
216244
-o _output/bin/webhook-server \
217245
-gcflags all=-trimpath=/ \
218246
-asmflags all=-trimpath=/ \

build/Dockerfile

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@
1414
#
1515

1616
# https://access.redhat.com/containers/?tab=tags#/registry.access.redhat.com/ubi9/go-toolset
17-
FROM registry.access.redhat.com/ubi9/go-toolset:1.24.4-1753853351 as builder
17+
# Use BUILDPLATFORM to ensure the builder always runs natively on the host machine
18+
FROM --platform=$BUILDPLATFORM registry.access.redhat.com/ubi9/go-toolset:1.24.4-1753853351 AS builder
19+
20+
# Accept TARGETARCH, which is automatically passed by the builder
21+
ARG TARGETARCH
22+
1823
ENV GOPATH=/go/
1924
USER root
2025
WORKDIR /devworkspace-operator
@@ -29,9 +34,9 @@ RUN go env GOPROXY && \
2934
# Copy the go source
3035
COPY . .
3136

32-
# compile workspace controller binaries, then webhook binaries
33-
RUN make compile-devworkspace-controller
34-
RUN make compile-webhook-server
37+
# Pass the target ARCH directly to make for robust cross-compilation
38+
RUN make GOARCH=${TARGETARCH} compile-devworkspace-controller
39+
RUN make GOARCH=${TARGETARCH} compile-webhook-server
3540

3641
# https://access.redhat.com/containers/?tab=tags#/registry.access.redhat.com/ubi9-minimal
3742
FROM registry.access.redhat.com/ubi9-minimal:9.6-1749489516

build/Dockerfile.arm64

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#
2+
# Copyright (c) 2019-2025 Red Hat, Inc.
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
16+
# https://access.redhat.com/containers/?tab=tags#/registry.access.redhat.com/ubi9-minimal
17+
FROM registry.access.redhat.com/ubi9-minimal:9.6-1749489516
18+
RUN microdnf -y update && \
19+
microdnf clean all && \
20+
rm -rf /var/cache/yum && \
21+
echo "Installed Packages" && \
22+
rpm -qa | sort -V && \
23+
echo "End Of Installed Packages"
24+
WORKDIR /
25+
COPY _output/bin/devworkspace-controller /usr/local/bin/devworkspace-controller
26+
COPY _output/bin/webhook-server /usr/local/bin/webhook-server
27+
28+
ENV USER_UID=1001 \
29+
USER_NAME=devworkspace-controller
30+
31+
COPY build/bin /usr/local/bin
32+
RUN /usr/local/bin/user_setup
33+
34+
USER ${USER_UID}
35+
36+
ENTRYPOINT ["/usr/local/bin/entrypoint"]
37+
CMD ["/usr/local/bin/devworkspace-controller"]
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# The base image is expected to contain
2+
# /bin/opm (with a serve subcommand) and /bin/grpc_health_probe
3+
FROM quay.io/operator-framework/opm:latest
4+
5+
# Configure the entrypoint and command
6+
ENTRYPOINT ["/bin/opm"]
7+
CMD ["serve", "/configs"]
8+
9+
# Copy declarative config root into image at /configs
10+
ADD olm-catalog/next /configs
11+
12+
# Set DC-specific label for the location of the DC root directory
13+
# in the image
14+
LABEL operators.operatorframework.io.index.configs.v1=/configs

build/make/deploy.mk

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,13 @@ _gen_configuration_env:
5858
cat $(CONTROLLER_ENV_FILE)
5959

6060
_store_tls_cert:
61-
mkdir -p /tmp/k8s-webhook-server/serving-certs/
61+
mkdir -p $(TMPDIR)/k8s-webhook-server/serving-certs/
6262
ifeq ($(PLATFORM),kubernetes)
63-
$(K8S_CLI) get secret devworkspace-operator-webhook-cert -n $(NAMESPACE) -o json | jq -r '.data["tls.crt"]' | base64 -d > /tmp/k8s-webhook-server/serving-certs/tls.crt
64-
$(K8S_CLI) get secret devworkspace-operator-webhook-cert -n $(NAMESPACE) -o json | jq -r '.data["tls.key"]' | base64 -d > /tmp/k8s-webhook-server/serving-certs/tls.key
63+
$(K8S_CLI) get secret devworkspace-operator-webhook-cert -n $(NAMESPACE) -o json | jq -r '.data["tls.crt"]' | base64 -d > $(TMPDIR)/k8s-webhook-server/serving-certs/tls.crt
64+
$(K8S_CLI) get secret devworkspace-operator-webhook-cert -n $(NAMESPACE) -o json | jq -r '.data["tls.key"]' | base64 -d > $(TMPDIR)/k8s-webhook-server/serving-certs/tls.key
6565
else
66-
$(K8S_CLI) get secret devworkspace-webhookserver-tls -n $(NAMESPACE) -o json | jq -r '.data["tls.crt"]' | base64 -d > /tmp/k8s-webhook-server/serving-certs/tls.crt
67-
$(K8S_CLI) get secret devworkspace-webhookserver-tls -n $(NAMESPACE) -o json | jq -r '.data["tls.key"]' | base64 -d > /tmp/k8s-webhook-server/serving-certs/tls.key
66+
$(K8S_CLI) get secret devworkspace-webhookserver-tls -n $(NAMESPACE) -o json | jq -r '.data["tls.crt"]' | base64 -d > $(TMPDIR)/k8s-webhook-server/serving-certs/tls.crt
67+
$(K8S_CLI) get secret devworkspace-webhookserver-tls -n $(NAMESPACE) -o json | jq -r '.data["tls.key"]' | base64 -d > $(TMPDIR)/k8s-webhook-server/serving-certs/tls.key
6868
endif
6969

7070
_check_controller_running:

build/make/olm.mk

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ build_bundle_and_index: _print_vars _check_skopeo_installed _check_opm_version
4646
--bundle-repo $${DWO_BUNDLE_IMG%%:*} \
4747
--bundle-tag $${DWO_BUNDLE_IMG##*:} \
4848
--index-image $(DWO_INDEX_IMG) \
49-
--container-tool $(DOCKER)
49+
--container-tool $(DOCKER) \
50+
--arch $(ARCH)
5051

5152
### register_catalogsource: create the catalogsource to make the operator be available on the marketplace
5253
register_catalogsource: _check_skopeo_installed

build/make/version.mk

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,8 @@ export ARCH := amd64
2323
else ifeq (aarch64,$(ARCH))
2424
export ARCH := arm64
2525
endif
26+
27+
# Set GOARCH to the host architecture *only if it's not already set*
28+
# This allows `make GOARCH=amd64 ...` to override the default.
29+
GOARCH ?= $(HOST_ARCH)
30+
export GOARCH

build/scripts/build_index_image.sh

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ set -e
55
PODMAN=podman
66
FORCE="false"
77
MULTI_ARCH="false"
8+
ARCH="amd64"
89
ARCHITECTURES="linux/amd64,linux/arm64,linux/ppc64le,linux/s390x"
910
DEBUG="false"
1011

@@ -43,9 +44,11 @@ Arguments:
4344
buildx will be used for building and pushing the images, instead of
4445
the container tool selected with --container-tool.
4546
--debug : Don't do any normal cleanup on exit, leaving repo in dirty state
46-
47-
Examples:
48-
1. Build and push bundle and index using default image repos
47+
--arch <ARCH> : The host architecture.
48+
This flag is ignored if --multi-arch is used.
49+
50+
Examples:
51+
1. Build and push bundle and index using default image repos
4952
$0 --force
5053
2. Build index and bundle using custom images
5154
$0 --bundle-repo <my_bundle_repo> --bundle-tag dev --index-image <my_index_image>
@@ -64,6 +67,7 @@ parse_args() {
6467
'--force') FORCE="true";;
6568
'--multi-arch') MULTI_ARCH="true";;
6669
'--debug') DEBUG="true";;
70+
'--arch') ARCH="$2"; shift 1;;
6771
*) echo "[ERROR] Unknown parameter is used: $1."; usage; exit 1;;
6872
esac
6973
shift 1
@@ -202,15 +206,43 @@ yq -Y -i --argjson entry "$ENTRY_JSON" '.entries |= . + [$entry]' "$CHANNEL_FILE
202206
echo "Validating current index"
203207
opm validate "$OUTDIR"
204208

209+
# Function to build and push an image for a specific architecture
210+
build_for_arch() {
211+
local IMAGE=$1
212+
local TARGET_ARCH=$2
213+
214+
local CACHE_DIR="build/opm-cache-${TARGET_ARCH}"
215+
local ARCH_IMAGE="${IMAGE}-${TARGET_ARCH}"
216+
217+
# Check if the build is native or a cross-build
218+
if [ "${ARCH}" == "${TARGET_ARCH}" ]; then
219+
# NATIVE BUILD: Pre-generate the cache
220+
${PODMAN} build . --platform linux/${TARGET_ARCH} -t "$ARCH_IMAGE" -f build/index.next.Dockerfile
221+
else
222+
# CROSS-BUILD: Build cache at runtime
223+
"${PODMAN}" build --platform "linux/${TARGET_ARCH}" \
224+
-t "${ARCH_IMAGE}" \
225+
-f build/index.next.no-cache.Dockerfile .
226+
fi
227+
228+
"${PODMAN}" push "${ARCH_IMAGE}"
229+
}
230+
205231
# Build index container
206232
echo "Building index image $INDEX_IMAGE"
207233
if [ "$MULTI_ARCH" == "true" ]; then
208234
docker buildx build . -t "$INDEX_IMAGE" -f "$DOCKERFILE" \
209235
--platform "$ARCHITECTURES" \
210236
--push
211237
else
212-
$PODMAN build . -t "$INDEX_IMAGE" -f "$DOCKERFILE"
213-
$PODMAN push "$INDEX_IMAGE" 2>&1
238+
build_for_arch $INDEX_IMAGE "amd64"
239+
build_for_arch $INDEX_IMAGE "arm64"
240+
241+
# Combine the two images into a single multi-arch manifest list
242+
"${PODMAN}" manifest rm "${INDEX_IMAGE}" || true
243+
"${PODMAN}" rmi "${INDEX_IMAGE}" || true
244+
"${PODMAN}" manifest create "${INDEX_IMAGE}" "${INDEX_IMAGE}-amd64" "${INDEX_IMAGE}-arm64"
245+
"${PODMAN}" manifest push "${INDEX_IMAGE}"
214246
fi
215247

216248

0 commit comments

Comments
 (0)