Skip to content

Commit 90c71fb

Browse files
committed
add receivers to notification history
Signed-off-by: wanjunlei <wanjunlei@kubesphere.io>
1 parent 369e876 commit 90c71fb

File tree

21 files changed

+272
-121
lines changed

21 files changed

+272
-121
lines changed

apis/v2beta2/common.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package v2beta2
22

33
import (
4+
"fmt"
5+
"regexp"
6+
47
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
58
"k8s.io/apimachinery/pkg/labels"
6-
"regexp"
79
)
810

911
type LabelSelector struct {
@@ -112,3 +114,29 @@ func LabelMatchSelector(label map[string]string, selector *LabelSelector) bool {
112114

113115
return ok
114116
}
117+
118+
type ValueSource struct {
119+
// Selects a key of a secret in the pod's namespace
120+
// +optional
121+
SecretKeyRef *SecretKeySelector `json:"secretKeyRef,omitempty" protobuf:"bytes,4,opt,name=secretKeyRef"`
122+
}
123+
124+
type Credential struct {
125+
// +optional
126+
Value string `json:"value,omitempty" protobuf:"bytes,2,opt,name=value"`
127+
ValueFrom *ValueSource `json:"valueFrom,omitempty" protobuf:"bytes,3,opt,name=valueFrom"`
128+
}
129+
130+
func (c *Credential) ToString() string {
131+
if len(c.Value) > 0 {
132+
return c.Value
133+
}
134+
135+
if c.ValueFrom != nil {
136+
if c.ValueFrom.SecretKeyRef != nil {
137+
return fmt.Sprintf("%s/%s/%s", c.ValueFrom.SecretKeyRef.Namespace, c.ValueFrom.SecretKeyRef.Name, c.ValueFrom.SecretKeyRef.Key)
138+
}
139+
}
140+
141+
return ""
142+
}

apis/v2beta2/notificationmanager_types.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,6 @@ type ConfigmapKeySelector struct {
5151
Key string `json:"key,omitempty" protobuf:"bytes,2,opt,name=key"`
5252
}
5353

54-
type ValueSource struct {
55-
// Selects a key of a secret in the pod's namespace
56-
// +optional
57-
SecretKeyRef *SecretKeySelector `json:"secretKeyRef,omitempty" protobuf:"bytes,4,opt,name=secretKeyRef"`
58-
}
59-
60-
type Credential struct {
61-
// +optional
62-
Value string `json:"value,omitempty" protobuf:"bytes,2,opt,name=value"`
63-
ValueFrom *ValueSource `json:"valueFrom,omitempty" protobuf:"bytes,3,opt,name=valueFrom"`
64-
}
65-
6654
// Sidecar defines a sidecar container which will be added to the notification manager deployment pod.
6755
type Sidecar struct {
6856
// The type of sidecar, it can be specified to any value.

config/samples/template.yaml

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,50 @@ data:
1818
{{ end }}
1919
{{ end }}{{- end }}
2020
21+
{{ define "nm.feishu.message" }}{{ if ne (len .Status) 0 }}[{{ .Status | translate }}] {{ end }}{{ . | message | escape }}{{ end }}
2122
{{ define "nm.feishu.post" }}
22-
en_us:
23-
content:
24-
{{ range .Alerts }}
25-
- - tag: text
26-
text: "{{ template "nm.default.message" . }}"{{ range .Labels.SortedPairs }}
27-
- - tag: text
28-
text: ' {{ .Name | translate }}: {{ .Value }}'
29-
{{- end }}
30-
{{ if ne (len .Annotations.SortedPairs.Filter) 0 }}
31-
{{ range .Annotations.SortedPairs.Filter "runbook_url" "message" "summary" "summary_cn" }}
32-
- - tag: text
33-
text: ' {{ .Name | translate }}: {{ .Value }}'
34-
{{- end }}
35-
{{- end }}
36-
- - tag: text
37-
text: ' '
38-
{{- end }}
23+
{
24+
"en_us": {
25+
"content": [
26+
{{- range $index, $alert := .Alerts }}
27+
{{- if ne $index 0 }}
28+
{{- "," }}
29+
{{- end }}
30+
[
31+
{
32+
"tag": "text",
33+
"text": "{{ template "nm.feishu.message" $alert }}"
34+
}
35+
],
36+
{{ range $alert.Labels.SortedPairs -}}
37+
[
38+
{
39+
"tag": "text",
40+
"text": " {{ .Name | translate }}: {{ .Value }}"
41+
}
42+
],
43+
{{ end -}}
44+
{{- if ne (len $alert.Annotations.SortedPairs.Filter) 0 -}}
45+
{{- range $alert.Annotations.SortedPairs.Filter "runbook_url" "message" "summary" "summary_cn" -}}
46+
[
47+
{
48+
"tag": "text",
49+
"text": " {{ .Name | translate }}: {{ .Value }}"
50+
}
51+
],
52+
{{ end -}}
53+
{{ end -}}
54+
[
55+
{
56+
"tag": "text",
57+
"text": " "
58+
}
59+
]
60+
{{- end }}
61+
]
62+
}
63+
}
3964
{{- end }}
40-
4165
{{ define "nm.feishu.text" }}{{ range .Alerts }}{{ template "nm.default.message" . }}{{ "\n" }}{{ range .Labels.SortedPairs }} {{ .Name | translate }}: {{ .Value }}{{ "\n" }}{{ end }}{{ range .Annotations.SortedPairs.Filter "runbook_url" "message" "summary" "summary_cn" }} {{ .Name | translate }}: {{ .Value }}{{ "\n" }}{{ end }}{{ "\n" }}{{ end }}{{- end }}
4266
4367
{{ define "nm.default.html" }}

go.mod

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.20
55
require (
66
github.com/alibabacloud-go/darabonba-openapi v0.1.5
77
github.com/alibabacloud-go/dysmsapi-20170525/v2 v2.0.1
8-
github.com/aws/aws-sdk-go-v2 v1.16.4
8+
github.com/aws/aws-sdk-go-v2 v1.16.5
99
github.com/aws/aws-sdk-go-v2/config v1.15.7
1010
github.com/aws/aws-sdk-go-v2/credentials v1.12.2
1111
github.com/aws/aws-sdk-go-v2/service/sns v1.17.6
@@ -51,7 +51,7 @@ require (
5151
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.9.5 // indirect
5252
github.com/aws/aws-sdk-go-v2/service/sso v1.11.5 // indirect
5353
github.com/aws/aws-sdk-go-v2/service/sts v1.16.6 // indirect
54-
github.com/aws/smithy-go v1.11.2 // indirect
54+
github.com/aws/smithy-go v1.11.3 // indirect
5555
github.com/beorn7/perks v1.0.1 // indirect
5656
github.com/cespare/xxhash/v2 v2.2.0 // indirect
5757
github.com/davecgh/go-spew v1.1.1 // indirect
@@ -112,4 +112,7 @@ require (
112112
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
113113
)
114114

115-
replace github.com/miekg/dns v1.0.14 => github.com/miekg/dns v1.1.47
115+
replace (
116+
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.5 => github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.6
117+
github.com/miekg/dns v1.0.14 => github.com/miekg/dns v1.1.47
118+
)

go.sum

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ github.com/aliyun/credentials-go v1.1.2 h1:qU1vwGIBb3UJ8BwunHDRFtAhS6jnQLnde/yk0
3131
github.com/aliyun/credentials-go v1.1.2/go.mod h1:ozcZaMR5kLM7pwtCMEpVmQ242suV6qTJya2bDq4X1Tw=
3232
github.com/aws/aws-sdk-go-v2 v1.16.4 h1:swQTEQUyJF/UkEA94/Ga55miiKFoXmm/Zd67XHgmjSg=
3333
github.com/aws/aws-sdk-go-v2 v1.16.4/go.mod h1:ytwTPBG6fXTZLxxeeCCWj2/EMYp/xDUgX+OET6TLNNU=
34+
github.com/aws/aws-sdk-go-v2 v1.16.5 h1:Ah9h1TZD9E2S1LzHpViBO3Jz9FPL5+rmflmb8hXirtI=
35+
github.com/aws/aws-sdk-go-v2 v1.16.5/go.mod h1:Wh7MEsmEApyL5hrWzpDkba4gwAPc5/piwLVLFnCxp48=
3436
github.com/aws/aws-sdk-go-v2/config v1.15.7 h1:PrzhYjDpWnGSpjedmEapldQKPW4x8cCNzUI8XOho1CM=
3537
github.com/aws/aws-sdk-go-v2/config v1.15.7/go.mod h1:exERlvqx1OoUHrxQpMgrmfSW0H6B1+r3xziZD3bBXRg=
3638
github.com/aws/aws-sdk-go-v2/credentials v1.12.2 h1:tX4EHQFU4+O9at5QjnwIKb/Qgv7MbgbUNtqTRF0Vu2M=
@@ -41,6 +43,8 @@ github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.11 h1:gsqHplNh1DaQunEKZ
4143
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.11/go.mod h1:tmUB6jakq5DFNcXsXOA/ZQ7/C8VnSKYkx58OI7Fh79g=
4244
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.5 h1:PLFj+M2PgIDHG//hw3T0O0KLI4itVtAjtxrZx4AHPLg=
4345
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.5/go.mod h1:fV1AaS2gFc1tM0RCb015FJ0pvWVUfJZANzjwoO4YakM=
46+
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.6 h1:eeXdGVtXEe+2Jc49+/vAzna3FAQnUD4AagAw8tzbmfc=
47+
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.6/go.mod h1:FwpAKI+FBPIELJIdmQzlLtRe8LQSOreMcM2wBsPMvvc=
4448
github.com/aws/aws-sdk-go-v2/internal/ini v1.3.12 h1:j0VqrjtgsY1Bx27tD0ysay36/K4kFMWRp9K3ieO9nLU=
4549
github.com/aws/aws-sdk-go-v2/internal/ini v1.3.12/go.mod h1:00c7+ALdPh4YeEUPXJzyU0Yy01nPGOq2+9rUaz05z9g=
4650
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.9.5 h1:gRW1ZisKc93EWEORNJRvy/ZydF3o6xLSveJHdi1Oa0U=
@@ -53,6 +57,8 @@ github.com/aws/aws-sdk-go-v2/service/sts v1.16.6 h1:aYToU0/iazkMY67/BYLt3r6/LT/m
5357
github.com/aws/aws-sdk-go-v2/service/sts v1.16.6/go.mod h1:rP1rEOKAGZoXp4iGDxSXFvODAtXpm34Egf0lL0eshaQ=
5458
github.com/aws/smithy-go v1.11.2 h1:eG/N+CcUMAvsdffgMvjMKwfyDzIkjM6pfxMJ8Mzc6mE=
5559
github.com/aws/smithy-go v1.11.2/go.mod h1:3xHYmszWVx2c0kIwQeEVf9uSm4fYZt67FBJnwub1bgM=
60+
github.com/aws/smithy-go v1.11.3 h1:DQixirEFM9IaKxX1olZ3ke3nvxRS2xMDteKIDWxozW8=
61+
github.com/aws/smithy-go v1.11.3/go.mod h1:Tg+OJXh4MB2R/uN61Ko2f6hTZwB/ZYGOtib8J3gBHzA=
5662
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
5763
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
5864
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
@@ -122,6 +128,7 @@ github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
122128
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
123129
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
124130
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
131+
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
125132
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
126133
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
127134
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=

pkg/internal/dingtalk/types.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,23 @@ func (r *Receiver) Clone() internal.Receiver {
134134
}
135135
}
136136

137+
func (r *Receiver) GetChannels() (string, interface{}) {
138+
m := map[string]interface{}{}
139+
if r.ChatBot != nil {
140+
m["chatbot"] = r.ChatBot.Webhook.ToString()
141+
}
142+
143+
if len(r.ChatIDs) > 0 {
144+
m["chat"] = r.ChatIDs
145+
}
146+
147+
if len(m) == 0 {
148+
return "", nil
149+
} else {
150+
return r.GetType(), m
151+
}
152+
}
153+
137154
type Config struct {
138155
*internal.Common
139156
AppKey *v2beta2.Credential `json:"appKey,omitempty"`

pkg/internal/discord/types.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ func (r *Receiver) Clone() internal.Receiver {
7272
return out
7373
}
7474

75+
func (r *Receiver) GetChannels() (string, interface{}) {
76+
if r.Webhook == nil {
77+
return "", nil
78+
} else {
79+
return r.GetType(), r.Webhook.ToString()
80+
}
81+
}
82+
7583
type Config struct {
7684
*internal.Common
7785
}

pkg/internal/email/types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ func (r *Receiver) Clone() internal.Receiver {
9393
}
9494
}
9595

96+
func (r *Receiver) GetChannels() (string, interface{}) {
97+
return r.GetType(), r.To
98+
}
99+
96100
type Config struct {
97101
*internal.Common
98102
From string `json:"from,omitempty"`

pkg/internal/feishu/types.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,27 @@ func (r *Receiver) Clone() internal.Receiver {
105105
}
106106
}
107107

108+
func (r *Receiver) GetChannels() (string, interface{}) {
109+
m := map[string]interface{}{}
110+
if r.ChatBot != nil {
111+
m["chatbot"] = r.ChatBot.Webhook.ToString()
112+
}
113+
114+
if len(r.User) > 0 {
115+
m["user"] = r.User
116+
}
117+
118+
if len(r.Department) > 0 {
119+
m["department"] = r.Department
120+
}
121+
122+
if len(m) == 0 {
123+
return "", nil
124+
} else {
125+
return r.GetType(), m
126+
}
127+
}
128+
108129
type Config struct {
109130
*internal.Common
110131
AppID *v2beta2.Credential `json:"appID,omitempty"`

pkg/internal/interface.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type Receiver interface {
1818
Clone() Receiver
1919
GetHash() string
2020
SetHash(h string)
21+
GetChannels() (string, interface{})
2122
}
2223

2324
type Config interface {

0 commit comments

Comments
 (0)