Skip to content

Commit a8d7454

Browse files
committed
Optimize the alarm tenant matching mechanism
Signed-off-by: wanjunlei <wanjunlei@kubesphere.io>
1 parent 829c34c commit a8d7454

File tree

6 files changed

+1130
-66
lines changed

6 files changed

+1130
-66
lines changed

pkg/constants/constants.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ const (
3838
AlertMessage = "message"
3939
AlertSummary = "summary"
4040
AlertSummaryCN = "summaryCn"
41+
RuleLevel = "rule_level"
42+
43+
RuleLevelGlobal = "global"
44+
RuleLevelCluster = "cluster"
45+
RuleLevelNamespace = "namespace"
4146

4247
ReceiverName = "receiver"
4348

pkg/controller/controller.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,6 @@ import (
1414

1515
"github.com/go-kit/kit/log"
1616
"github.com/go-kit/kit/log/level"
17-
"github.com/kubesphere/notification-manager/apis/v2beta2"
18-
"github.com/kubesphere/notification-manager/pkg/constants"
19-
"github.com/kubesphere/notification-manager/pkg/internal"
20-
"github.com/kubesphere/notification-manager/pkg/template"
21-
"github.com/kubesphere/notification-manager/pkg/utils"
2217
"github.com/modern-go/reflect2"
2318
v1 "k8s.io/api/core/v1"
2419
rbacv1 "k8s.io/api/rbac/v1"
@@ -31,6 +26,12 @@ import (
3126
"sigs.k8s.io/controller-runtime/pkg/client"
3227
kconfig "sigs.k8s.io/controller-runtime/pkg/client/config"
3328
"sigs.k8s.io/yaml"
29+
30+
"github.com/kubesphere/notification-manager/apis/v2beta2"
31+
"github.com/kubesphere/notification-manager/pkg/constants"
32+
"github.com/kubesphere/notification-manager/pkg/internal"
33+
"github.com/kubesphere/notification-manager/pkg/template"
34+
"github.com/kubesphere/notification-manager/pkg/utils"
3435
)
3536

3637
const (

pkg/route/router.go

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,20 @@ import (
77

88
"github.com/go-kit/kit/log"
99
"github.com/go-kit/kit/log/level"
10+
"github.com/modern-go/reflect2"
11+
1012
"github.com/kubesphere/notification-manager/apis/v2beta2"
1113
"github.com/kubesphere/notification-manager/pkg/constants"
1214
"github.com/kubesphere/notification-manager/pkg/controller"
1315
"github.com/kubesphere/notification-manager/pkg/internal"
1416
"github.com/kubesphere/notification-manager/pkg/stage"
1517
"github.com/kubesphere/notification-manager/pkg/template"
1618
"github.com/kubesphere/notification-manager/pkg/utils"
17-
"github.com/modern-go/reflect2"
1819
)
1920

2021
const (
21-
RouterPolicyAll = "All"
22-
RouterFirst = "RouterFirst"
22+
RouterFirst = "RouterFirst"
23+
RouterOnly = "RouterOnly"
2324
)
2425

2526
type routeStage struct {
@@ -56,9 +57,14 @@ func (s *routeStage) Exec(ctx context.Context, l log.Logger, data interface{}) (
5657
// Grouping alerts by cluster and namespace
5758
alertMap := make(map[string][]*template.Alert)
5859
for _, alert := range input {
59-
ns := alert.Labels[constants.Namespace]
60-
cluster := alert.Labels[constants.Cluster]
61-
key := fmt.Sprintf("%s|%s", cluster, ns)
60+
key := ""
61+
rl := alert.Labels[constants.RuleLevel]
62+
if rl == constants.RuleLevelNamespace {
63+
ns := alert.Labels[constants.Namespace]
64+
cluster := alert.Labels[constants.Cluster]
65+
key = fmt.Sprintf("%s|%s", cluster, ns)
66+
}
67+
6268
as := alertMap[key]
6369
as = append(as, alert)
6470
alertMap[key] = as
@@ -67,18 +73,19 @@ func (s *routeStage) Exec(ctx context.Context, l log.Logger, data interface{}) (
6773
m := make(map[string]*packet)
6874
routePolicy := s.notifierCtl.GetRoutePolicy()
6975
for key, alerts := range alertMap {
70-
flag := false
71-
pair := strings.Split(key, "|")
72-
cluster := pair[0]
73-
ns := pair[1]
7476
var tenantRcvs []internal.Receiver
77+
if routePolicy != RouterOnly {
78+
pair := strings.Split(key, "|")
79+
cluster := pair[0]
80+
ns := ""
81+
if len(pair) > 1 {
82+
ns = pair[1]
83+
}
84+
tenantRcvs = s.notifierCtl.RcvsFromNs(cluster, &ns)
85+
}
7586
for _, alert := range alerts {
7687
rcvs := s.rcvsFromRouter(alert, routers)
77-
if routePolicy == RouterPolicyAll || (routePolicy == RouterFirst && len(rcvs) == 0) {
78-
if len(tenantRcvs) == 0 && !flag {
79-
tenantRcvs = s.notifierCtl.RcvsFromNs(cluster, &ns)
80-
flag = true
81-
}
88+
if !(routePolicy == RouterFirst && len(rcvs) != 0) {
8289
rcvs = append(rcvs, tenantRcvs...)
8390
}
8491

sidecar/kubesphere/4.0.0/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Use of this source code is governed by a Apache license
33
# that can be found in the LICENSE file.
44

5-
IMG ?= kubesphere/notification-tenant-sidecar:v4.0.2
5+
IMG ?= kubesphere/notification-tenant-sidecar:v4.2.0
66
AMD64 ?= -amd64
77

88
all: docker-build

sidecar/kubesphere/4.0.0/backend.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,9 @@ func getTenantInfoFromCluster(cluster string, users []string) (map[string][]stri
119119
ResourceAttributes: &iamv1beta1.ResourceAttributes{
120120
Namespace: namespace,
121121
Verb: "get",
122-
Group: "notification.kubesphere.io",
123-
Version: "v2beta2",
124-
Resource: "receivenotification",
122+
Group: "",
123+
Version: "v1",
124+
Resource: "pods",
125125
},
126126
NonResourceAttributes: nil,
127127
User: user, // "X-Remote-User" request header

0 commit comments

Comments
 (0)