# SPDX-License-Identifier: Apache-2.0
# The GPU queue's KAI scheduler: upstream v0.18.8 plus upstream main 60f54332 (KAI-Scheduler#2545, unreleased as of
# v0.18.9). v0.18.8's informer transform drops PodScheduled from cached pods, so every scheduling cycle (1 s) re-patches
# the condition of every Pending queue pod: a full ~50 KB pod write per pending pod per second into dqlite (2026-11-26
# control-plane incident). The patch keeps PodScheduled in the cache, so an unchanged condition is not re-patched.
# Only /workspace/app is replaced in the stock image, built as upstream builds it (build/makefile: golang 1.26.10,
# CGO_ENABLED=0, cmd/scheduler/main.go). Exit: back to the stock image once a KAI release contains #2545.
ARG UPSTREAM_TAG=v0.18.8
ARG UPSTREAM_IMAGE=ghcr.io/kai-scheduler/kai-scheduler/scheduler:v0.18.8@sha256:25fe45872c262874c684dfe896c4cc2cfdd8f8f81a0ea006cfec882cff90f09e

FROM golang:1.26.10-bookworm AS build
ARG UPSTREAM_TAG
WORKDIR /build
RUN git clone --depth 1 --branch "${UPSTREAM_TAG}" https://github.com/kai-scheduler/KAI-Scheduler.git .
# All modules up front, retried: a runner's DNS can time out mid-fetch (first build, 2026-11-26).
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
COPY podscheduled-cache.patch /tmp/
RUN git apply --verbose /tmp/podscheduled-cache.patch \
 && grep -q "c.Type == v1.PodResizePending || c.Type == v1.PodScheduled" pkg/scheduler/cache/pod_transform.go \
 && CGO_ENABLED=0 go test ./pkg/scheduler/cache/ -run TestCache -args -ginkgo.focus=compactSchedulerPod \
 && CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -buildvcs=false -ldflags "\
      -X 'github.com/kai-scheduler/KAI-scheduler/pkg/scheduler/version.gitCommit=$(git rev-parse HEAD)' \
      -X 'github.com/kai-scheduler/KAI-scheduler/pkg/scheduler/version.gitVersion=${UPSTREAM_TAG}-arbi' \
      -X 'github.com/kai-scheduler/KAI-scheduler/pkg/scheduler/version.gitTreeState=dirty'" \
      -o /artifacts/app cmd/scheduler/main.go

FROM ${UPSTREAM_IMAGE}
ARG UPSTREAM_TAG
LABEL org.opencontainers.image.version="${UPSTREAM_TAG}-arbi" \
      org.opencontainers.image.description="KAI scheduler ${UPSTREAM_TAG} with KAI-Scheduler#2545 (PodScheduled kept in the pod cache; arbi-serve docker/kai-scheduler)"
COPY --from=build --chmod=0755 --chown=65532:0 /artifacts/app /workspace/app
