Skip to content

Commit

Permalink
Re-add docker multi-platform support
Browse files Browse the repository at this point in the history
  • Loading branch information
ldevulder authored and ldevulder committed Nov 17, 2023
1 parent f329402 commit 499f795
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 48 deletions.
29 changes: 5 additions & 24 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
ARG GOLANG_IMAGE
ARG BUILD_OS_DISTRI
ARG BUILD_OS_VERSION
ARG GO_VERSION=1.20

FROM $GOLANG_IMAGE as elemental-bin
FROM --platform=${BUILDPLATFORM} golang:${GO_VERSION}-alpine as elemental-bin

ENV CGO_ENABLED=0
WORKDIR /src/
Expand All @@ -22,25 +20,8 @@ ARG ELEMENTAL_VERSION=0.0.1
ARG ELEMENTAL_COMMIT=""
ENV ELEMENTAL_VERSION=${ELEMENTAL_VERSION}
ENV ELEMENTAL_COMMIT=${ELEMENTAL_COMMIT}
RUN go build \
RUN GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build \
-ldflags "-w -s \
-X github.com/rancher/elemental-toolkit/internal/version.version=$ELEMENTAL_VERSION \
-X github.com/rancher/elemental-toolkit/internal/version.gitCommit=$ELEMENTAL_COMMIT" \
-X github.com/rancher/elemental-toolkit/internal/version.version=${ELEMENTAL_VERSION} \
-X github.com/rancher/elemental-toolkit/internal/version.gitCommit=${ELEMENTAL_COMMIT}" \
-o /usr/bin/elemental

FROM $BUILD_OS_DISTRI:$BUILD_OS_VERSION AS elemental
# This helps invalidate the cache on each build so the following steps are really run again getting the latest packages
# versions, as long as the elemental commit has changed
ARG ELEMENTAL_COMMIT=""
ENV ELEMENTAL_COMMIT=${ELEMENTAL_COMMIT}

# Copy the built CLI
COPY --from=elemental-bin /usr/bin/elemental /usr/bin/elemental

# Fix for blkid only using udev on opensuse
RUN echo "EVALUATE=scan" >> /etc/blkid.conf
ENTRYPOINT ["/usr/bin/elemental"]

# Add to /system/oem folder so install/upgrade/reset hooks will run when running this container.
# Needed for boot-assessment
COPY pkg/features/embedded/cloud-config-essentials/system/oem /system/oem/
35 changes: 35 additions & 0 deletions Dockerfile.toolkit
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Elemental CLI is built in another Dockerfile that support multi-arch
ARG OS_IMAGE=opensuse/leap
ARG OS_VERSION=15.5
ARG BIN_REPO
ARG VERSION

FROM ${BIN_REPO}:${VERSION} AS bin
FROM ${OS_IMAGE}:${OS_VERSION} AS elemental-toolkit

RUN ARCH=$(uname -m); \
[[ "${ARCH}" == "aarch64" ]] && ARCH="arm64"; \
zypper install -y --no-recommends xfsprogs \
parted \
util-linux-systemd \
e2fsprogs \
util-linux \
udev \
rsync \
grub2 \
dosfstools \
grub2-${ARCH}-efi \
squashfs \
mtools \
xorriso \
cosign \
gptfdisk \
lvm2

# Copy the built CLI
COPY --from=bin /usr/bin/elemental /usr/bin/elemental
ENTRYPOINT ["/usr/bin/elemental"]

# Add to /system/oem folder so install/upgrade/reset hooks will run when running this container.
# Needed for boot-assessment
COPY pkg/features/embedded/cloud-config-essentials/system/oem /system/oem/
63 changes: 39 additions & 24 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,27 @@ PLATFORM?=linux/$(ARCH)
IMAGE_SIZE?=20G
PACKER_TARGET?=qemu.elemental-${ARCH}
REPO?=local/elemental-$(FLAVOR)
BIN_REPO?=local/elemental-bin
TOOLKIT_REPO?=local/elemental-toolkit
DOCKER?=docker

# Used for docker build
BUILD_OS_DISTRI?=registry.opensuse.org/opensuse/bci/bci-minimal
BUILD_OS_VERSION?=latest
GO_VERSION?=1.20
GOLANG_IMAGE?=registry.opensuse.org/opensuse/bci/golang:$(GO_VERSION)

