# Backup tooling image for the three nightly state-backup CronJobs
# (monitoring/grafana-backup, forgejo/cluster-backup, sirius-growth/leadgen-backup).
#
# Why this exists (card t_9d0428d8): every one of those jobs used to run
# `apk add --no-cache rsync openssh-client` at container start, which dialled
# dl-cdn.alpinelinux.org (Fastly anycast 151.101.x.132:443) on every run. That
# made the nightly backup depend on the public internet, tripped the
# `egress-new-dst` flow alert whenever Fastly rotated an address, and put a
# package install on the one pod that carries the Grafana PVC and an SSH key.
# The toolchain is baked in at build time instead, and the pods get an egress
# NetworkPolicy that admits DNS + the backup host only.
#
# Base image is pinned by digest (multi-arch index digest; this build is
# linux/amd64 because all three jobs are nodeSelector'd to siriussec).
FROM python:3.12-alpine@sha256:c5ba3deef00c7758561a0f03c26f44f10589263d40e732b8748e0d1f38f6dd89

# kubectl is needed by forgejo/cluster-backup only (CNPG `get cluster` +
# `exec` pg_dump); the cluster runs v1.36.4, so the client is pinned to the
# same minor and its release checksum is verified at build time.
ARG KUBECTL_VERSION=v1.36.4
ARG KUBECTL_SHA256=8b8f088da2dab964f853b38464033b1be15ede2839eca751482357c45abdd05a

LABEL org.opencontainers.image.title="backup-tools" \
      org.opencontainers.image.description="python3.12-alpine + rsync + openssh-client + bash + kubectl for the nightly state backups" \
      org.opencontainers.image.source="https://forgejo.siriusdevops.com/sirius/homek8s"

RUN set -eux; \
    # bash: cluster-backup and leadgen-backup use arrays, `set -o pipefail`
    # and `tee >(...)` process substitution, so /bin/sh (busybox ash) is not
    # enough -- the CronJob entrypoints stay /bin/bash, unchanged.
    apk add --no-cache rsync openssh-client bash; \
    test "${TARGETARCH:-amd64}" = "amd64" || { echo "kubectl pin is amd64-only; the backup jobs run on siriussec" >&2; exit 1; }; \
    apk add --no-cache --virtual .fetch curl ca-certificates; \
    curl -fsSL -o /usr/local/bin/kubectl \
      "https://dl.k8s.io/release/${KUBECTL_VERSION}/bin/linux/amd64/kubectl"; \
    echo "${KUBECTL_SHA256}  /usr/local/bin/kubectl" | sha256sum -c -; \
    chmod 0755 /usr/local/bin/kubectl; \
    apk del .fetch; \
    rm -rf /var/cache/apk/*

# Build-time proof that nothing has to be installed at run time.
RUN set -eux; \
    rsync --version | head -1; \
    ssh -V; \
    bash --version | head -1; \
    python3 --version; \
    kubectl version --client

CMD ["/bin/sh"]
