# SPDX-License-Identifier: Apache-2.0
# NVIDIA's k8s device plugin v0.20.1 with config-driven resource naming switched back on, for the GPU queue's dev pool
# only (argocd-apps apps/nvidia-device-plugin-dev*.yaml; prod keeps the stock image). Upstream implements per-model
# resource names (config `resources.gpus: [{pattern, name}]`, first match wins, the default "*" -> nvidia.com/gpu last)
# but v0.20.1 drops that config at startup (spec.DisableResourceNamingInConfig, "not yet supported"). The patch removes
# that one call and nothing else, so a mixed node advertises its RTX 5090 as nvidia.com/rtx5090 and its other cards as
# nvidia.com/gpu: a job gets the card model it requested, per card (docs/gpu-queue.md, "Mixed nodes").
# Exit: back to the stock image (and this directory deleted) once upstream enables resource naming.
# The image is the stock one with only /usr/bin/nvidia-device-plugin replaced, built from the upstream tag as upstream
# builds it (deployments/container/Dockerfile: golang:1.27.1, `make cmds`).
ARG UPSTREAM_TAG=v0.20.1
ARG UPSTREAM_IMAGE=nvcr.io/nvidia/k8s-device-plugin:v0.20.1@sha256:27c1b2553a690ca3d29889dcd61df97525d46f58272c0be8dc63c2b18f120075

FROM golang:1.27.1 AS build
ARG UPSTREAM_TAG
WORKDIR /build
RUN git clone --depth 1 --branch "${UPSTREAM_TAG}" https://github.com/NVIDIA/k8s-device-plugin.git .
COPY resource-naming.patch /tmp/
RUN git apply --verbose /tmp/resource-naming.patch \
 && ! grep -rn "DisableResourceNamingInConfig(config)" cmd/nvidia-device-plugin/ \
 && mkdir /artifacts \
 && make PREFIX=/artifacts VERSION="${UPSTREAM_TAG}-arbi" GIT_COMMIT="$(git rev-parse HEAD)" cmd-nvidia-device-plugin

FROM ${UPSTREAM_IMAGE}
ARG UPSTREAM_TAG
LABEL org.opencontainers.image.version="${UPSTREAM_TAG}-arbi" \
      org.opencontainers.image.description="NVIDIA device plugin ${UPSTREAM_TAG} with config resource naming enabled (arbi-serve docker/nvidia-device-plugin)"
COPY --from=build /artifacts/nvidia-device-plugin /usr/bin/nvidia-device-plugin
