diff --git a/config/i18n/zh-cn.yaml b/config/i18n/zh-cn.yaml index 82b08402..326c1b27 100644 --- a/config/i18n/zh-cn.yaml +++ b/config/i18n/zh-cn.yaml @@ -31,6 +31,9 @@ data: rule_id: "告警规则" owner_kind: "目标类型" workload: "工作负载" + rule_group: "规则组" + rule_level: "规则级别" + name: "名称" kind: ConfigMap metadata: name: zh-cn diff --git a/config/samples/bundle.yaml b/config/samples/bundle.yaml index 6cbb10e4..b780a0a5 100644 --- a/config/samples/bundle.yaml +++ b/config/samples/bundle.yaml @@ -21,7 +21,7 @@ data: content: {{ range .Alerts }} - - tag: text - text: '{{ template "nm.default.message" . }}'{{ range .Labels.SortedPairs }} + text: "{{ template "nm.default.message" . }}"{{ range .Labels.SortedPairs }} - - tag: text text: ' {{ .Name | translate }}: {{ .Value }}' {{- end }} diff --git a/config/samples/template.yaml b/config/samples/template.yaml index 65266d6e..9a773e72 100644 --- a/config/samples/template.yaml +++ b/config/samples/template.yaml @@ -21,7 +21,7 @@ data: content: {{ range .Alerts }} - - tag: text - text: '{{ template "nm.default.message" . }}'{{ range .Labels.SortedPairs }} + text: "{{ template "nm.default.message" . }}"{{ range .Labels.SortedPairs }} - - tag: text text: ' {{ .Name | translate }}: {{ .Value }}' {{- end }} diff --git a/docs/crds/receiver.md b/docs/crds/receiver.md index c76035d0..b5839189 100644 --- a/docs/crds/receiver.md +++ b/docs/crds/receiver.md @@ -482,9 +482,9 @@ An email receiver only receives "warning" notifications. email: alertSelector: matchExpressions: - - key: serverity + - key: severity operator: In - value: + values: - error - critical ``` diff --git a/helm/templates/template.yaml b/helm/templates/template.yaml index 570c08e7..8fbf256f 100644 --- a/helm/templates/template.yaml +++ b/helm/templates/template.yaml @@ -1,7 +1,7 @@ apiVersion: v1 data: template: | - {{ "{{ define \"nm.default.message\" }}{{ if ne (len .Status) 0 }}[{{ .Status | translate }}] {{ end }}{{ .Message }}{{ end }}" }} + {{ "{{ define \"nm.default.message\" }}{{ if ne (len .Status) 0 }}[{{ .Status | translate }}] {{ end }}{{ . | message }}{{ end }}" }} {{ "{{ define \"nm.default.message.cn\" }}{{ if ne (len .Status) 0 }}[{{ .Status | translate }}] {{ end }}{{ .MessageCN }}{{ end }}" }} {{ "{{ define \"nm.default.subject\" }}{{ if eq (len .Alerts) 1 }}{{ range .Alerts }}{{ template \"nm.default.message\" . }}{{ end }}{{ else }}{{ .Alerts | len }} {{ .Status }} alerts{{ if gt (len .GroupLabels.SortedPairs) 1 }} for {{ range .GroupLabels.SortedPairs }}{{ .Name | translate }}={{ .Value }} {{ end }}{{ end }}{{ end }}{{ end }}" }} @@ -21,7 +21,7 @@ data: {{ " content:" }} {{ " {{ range .Alerts }}" }} {{ " - - tag: text" }} - {{ " text: '{{ template \"nm.default.message\" . }}'{{ range .Labels.SortedPairs }}" }} + {{ " text: \"{{ template \"nm.default.message\" . }}\"{{ range .Labels.SortedPairs }}" }} {{ " - - tag: text" }} {{ " text: ' {{ .Name | translate }}: {{ .Value }}'" }} {{ " {{- end }}" }} diff --git a/helm/values.yaml b/helm/values.yaml index 486e3855..4027efe6 100644 --- a/helm/values.yaml +++ b/helm/values.yaml @@ -85,3 +85,6 @@ notificationmanager: template: text: name: notification-manager-template + env: + - name: TZ + value: Asia/Shanghai diff --git a/pkg/template/template.go b/pkg/template/template.go index 322b4de5..aad0f040 100644 --- a/pkg/template/template.go +++ b/pkg/template/template.go @@ -41,6 +41,9 @@ var DefaultFuncs = FuncMap{ "stringSlice": func(s ...string) []string { return s }, + "escape": func(text string) string { + return strings.ReplaceAll(strings.ReplaceAll(text, "'", "\\'"), "\"", "\\") + }, } type Template struct { diff --git a/pkg/template/types.go b/pkg/template/types.go index 69e3aeb3..dc865a04 100644 --- a/pkg/template/types.go +++ b/pkg/template/types.go @@ -9,6 +9,10 @@ import ( "github.com/kubesphere/notification-manager/pkg/constants" ) +var ( + keysNeedToHiden = []string{"rule_id"} +) + type Data struct { Alerts Alerts `json:"alerts"` @@ -100,6 +104,9 @@ func (kv KV) SortedPairs() Pairs { sortStart = 0 ) for k := range kv { + if utils.StringInList(k, keysNeedToHiden) { + continue + } if k == constants.AlertName { keys = append([]string{k}, keys...) sortStart = 1 diff --git a/test/testdata/alert-auditing.json b/test/testdata/alert-auditing.json new file mode 100644 index 00000000..2fb0f33e --- /dev/null +++ b/test/testdata/alert-auditing.json @@ -0,0 +1,125 @@ +{ + "alerts": [ + { + "status": "firing", + "labels": { + "user": "admin", + "verb": "update", + "alertname": "ResourceChange", + "alerttype": "auditing", + "group": "[\"system:authenticated\"]", + "name": "default-email-config", + "requestReceivedTimestamp": "2022-11-03 15:11:56.676195 +0800 CST", + "resource": "configs" + }, + "annotations": { + "message": "admin update configs 'default-email-config'" + }, + "startsAt": "2022-11-03T07:11:59.012609645Z", + "endsAt": "0001-01-01T00:00:00Z" + }, + { + "status": "firing", + "labels": { + "alerttype": "auditing", + "group": "[\"system:authenticated\"]", + "name": "default-email-config", + "requestReceivedTimestamp": "2022-11-03 15:12:23.002229 +0800 CST", + "resource": "configs", + "user": "admin", + "verb": "update", + "alertname": "ResourceChange" + }, + "annotations": { + "message": "admin update configs 'default-email-config'" + }, + "startsAt": "2022-11-03T07:12:25.886979201Z", + "endsAt": "0001-01-01T00:00:00Z" + }, + { + "status": "firing", + "labels": { + "user": "admin", + "verb": "update", + "alertname": "ResourceChange", + "alerttype": "auditing", + "group": "[\"system:authenticated\"]", + "name": "global-email-config-secret", + "requestReceivedTimestamp": "2022-11-03 15:11:57.575266 +0800 CST", + "resource": "secrets" + }, + "annotations": { + "message": "admin update secrets 'global-email-config-secret'" + }, + "startsAt": "2022-11-03T07:11:59.011971811Z", + "endsAt": "0001-01-01T00:00:00Z" + }, + { + "status": "firing", + "labels": { + "alerttype": "auditing", + "group": "[\"system:authenticated\"]", + "name": "global-email-config-secret", + "requestReceivedTimestamp": "2022-11-03 15:12:23.883493 +0800 CST", + "resource": "secrets", + "user": "admin", + "verb": "update", + "alertname": "ResourceChange" + }, + "annotations": { + "message": "admin update secrets 'global-email-config-secret'" + }, + "startsAt": "2022-11-03T07:12:25.886428149Z", + "endsAt": "0001-01-01T00:00:00Z" + }, + { + "status": "firing", + "labels": { + "alerttype": "auditing", + "group": "[\"system:authenticated\"]", + "name": "global-email-receiver", + "requestReceivedTimestamp": "2022-11-03 15:11:58.205487 +0800 CST", + "resource": "receivers", + "user": "admin", + "verb": "update", + "alertname": "ResourceChange" + }, + "annotations": { + "message": "admin update receivers 'global-email-receiver'" + }, + "startsAt": "2022-11-03T07:11:59.011508968Z", + "endsAt": "0001-01-01T00:00:00Z" + }, + { + "status": "firing", + "labels": { + "alertname": "ResourceChange", + "alerttype": "auditing", + "group": "[\"system:authenticated\"]", + "name": "global-email-receiver", + "requestReceivedTimestamp": "2022-11-03 15:12:24.59289 +0800 CST", + "resource": "receivers", + "user": "admin", + "verb": "update" + }, + "annotations": { + "message": "admin update receivers 'global-email-receiver'" + }, + "startsAt": "2022-11-03T07:12:25.882506111Z", + "endsAt": "0001-01-01T00:00:00Z" + } + ], + "groupLabels": { + "alertname": "ResourceChange" + }, + "commonLabels": { + "alertname": "ResourceChange", + "alerttype": "auditing", + "group": "[\"system:authenticated\"]", + "user": "admin", + "verb": "update" + }, + "commonAnnotations": { + + } +}