# 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.
# if deploymentType is empty, or explicitly set to basic, use basic as default
{{- if or (not .Values.deploymentType) (eq .Values.deploymentType "basic") -}}
{{- range $serviceName, $serviceSpec := .Values.spec.services }}
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ $.Release.Name }}-{{ $serviceName | lower }}
  labels:
    app: {{ $.Release.Name }}-{{ $serviceName | lower }}
spec:
  replicas: {{ $serviceSpec.replicas }}
  selector:
    matchLabels:
      app: {{ $.Release.Name }}-{{ $serviceName | lower }}
  template:
    metadata:
      labels:
        app: {{ $.Release.Name }}-{{ $serviceName | lower }}
    spec:
      {{- if $.Values.imagePullSecrets }}
      imagePullSecrets:
        {{ $.Values.imagePullSecrets | toYaml | nindent 8 }}
      {{- end }}
      containers:
      - name: {{ $.Release.Name }}-{{ $serviceName | lower }}
        image: {{ $serviceSpec.extraPodSpec.mainContainer.image }}
        workingDir: {{ $serviceSpec.extraPodSpec.mainContainer.workingDir }}
        {{- if $serviceSpec.extraPodSpec.mainContainer.command }}
        command:
        {{- $serviceSpec.extraPodSpec.mainContainer.command | toYaml | nindent 8 }}
        {{- end }}
        {{- if $serviceSpec.extraPodSpec.mainContainer.args }}
        args:
        {{- $serviceSpec.extraPodSpec.mainContainer.args | toYaml | nindent 8 }}
        {{- end }}
        {{ if $serviceSpec.resources }}
        resources:
          requests:
            {{ if $serviceSpec.resources.cpu }}
            cpu: "{{ $serviceSpec.resources.cpu }}"
            {{ end }}
            {{ if $serviceSpec.resources.memory }}
            memory: "{{ $serviceSpec.resources.memory }}"
            {{ end }}
            {{ if $serviceSpec.resources.gpu }}
            nvidia.com/gpu: "{{ $serviceSpec.resources.gpu }}"
            {{ end }}
          limits:
            {{ if $serviceSpec.resources.cpu }}
            cpu: "{{ $serviceSpec.resources.cpu }}"
            {{ end }}
            {{ if $serviceSpec.resources.memory }}
            memory: "{{ $serviceSpec.resources.memory }}"
            {{ end }}
            {{ if $serviceSpec.resources.gpu }}
            nvidia.com/gpu: "{{ $serviceSpec.resources.gpu }}"
            {{ end }}
        {{ end }}
        {{- if $serviceSpec.envFromSecret }}
        envFrom:
        - secretRef:
            name: {{ $serviceSpec.envFromSecret }}
        {{- end }}
        env:
        - name: DYNAMO_PORT
          value: "{{ $.Values.dynamoPort | default 8000 }}"
        {{- if $.Values.etcdAddr }}
        - name: ETCD_ENDPOINTS
          value: "{{ $.Values.etcdAddr }}"
        {{- end }}
        {{- if $.Values.natsAddr }}
        - name: NATS_SERVER
          value: "{{ $.Values.natsAddr }}"
        {{- end }}
        ports:
        - name: health
          containerPort: {{ $.Values.healthPort | default 5000 }}
        livenessProbe:
          {{- if $serviceSpec.livenessProbe }}
          {{ $serviceSpec.livenessProbe | toYaml | nindent 10 }}
          {{- else }}
          initialDelaySeconds: 60
          periodSeconds: 60
          timeoutSeconds: 5
          failureThreshold: 10
          successThreshold: 1
          httpGet:
            path: /healthz
            port: health
            scheme: HTTP
          {{- end }}
        readinessProbe:
          {{- if $serviceSpec.readinessProbe }}
          {{ $serviceSpec.readinessProbe | toYaml | nindent 10 }}
          {{- else }}
          initialDelaySeconds: 60
          periodSeconds: 60
          timeoutSeconds: 5
          failureThreshold: 10
          successThreshold: 1
          httpGet:
            path: /readyz
            port: health
            scheme: HTTP
          {{- end }}
{{- end }}
{{- end }}