From b4a5ebd250d86252d196695cedb99ebf681e3a17 Mon Sep 17 00:00:00 2001 From: wanjunlei Date: Tue, 2 Jan 2024 17:25:10 +0800 Subject: [PATCH] add receiver name to the notifications Signed-off-by: wanjunlei --- helm/templates/_helpers.tpl | 8 +++++ helm/templates/clusterrolebindings.yaml | 6 ++-- helm/templates/notificationmanagers.yaml | 10 +++++- helm/templates/operator.yaml | 1 + helm/templates/rolebindings.yaml | 3 +- helm/templates/roles.yaml | 1 + helm/templates/serviceaccount.yaml | 1 + helm/templates/services.yaml | 2 ++ helm/templates/template.yaml | 2 ++ helm/templates/validating.yaml | 12 ++++--- helm/templates/zh-cn.yaml | 40 ++++++++++++++++++++++++ helm/values.yaml | 9 +++++- pkg/constants/constants.go | 2 ++ pkg/notify/notify.go | 9 ++++++ 14 files changed, 95 insertions(+), 11 deletions(-) create mode 100644 helm/templates/_helpers.tpl create mode 100644 helm/templates/zh-cn.yaml diff --git a/helm/templates/_helpers.tpl b/helm/templates/_helpers.tpl new file mode 100644 index 00000000..500a5755 --- /dev/null +++ b/helm/templates/_helpers.tpl @@ -0,0 +1,8 @@ +{{/*namespace*/}} +{{- define "nm.namespaceOverride" -}} + {{- if .Values.namespaceOverride -}} + {{- .Values.namespaceOverride -}} + {{- else -}} + {{- .Release.Namespace -}} + {{- end -}} +{{- end -}} diff --git a/helm/templates/clusterrolebindings.yaml b/helm/templates/clusterrolebindings.yaml index 54f75eba..a518c59c 100644 --- a/helm/templates/clusterrolebindings.yaml +++ b/helm/templates/clusterrolebindings.yaml @@ -9,7 +9,7 @@ roleRef: subjects: - kind: ServiceAccount name: notification-manager-sa - namespace: {{ .Release.Namespace }} + namespace: {{ include "nm.namespaceOverride" . }} --- apiVersion: rbac.authorization.k8s.io/v1 @@ -23,7 +23,7 @@ roleRef: subjects: - kind: ServiceAccount name: notification-manager-sa - namespace: {{ .Release.Namespace }} + namespace: {{ include "nm.namespaceOverride" . }} {{- if .Values.kubesphere.enabled }} {{- if or (eq .Values.kubesphere.version "v3.1.0") (eq .Values.kubesphere.version "v3.2.0") }} @@ -39,6 +39,6 @@ roleRef: subjects: - kind: ServiceAccount name: notification-manager-sa - namespace: {{ .Release.Namespace }} + namespace: {{ include "nm.namespaceOverride" . }} {{- end}} {{- end}} diff --git a/helm/templates/notificationmanagers.yaml b/helm/templates/notificationmanagers.yaml index e654ec96..ee8440b2 100644 --- a/helm/templates/notificationmanagers.yaml +++ b/helm/templates/notificationmanagers.yaml @@ -38,11 +38,19 @@ spec: {{- if .Values.kubesphere.enabled }} sidecars: tenant: + {{- if .Values.kubesphere.image }} + image: {{ .Values.kubesphere.image }} + {{- else }} image: kubesphere/notification-tenant-sidecar:{{ .Values.kubesphere.version }} + {{- end }} name: tenant type: kubesphere {{- end }} template: {{- toYaml .Values.notificationmanager.template | nindent 4 }} groupLabels: - {{- toYaml .Values.notificationmanager.groupLabels | nindent 4 }} \ No newline at end of file + {{- toYaml .Values.notificationmanager.groupLabels | nindent 4 }} + annotations: + {{- toYaml .Values.notificationmanager.annotations | nindent 4 }} + labels: + {{- toYaml .Values.notificationmanager.labels | nindent 4 }} \ No newline at end of file diff --git a/helm/templates/operator.yaml b/helm/templates/operator.yaml index d66955b9..2088821b 100644 --- a/helm/templates/operator.yaml +++ b/helm/templates/operator.yaml @@ -4,6 +4,7 @@ metadata: labels: control-plane: controller-manager name: notification-manager-operator + namespace: {{ include "nm.namespaceOverride" . }} spec: replicas: 1 selector: diff --git a/helm/templates/rolebindings.yaml b/helm/templates/rolebindings.yaml index b724140f..62a931e4 100644 --- a/helm/templates/rolebindings.yaml +++ b/helm/templates/rolebindings.yaml @@ -2,6 +2,7 @@ apiVersion: rbac.authorization.k8s.io/v1 kind: RoleBinding metadata: name: notification-manager-leader-election-rolebinding + namespace: {{ include "nm.namespaceOverride" . }} roleRef: apiGroup: rbac.authorization.k8s.io kind: Role @@ -9,5 +10,5 @@ roleRef: subjects: - kind: ServiceAccount name: notification-manager-sa - namespace: {{ .Release.Namespace }} + namespace: {{ include "nm.namespaceOverride" . }} diff --git a/helm/templates/roles.yaml b/helm/templates/roles.yaml index db98efbb..e31eba9d 100644 --- a/helm/templates/roles.yaml +++ b/helm/templates/roles.yaml @@ -2,6 +2,7 @@ apiVersion: rbac.authorization.k8s.io/v1 kind: Role metadata: name: notification-manager-leader-election-role + namespace: {{ include "nm.namespaceOverride" . }} rules: - apiGroups: - "" diff --git a/helm/templates/serviceaccount.yaml b/helm/templates/serviceaccount.yaml index f3f72e5b..e05464da 100644 --- a/helm/templates/serviceaccount.yaml +++ b/helm/templates/serviceaccount.yaml @@ -2,4 +2,5 @@ apiVersion: v1 kind: ServiceAccount metadata: name: notification-manager-sa + namespace: {{ include "nm.namespaceOverride" . }} diff --git a/helm/templates/services.yaml b/helm/templates/services.yaml index 92933b49..4276703d 100644 --- a/helm/templates/services.yaml +++ b/helm/templates/services.yaml @@ -4,6 +4,7 @@ metadata: labels: control-plane: controller-manager name: notification-manager-controller-metrics + namespace: {{ include "nm.namespaceOverride" . }} spec: ports: - name: https @@ -19,6 +20,7 @@ apiVersion: v1 kind: Service metadata: name: notification-manager-webhook + namespace: {{ include "nm.namespaceOverride" . }} spec: ports: - port: 443 diff --git a/helm/templates/template.yaml b/helm/templates/template.yaml index e5a6b0c9..9986bb5e 100644 --- a/helm/templates/template.yaml +++ b/helm/templates/template.yaml @@ -155,3 +155,5 @@ data: kind: ConfigMap metadata: name: notification-manager-template + namespace: {{ include "nm.namespaceOverride" . }} + diff --git a/helm/templates/validating.yaml b/helm/templates/validating.yaml index 22d382f9..c88602aa 100644 --- a/helm/templates/validating.yaml +++ b/helm/templates/validating.yaml @@ -1,5 +1,6 @@ +{{- $name := include "nm.namespaceOverride" . }} {{- $ca := genCA "webhook-ca" 36500 -}} -{{- $dns := cat "notification-manager-webhook." .Release.Namespace ".svc" | nospace -}} +{{- $dns := cat "notification-manager-webhook." $name ".svc" | nospace -}} {{- $cert := genSignedCert "notification-manager-webhook" (list) (list $dns) 36500 $ca -}} apiVersion: v1 data: @@ -8,6 +9,7 @@ data: kind: Secret metadata: name: notification-manager-webhook-server-cert + namespace: {{ include "nm.namespaceOverride" . }} type: kubernetes.io/tls --- @@ -22,7 +24,7 @@ webhooks: caBundle: {{ $ca.Cert | b64enc }} service: name: notification-manager-webhook - namespace: {{ .Release.Namespace }} + namespace: {{ include "nm.namespaceOverride" . }} path: /validate-notification-kubesphere-io-v2beta2-config failurePolicy: Fail name: vconfig.notification.kubesphere.io @@ -43,7 +45,7 @@ webhooks: caBundle: {{ $ca.Cert | b64enc }} service: name: notification-manager-webhook - namespace: {{ .Release.Namespace }} + namespace: {{ include "nm.namespaceOverride" . }} path: /validate-notification-kubesphere-io-v2beta2-receiver failurePolicy: Fail name: vreceiver.notification.kubesphere.io @@ -64,7 +66,7 @@ webhooks: caBundle: {{ $ca.Cert | b64enc }} service: name: notification-manager-webhook - namespace: {{ .Release.Namespace }} + namespace: {{ include "nm.namespaceOverride" . }} path: /validate-notification-kubesphere-io-v2beta2-router failurePolicy: Fail name: vrouter.notification.kubesphere.io @@ -85,7 +87,7 @@ webhooks: caBundle: {{ $ca.Cert | b64enc }} service: name: notification-manager-webhook - namespace: {{ .Release.Namespace }} + namespace: {{ include "nm.namespaceOverride" . }} path: /validate-notification-kubesphere-io-v2beta2-silence failurePolicy: Fail name: vsilence.notification.kubesphere.io diff --git a/helm/templates/zh-cn.yaml b/helm/templates/zh-cn.yaml new file mode 100644 index 00000000..0440bca3 --- /dev/null +++ b/helm/templates/zh-cn.yaml @@ -0,0 +1,40 @@ +apiVersion: v1 +data: + zh-cn: | + - name: zh-cn + dictionary: + alert: "告警" + alerts: "告警" + firing: "触发中" + resolved: "已解决" + alertname: "告警名称" + alerttype: "告警类型" + cluster: "集群" + namespace: "项目" + severity: "告警级别" + container: "容器" + pod: "容器组" + service: "服务" + deployment: "部署" + job: "任务" + daemonset: "守护进程集" + statefulset: "有状态副本集" + instance: "实例" + resource: "资源" + user: "用户" + verb: "操作" + group: "用户组" + requestReceivedTimestamp: "请求接收时间" + role: "角色" + host_ip: "主机IP" + node: "节点" + rule_id: "告警规则" + owner_kind: "目标类型" + workload: "工作负载" + rule_group: "规则组" + rule_level: "规则级别" + name: "名称" +kind: ConfigMap +metadata: + name: zh-cn + namespace: {{ include "nm.namespaceOverride" . }} \ No newline at end of file diff --git a/helm/values.yaml b/helm/values.yaml index c56ff741..e9ba96fb 100644 --- a/helm/values.yaml +++ b/helm/values.yaml @@ -1,5 +1,7 @@ +namespaceOverride: "" + kubesphere: - enabled: fase + enabled: false # supported version: v3.1.0, v3.2.0 version: v3.2.0 @@ -78,8 +80,13 @@ notificationmanager: - alertname - namespace template: + language: English + languagePack: + - name: zh-cn + namespace: kubesphere-monitoring-system text: name: notification-manager-template + namespace: kubesphere-monitoring-system env: - name: TZ value: Asia/Shanghai diff --git a/pkg/constants/constants.go b/pkg/constants/constants.go index 8d93ec64..a30be1d9 100644 --- a/pkg/constants/constants.go +++ b/pkg/constants/constants.go @@ -38,6 +38,8 @@ const ( AlertSummary = "summary" AlertSummaryCN = "summaryCn" + ReceiverName = "receiver" + Verify = "verify" Notification = "notification" diff --git a/pkg/notify/notify.go b/pkg/notify/notify.go index 7f2dc65b..9739bf83 100644 --- a/pkg/notify/notify.go +++ b/pkg/notify/notify.go @@ -98,6 +98,7 @@ func (s *notifyStage) Exec(ctx context.Context, l log.Logger, data interface{}) for k, v := range input { receiver := k ds := v + s.addExtensionLabels(receiver, ds) nf, err := factories[receiver.GetType()](l, receiver, s.notifierCtl) if err != nil { e := err @@ -118,3 +119,11 @@ func (s *notifyStage) Exec(ctx context.Context, l log.Logger, data interface{}) return ctx, alertMap, group.Wait() } + +func (s *notifyStage) addExtensionLabels(receiver internal.Receiver, data []*template.Data) { + for _, d := range data { + for _, alert := range d.Alerts { + alert.Labels[constants.ReceiverName] = receiver.GetName() + } + } +}