apiVersion: v1
kind: ConfigMap
metadata:
  name: community-alertmanager
  namespace: community-monitor
data:
  alertmanager.yml: |
    route:
      receiver: "prometheus-alert-center-notifications"
      repeat_interval: 12h
      group_wait: 30s
      group_interval: 5m
      group_by: [ alertname ]
      routes:
      - receiver: prometheus-alert-center-notifications
        continue: true
      - receiver: slack-notifications
        continue: true

    receivers:
      - name: slack-notifications
        slack_configs:
          - channel: community-sre-alert
            api_url: https://hooks.slack.com/services/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
            send_resolved: true
            color: '{{ template "slack.color" . }}'
            title: '{{ template "slack.title" . }}'
            text: '{{ template "slack.text" . }}'
      - name: prometheus-alert-center-notifications
        webhook_configs:
        - send_resolved: true
          url: http://prometheus-alert-center/prometheusalert?type=fs&tpl=prometheus-fs&fsurl=https://open.feishu.cn/open-apis/bot/v2/hook/b67e6ac4-0af4-42d3-9a70-e1d454f23094&at=all
          max_alerts: 0

    templates:
    - "/config/slack.tmpl"
  slack.tmpl: |
    {{/* Severity of the alert */}}
    {{ define "__alert_severity" -}}
        {{- if eq .CommonLabels.severity "critical" -}}
        *Severity:* `Critical`
        {{- else if eq .CommonLabels.severity "warning" -}}
        *Severity:* `Warning`
        {{- else if eq .CommonLabels.severity "info" -}}
        *Severity:* `Info`
        {{- else -}}
        *Severity:* :question: {{ .CommonLabels.severity }}
        {{- end }}
    {{- end }}

    {{/* Title of the Slack alert */}}
    {{ define "slack.title" -}}
      [{{ .Status | toUpper -}}
      {{ if eq .Status "firing" }}:{{ .Alerts.Firing | len }}{{- end -}}
      ] {{ .CommonLabels.alertname }}
    {{- end }}


    {{/* Color of Slack attachment (appears as line next to alert )*/}}
    {{ define "slack.color" -}}
        {{ if eq .Status "firing" -}}
            {{ if eq .CommonLabels.severity "warning" -}}
                warning
            {{- else if eq .CommonLabels.severity "critical" -}}
                danger
            {{- else -}}
                #439FE0
            {{- end -}}
        {{ else -}}
        good
        {{- end }}
    {{- end }}

    {{/* The text to display in the alert */}}
    {{ define "slack.text" -}}

        {{ template "__alert_severity" . }}
        {{- if (index .Alerts 0).Annotations.summary }}
        {{- "\n" -}}
        *Summary:* {{ (index .Alerts 0).Annotations.summary }}
        {{- end }}

        {{ range .Alerts }}

            {{- if .Annotations.description }}
            {{- "\n" -}}
            {{ .Annotations.description }}
            {{- "\n" -}}
            {{- end }}
            {{- if .Annotations.message }}
            {{- "\n" -}}
            {{ .Annotations.message }}
            {{- "\n" -}}
            {{- end }}

        {{- end }}

    {{- end }}

---

apiVersion: apps/v1
kind: Deployment
metadata:
  name: alertmanager
  namespace: community-monitor
  labels:
    app: community-alertmanager
spec:
  selector:
    matchLabels:
      app: community-alertmanager
  replicas: 1
  template:
    metadata:
      labels:
        app: community-alertmanager
    spec:
      containers:
      - name: alertmanager
        image: prom/alertmanager:v0.24.0
        volumeMounts:
        - name: data-volume
          mountPath: /alertmanager/data
        - name: alertmanager-conf
          mountPath: /etc/alertmanager/alertmanager.yml
          subPath: alertmanager.yml
        - name: alertmanager-conf
          mountPath: /config/slack.tmpl
          subPath: slack.tmpl
        livenessProbe:
          httpGet:
            path: /-/healthy
            port: http
          periodSeconds: 10
          initialDelaySeconds: 10
          timeoutSeconds: 4
        readinessProbe:
          httpGet:
            path: /-/ready
            port: http
          initialDelaySeconds: 3
          periodSeconds: 5
          successThreshold: 1
          timeoutSeconds: 3
        ports:
        - containerPort: 9093
          name: http
        securityContext:
          runAsNonRoot: true
          runAsUser: 1000
        resources:
          limits:
            cpu: "1"
            memory: 512Mi
          requests:
            cpu: "0.25"
            memory: 128Mi
      - name: config-reloader
        image: uhub.service.ucloud.cn/uk8s/configmap-reload:v0.3.0
        imagePullPolicy: IfNotPresent
        args:
        - -webhook-url=http://127.0.0.1:9093/-/reload
        - -volume-dir=/etc/alertmanager/config
        resources:
          limits:
            cpu: 100m
            memory: 25Mi
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: FallbackToLogsOnError
        volumeMounts:
        - name: alertmanager-conf
          mountPath: /etc/alertmanager/config
          readOnly: true
      volumes:
      - name: data-volume
        persistentVolumeClaim:
          claimName: community-alertmanager-ufs-01
      - name: alertmanager-conf
        configMap:
          name: community-alertmanager
          defaultMode: 420

---

apiVersion: v1
kind: Service
metadata:
  labels:
    app: community-alertmanager
  name: community-alertmanager
  namespace: community-monitor
spec:
  ports:
  - name: http
    port: 80
    protocol: TCP
    targetPort: http
  selector:
    app: community-alertmanager

---

apiVersion: v1
kind: PersistentVolume
metadata:
  name: community-alertmanager-ufs-01
spec:
  capacity:
    storage: 50Gi
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  nfs:
    path: /community-monitor/alertmanager/
    server: 10.19.173.98
  mountOptions:
    - nolock
    - nfsvers=4.0

---

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: community-alertmanager-ufs-01
  namespace: community-monitor
spec:
  accessModes:
  - ReadWriteMany
  resources:
    requests:
      storage: 50Gi
