Skip to content

Commit f877bb1

Browse files
fcanovaileonardocesxdNiccoloFei
authored
ci: build amd64 and arm64 images for the sidecar and the plugin (#26)
Adds support for building and publishing Docker images for both amd64 and arm64 architectures. Ensures compatibility across multiple platforms by using cross-compilation. Updates relevant configuration files for CI/CD to handle the new build process. Fixes issues related to Python version conflicts and ensures the correct directory structure in the final image. Signed-off-by: Francesco Canovai <[email protected]> Signed-off-by: Leonardo Cecchi <[email protected]> Signed-off-by: Jonathan Gonzalez V. <[email protected]> Signed-off-by: Niccolò Fei <[email protected]> Co-authored-by: Leonardo Cecchi <[email protected]> Co-authored-by: Jonathan Gonzalez V. <[email protected]> Co-authored-by: Niccolò Fei <[email protected]>
1 parent 738afcb commit f877bb1

File tree

4 files changed

+107
-13
lines changed

4 files changed

+107
-13
lines changed

Taskfile.yml

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,31 @@ tasks:
6666
sources:
6767
- ./**/*.go
6868

69-
build-image:
70-
desc: Build a container image for the plugin
69+
build-plugin-image:
70+
desc: Build the operator container image for the plugin
7171
env:
7272
# renovate: datasource=git-refs depName=docker lookupName=https://github.com/purpleclay/daggerverse currentValue=main
7373
DAGGER_DOCKER_SHA: d7438770bfab8844a89c2923b9e2942e78de5239
7474
cmds:
7575
- >
7676
GITHUB_REF= dagger -s call -m github.com/purpleclay/daggerverse/docker@${DAGGER_DOCKER_SHA}
77-
build --dir . --platform linux/amd64 image > /dev/null
77+
build --dir . --file containers/Dockerfile.plugin --platform linux/amd64 image > /dev/null
78+
79+
build-sidecar-image:
80+
desc: Build the sidecar container image for the plugin
81+
env:
82+
# renovate: datasource=git-refs depName=docker lookupName=https://github.com/purpleclay/daggerverse currentValue=main
83+
DAGGER_DOCKER_SHA: d7438770bfab8844a89c2923b9e2942e78de5239
84+
cmds:
85+
- >
86+
GITHUB_REF= dagger -s call -m github.com/purpleclay/daggerverse/docker@${DAGGER_DOCKER_SHA}
87+
build --dir . --file containers/Dockerfile.sidecar --platform linux/amd64 image > /dev/null
88+
89+
build-images:
90+
desc: Build the container images for the plugin
91+
deps:
92+
- build-plugin-image
93+
- build-sidecar-image
7894

7995
ci:
8096
desc: Run the CI pipeline
@@ -84,7 +100,7 @@ tasks:
84100
- uncommitted
85101
- lint
86102
- go-test
87-
- build-image
103+
- build-images
88104

89105
publish:
90106
desc: Build and publish a container image for the plugin
@@ -99,7 +115,8 @@ tasks:
99115
- REGISTRY_USER
100116
- REGISTRY_PASSWORD
101117
vars:
102-
IMAGE_NAME: ghcr.io/{{.GITHUB_REPOSITORY}}{{if not (hasPrefix "refs/tags/v" .GITHUB_REF)}}-testing{{end}}
118+
PLUGIN_IMAGE_NAME: ghcr.io/{{.GITHUB_REPOSITORY}}{{if not (hasPrefix "refs/tags/v" .GITHUB_REF)}}-testing{{end}}
119+
SIDECAR_IMAGE_NAME: ghcr.io/{{.GITHUB_REPOSITORY}}-sidecar{{if not (hasPrefix "refs/tags/v" .GITHUB_REF)}}-testing{{end}}
103120
# remove /merge suffix from the branch name. This is a workaround for the GitHub workflow on PRs,
104121
# where the branch name is suffixed with /merge. Prepend pr- to the branch name on PRs.
105122
IMAGE_VERSION: '{{regexReplaceAll "(\\d+)/merge" .GITHUB_REF_NAME "pr-${1}"}}'
@@ -110,8 +127,13 @@ tasks:
110127
- >
111128
dagger -s call -m github.com/purpleclay/daggerverse/docker@${DAGGER_DOCKER_SHA}
112129
--registry ghcr.io --username $REGISTRY_USER --password env:REGISTRY_PASSWORD
113-
build --dir . --platform linux/amd64
114-
publish --ref {{.IMAGE_NAME}} --tags {{.IMAGE_VERSION}}
130+
build --dir . --file containers/Dockerfile.plugin --platform linux/amd64 --platform linux/arm64
131+
publish --ref {{.PLUGIN_IMAGE_NAME}} --tags {{.IMAGE_VERSION}}
132+
- >
133+
dagger -s call -m github.com/purpleclay/daggerverse/docker@${DAGGER_DOCKER_SHA}
134+
--registry ghcr.io --username $REGISTRY_USER --password env:REGISTRY_PASSWORD
135+
build --dir . --file containers/Dockerfile.sidecar --platform linux/amd64 --platform linux/arm64
136+
publish --ref {{.SIDECAR_IMAGE_NAME}} --tags {{.IMAGE_VERSION}}
115137
116138
manifest:
117139
desc: Update the image in the Kustomization

Dockerfile renamed to containers/Dockerfile.plugin

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,42 @@ ARG TARGETARCH
55

66
WORKDIR /workspace
77
# Copy the Go Modules manifests
8-
COPY go.mod go.mod
9-
COPY go.sum go.sum
8+
COPY ../go.mod go.mod
9+
COPY ../go.sum go.sum
1010
# cache deps before building and copying source so that we don't need to re-download as much
1111
# and so that source changes don't invalidate our downloaded layer
1212
RUN go mod download
1313

1414
# Copy the go source
15-
COPY cmd/instance/main.go cmd/instance/main.go
16-
COPY api/ api/
17-
COPY internal/ internal/
15+
COPY ../cmd/operator/main.go cmd/operator/main.go
16+
COPY ../api api/
17+
COPY ../internal internal/
1818

1919
# Build
2020
# the GOARCH has not a default value to allow the binary be built according to the host where the command
2121
# was called. For example, if we call make docker-build in a local env which has the Apple Silicon M1 SO
2222
# the docker BUILDPLATFORM arg will be linux/arm64 when for Apple x86 it will be linux/amd64. Therefore,
2323
# by leaving it empty we can ensure that the container and binary shipped on it will have the same platform.
24-
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -o manager cmd/instance/main.go
24+
RUN --mount=type=cache,target=/go/pkg/mod --mount=type=cache,target=/root/.cache/go-build \
25+
CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -o manager cmd/operator/main.go
2526

2627
# Use distroless as minimal base image to package the manager binary
2728
# Refer to https://github.com/GoogleContainerTools/distroless for more details
2829
FROM gcr.io/distroless/static:nonroot
30+
31+
ENV SUMMARY="CloudNativePG Barman plugin" \
32+
DESCRIPTION="Container image that provides the barman-cloud plugin"
33+
34+
LABEL summary="$SUMMARY" \
35+
description="$DESCRIPTION" \
36+
io.k8s.display-name="$SUMMARY" \
37+
io.k8s.description="$DESCRIPTION" \
38+
name="CloudNativePG Barman plugin" \
39+
vendor="CloudNativePG Contributors" \
40+
url="https://cloudnative-pg.io/" \
41+
version="" \
42+
release="1"
43+
2944
WORKDIR /
3045
COPY --from=builder /workspace/manager .
3146
USER 65532:65532

containers/Dockerfile.sidecar

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Sidecar
2+
# The container needs to provide and build two components:
3+
# * barman-cloud
4+
# * instance plugin
5+
# Both components are built before going into a distroless container
6+
7+
# Build barman-cloud
8+
# pip will build everything inside /usr/ since this is the case
9+
# we should build and then copy every file into a destination that will
10+
# then copy into the distroless container
11+
FROM python:3.12-slim AS pythonbuilder
12+
RUN apt-get update && \
13+
apt-get install -y postgresql-common build-essential && \
14+
/usr/share/postgresql-common/pgdg/apt.postgresql.org.sh -y && \
15+
apt-get install -y libpq-dev && \
16+
pip install barman[azure,cloud,google,snappy]==3.11.1 setuptools
17+
# Prepare a new /usr/ directory with the files we'll need in the final image
18+
RUN mkdir /new-usr/ && \
19+
cp -r --parents /usr/local/lib/ /usr/lib/*-linux-gnu/ /usr/local/bin/ \
20+
/new-usr/
21+
22+
# Build instance
23+
# This step builds a simple instance app that will manage and handle
24+
# the barman-cloud commands inside the sidecar
25+
FROM --platform=$BUILDPLATFORM golang:1.23.1 AS gobuilder
26+
ENV CGO_ENABLED=0
27+
COPY .. /src
28+
ARG TARGETOS
29+
ARG TARGETARCH
30+
RUN --mount=type=cache,target=/go/pkg/mod --mount=type=cache,target=/root/.cache/go-build \
31+
GOOS=$TARGETOS GOARCH=$TARGETARCH go build -C /src -o /build/instance /src/cmd/instance/main.go
32+
33+
# Joint process
34+
# Now we put everything that was build from the origin into our
35+
# distroless container
36+
FROM gcr.io/distroless/python3-debian12:nonroot
37+
38+
ENV SUMMARY="CloudNativePG Barman plugin" \
39+
DESCRIPTION="Container image that provides the barman-cloud sidecar"
40+
41+
LABEL summary="$SUMMARY" \
42+
description="$DESCRIPTION" \
43+
io.k8s.display-name="$SUMMARY" \
44+
io.k8s.description="$DESCRIPTION" \
45+
name="CloudNativePG Barman plugin sidecar" \
46+
vendor="CloudNativePG Contributors" \
47+
url="https://cloudnative-pg.io/" \
48+
version="" \
49+
release="1"
50+
51+
COPY --from=pythonbuilder /new-usr/* /usr/
52+
COPY --from=gobuilder /build/instance /usr/local/bin/instance
53+
USER 26:26
54+
ENTRYPOINT ["/usr/local/bin/instance"]

internal/cnpgi/operator/lifecycle.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ func (impl LifecycleImplementation) LifecycleHook(
8585
Value: "/controller/wal-restore-spool",
8686
},
8787
},
88+
Command: []string{
89+
"/usr/local/bin/instance",
90+
},
8891
}, true)
8992
if err != nil {
9093
return nil, err

0 commit comments

Comments
 (0)