Skip to content

Commit 9342542

Browse files
[Devtools Week] Multi-platform support for build binary and container images (#68)
* add target arch support to build container make rules Signed-off-by: Michael Valdron <mvaldron@redhat.com> * update manager rule to build for other platforms Signed-off-by: Michael Valdron <mvaldron@redhat.com> * update env variables table in README Signed-off-by: Michael Valdron <mvaldron@redhat.com> * add 'Using other platforms' section to contributing guide Signed-off-by: Michael Valdron <mvaldron@redhat.com> * fix OpenShift CI: set default target arch build arg to 'amd64' Signed-off-by: Michael Valdron <mvaldron@redhat.com> --------- Signed-off-by: Michael Valdron <mvaldron@redhat.com>
1 parent cb5c55e commit 9342542

File tree

4 files changed

+73
-6
lines changed

4 files changed

+73
-6
lines changed

CONTRIBUTING.md

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,48 @@ The Makefile currently supports both Docker and Podman. To run the proper comman
6060

6161
By default, http/2 on the webhook server is disabled due to [CVE-2023-44487](https://github.com/advisories/GHSA-qppj-fm5r-hxr3).
6262

63-
If you want to enable http/2 for the webhook server, build with `ENABLE_WEBHOOK_HTTP2=true make docker-build` or with
63+
If you want to enable http/2 for the webhook server, build with `ENABLE_WEBHOOK_HTTP2=true make <engine>-build` or with
6464
`ENABLE_WEBHOOK_HTTP2=true make run` if running locally.
6565

66+
##### Using other platforms
67+
68+
If you need to target another platform for container builds, such as Apple silicon, you can use `TARGET_ARCH=<architecture> make <engine>-build`.
69+
70+
For example, to target container build to `arm64` run the following:
71+
72+
```sh
73+
TARGET_ARCH=arm64 make <engine>-build
74+
```
75+
76+
**Note:** Container builds only use `linux` as the operating system as local cluster runtime environments, such as `minikube` environments, run under Linux virtual machines for other operating systems. For example, _Apple silicon_ would just use the `arm64` container build.
77+
78+
For local builds, you can also set the target operating system:
79+
80+
**Apple silicon**
81+
82+
```sh
83+
export TARGET_OS=darwin
84+
export TARGET_ARCH=arm64
85+
make manager
86+
```
87+
88+
**Linux ARM**
89+
90+
```sh
91+
export TARGET_ARCH=arm64
92+
make manager
93+
```
94+
95+
**Windows**
96+
97+
```sh
98+
export TARGET_OS=windows
99+
export TARGET_ARCH=amd64
100+
make manager
101+
```
102+
103+
By default, `amd64` is used for the target architecture and `linux` is used for the target operating system.
104+
66105
### Testing your Changes
67106

68107
All changes delivered to the Devfile Registry Operator are expected to be sufficiently tested. This may include validating that existing tests pass, updating tests, or adding new tests.

Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
# Build the manager binary
1717
FROM golang:1.19 as builder
18-
ARG TARGETARCH
18+
ARG TARGETARCH=amd64
1919

2020
WORKDIR /workspace
2121
# Copy the Go Modules manifests
@@ -43,7 +43,7 @@ ENV ENABLE_WEBHOOK_HTTP2=${ENABLE_WEBHOOK_HTTP2}
4343

4444
# Use distroless as minimal base image to package the manager binary
4545
# Refer to https://github.com/GoogleContainerTools/distroless for more details
46-
FROM gcr.io/distroless/static:nonroot
46+
FROM gcr.io/distroless/static:nonroot-${TARGETARCH}
4747
WORKDIR /
4848
COPY --from=builder /workspace/manager .
4949
USER 1001

Makefile

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ KUSTOMIZE ?= $(LOCALBIN)/kustomize
101101
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
102102
ENVTEST ?= $(LOCALBIN)/setup-envtest
103103

104+
## Target platform
105+
TARGET_OS ?= linux # `make manager` only
106+
TARGET_ARCH ?= amd64
107+
104108
##@ Development
105109

106110
.PHONY: test
@@ -119,7 +123,7 @@ test-integration:
119123

120124
.PHONY: build manager
121125
manager: manifests generate fmt vet ## Build manager binary.
122-
go build -o $(LOCALBIN)/manager ./main.go
126+
GOOS=${TARGET_OS} GOARCH=${TARGET_ARCH} go build -o $(LOCALBIN)/manager ./main.go
123127

124128
.PHONY: run
125129
run: manifests generate fmt vet ## Run a controller from your host.
@@ -171,7 +175,8 @@ generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and
171175
# Build the docker image
172176
.PHONY: docker-build
173177
docker-build:
174-
docker build . -t ${IMG} --build-arg ENABLE_WEBHOOKS=${ENABLE_WEBHOOKS} \
178+
docker build . -t ${IMG} --build-arg TARGETARCH=${TARGET_ARCH} \
179+
--build-arg ENABLE_WEBHOOKS=${ENABLE_WEBHOOKS} \
175180
--build-arg ENABLE_WEBHOOK_HTTP2=${ENABLE_WEBHOOK_HTTP2}
176181

177182
# Push the docker image
@@ -200,7 +205,9 @@ docker-buildx: test ## Build and push docker image for the manager for cross-pla
200205
# Build the podman image
201206
.PHONY: podman-build
202207
podman-build:
203-
podman build . -t ${IMG} --build-arg ENABLE_WEBHOOKS=${ENABLE_WEBHOOKS}
208+
podman build . -t ${IMG} --build-arg TARGETARCH=${TARGET_ARCH} \
209+
--build-arg ENABLE_WEBHOOKS=${ENABLE_WEBHOOKS} \
210+
--build-arg ENABLE_WEBHOOK_HTTP2=${ENABLE_WEBHOOK_HTTP2}
204211

205212
# Push the podman image
206213
.PHONY: podman-push

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,27 @@ The repository contains a Makefile; building and deploying can be configured via
6464
|variable|purpose|default value|
6565
|---|---|---|
6666
| `IMG` | Image used for controller (run makefile, if `IMG` is updated) | `quay.io/devfile/registry-operator:next` |
67+
| `BUNDLE_IMG` | Image used for bundle OLM package | `quay.io/devfile/registry-operator-bundle:<latest_version>` |
68+
| `CERT_MANAGER_VERSION` | Version of `cert-manager` installed using `make install-cert` | `v1.11.0` |
69+
| `ENABLE_WEBHOOKS` | If `false`, disables operator webhooks | `true` |
70+
| `ENABLE_WEBHOOK_HTTP2` | Overrides webhook HTTP server deployment to use http/2 if set to `true`, **not recommended** | `false` |
71+
| `BUNDLE_CHANNELS` | Sets the list channel(s) include bundle build under | `alpha` |
72+
| `BUNDLE_DEFAULT_CHANNEL` | Sets the default channel to use when installing the bundle | |
73+
| `ENVTEST_K8S_VERSION` | Version of k8s to use for the test environment | `1.26` (current) |
74+
| `CONTROLLER_TOOLS_VERSION` | Version of the controller tools | `v0.9.2` |
75+
| `KUSTOMIZE_VERSION` | Version of kustomize | `v3.8.7` |
76+
| `GOBIN` | Path to install Go binaries to | `${GOPATH}/bin` |
77+
| `K8S_CLI` | Path to CLI tool to use with the target cluster environment, `kubectl` or `oc` | Either `oc` or `kubectl` if installed in that order |
78+
| `OPERATOR_SDK_CLI` | CLI path to `operator-sdk` tool | `operator-sdk` |
79+
| `SHELL` | Active shell to use with make | `/usr/bin/env bash -o pipefail` |
80+
| `LOCALBIN` | Path to place project binaries | `./bin` |
81+
| `KUSTOMIZE` | Path to target `kustomize` binary | `${LOCALBIN}/kustomize` |
82+
| `CONTROLLER_GEN` | Path to target `controller-gen` binary | `${LOCALBIN}/controller-gen` |
83+
| `ENVTEST` | Path to target `setup-envtest` binary | `${LOCALBIN}/setup-envtest` |
84+
| `TARGET_ARCH` | Target architecture for operator manager builds, possible values: `amd64`, `arm64`, `s390x`, `ppc64le` | `amd64` |
85+
| `TARGET_OS` | Target operating system for operator manager build, **only for `make manager`** | `linux` |
86+
| `PLATFORMS` | Target architecture(s) for `make docker-buildx` | All supported: `linux/arm64,linux/amd64,linux/s390x,linux/ppc64le` |
87+
| `KUSTOMIZE_INSTALL_SCRIPT` | URL of kustomize installation script, see [kustomize installation instructions](https://kubectl.docs.kubernetes.io/installation/kustomize/binaries/) | `https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh` |
6788

6889
Some of the rules supported by the makefile:
6990

0 commit comments

Comments
 (0)