GIT_COMMIT ?= $(shell git rev-parse HEAD)
GIT_COMMIT_SHORT ?= $(shell git rev-parse --short HEAD)
GIT_TAG ?= $(shell git describe --candidates=50 --abbrev=0 --tags 2>/dev/null || echo "v0.0.1" )
VERSION ?= ${GIT_TAG}-g${GIT_COMMIT_SHORT}

PKG := ./cmd ./pkg/...
LDFLAGS := -w -s
LDFLAGS += -X "github.com/rancher/elemental-toolkit/internal/version.version=${GIT_TAG}"
LDFLAGS += -X "github.com/rancher/elemental-toolkit/internal/version.gitCommit=${GIT_COMMIT}"
BASE_OS_IMAGE?=opensuse/leap
BASE_OS_VERSION?=15.5

GIT_COMMIT?=$(shell git rev-parse HEAD)
GIT_COMMIT_SHORT?=$(shell git rev-parse --short HEAD)
GIT_TAG?=$(shell git describe --candidates=50 --abbrev=0 --tags 2>/dev/null || echo "v0.0.1" )
VERSION?=${GIT_TAG}-g${GIT_COMMIT_SHORT}

PKG:=./cmd ./pkg/...
LDFLAGS:=-w -s
LDFLAGS+=-X "github.com/rancher/elemental-toolkit/internal/version.version=${GIT_TAG}"
LDFLAGS+=-X "github.com/rancher/elemental-toolkit/internal/version.gitCommit=${GIT_COMMIT}"

# For RISC-V 64bit support
ifeq ($(PLATFORM),linux/riscv64)
BASE_OS_IMAGE=registry.opensuse.org/opensuse/factory/riscv/images/opensuse/tumbleweed
BASE_OS_VERSION=latest
endif

# default target
.PHONY: all
Expand All @@ -41,13 +44,21 @@ include make/Makefile.test

.PHONY: build
build:
$(DOCKER) build --platform ${PLATFORM} ${DOCKER_ARGS} \
--build-arg ELEMENTAL_VERSION=${GIT_TAG} \
--build-arg ELEMENTAL_COMMIT=${GIT_COMMIT} \
--build-arg BUILD_OS_DISTRI=${BUILD_OS_DISTRI} \
--build-arg BUILD_OS_VERSION=${BUILD_OS_VERSION} \
--build-arg GOLANG_IMAGE=${GOLANG_IMAGE} \
--target elemental -t ${TOOLKIT_REPO}:${VERSION} .
$(DOCKER) build --platform $(PLATFORM) ${DOCKER_ARGS} \
--build-arg ELEMENTAL_VERSION=$(GIT_TAG) \
--build-arg ELEMENTAL_COMMIT=$(GIT_COMMIT) \
--target elemental-bin -t $(BIN_REPO):$(VERSION) .
$(DOCKER) build --platform $(PLATFORM) ${DOCKER_ARGS} \
--build-arg BIN_REPO=$(BIN_REPO) \
--build-arg VERSION=$(VERSION) \
--build-arg OS_IMAGE=$(BASE_OS_IMAGE) \
--build-arg OS_VERSION=$(BASE_OS_VERSION) \
--file Dockerfile.toolkit \
--target elemental-toolkit -t $(TOOLKIT_REPO):$(VERSION) .

.PHONY: push-bin
push-bin:
$(DOCKER) push $(BIN_REPO):$(VERSION)

.PHONY: push-toolkit
push-toolkit:
Expand All @@ -59,7 +70,11 @@ build-cli:

.PHONY: build-os
build-os: build
$(DOCKER) build examples/$(FLAVOR) --platform ${PLATFORM} ${DOCKER_ARGS} --build-arg TOOLKIT_REPO=$(TOOLKIT_REPO) --build-arg VERSION=$(VERSION) --build-arg REPO=$(REPO) -t $(REPO):$(VERSION)
$(DOCKER) build --platform ${PLATFORM} ${DOCKER_ARGS} \
--build-arg TOOLKIT_REPO=$(TOOLKIT_REPO) \
--build-arg VERSION=$(VERSION) \
--build-arg REPO=$(REPO) -t $(REPO):$(VERSION) \
examples/$(FLAVOR)

.PHONY: push-os
push-os:
Expand Down

0 comments on commit 499f795

Please sign in to comment.