apiVersion: apps/v1
kind: Deployment
metadata:
  name: pastebin-app
  namespace: default
spec:
  replicas: 2  # Ahora hay 2 instancias en HA
  selector:
    matchLabels:
      app: pastebin
  template:
    metadata:
      labels:
        app: pastebin
    spec:
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            - labelSelector:
                matchLabels:
                  app: pastebin
              topologyKey: "kubernetes.io/hostname"  # Evita que se programen en el mismo nodo
      initContainers:
        - name: fix-permissions
          image: busybox
          command: ["sh", "-c", "chown -R 1000:1000 /app/uploads"]
          volumeMounts:
            - name: uploads
              mountPath: /app/uploads
      containers:
        - name: pastebin-container
          image: prietus/pastebin-app:2.22
          ports:
            - containerPort: 5000
          env:
            - name: GUNICORN_WORKERS
              value: "3"  # Limita el número de workers
            - name: POSTGRES_MAX_CONNECTIONS
              value: "10"  # Limita conexiones abiertas por pod
            - name: ELASTICSEARCH_MAX_CONNECTIONS
              value: "25"
            - name: DB_ENGINE
              value: "postgres"
            - name: POSTGRES_HOST
              value: "pastebin-postgres"
            - name: VALID_USER
              valueFrom:
                configMapKeyRef:
                  name: pastebin-config
                  key: VALID_USER
            - name: VALID_PASS
              valueFrom:
                configMapKeyRef:
                  name: pastebin-config
                  key: VALID_PASS
            - name: POSTGRES_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: pastebin-secret
                  key: POSTGRES_PASSWORD
            - name: DATABASE_URL
              value: "postgresql://mypasteuser:$(POSTGRES_PASSWORD)@pgbouncer:6432/mypastedb"
            - name: ELASTICSEARCH_HOST
              value: "http://elasticsearch:9200"
            - name: SMTP_USE_TLS
              valueFrom:
                configMapKeyRef:
                  name: pastebin-config
                  key: SMTP_USE_TLS
            - name: SMTP_USE_SSL
              valueFrom:
                configMapKeyRef:
                  name: pastebin-config
                  key: SMTP_USE_SSL
            - name: SMTP_SERVER
              value: "priet.us"
            - name: SMTP_PORT
              valueFrom:
                configMapKeyRef:
                  name: pastebin-config
                  key: SMTP_PORT
            - name: SMTP_USERNAME
              valueFrom:
                secretKeyRef:
                  name: pastebin-secret
                  key: SMTP_USERNAME
            - name: SMTP_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: pastebin-secret
                  key: SMTP_PASSWORD
            - name: DEEPSEEK_API_KEY  # Se agregó la API Key de DeepSeek desde un Secret
              valueFrom:
                secretKeyRef:
                  name: deepseek-api-key
                  key: DEEPSEEK_API_KEY
          volumeMounts:
            - name: uploads
              mountPath: /app/uploads
      volumes:
        - name: uploads
          persistentVolumeClaim:
            claimName: uploads-pvc

---
apiVersion: v1
kind: Service
metadata:
  name: pastebin-service
  namespace: default
spec:
  ports:
    - port: 80
      targetPort: 5000
  selector:
    app: pastebin
  type: ClusterIP  # Balancea tráfico entre los pods

