apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata:
  name: funk-k8s-alerts
  namespace: monitoring
  labels:
    release: kube-prometheus-stack
spec:
  groups:
    - name: funk.kubernetes
      interval: 1m
      rules:

        # --- Nœuds ---
        - alert: KubeNodeNotReady
          expr: |
            kube_node_status_condition{condition="Ready",status="true"} == 0
          for: 5m
          labels:
            severity: critical
          annotations:
            summary: "Nœud k8s NotReady : {{ $labels.node }}"
            description: "Le nœud {{ $labels.node }} est NotReady depuis 5 min. Les workloads peuvent être impactés."

        # --- Pods ---
        - alert: PodCrashLooping
          expr: |
            increase(kube_pod_container_status_restarts_total[15m]) > 3
          for: 5m
          labels:
            severity: warning
          annotations:
            summary: "Pod en CrashLoop : {{ $labels.namespace }}/{{ $labels.pod }}"
            description: "{{ $labels.container }} a redémarré {{ $value | printf \"%.0f\" }} fois en 15 min."

        - alert: PodFailedLong
          expr: |
            kube_pod_status_phase{phase=~"Failed|Unknown"} == 1
          for: 10m
          labels:
            severity: warning
          annotations:
            summary: "Pod en échec : {{ $labels.namespace }}/{{ $labels.pod }}"
            description: "Le pod est en phase {{ $labels.phase }} depuis 10 min."

        - alert: PodPendingLong
          expr: |
            kube_pod_status_phase{phase="Pending"} == 1
          for: 15m
          labels:
            severity: warning
          annotations:
            summary: "Pod Pending depuis 15 min : {{ $labels.namespace }}/{{ $labels.pod }}"
            description: "Vérifier les ressources disponibles, PVCs, et les événements du pod."

        # --- Deployments ---
        - alert: DeploymentUnavailable
          expr: |
            kube_deployment_status_replicas_available
              < kube_deployment_spec_replicas
          for: 5m
          labels:
            severity: warning
          annotations:
            summary: "Deployment dégradé : {{ $labels.namespace }}/{{ $labels.deployment }}"
            description: "{{ $labels.deployment }} : {{ $value }} réplicas disponibles sur {{ $labels.replicas }} attendus."

        # --- Stockage ---
        - alert: PVCNotBound
          expr: |
            kube_persistentvolumeclaim_status_phase{phase!="Bound"} == 1
          for: 10m
          labels:
            severity: warning
          annotations:
            summary: "PVC non lié : {{ $labels.namespace }}/{{ $labels.persistentvolumeclaim }}"
            description: "La PVC est en phase {{ $labels.phase }} depuis 10 min. Vérifier le NFS provisioner."

        # --- ArgoCD ---
        - alert: ArgoCDAppOutOfSync
          expr: |
            count(argocd_app_info{sync_status!="Synced"}) > 0
          for: 30m
          labels:
            severity: warning
          annotations:
            summary: "Application(s) ArgoCD OutOfSync depuis 30 min"
            description: "{{ $value }} app(s) ne sont pas synchronisées. Vérifier http://argocd.lab.local"
