# Source: pwnedworker/templates/deployment-worker.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: pwnedworker-worker
  labels:
    app.kubernetes.io/name: pwnedworker
    app.kubernetes.io/instance: pwnedworker
    app.kubernetes.io/managed-by: Helm
    helm.sh/chart: pwnedworker-0.1.0
    app.kubernetes.io/component: worker
spec:
  replicas: 1
  selector:
    matchLabels:
      app.kubernetes.io/name: pwnedworker
      app.kubernetes.io/instance: pwnedworker
      app.kubernetes.io/component: worker
  template:
    metadata:
      labels:
        app.kubernetes.io/name: pwnedworker
        app.kubernetes.io/instance: pwnedworker
        app.kubernetes.io/component: worker
    spec:
      serviceAccountName: pwnedworker
      nodeSelector:
        kubernetes.io/arch: amd64
      # O Kubernetes não tem "depends_on": Pods sobem em paralelo. Quem
      # aplica a migração é o entrypoint do Deployment "web", então sem isso
      # o worker pode começar a consumir a fila com o schema ainda
      # incompleto. "migrate --check" sai com código != 0 enquanto houver
      # migração pendente (e também enquanto o Postgres não aceitar conexão),
      # então o init trava aqui até o banco estar realmente pronto.
      initContainers:
        - name: wait-for-migrations
          image: "registry.7dev.net.br/pwnedworker/app:0.1.15"
          imagePullPolicy: IfNotPresent
          command:
            - "sh"
            - "-c"
            - "until python manage.py migrate --check; do echo 'aguardando migracoes do web...'; sleep 5; done"
          envFrom:
            - configMapRef:
                name: pwnedworker
            - secretRef:
                name: pwnedworker
      containers:
        - name: worker
          image: "registry.7dev.net.br/pwnedworker/app:0.1.15"
          imagePullPolicy: IfNotPresent
          # Sobrescreve o entrypoint padrão da imagem (que sobe o Gunicorn) —
          # mesmo padrão do serviço "worker" no docker-compose.prod.yml.
          command: ["celery"]
          args:
            - "-A"
            - "pwnedworker"
            - "worker"
            - "-l"
            - "info"
            - "--concurrency=4"
          envFrom:
            - configMapRef:
                name: pwnedworker
            - secretRef:
                name: pwnedworker
          livenessProbe:
            exec:
              command:
                - "celery"
                - "-A"
                - "pwnedworker"
                - "inspect"
                - "ping"
            initialDelaySeconds: 20
            periodSeconds: 30
            timeoutSeconds: 10
          resources:
            limits:
              cpu: 500m
              memory: 1Gi
            requests:
              cpu: 250m
              memory: 512Mi
