Skip to content

Commit

Permalink
Add multiarch container builds
Browse files Browse the repository at this point in the history
  • Loading branch information
brancz committed Mar 2, 2022
1 parent 9d37ad7 commit e37ccfb
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 18 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/container.yml
Expand Up @@ -21,6 +21,12 @@ jobs:
shell: bash
run: echo "GITHUB_BRANCH_NAME=$(echo ${GITHUB_REF#refs/heads/} | tr / -)" >> $GITHUB_ENV

# To build multi-arch images, see also: https://github.com/redhat-actions/buildah-build#multi-arch-builds
- name: Install qemu dependency
run: |
sudo apt-get update
sudo apt-get install -y qemu-user-static
- name: Build container
run: make container

Expand Down
35 changes: 23 additions & 12 deletions Dockerfile
@@ -1,5 +1,7 @@
# Equivalent of docker.io/golang:1.17.5-bullseye on 06.01.2022
FROM docker.io/golang@sha256:90fb73aa50a20c85ecfaf619002dceeabff306ae19beec149e6be39928ee7f2d as build
ARG GOLANG_BASE
ARG DEBIAN_BASE

FROM ${GOLANG_BASE} as build

# For more information about the snapshots, see: https://snapshot.debian.org/
RUN echo "\
Expand All @@ -14,8 +16,14 @@ RUN apt-get -o Acquire::Check-Valid-Until="false" update -y && \
apt-get install --no-install-recommends -y clang-11 llvm-11 make gcc coreutils elfutils binutils zlib1g-dev libelf-dev ca-certificates netbase && \
ln -s /usr/bin/clang-11 /usr/bin/clang && \
ln -s /usr/bin/llc-11 /usr/bin/llc

WORKDIR /parca-agent

ARG ARCH=amd64
ENV GOOS=linux
ENV ARCH=$ARCH
ENV GOARCH=$ARCH

COPY go.mod go.sum /parca-agent/
RUN go mod download -modcacherw

Expand All @@ -26,19 +34,22 @@ RUN make bpf
COPY . /parca-agent
RUN make build

# Equivalent of docker.io/debian:bullseye-slim on 06.01.2022
FROM docker.io/debian@sha256:96e65f809d753e35c54b7ba33e59d92e53acc8eb8b57bf1ccece73b99700b3b0 as all
FROM ${DEBIAN_BASE} as all

ARG LINUX_ARCH=x86_64
ENV LINUX_ARCH=$LINUX_ARCH

COPY --from=build /etc/nsswitch.conf /etc/nsswitch.conf
COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
COPY --from=build /usr/share/zoneinfo /usr/share/zoneinfo
COPY --from=build /lib/x86_64-linux-gnu/libpthread.so.0 /lib/x86_64-linux-gnu/libpthread.so.0
COPY --from=build /usr/lib/x86_64-linux-gnu/libelf-0.183.so /usr/lib/x86_64-linux-gnu/libelf-0.183.so
COPY --from=build /usr/lib/x86_64-linux-gnu/libdw.so.1 /usr/lib/x86_64-linux-gnu/libdw.so.1
RUN ln -s /usr/lib/x86_64-linux-gnu/libelf-0.183.so /usr/lib/x86_64-linux-gnu/libelf.so.1
COPY --from=build /lib/x86_64-linux-gnu/libz.so.1 /lib/x86_64-linux-gnu/libz.so.1
COPY --from=build /lib/x86_64-linux-gnu/libc.so.6 /lib/x86_64-linux-gnu/libc.so.6
COPY --from=build /usr/lib/x86_64-linux-gnu/libbfd-2.35.2-system.so /usr/lib/x86_64-linux-gnu/libbfd-2.35.2-system.so
COPY --from=build /lib/x86_64-linux-gnu/libdl.so.2 /lib/x86_64-linux-gnu/libdl.so.2
COPY --from=build /lib/${LINUX_ARCH}-linux-gnu/libpthread.so.0 /lib/${LINUX_ARCH}-linux-gnu/libpthread.so.0
COPY --from=build /usr/lib/${LINUX_ARCH}-linux-gnu/libelf-0.183.so /usr/lib/${LINUX_ARCH}-linux-gnu/libelf-0.183.so
COPY --from=build /usr/lib/${LINUX_ARCH}-linux-gnu/libdw.so.1 /usr/lib/${LINUX_ARCH}-linux-gnu/libdw.so.1
RUN ln -s /usr/lib/${LINUX_ARCH}-linux-gnu/libelf-0.183.so /usr/lib/${LINUX_ARCH}-linux-gnu/libelf.so.1
COPY --from=build /lib/${LINUX_ARCH}-linux-gnu/libz.so.1 /lib/${LINUX_ARCH}-linux-gnu/libz.so.1
COPY --from=build /lib/${LINUX_ARCH}-linux-gnu/libc.so.6 /lib/${LINUX_ARCH}-linux-gnu/libc.so.6
COPY --from=build /usr/lib/${LINUX_ARCH}-linux-gnu/libbfd-2.35.2-system.so /usr/lib/${LINUX_ARCH}-linux-gnu/libbfd-2.35.2-system.so
COPY --from=build /lib/${LINUX_ARCH}-linux-gnu/libdl.so.2 /lib/${LINUX_ARCH}-linux-gnu/libdl.so.2
COPY --from=build /usr/bin/objcopy /usr/bin/objcopy
COPY --from=build /usr/bin/eu-strip /usr/bin/eu-strip
COPY --from=build /parca-agent/dist/parca-agent /bin/parca-agent
Expand Down
25 changes: 19 additions & 6 deletions Makefile
Expand Up @@ -3,6 +3,8 @@ all: bpf build

