#  SPDX-FileCopyrightText:  Copyright The Kubernetes Authors.
# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#  #
#  http://www.apache.org/licenses/LICENSE-2.0
#  #
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.
#  Modifications Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES

# Dockerfile.epp - Custom Dockerfile for GAIE EPP. This is to be used with the deploy/inference-gateway/build-epp-dynamo.sh

ARG BUILDER_IMAGE=golang:1.24
ARG BASE_IMAGE=ubuntu:22.04

############################
# Builder
############################
FROM ${BUILDER_IMAGE} AS builder

ENV CGO_ENABLED=1
# be explicit; helps cgo when linking libstdc++
ENV CC=gcc
ENV CXX=g++

# C/C++ toolchain for cgo, and libstdc++ for link-time
RUN apt-get update && apt-get install -y --no-install-recommends \
  build-essential \
  gcc g++ \
  libc6-dev \
  ca-certificates \
  && rm -rf /var/lib/apt/lists/*

ARG COMMIT_SHA=unknown
ARG BUILD_REF

WORKDIR /src

# deps first (cache)
COPY go.mod go.sum ./
RUN go mod download

# source
COPY cmd/epp ./cmd/epp
COPY pkg/epp ./pkg/epp
COPY internal ./internal
COPY api ./api

# sanity (optional)
RUN ls -la pkg/epp/scheduling/plugins/dynamo_kv_scorer/include/ || echo "Headers not found"
RUN ls -la pkg/epp/scheduling/plugins/dynamo_kv_scorer/lib/ || echo "Library not found"

# build
WORKDIR /src/cmd/epp
RUN go build \
  -ldflags="-X sigs.k8s.io/gateway-api-inference-extension/pkg/epp/metrics.CommitSHA=${COMMIT_SHA} -X sigs.k8s.io/gateway-api-inference-extension/pkg/epp/metrics.BuildRef=${BUILD_REF}" \
  -o /epp

############################
# Runtime
############################
FROM ${BASE_IMAGE} AS runtime

ARG DYNAMO_COMMIT_SHA
ENV DYNAMO_COMMIT_SHA=$DYNAMO_COMMIT_SHA

# Minimal runtime deps; include libstdc++ runtime for -lstdc++
RUN apt-get update && apt-get install -y --no-install-recommends \
  ca-certificates \
  libstdc++6 \
  && rm -rf /var/lib/apt/lists/* \
  && groupadd -r nonroot && useradd -r -g nonroot -m -d /home/nonroot nonroot \
  && mkdir -p /home/nonroot/.cache/huggingface/hub \
  && chown -R nonroot:nonroot /home/nonroot

WORKDIR /
COPY --from=builder /epp /epp

# Set HOME so ModelExpress can find the cache directory
ENV HOME=/home/nonroot

USER nonroot:nonroot
ENTRYPOINT ["/epp"]
