# SPDX-License-Identifier: Apache-2.0
# MicroK8s' k8s-dqlite (v1.8.1 c22704200d18, the binary in snap microk8s 1.34/stable rev 9063, built the same way:
# `make static`, musl, dqlite v1.18.2) with ONE change: canonical/dqlite#909 backported onto dqlite v1.18.2. Upstream
# released #909 only in dqlite v1.18.8, 102 commits later (disk mode removed, snapshot format reworked), and no snap
# ships it. v1.18.2's vfsMainFileShmMap aborts (vfs.c:1508 region_index == mappedShmRegions.len) when a connection
# asks for a WAL-index region past the ones it mapped, which happens once the WAL grew while another reader held it:
# ~40 k8s-dqlite aborts on our control plane since 2026-11-23. The backport maps every region up to the requested one.
# The test stage builds plain v1.18.2 and runs #909's own regression test twice: it must ABORT unpatched and PASS
# patched. Exit: when MicroK8s ships a k8s-dqlite on a dqlite with #909, the snap's own binary returns.
ARG K8S_DQLITE_REV=c22704200d18466b1e638cbd489eba92458306cb

FROM golang:1.23.3-bookworm AS deps
RUN apt-get update -qq && DEBIAN_FRONTEND=noninteractive apt-get install -y -qq --no-install-recommends \
      build-essential automake libtool gettext autopoint tclsh tcl libsqlite3-dev pkg-config git \
      libuv1-dev liblz4-dev file > /dev/null

FROM deps AS test
COPY dqlite-909-backport.patch /patches/
RUN git clone -q --depth 1 --branch v1.18.2 https://github.com/canonical/dqlite.git /dqlite \
 && cd /dqlite && git apply --include='test/*' /patches/dqlite-909-backport.patch \
 && autoreconf -i > /dev/null && ./configure -q --enable-build-raft > /dev/null && make -s -j"$(nproc)" unit-test > /dev/null \
 && if ./unit-test vfs_extra/multiregion_wal_index > /tmp/unpatched.log 2>&1; then \
      echo "the regression test passed WITHOUT the fix: it does not reproduce dqlite#908"; exit 1; fi \
 && { grep -m1 "mappedShmRegions" /tmp/unpatched.log || tail -5 /tmp/unpatched.log; } \
 && git apply --exclude='test/*' /patches/dqlite-909-backport.patch && make -s -j"$(nproc)" unit-test > /dev/null \
 && ./unit-test vfs_extra/multiregion_wal_index && ./unit-test vfs_extra && ./unit-test vfs \
 && touch /tested

FROM deps AS build
ARG K8S_DQLITE_REV
COPY --from=test /tested /tested
COPY dqlite-909-backport.patch /patches/
WORKDIR /src
RUN git clone -q https://github.com/canonical/k8s-dqlite.git . && git checkout -q "${K8S_DQLITE_REV}" \
 && git describe --tags --exact-match | grep -qx v1.8.1 \
 && grep -qx 'TAG_DQLITE="v1.18.2"' hack/env.sh \
 && printf '#!/bin/bash\ntrue\n' > hack/deps.sh \
 && sed -i 's|^\(\s*\)git clone "${REPO_DQLITE}" --depth 1 --branch "${TAG_DQLITE}" > /dev/null$|&\n\1git -C dqlite apply /patches/dqlite-909-backport.patch|' hack/static-dqlite.sh \
 && grep -q "apply /patches/dqlite-909-backport.patch" hack/static-dqlite.sh
RUN for i in 1 2 3 4 5; do go mod download && exit 0; echo "go mod download failed (try $i)"; sleep 15; done; exit 1
RUN make static > /tmp/build.log 2>&1 || { tail -50 /tmp/build.log; exit 1; } \
 && grep -q "mappedShmRegions.len = i + 1" hack/.build/static/dqlite/src/vfs.c \
 && file bin/static/k8s-dqlite | grep -q "statically linked" \
 && bin/static/k8s-dqlite --help > /dev/null 2>&1 \
 && mkdir /out && cp bin/static/k8s-dqlite /out/k8s-dqlite && (cd /out && sha256sum k8s-dqlite > k8s-dqlite.sha256) \
 && cat /out/k8s-dqlite.sha256

FROM scratch
COPY --from=build /out/ /
