---
apiVersion: v1
kind: ConfigMap
metadata:
  name: litellm-config
  namespace: ai-stack
data:
  config.yaml: |
    model_list:
      # Modelos locales via Ollama
      - model_name: qwen2.5:3b
        litellm_params:
          model: ollama/qwen2.5:3b
          api_base: http://ollama.ai-stack.svc.cluster.local:11434

      - model_name: phi3.5
        litellm_params:
          model: ollama/phi3.5:latest
          api_base: http://ollama.ai-stack.svc.cluster.local:11434

      - model_name: gemma2:2b
        litellm_params:
          model: ollama/gemma2:2b
          api_base: http://ollama.ai-stack.svc.cluster.local:11434

      # Claude via Anthropic
      - model_name: claude-haiku
        litellm_params:
          model: claude-haiku-4-5
          api_key: os.environ/ANTHROPIC_API_KEY

      - model_name: claude-sonnet
        litellm_params:
          model: claude-sonnet-4-6
          api_key: os.environ/ANTHROPIC_API_KEY

      # Gemini
      - model_name: gemini-flash
        litellm_params:
          model: gemini/gemini-2.5-flash
          api_key: os.environ/GEMINI_API_KEY

      - model_name: gemini-pro
        litellm_params:
          model: gemini/gemini-3.1-pro-preview
          api_key: os.environ/GEMINI_API_KEY

    litellm_settings:
      drop_params: true
      request_timeout: 120

    general_settings:
      master_key: os.environ/LITELLM_MASTER_KEY

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: litellm
  namespace: ai-stack
spec:
  replicas: 1
  selector:
    matchLabels:
      app: litellm
  template:
    metadata:
      labels:
        app: litellm
    spec:
      containers:
        - name: litellm
          image: ghcr.io/berriai/litellm:main-latest
          ports:
            - containerPort: 4000
          command: ["litellm", "--config", "/app/config.yaml", "--port", "4000"]
          env:
            - name: ANTHROPIC_API_KEY
              valueFrom:
                secretKeyRef:
                  name: ai-stack-secrets
                  key: anthropic_key
            - name: GEMINI_API_KEY
              valueFrom:
                secretKeyRef:
                  name: ai-stack-secrets
                  key: gemini_key
            - name: LITELLM_MASTER_KEY
              valueFrom:
                secretKeyRef:
                  name: ai-stack-secrets
                  key: litellm_master_key
          volumeMounts:
            - name: config
              mountPath: /app/config.yaml
              subPath: config.yaml
          resources:
            requests:
              memory: "256Mi"
              cpu: "100m"
            limits:
              memory: "512Mi"
              cpu: "500m"
          livenessProbe:
            httpGet:
              path: /health/liveliness
              port: 4000
            initialDelaySeconds: 30
            periodSeconds: 15
      volumes:
        - name: config
          configMap:
            name: litellm-config

---
apiVersion: v1
kind: Service
metadata:
  name: litellm
  namespace: ai-stack
spec:
  selector:
    app: litellm
  ports:
    - port: 4000
      targetPort: 4000
  type: ClusterIP

---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: litellm
  namespace: ai-stack
  annotations:
    cert-manager.io/cluster-issuer: "letsencrypt-prod"
    nginx.ingress.kubernetes.io/proxy-read-timeout: "300"
spec:
  ingressClassName: nginx
  tls:
    - hosts:
        - litellm.cloud.mielito.com
      secretName: litellm-tls
  rules:
    - host: litellm.cloud.mielito.com
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: litellm
                port:
                  number: 4000
