# 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 eq .Values.deploymentType "grove" }}
---
apiVersion: grove.io/v1alpha1
kind: PodGangSet
metadata:
  name: {{ $.Release.Name }}
  labels:
    app: {{ $.Release.Name }}
spec:
  replicas: 1
  template:
    terminationDelay: 1h
    cliques:
    {{- range $serviceName, $serviceSpec := .Values.spec.services }}
    - name: {{ $serviceName | lower }}
      spec:
        roleName: {{ $serviceName | lower }}
        replicas: {{ $serviceSpec.replicas }}
        podSpec:
          {{- if $.Values.imagePullSecrets }}
          imagePullSecrets:
            {{ $.Values.imagePullSecrets | toYaml | nindent 12 }}
          {{- end }}
          containers:
          - name: main
            image: {{ $serviceSpec.extraPodSpec.mainContainer.image }}
            {{- 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 }}
            workingDir: {{ $serviceSpec.extraPodSpec.mainContainer.workingDir }}
            {{- if $serviceSpec.extraPodSpec.mainContainer.command }}
            command:
            {{- $serviceSpec.extraPodSpec.mainContainer.command | toYaml | nindent 14 }}
            {{- else }}
              {{- if $serviceSpec.componentType | eq "frontend" }}
            command:
            - python3
              {{- else }}
            command:
            - /bin/sh
            - -c
              {{- if not $serviceSpec.extraPodSpec.mainContainer.args }}
                {{- fail (printf "spec.services[%s].extraPodSpec.mainContainer.args must be set for non-frontend components" $serviceName) }}
              {{- end }}
              {{- end }}
            {{- end }}
            {{- if $serviceSpec.extraPodSpec.mainContainer.args }}
            args:
            {{- $serviceSpec.extraPodSpec.mainContainer.args | toYaml | nindent 14 }}
            {{- else if $serviceSpec.componentType | eq "frontend" }}
            args:
            - -m
            - dynamo.frontend
            {{- end }}
            env:
            {{- if $.Values.dynamoNamespace }}
            - name: DYN_NAMESPACE
              value: {{ $.Values.dynamoNamespace }}
            {{- end }}
            {{- if $.Values.etcdAddr }}
            - name: ETCD_ENDPOINTS
              value: "{{ $.Values.etcdAddr }}"
            {{- end }}
            {{- if $.Values.natsAddr }}
            - name: NATS_SERVER
              value: "{{ $.Values.natsAddr }}"
            {{- end }}
            {{- if $serviceSpec.componentType | eq "frontend" }}
            - name: DYNAMO_PORT
              value: "{{ $.Values.dynamoPort | default 8000 }}"
            - name: DYN_HTTP_PORT
              value: "{{ $.Values.dynamoPort | default 8000 }}"
            {{- else if $serviceSpec.componentType | eq "worker" }}
            - name: DYN_SYSTEM_ENABLED
              value: "true"
            - name: DYN_SYSTEM_PORT
              value: "{{ $.Values.dynamoSystemPort | default 9090 }}"
            - name: DYN_SYSTEM_USE_ENDPOINT_HEALTH_STATUS
              value: "[\"generate\"]"
            {{- end }}
            {{- if $serviceSpec.envFromSecret }}
            envFrom:
            - secretRef:
                name: {{ $serviceSpec.envFromSecret }}
            {{- end }}
            {{- if $serviceSpec.componentType | eq "frontend" }}
            ports:
            - name: http
              containerPort: {{ $.Values.dynamoPort | default 8000 }}
              protocol: TCP
            {{- else if $serviceSpec.componentType | eq "worker" }}
            ports:
            - name: system
              containerPort: {{ $.Values.dynamoSystemPort | default 9090 }}
              protocol: TCP
            {{- end }}
            {{- if and $serviceSpec.componentType (or (eq $serviceSpec.componentType "frontend") (eq $serviceSpec.componentType "worker")) }}
            livenessProbe:
              {{- if $serviceSpec.livenessProbe }}
              {{ $serviceSpec.livenessProbe | toYaml | nindent 10 }}
              {{- else }}
              initialDelaySeconds: 60
              periodSeconds: 60
              timeoutSeconds: 5
              failureThreshold: 10
              successThreshold: 1
              {{- if $serviceSpec.componentType | eq "frontend" }}
              httpGet:
                path: /health
                port: http
              {{- else if $serviceSpec.componentType | eq "worker" }}
              httpGet:
                path: /live
                port: system
              {{- else }}
              httpGet:
                path: /healthz
                port: health
                scheme: HTTP
              {{- end }}
              {{- end }}
            readinessProbe:
              {{- if $serviceSpec.readinessProbe }}
              {{ $serviceSpec.readinessProbe | toYaml | nindent 10 }}
              {{- else }}
              initialDelaySeconds: 60
              periodSeconds: 60
              timeoutSeconds: 5
              failureThreshold: 10
              successThreshold: 1
              {{- if $serviceSpec.componentType | eq "frontend" }}
              exec:
                command:
                  - /bin/sh
                  - -c
                  - curl -s http://localhost:${DYNAMO_PORT}/health | jq -e ".status == \"healthy\""
              {{- else if $serviceSpec.componentType | eq "worker" }}
              httpGet:
                path: /health
                port: system
              {{- else }}
              httpGet:
                path: /readyz
                port: health
                scheme: HTTP
              {{- end }}
              {{- end }}
            {{- end }}
    {{- end }}
{{- end }}
