apiVersion: v1
kind: ConfigMap
metadata:
  name: alertmanager-config
  namespace: monitoring
data:
  alertmanager.yml: |
    global:
      resolve_timeout: 5m
      # Slack configuration (example)
      # slack_api_url: 'https://hooks.slack.com/services/YOUR/SLACK/WEBHOOK'
    
    # Templates
    templates:
      - '/etc/alertmanager/*.tmpl'
    
    # Route tree
    route:
      receiver: 'default'
      group_by: ['alertname', 'cluster', 'service']
      group_wait: 10s
      group_interval: 10s
      repeat_interval: 12h
      routes:
        - match:
            severity: critical
          receiver: 'critical-alerts'
          continue: true
        - match:
            severity: warning
          receiver: 'warning-alerts'
    
    # Inhibition rules
    inhibit_rules:
      - source_match:
          severity: 'critical'
        target_match:
          severity: 'warning'
        equal: ['alertname', 'instance']
    
    # Receivers
    receivers:
      - name: 'default'
        webhook_configs:
          - url: 'http://webhook-receiver:5001/webhook'
            send_resolved: true
      
      - name: 'critical-alerts'
        webhook_configs:
          - url: 'http://webhook-receiver:5001/webhook'
            send_resolved: true
        # Email configuration (example)
        # email_configs:
        #   - to: 'oncall@example.com'
        #     from: 'alertmanager@example.com'
        #     smarthost: 'smtp.example.com:587'
        #     auth_username: 'alertmanager@example.com'
        #     auth_password: 'password'
      
      - name: 'warning-alerts'
        webhook_configs:
          - url: 'http://webhook-receiver:5001/webhook'
            send_resolved: true
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: alertmanager
  namespace: monitoring
  labels:
    app: alertmanager
spec:
  replicas: 1
  selector:
    matchLabels:
      app: alertmanager
  template:
    metadata:
      labels:
        app: alertmanager
    spec:
      containers:
        - name: alertmanager
          image: prom/alertmanager:v0.26.0
          imagePullPolicy: IfNotPresent
          ports:
            - name: web
              containerPort: 9093
              protocol: TCP
          args:
            - '--config.file=/etc/alertmanager/alertmanager.yml'
            - '--storage.path=/alertmanager'
            - '--web.external-url=http://alertmanager:9093'
          volumeMounts:
            - name: config
              mountPath: /etc/alertmanager
            - name: storage
              mountPath: /alertmanager
          resources:
            requests:
              cpu: "100m"
              memory: "256Mi"
            limits:
              cpu: "500m"
              memory: "512Mi"
          livenessProbe:
            httpGet:
              path: /-/healthy
              port: web
            initialDelaySeconds: 30
            periodSeconds: 30
          readinessProbe:
            httpGet:
              path: /-/ready
              port: web
            initialDelaySeconds: 10
            periodSeconds: 10
      volumes:
        - name: config
          configMap:
            name: alertmanager-config
        - name: storage
          emptyDir: {}
---
apiVersion: v1
kind: Service
metadata:
  name: alertmanager
  namespace: monitoring
  labels:
    app: alertmanager
spec:
  type: ClusterIP
  ports:
    - port: 9093
      targetPort: web
      protocol: TCP
      name: web
  selector:
    app: alertmanager