SHELL := /bin/bash

ALL_ARCH ?= amd64 arm64

# tools:
CMD_LLC ?= llc
CMD_CLANG ?= clang
Expand All @@ -11,7 +13,19 @@ CMD_GIT ?= git
CMD_EMBEDMD ?= embedmd
# environment:
ARCH_UNAME := $(shell uname -m)
ARCH ?= $(ARCH_UNAME:aarch64=arm64)

ifeq ($(ARCH_UNAME), x86_64)
ARCH ?= amd64
else
ARCH ?= arm64
endif

ifeq ($(ARCH), amd64)
LINUX_ARCH ?= x86_64=x86
else
LINUX_ARCH ?= aarch64=arm64
endif

KERN_RELEASE ?= $(shell uname -r)
KERN_BLD_PATH ?= $(if $(KERN_HEADERS),$(KERN_HEADERS),/lib/modules/$(KERN_RELEASE)/build)
KERN_SRC_PATH ?= $(if $(KERN_HEADERS),$(KERN_HEADERS),$(if $(wildcard /lib/modules/$(KERN_RELEASE)/source),/lib/modules/$(KERN_RELEASE)/source,$(KERN_BLD_PATH)))
Expand Down Expand Up @@ -103,15 +117,14 @@ $(BPF_BUNDLE): $(BPF_SRC) $(LIBBPF_HEADERS)/bpf $(BPF_HEADERS)
.PHONY: bpf
bpf: $(OUT_BPF)

linux_arch := $(ARCH:x86_64=x86)
ifndef DOCKER
$(OUT_BPF): $(BPF_SRC) $(LIBBPF_HEADERS) $(LIBBPF_OBJ) | $(OUT_DIR) $(bpf_compile_tools)
mkdir -p $(OUT_BPF_DIR)
@v=$$($(CMD_CLANG) --version); test $$(echo $${v#*version} | head -n1 | cut -d '.' -f1) -ge '9' || (echo 'required minimum clang version: 9' ; false)
$(CMD_CLANG) -S \
-D__BPF_TRACING__ \
-D__KERNEL__ \
-D__TARGET_ARCH_$(linux_arch) \
-D__TARGET_ARCH_$(LINUX_ARCH) \
-I $(LIBBPF_HEADERS)/bpf \
-I $(BPF_HEADERS) \
-Wno-address-of-packed-member \
Expand Down Expand Up @@ -192,15 +205,15 @@ check_%:

.PHONY: container
container:
buildah build-using-dockerfile --timestamp 0 --layers -t $(OUT_DOCKER):$(VERSION)
./make-containers.sh $(OUT_DOCKER):$(VERSION)

.PHONY: push-container
push-container:
buildah push $(OUT_DOCKER):$(VERSION)
podman push $(OUT_DOCKER):$(VERSION)

.PHONY: push-quay-container
push-quay-container:
buildah push $(OUT_DOCKER):$(VERSION) quay.io/parca/parca-agent:$(VERSION)
podman push $(OUT_DOCKER):$(VERSION) quay.io/parca/parca-agent:$(VERSION)

.PHONY: internal/pprof
internal/pprof:
Expand Down
37 changes: 37 additions & 0 deletions make-containers.sh
@@ -0,0 +1,37 @@
#!/usr/bin/env bash
set +x

MANIFEST="$1"
ARCHS=('arm64' 'amd64')
LINUX_ARCHS=('aarch64' 'x86_64')

# podman manifest inspect docker.io/golang:1.17.7-bullseye
# SHA order is respectively arm64, amd64
DOCKER_GOLANG_SHAS=(
'docker.io/golang@sha256:55bebd100dc82c17d2111bff177156fd4aa4f5db37a396b5834c645b19208d44'
'docker.io/golang@sha256:1996dda6ea808b9909acf67f594e68948bf630bade0bf61b3a2025f5aadc3ada'
)

# podman manifest inspect docker.io/debian:bullseye-slim
# SHA order is respectively arm64, amd64
DOCKER_DEBIAN_SHAS=(
'docker.io/debian@sha256:306e5b78ae40c2467397e2cfb560dc9ff8ae935c1c7b2ed8224eb6aecc76cd32'
'docker.io/debian@sha256:7c78fedca85eec82669ff06969250175edac0750cb883628dfe7be18cb906928'
)

for i in "${!ARCHS[@]}"; do
ARCH=${ARCHS[$i]}
LINUX_ARCH=${LINUX_ARCHS[$i]}
DOCKER_GOLANG_SHA=${DOCKER_GOLANG_SHAS[$i]}
DOCKER_DEBIAN_SHA=${DOCKER_DEBIAN_SHAS[$i]}
echo "Building manifest for $MANIFEST with arch \"$ARCH\" which is linux-arch \"$LINUX_ARCH\""
podman build \
--build-arg GOLANG_BASE=$DOCKER_GOLANG_SHA \
--build-arg DEBIAN_BASE=$DOCKER_DEBIAN_SHA \
--build-arg ARCH=$ARCH \
--build-arg ARCH=$ARCH \
--build-arg LINUX_ARCH=$LINUX_ARCH \
--arch $ARCH \
--timestamp 0 \
--manifest $MANIFEST .; \
done

0 comments on commit e37ccfb

Please sign in to comment.