apiVersion: v1
kind: ConfigMap
metadata:
  name: prometheus-alertmanager-dingtalk
  namespace: kube-system 
  labels:
    kubernetes.io/cluster-service: "true"
    addonmanager.kubernetes.io/mode: EnsureExists
data:
  config.yaml: |
    listenUri: 0.0.0.0:8000
    logLevel: debug

    # DingTalk
    dingTalkUri: https://oapi.dingtalk.com/robot/send?access_token=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    
    # Dingtalk Security Settings Type
    # 1. CustomKeywords        最多可以设置10个关键词，消息中至少包含其中1个关键词才可以发送成功
    # 2. Endorsement           加签, 需要提供 SecretKey
    # 3. IPAddressSegment      只有来自IP地址范围内的请求才会被正常处理
    securitySettingsType: Endorsement
    secretKey: XXXXXXXXXXXXXXXXXXXXXXXXXXx
    
    templatePath: /etc/prometheus-alertmanager-dingtalk/dingtalk.tmpl
    
    allowLables:
      - severity
      - description
      - kubernetes_namespace

  dingtalk.tmpl: |
    {{ define "__title__" -}}
    [{{ .Status | ToUpper }}{{ if eq .Status "firing" }}:{{ .Alerts | len }}{{ end }}] {{ index .GroupLabels.AlertName }}
    {{- end }}
    
    
    {{ define "__alertmanagerURL" -}}
    {{ .ExternalURL }}/#/alerts?receiver={{ .Receiver }}
    {{- end }}
    
    
    {{ define "__content__" -}}
    #### **\[{{ .Status | ToUpper }}{{ if eq .Status "firing" }}:{{ .Alerts | len }}{{ end }}\]** **[{{ index .GroupLabels.AlertName }}]({{ template "__alertmanagerURL" . }})**
    {{- end }}
    
    
    {{ define "__alerts_common_labels__" }}
    {{ range .SortedAllowPairs }}
    > **{{ .Name | Title }}**: {{ .Value | html }}
    {{ end }}
    {{- end }}
    
    
    {{ define "__alerts_instance_lists__" }}
    {{ range $i, $v := . }}
    ##### **警报: {{ Increase $i }}**
    {{ range .Annotations.SortedAllowPairs }}
    > ##### **{{ .Name | Title }}**: {{ .Value | html }}
    {{ end }}
    > ###### Report From **[Prometheus]({{ .GeneratorURL }})** at **{{ FormatTime .StartsAt }}**
    {{- end }}
    ![screenshot](https://prom.i-morefun.com/MaineCoon.jpg)
    {{- end }}
    
    
    {{ define "__text__" -}}
    {{ template "__content__" . }}
    {{ template "__alerts_common_labels__" .CommonLabels }}
    {{ template "__alerts_instance_lists__" .Alerts }}
    {{- end }}
