Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix some bugs #173

Merged
merged 1 commit into from
Nov 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions config/i18n/zh-cn.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ data:
rule_id: "告警规则"
owner_kind: "目标类型"
workload: "工作负载"
rule_group: "规则组"
rule_level: "规则级别"
name: "名称"
kind: ConfigMap
metadata:
name: zh-cn
Expand Down
2 changes: 1 addition & 1 deletion config/samples/bundle.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
2 changes: 1 addition & 1 deletion config/samples/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
4 changes: 2 additions & 2 deletions docs/crds/receiver.md
Original file line number Diff line number Diff line change
Expand Up @@ -482,9 +482,9 @@ An email receiver only receives "warning" notifications.
email:
alertSelector:
matchExpressions:
- key: serverity
- key: severity
operator: In
value:
values:
- error
- critical
```
Expand Down
4 changes: 2 additions & 2 deletions helm/templates/template.yaml
Original file line number Diff line number Diff line change
@@ -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 }}" }}
Expand All @@ -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 }}" }}
Expand Down
3 changes: 3 additions & 0 deletions helm/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,6 @@ notificationmanager:
template:
text:
name: notification-manager-template
env:
- name: TZ
value: Asia/Shanghai
3 changes: 3 additions & 0 deletions pkg/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 7 additions & 0 deletions pkg/template/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import (
"github.com/kubesphere/notification-manager/pkg/constants"
)

var (
keysNeedToHiden = []string{"rule_id"}
)

type Data struct {
Alerts Alerts `json:"alerts"`

Expand Down Expand Up @@ -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
Expand Down
125 changes: 125 additions & 0 deletions test/testdata/alert-auditing.json
Original file line number Diff line number Diff line change
@@ -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": {

}
}