# =====================================================================
# AlertmanagerConfig — routing alert per severità
# Applica con: kubectl apply -f 05-alertmanager-config.yaml
#
# Richiede i Secret con le credenziali (webhook Slack, integration key
# PagerDuty) creati separatamente, es.:
#
#   kubectl create secret generic alertmanager-slack \
#     -n monitoring --from-literal=url='https://hooks.slack.com/services/XXX/YYY/ZZZ'
#
#   kubectl create secret generic alertmanager-pagerduty \
#     -n monitoring --from-literal=serviceKey='YOUR_PAGERDUTY_INTEGRATION_KEY'
# =====================================================================

apiVersion: monitoring.coreos.com/v1alpha1
kind: AlertmanagerConfig
metadata:
  name: routing-config
  namespace: monitoring
  labels:
    # deve matchare alertmanagerConfigSelector del chart
    # (di default kube-prometheus-stack seleziona per release label)
    release: kube-prometheus-stack
spec:
  route:
    groupBy: ["alertname", "namespace"]
    groupWait: 30s
    groupInterval: 5m
    repeatInterval: 4h
    receiver: "slack-default"
    routes:
      - matchers:
          - name: severity
            value: critical
            matchType: "="
        receiver: "pagerduty-critical"
        continue: true   # invia ANCHE a slack-default per visibilità
      - matchers:
          - name: severity
            value: warning
            matchType: "="
        receiver: "slack-default"

  receivers:
    - name: "slack-default"
      slackConfigs:
        - apiURL:
            name: alertmanager-slack
            key: url
          channel: "#alerts-k8s"
          sendResolved: true
          title: '{{ "{{" }} .CommonAnnotations.summary {{ "}}" }}'
          text: '{{ "{{" }} .CommonAnnotations.description {{ "}}" }}'

    - name: "pagerduty-critical"
      pagerdutyConfigs:
        - serviceKey:
            name: alertmanager-pagerduty
            key: serviceKey
          severity: "critical"
          description: '{{ "{{" }} .CommonAnnotations.summary {{ "}}" }}'
          sendResolved: true
