# 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.

{{- /* ------------ file-scope vars (no output) ------------ */ -}}
{{- $platformNs   := default .Release.Namespace .Values.platformNamespace -}}
{{- $platformName := default "dynamo-platform" .Values.platformReleaseName -}}
{{- $useDynamo    := default false .Values.epp.useDynamo -}}
{{- $resolvedDynNs := (include "dynamo-gaie.dynamoNamespace" .) | trim -}}
{{- $ns           := ternary (required "set dynamoGraphDeploymentName when epp.useDynamo=true" $resolvedDynNs) "" $useDynamo -}}
{{- $kv           := default "16" .Values.epp.dynamo.kvBlockSize -}}
{{- $useEtcd      := default false .Values.epp.dynamo.useEtcd -}}
{{- $eppImage     := required "extension.image is required - set via --set-string extension.image=$EPP_IMAGE or in values file" .Values.extension.image }}

---
# Deployment for the EPP (Endpoint Picker Plugin)
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .Values.model.shortName }}-epp
  namespace: {{ .Release.Namespace }}
  labels:
    app: {{ .Values.model.shortName }}-epp
spec:
  replicas: 1
  selector:
    matchLabels:
      app: {{ .Values.model.shortName }}-epp
  template:
    metadata:
      labels:
        app: {{ .Values.model.shortName }}-epp
    spec:
      terminationGracePeriodSeconds: 130

      {{- if .Values.imagePullSecrets }}
      imagePullSecrets:
      {{- range .Values.imagePullSecrets }}
        - name: {{ . | quote }}
      {{- end }}
      {{- end }}

      containers:
      - name: epp
        image: "{{ $eppImage }}"
        imagePullPolicy: {{ default "IfNotPresent" .Values.epp.imagePullPolicy }}
        args:
        {{- if .Values.epp.argsOverride }}
        {{- toYaml .Values.epp.argsOverride | nindent 8 }}
        {{- else }}
          - -pool-name
          - "{{ .Values.model.shortName }}-pool"
          - -pool-namespace
          - "{{ .Release.Namespace }}"
          - -pool-group
          - "inference.networking.k8s.io"
          - -v
          - "4"
          - --zap-encoder
          - "json"
          - -grpc-port
          - "9002"
          - -grpc-health-port
          - "9003"
          {{- if $useDynamo }}
          - -config-file
          - "{{ .Values.epp.configFile }}"
          {{- end }}
        {{- end }}

        volumeMounts:
          - name: hf-cache
            mountPath: /home/nonroot/.cache
        {{- if $useDynamo }}
          - name: epp-config
            mountPath: /etc/epp
            readOnly: true
        {{- end }}

        env:
        {{- if $useDynamo }}
          - name: POD_NAMESPACE
            valueFrom:
              fieldRef:
                fieldPath: metadata.namespace
          - name: POD_NAME
            valueFrom:
              fieldRef:
                fieldPath: metadata.name
          - name: POD_UID
            valueFrom:
              fieldRef:
                fieldPath: metadata.uid
          {{- if $useEtcd }}
          - name: ETCD_ENDPOINTS
            value: "{{ $platformName }}-etcd.{{ $platformNs }}:2379"
          {{- else }}
          - name: DYN_DISCOVERY_BACKEND
            value: "kubernetes"
          {{- end }}
          - name: NATS_SERVER
            value: "nats://{{ $platformName }}-nats.{{ $platformNs }}:4222"
          - name: DYN_NAMESPACE
            value: "{{ $ns }}"
          - name: DYN_KV_BLOCK_SIZE
            value: "{{ $kv }}"
          - name: USE_STREAMING
            value: "true"
          # HuggingFace token for downloading model config files
          # Without this, HuggingFace rate-limits requests (429 Too Many Requests)
          - name: HF_TOKEN
            valueFrom:
              secretKeyRef:
                name: hf-token-secret
                key: HF_TOKEN
                optional: true
        {{- end }}
        {{- range .Values.epp.extraEnv }}
          - name: {{ .name }}
            value: {{ .value | quote }}
        {{- end }}
          - name: RUST_LOG
            value: "debug,dynamo_llm::kv_router=trace"

        ports:
          - containerPort: 9002
          - containerPort: 9003
          - name: metrics
            containerPort: 9090
        livenessProbe:
          grpc:
            port: 9003
            service: inference-extension
          initialDelaySeconds: 5
          periodSeconds: 10
        readinessProbe:
          grpc:
            port: 9003
            service: inference-extension
          initialDelaySeconds: 5
          periodSeconds: 10

      volumes:
        - name: hf-cache
          emptyDir: {}
      {{- if $useDynamo }}
        - name: epp-config
          configMap:
            name: {{ include "dynamo-gaie.fullname" . }}-epp-config
            items:
              - key: epp-config-dynamo.yaml
                path: epp-config-dynamo.yaml
      {{- end }}
