# SPDX-FileCopyrightText: Copyright (c) 2025 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 -}}
{{- $dynNsAll     := default .Values.dynamoNamespace .Values.epp.dynamo.namespace -}}
{{- $ns           := ternary (required "set epp.dynamo.namespace (or top-level dynamoNamespace) when epp.useDynamo=true" $dynNsAll) "" $useDynamo -}}
{{- $comp         := default "backend" .Values.epp.dynamo.component -}}
{{- $kv           := default "16" .Values.epp.dynamo.kvBlockSize -}}
{{- $std          := .Values.extension.standardImage -}}
{{- $dyn          := .Values.extension.dynamoImage -}}
{{- $fallback     := ternary $dyn $std .Values.epp.useDynamo -}}
{{- $eppImage     := default $fallback .Values.extension.image -}}


---  # <-- start of actual YAML document
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 }}
          - -poolName
          - "{{ .Values.model.shortName }}-pool"
          - -poolNamespace
          - "{{ .Release.Namespace }}"
          - -v
          - "4"
          - --zap-encoder
          - "json"
          - -grpcPort
          - "9002"
          - -grpcHealthPort
          - "9003"
          {{- if $useDynamo }}
          - -configFile
          - "{{ .Values.epp.configFile }}"
          {{- end }}
        {{- end }}

        {{- if $useDynamo }}
        volumeMounts:
          - name: epp-config
            mountPath: /etc/epp
            readOnly: true
        {{- end }}

        env:
        {{- if $useDynamo }}
          - name: ETCD_ENDPOINTS
            value: "{{ $platformName }}-etcd.{{ $platformNs }}:2379"
          - name: NATS_SERVER
            value: "nats://{{ $platformName }}-nats.{{ $platformNs }}:4222"
          - name: DYN_NAMESPACE
            value: "{{ $ns }}"
          - name: DYNAMO_COMPONENT
            value: "{{ $comp }}"
          - name: DYNAMO_KV_BLOCK_SIZE
            value: "{{ $kv }}"
          - name: DYNAMO_ROUTER_REPLICA_SYNC
            value: "true"
          - name: USE_STREAMING
            value: "true"
        {{- end }}
        {{- range .Values.epp.extraEnv }}
          - name: {{ .name }}
            value: {{ .value | quote }}
        {{- end }}

        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

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