Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OCPVE-361: chore: allow multi-arch with ARCH / OS Flag #392

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# https://docs.docker.com/engine/reference/builder/#automatic-platform-args-in-the-global-scope
ARG TARGETOS
ARG TARGETARCH
ARG TARGETPLATFORM
# Build the manager binary
FROM golang:1.20 as builder

Expand All @@ -17,12 +21,12 @@ COPY cmd/ cmd/
COPY pkg/ pkg/

# Build
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build --ldflags "-s -w" -a -o manager main.go
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build --ldflags "-s -w" -a -o vgmanager cmd/vgmanager/main.go
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build --ldflags "-s -w" -a -o metricsexporter cmd/metricsexporter/exporter.go
RUN CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH go build --ldflags "-s -w" -a -o manager main.go
RUN CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH go build --ldflags "-s -w" -a -o vgmanager cmd/vgmanager/main.go
RUN CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH go build --ldflags "-s -w" -a -o metricsexporter cmd/metricsexporter/exporter.go

# vgmanager needs 'nsenter' and other basic linux utils to correctly function
FROM registry.access.redhat.com/ubi9/ubi-minimal:9.2
FROM --platform=$TARGETPLATFORM registry.access.redhat.com/ubi9/ubi-minimal:9.2

# Update the image to get the latest CVE updates
RUN microdnf update -y && \
Expand Down
8 changes: 5 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -169,20 +169,22 @@ run: manifests generate ## Run the Operator from your host.
##@ Build

IMAGE_BUILD_CMD ?= $(shell command -v docker 2>&1 >/dev/null && echo docker || echo podman)
OS ?= linux
ARCH ?= amd64

all: build

build: generate fmt vet ## Build manager binary.
go build -o bin/manager main.go
GOOS=$(OS) GOARCH=$(ARCH) go build -o bin/manager main.go

build-vgmanager: generate fmt vet ## Build vg manager binary.
go build -o bin/vgmanager cmd/vgmanager/main.go
GOOS=$(OS) GOARCH=$(ARCH) go build -o bin/vgmanager cmd/vgmanager/main.go

build-prometheus-alert-rules: jsonnet monitoring/mixin.libsonnet monitoring/alerts/alerts.jsonnet monitoring/alerts/*.libsonnet
$(JSONNET) -S monitoring/alerts/alerts.jsonnet > config/prometheus/prometheus_rules.yaml

docker-build: ## Build docker image with the manager.
$(IMAGE_BUILD_CMD) build -t ${IMG} .
$(IMAGE_BUILD_CMD) build --platform=${OS}/${ARCH} -t ${IMG} .
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not work on darwin/arm64:

➜  ✗ make docker-build
podman build --platform=darwin/arm64 -t quay.io/test/lvms-operator:tag .
[1/2] STEP 1/13: FROM golang:1.20 AS builder
Resolving "golang" using unqualified-search registries (/etc/containers/registries.conf.d/999-podman-machine.conf)
Trying to pull docker.io/library/golang:1.20...
Error: creating build container: choosing an image from manifest list docker://golang:1.20: no image found in manifest list for architecture arm64, variant "", OS darwin

make: *** [docker-build] Error 125

I don't know if we will ever need to build container images for different platforms from here. Can we make this at least parametric, meaning it will build for linux/amd64 by default unless related parameters are specified?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So it was actually parametric already, but used go env variables for OS / ARCH. I just hardcoded the Args to linux/amd64 now. You should be able to run this normally without any changes now.


docker-push: ## Push docker image with the manager.
$(IMAGE_BUILD_CMD) push ${IMG}
Expand Down