Skip to content

Commit 0295322

Browse files
committed
build(cpex): slim authbridge-cpex to musl/alpine on CPEX v0.2.0-alpha.2
The authbridge-cpex image was ~151 MB, dominated by the debian:trixie-slim runtime base needed for the glibc FFI asset — the FFI binary itself was a small part of the delta. Switch to the musl FFI asset + Alpine base and pin the CPEX release that carries the size-first profile and the cedar small-stack fix. Result: 151 MB -> ~42 MB (binary 38 MB -> 28 MB), full 9/9 hr-cpex scenarios green on the musl image. Dockerfile: - CPEX_LIBC defaults to musl; runtime base debian:trixie-slim -> alpine:3.20 (same digest the proxy/lite images pin); builder golang:bookworm -> golang:1.25-alpine + gcc/musl-dev for the CGO link. - Pin the multi-arch *index* digest for golang:1.25-alpine, not a single-arch sub-manifest: unlike the pure-Go images (CGO_ENABLED=0, which cross-compile), this links a native-arch libcpex_ffi.a, so the builder must run on the target arch. The amd64-only digest broke arm64 builds. - Install libgcc in the runtime stage — the FFI links cedar/Rust unwinder symbols (_Unwind_*) that glibc bases ship implicitly but Alpine does not. - CGO_LDFLAGS drops -ldl (musl folds it into libc). Dependencies: - CPEX_FFI_VERSION v0.2.0-alpha.1 -> v0.2.0-alpha.2 (FFI ABI unchanged, stays 2). - Go binding github.com/contextforge-org/cpex/go/cpex bumped to v0.2.0-alpha.2 in authlib and cmd/authbridge-cpex. Signed-off-by: Frederico Araujo <frederico.araujo@ibm.com>
1 parent 86724b1 commit 0295322

6 files changed

Lines changed: 48 additions & 27 deletions

File tree

authbridge/authlib/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/kagenti/kagenti-extensions/authbridge/authlib
33
go 1.25.4
44

