Skip to content

Commit

Permalink
fix some bugs
Browse files Browse the repository at this point in the history
Signed-off-by: wanjunlei <[email protected]>
  • Loading branch information
wanjunlei committed Nov 25, 2022
1 parent 80c55f1 commit 0f042e9
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 6 deletions.
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": {

}
}

0 comments on commit 0f042e9

Please sign in to comment.