Skip to content
This repository was archived by the owner on Mar 28, 2025. It is now read-only.

Commit 89f5fac

Browse files
author
Wojciech Kocjan
authored
feat: support multi-arch builds of telegraf-operator (#69)
1 parent ce25ebd commit 89f5fac

File tree

4 files changed

+92
-17
lines changed

4 files changed

+92
-17
lines changed

.circleci/config.yml

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ workflows:
1010
- container_image:
1111
filters:
1212
branches:
13-
only: master
13+
only:
14+
- master
1415
container-image-release:
1516
jobs:
1617
- container_image_release:
@@ -77,28 +78,48 @@ jobs:
7778
- /go/pkg/mod
7879
when: always
7980
container_image:
80-
docker:
81-
- image: docker:stable-git
81+
# using a full VM is needed to build multi-arch container images
82+
machine:
83+
image: ubuntu-2004:202107-02
8284
steps:
8385
- checkout
84-
- setup_remote_docker
8586
- run:
8687
name: Docker build and push using SHA
8788
command: |
89+
export DOCKER_BUILDKIT=1
8890
IMAGE="quay.io/influxdb/telegraf-operator"
89-
docker build --no-cache -t "$IMAGE:$CIRCLE_SHA1" .
9091
docker login -u "$QUAY_USER" -p "$QUAY_PASS" quay.io
91-
docker push "$IMAGE:$CIRCLE_SHA1"
92+
93+
# create a new builder to support multi-arch images as the default builder doesn't support it
94+
docker buildx create --name "multiarch" || true
95+
docker buildx use "multiarch"
96+
97+
# build image
98+
docker buildx build \
99+
--output=type=registry \
100+
--platform linux/amd64,linux/arm64,linux/arm/v7 \
101+
--tag "$IMAGE:$CIRCLE_SHA1" \
102+
--file Dockerfile.multi-arch .
92103
container_image_release:
93-
docker:
94-
- image: docker:stable-git
104+
# using a full VM is needed to build multi-arch container images
105+
machine:
106+
image: ubuntu-2004:202107-02
95107
steps:
96108
- checkout
97-
- setup_remote_docker
98109
- run:
99110
name: Docker build and push using release tag
100111
command: |
112+
export DOCKER_BUILDKIT=1
101113
IMAGE="quay.io/influxdb/telegraf-operator"
102-
docker build --no-cache -t "$IMAGE:$CIRCLE_TAG" .
103114
docker login -u "$QUAY_USER" -p "$QUAY_PASS" quay.io
104-
docker push "$IMAGE:$CIRCLE_TAG"
115+
116+
# create a new builder to support multi-arch images as the default builder doesn't support it
117+
docker buildx create --name "multiarch" || true
118+
docker buildx use "multiarch"
119+
120+
# build image
121+
docker buildx build \
122+
--output=type=registry \
123+
--platform linux/amd64,linux/arm64,linux/arm/v7 \
124+
--tag "$IMAGE:$CIRCLE_TAG" \
125+
--file Dockerfile.multi-arch .

Dockerfile

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,16 @@ FROM golang:1.16 as builder
33

44
WORKDIR /workspace
55
# Copy the Go Modules manifests
6-
COPY go.mod go.mod
7-
COPY go.sum go.sum
6+
COPY go.mod go.sum ./
87
# cache deps before building and copying source so that we don't need to re-download as much
98
# and so that source changes don't invalidate our downloaded layer
109
RUN go mod download
1110

12-
# Copy the go source
11+
# Copy the go source, build script and run it
12+
# NOTE: please ensure that file list to copy is synced with Dockerfile.multi-arch
1313
COPY main.go sidecar.go handler.go class_data.go errors.go watcher.go updater.go ./
14-
15-
# Build
16-
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -o manager *.go
14+
COPY scripts/build-manager.sh /build-manager.sh
15+
RUN /build-manager.sh
1716

1817
# Use distroless as minimal base image to package the manager binary
1918
# Refer to https://github.com/GoogleContainerTools/distroless for more details

Dockerfile.multi-arch

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Build the manager binary
2+
FROM --platform=$BUILDPLATFORM golang:1.16 as builder
3+
4+
WORKDIR /workspace
5+
# Copy the Go Modules manifests
6+
COPY go.mod go.sum ./
7+
# cache deps before building and copying source so that we don't need to re-download as much
8+
# and so that source changes don't invalidate our downloaded layer
9+
RUN go mod download
10+
11+
# Copy the go source, build script and run it
12+
# NOTE: please ensure that file list to copy is synced with Dockerfile
13+
COPY main.go sidecar.go handler.go class_data.go errors.go watcher.go updater.go ./
14+
COPY scripts/build-manager.sh /build-manager.sh
15+
ARG TARGETPLATFORM
16+
ARG BUILDPLATFORM
17+
RUN /build-manager.sh $TARGETPLATFORM $BUILDPLATFORM
18+
19+
# Use distroless as minimal base image to package the manager binary
20+
# Refer to https://github.com/GoogleContainerTools/distroless for more details
21+
FROM gcr.io/distroless/static:nonroot
22+
WORKDIR /
23+
COPY --from=builder /workspace/manager .
24+
USER nonroot:nonroot
25+
26+
ENTRYPOINT ["/manager"]

scripts/build-manager.sh

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/bin/sh
2+
3+
export CGO_ENABLED=0
4+
export GO111MODULE=on
5+
export GOOS=linux
6+
7+
# default to building amd64 binary, same as prior to multi-arch support
8+
TARGETPLATFORM="${1:-linux/amd64}"
9+
10+
# default to building same platform as the target one
11+
BUILDPLATFORM="${2:-}"
12+
13+
if [ "${TARGETPLATFORM}" = "linux/amd64" ] ; then
14+
export GOARCH=amd64
15+
elif [ "${TARGETPLATFORM}" = "linux/arm64" ] ; then
16+
export GOARCH=arm64
17+
elif [ "${TARGETPLATFORM}" = "linux/arm/v7" ] ; then
18+
export GOARCH=arm
19+
else
20+
echo >&2 "Unknown TARGETPLATFORM: \"${TARGETPLATFORM}\""
21+
exit 1
22+
fi
23+
24+
echo "Compiling telegraf-operator binary for $GOOS / $GOARCH (from $TARGETPLATFORM)"
25+
if [ "$BUILDPLATFORM" != "" ] ; then
26+
echo " (using $BUILDPLATFORM for compilation)"
27+
fi
28+
29+
go build -a -o manager *.go

0 commit comments

Comments
 (0)