55
require (
6-
github.com/contextforge-org/cpex/go/cpex v0.2.0-alpha.1
6+
github.com/contextforge-org/cpex/go/cpex v0.2.0-alpha.2
77
github.com/envoyproxy/go-control-plane/envoy v1.37.0
88
github.com/fsnotify/fsnotify v1.10.1
99
github.com/gobwas/glob v0.2.3

authbridge/authlib/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ github.com/containerd/platforms v0.2.1 h1:zvwtM3rz2YHPQsF2CHYM8+KtB5dvhISiXh5ZpS
3232
github.com/containerd/platforms v0.2.1/go.mod h1:XHCb+2/hzowdiut9rkudds9bE5yJ7npe7dG/wG+uFPw=
3333
github.com/contextforge-org/cpex/go/cpex v0.2.0-alpha.1 h1:HWKqhOoh3dEscE8jWe0dYiOOegLW1liQmQ/RqBj7+Ks=
3434
github.com/contextforge-org/cpex/go/cpex v0.2.0-alpha.1/go.mod h1:SuDkdY76asy9MP/72YKqhc2FBcFZ9t5PKe3CeXSO4/g=
35+
github.com/contextforge-org/cpex/go/cpex v0.2.0-alpha.2 h1:Ddpq+JFjnA7G4VUiHJPP2520GPmIv2XmikUV2n/gRd0=
36+
github.com/contextforge-org/cpex/go/cpex v0.2.0-alpha.2/go.mod h1:SuDkdY76asy9MP/72YKqhc2FBcFZ9t5PKe3CeXSO4/g=
3537
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3638
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
3739
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v0.2.0-alpha.1
1+
v0.2.0-alpha.2

authbridge/cmd/authbridge-cpex/Dockerfile

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,12 @@ ARG CPEX_FFI_VERSION
4646
ARG CPEX_FFI_ABI
4747
ARG TARGETARCH
4848
ARG TARGETOS=linux
49-
# LIBC defaults to gnu (Debian/Ubuntu container hosts). Override to
50-
# `musl` for Alpine-based deployments.
51-
ARG CPEX_LIBC=gnu
49+
# LIBC selects which CPEX FFI asset variant to link. We default to `musl`
50+
# so the runtime stage can be Alpine (~8 MB) instead of debian:trixie-slim
51+
# (~80-100 MB) — the base image, not the FFI binary, dominates this image's
52+
# size. Override to `gnu` only for a glibc runtime (then also flip the
53+
# builder/runtime bases below back to bookworm/debian).
54+
ARG CPEX_LIBC=musl
5255
ARG FFI_ASSET=cpex-ffi-${CPEX_FFI_VERSION}-${TARGETOS}-${TARGETARCH}-${CPEX_LIBC}.tar.gz
5356

5457
ARG CPEX_COSIGN_IDENTITY=https://github.com/contextforge-org/cpex/.github/workflows/release-ffi.yaml@refs/tags/${CPEX_FFI_VERSION}
@@ -101,11 +104,17 @@ RUN set -eu; \
101104
echo "[ffi] extracted libcpex_ffi.a + VERSION=$got_version + FFI_ABI=$got_abi (asserted)"
102105

103106
# ---------- Stage 2: build authbridge-cpex with cgo + -tags cpex ----
104-
# golang:bookworm (glibc) — alpine + musl would need extra ring/
105-
# tokio cross-compile setup that the design doc defers to PR 2.
106-
FROM golang:1.25-bookworm@sha256:bb979b278ffb8d31c8b07336fd187ef8fafc8766ebeaece524304483ea137e96 AS builder
107+
# golang:alpine (musl) so we link the musl CPEX FFI asset and run on the
108+
# small Alpine base. CGO needs a C compiler + musl headers (gcc, musl-dev).
109+
#
110+
# IMPORTANT: this pins the multi-arch *index* digest, not a single-arch
111+
# sub-manifest. Unlike the pure-Go images (CGO_ENABLED=0, which cross-compile
112+
# from any builder arch), this build links a native-arch libcpex_ffi.a, so the
113+
# builder must run on the target arch. An amd64-only digest would make the
114+
# arm64 build either fail to find an arm64 builder or link an incompatible .a.
115+
FROM golang:1.25-alpine@sha256:8d95af53d0d58e1759ddb4028285d9b1239067e4fbf4f544618cad0f60fbc354 AS builder
107116

108-
RUN apt-get update && apt-get install -y --no-install-recommends git ca-certificates && rm -rf /var/lib/apt/lists/*
117+
RUN apk add --no-cache git ca-certificates gcc musl-dev
109118

110119
WORKDIR /app
111120

@@ -116,27 +125,35 @@ COPY --from=ffi /ffi/libcpex_ffi.a /libs/libcpex_ffi.a
116125

117126
ENV GOWORK=off
118127
ENV CGO_ENABLED=1
119-
ENV CGO_LDFLAGS="-L/libs -lcpex_ffi -lm -ldl"
120-
# Let Go auto-download the toolchain version go.mod requires. The
121-
# pinned base image carries 1.25.1; cpex bindings need 1.25.4 (and
122-
# transitively bumps our cmd/authbridge-cpex/go.mod). GOTOOLCHAIN=auto
123-
# means "download the version go.mod asks for" — keeps the base image
124-
# pin stable across toolchain bumps.
128+
# musl folds libdl/libpthread into libc, so there is no separate -ldl to
129+
# link against (as there is on glibc). Keep -lm for the FFI's math symbols.
130+
#
131+
# No stack-size linker flag is needed: musl's small default thread stack
132+
# (128 KB) used to trip cedar-policy's stacker recursion guard ("recursion
133+
# limit reached"), but CPEX >= v0.2.0-alpha.2 wraps its cedar dispatch in
134+
# stacker::maybe_grow, so the FFI grows its own stack regardless of the host.
135+
ENV CGO_LDFLAGS="-L/libs -lcpex_ffi -lm"
136+
# Let Go auto-download the toolchain version go.mod requires. The pinned
137+
# base image carries 1.25.1; cpex bindings need 1.25.4 (and transitively
138+
# bump our cmd/authbridge-cpex/go.mod). GOTOOLCHAIN=auto means "download
139+
# the version go.mod asks for" — keeps the base image pin stable across
140+
# toolchain bumps.
125141
ENV GOTOOLCHAIN=auto
142+
# -trimpath drops absolute build paths for reproducibility; -s -w drop the
143+
# Go symbol table + DWARF (panic traces still resolve via pclntab).
126144
RUN cd cmd/authbridge-cpex && \
127-
go build -tags cpex -ldflags="-s -w" -o /authbridge-cpex .
145+
go build -tags cpex -trimpath -ldflags="-s -w" -o /authbridge-cpex .
128146

129147
# ---------- Stage 3: runtime ----------------------------------------
130-
# debian:trixie (Debian 13) for GLIBC 2.39+ — the cpex release .a is
131-
# built against that floor. bookworm (Debian 12, GLIBC 2.36) link-loads
132-
# but fails at runtime with "GLIBC_2.39 not found". When we eventually
133-
# move to a statically-linked CGO build (or the musl asset variant),
134-
# we can downgrade this back to the smaller base.
135-
FROM debian:trixie-slim@sha256:b6e2a152f22a40ff69d92cb397223c906017e1391a73c952b588e51af8883bf8
136-
137-
RUN apt-get update && apt-get install -y --no-install-recommends \
138-
bash ca-certificates && \
139-
rm -rf /var/lib/apt/lists/*
148+
# Alpine — same base + digest the authbridge-proxy / -lite images pin. The
149+
# musl FFI asset links against musl, so no glibc floor and no debian base.
150+
# entrypoint.sh is bash, so we install bash (matching the proxy image).
151+
FROM alpine:3.20@sha256:beefdbd8a1da6d2915566fde36db9db0b524eb737fc57cd1367effd16dc0d06d
152+
153+
# libgcc provides libgcc_s.so.1 — the C++/Rust stack unwinder the FFI links
154+
# against (the CPEX .a is built panic=unwind, so it references _Unwind_*).
155+
# glibc bases ship it implicitly; on Alpine it must be installed explicitly.
156+
RUN apk add --no-cache bash ca-certificates libgcc
140157

141158
COPY --from=builder --chmod=755 /authbridge-cpex /usr/local/bin/authbridge-cpex
142159
COPY --chmod=755 cmd/authbridge-cpex/entrypoint.sh /usr/local/bin/entrypoint.sh

authbridge/cmd/authbridge-cpex/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ require github.com/kagenti/kagenti-extensions/authbridge/authlib v0.0.0-00010101
66

77
require (
88
github.com/Microsoft/go-winio v0.6.2 // indirect
9-
github.com/contextforge-org/cpex/go/cpex v0.2.0-alpha.1 // indirect
9+
github.com/contextforge-org/cpex/go/cpex v0.2.0-alpha.2 // indirect
1010
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.4.0 // indirect
1111
github.com/fsnotify/fsnotify v1.10.1 // indirect
1212
github.com/go-jose/go-jose/v4 v4.1.4 // indirect

authbridge/cmd/authbridge-cpex/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ github.com/containerd/platforms v0.2.1 h1:zvwtM3rz2YHPQsF2CHYM8+KtB5dvhISiXh5ZpS
2020
github.com/containerd/platforms v0.2.1/go.mod h1:XHCb+2/hzowdiut9rkudds9bE5yJ7npe7dG/wG+uFPw=
2121
github.com/contextforge-org/cpex/go/cpex v0.2.0-alpha.1 h1:HWKqhOoh3dEscE8jWe0dYiOOegLW1liQmQ/RqBj7+Ks=
2222
github.com/contextforge-org/cpex/go/cpex v0.2.0-alpha.1/go.mod h1:SuDkdY76asy9MP/72YKqhc2FBcFZ9t5PKe3CeXSO4/g=
23+
github.com/contextforge-org/cpex/go/cpex v0.2.0-alpha.2 h1:Ddpq+JFjnA7G4VUiHJPP2520GPmIv2XmikUV2n/gRd0=
24+
github.com/contextforge-org/cpex/go/cpex v0.2.0-alpha.2/go.mod h1:SuDkdY76asy9MP/72YKqhc2FBcFZ9t5PKe3CeXSO4/g=
2325
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
2426
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2527
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=

0 commit comments

Comments
 (0)