apiVersion: apps/v1
kind: Deployment
metadata:
  name: pastebin-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: pastebin
  template:
    metadata:
      labels:
        app: pastebin
    spec:
      # InitContainers para ajustar permisos de ambos volúmenes
      initContainers:
        - name: fix-permissions-database
          image: busybox
          command: ["sh", "-c", "chown -R 1000:1000 /app/database"]
          volumeMounts:
            - name: database
              mountPath: /app/database
        - name: fix-permissions-uploads
          image: busybox
          command: ["sh", "-c", "chown -R 1000:1000 /app/uploads"]
          volumeMounts:
            - name: uploads
              mountPath: /app/uploads
      # Contenedor principal
      containers:
        - name: pastebin-container
          image: prietus/pastebin-app:1.4.1.1
          ports:
            - containerPort: 5000
          securityContext:
            runAsUser: 0
            runAsGroup: 0
          env:
            # Variables de entorno para configuración SMTP
            - 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
              valueFrom:
                configMapKeyRef:
                  name: pastebin-config
                  key: SMTP_SERVER
            - 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
            # Variables adicionales
            - name: VALID_USER
              value: "admin"
            - name: VALID_PASS
              value: "password"
            - name: SECRET_KEY
              valueFrom:
                secretKeyRef:
                  name: pastebin-secret
                  key: SECRET_KEY
            - name: JWT_EXP_DELTA_SECONDS
              valueFrom:
                configMapKeyRef:
                  name: pastebin-config
                  key: JWT_EXP_DELTA_SECONDS
          volumeMounts:
            - name: database
              mountPath: /app/database
            - name: uploads
              mountPath: /app/uploads
      # Declaración de volúmenes
      volumes:
        - name: database
          persistentVolumeClaim:
            claimName: database-pvc
        - name: uploads
          persistentVolumeClaim:
            claimName: uploads-pvc

