Skip to content

Commit

Permalink
Merge pull request #268 from wanjunlei/release-2.5
Browse files Browse the repository at this point in the history
cherry pick from master
  • Loading branch information
benjaminhuo authored May 14, 2024
2 parents ca3ee81 + becde0b commit e6ccf34
Show file tree
Hide file tree
Showing 17 changed files with 234 additions and 133 deletions.
75 changes: 75 additions & 0 deletions .github/workflows/push-sidecar-image.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#
# Copyright 2022 The Notification-Manager Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

name: WorkFlow for Building sidecar image

on:
push:
branches:
- 'master'
paths:
- '.github/workflows/push-sidecar-image.yaml'
- 'sidecar/kubesphere/4.0.0/backend.go'
- 'sidecar/kubesphere/4.0.0/Dockerfile'
- 'sidecar/kubesphere/4.0.0/main.go'
- 'sidecar/kubesphere/4.0.0/Makefile'
- 'sidecar/kubesphere/4.0.0/go.sum'
- 'sidecar/kubesphere/4.0.0/go.mod'

env:
REPO_OP: 'kubesphere'

jobs:
build:
runs-on: ubuntu-latest
timeout-minutes: 30
name: Build Operator Image
steps:
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: 1.20.x

- uses: actions/cache@v2
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}

- name: Checkout code
uses: actions/checkout@v2
with:
fetch-depth: 0

- name: Set up QEMU
id: qemu
uses: docker/setup-qemu-action@v1
with:
image: tonistiigi/binfmt:latest
platforms: all

- name: Login to Docker Hub
uses: docker/login-action@v1
with:
username: ${{ secrets.REGISTRY_USER }}
password: ${{ secrets.REGISTRY_PASSWORD }}

- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v1

- name: Build and Push image
run: |
cd sidecar/kubesphere/4.0.0 && make
3 changes: 1 addition & 2 deletions config/samples/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ data:
{{ 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 }} {{ if ne (len .Status) 0 }}{{ .Status }} {{ end }}alerts{{ if gt (len .GroupLabels.SortedPairs) 0 }} for {{ range .GroupLabels.SortedPairs }}{{ .Name | translate }}={{ .Value }} {{ end }}{{ end }}{{ end }}{{ end }}
{{ define "nm.subject" }}{{ .Alerts | len }} {{ if ne (len .Status) 0 }}{{ .Status }} {{ end }}alerts{{ if gt (len .CommonLabels.SortedPairs) 0 }} for {{ range .CommonLabels.SortedPairs }}{{ .Name | translate }}={{ .Value }} {{ end }}{{ end }}{{ end }}
{{ define "nm.subject" -}}{{- $rawStatus := .Status -}}{{- $severity := index .CommonLabels "severity" -}}{{- $status := "" -}}{{- if eq $rawStatus "firing" -}}{{- $status = "FIRING" -}}{{- else if eq $rawStatus "resolved" -}}{{- $status = "RESOLVED" -}}{{- else -}}{{- $status = "UNKNOWN" -}}{{- end -}}{{- $numAlerts := len .Alerts -}}{{- if eq $numAlerts 0 -}}Show nothing{{- else if eq $numAlerts 1 -}}{{- range .Alerts -}}{{- $alertType := .Labels.alerttype -}}{{- $alertName := .Labels.alertname -}}{{- $cluster := .Labels.cluster -}}{{- $node := .Labels.node -}}{{- $pod := .Labels.pod -}}{{- $namespace := .Labels.namespace -}}{{- $output := printf "[%s]" $status -}}{{- if $severity -}}{{- $output = printf "%s | severity=%s" $output $severity -}}{{- end -}}{{- if $alertType -}}{{- $output = printf "%s | alerttype=%s" $output $alertType -}}{{- end -}}{{- if $alertName -}}{{- $output = printf "%s | alertname=%s" $output $alertName -}}{{- end -}}{{- if $cluster -}}{{- $output = printf "%s | cluster=%s" $output $cluster -}}{{- end -}}{{- if $node -}}{{- $output = printf "%s | node=%s" $output $node -}}{{- end -}}{{- if $namespace -}}{{- $output = printf "%s | namespace=%s" $output $namespace -}}{{- end -}}{{- if $pod -}}{{- $output = printf "%s | pod=%s" $output $pod -}}{{- end -}}{{- $output -}}{{- end -}}{{- else -}}{{- $alertType := index .GroupLabels "alerttype" -}}{{- $alertName := index .GroupLabels "alertname" -}}{{- $cluster := index .GroupLabels "cluster" -}}{{- $namespace := index .GroupLabels "namespace" -}}{{- $node := index .GroupLabels "node" -}}{{- $output := printf "[%s]" $status -}}{{- if $severity -}}{{- $output = printf "%s | severity=%s" $output $severity -}}{{- end -}}{{- if $alertType -}}{{- $output = printf "%s | alerttype=%s" $output $alertType -}}{{- end -}}{{- if $alertName -}}{{- $output = printf "%s | alertname=%s" $output $alertName -}}{{- end -}}{{- if $cluster -}}{{- $output = printf "%s | cluster=%s" $output $cluster -}}{{- end -}}{{- if $namespace -}}{{- $output = printf "%s | namespace=%s" $output $namespace -}}{{- end -}}{{- if $node -}}{{- $output = printf "%s | node=%s" $output $node -}}{{- end -}}{{- $output -}}{{- end -}}{{- end -}}
{{ define "nm.default.text" }}{{ range .Alerts }}{{ template "nm.default.message" . }}
{{ range .Labels.SortedPairs }} {{ .Name | translate }}: {{ .Value }}
{{ end }}{{ range .Annotations.SortedPairs.Filter "runbook_url" "message" "summary" "summary_cn" }} {{ .Name | translate }}: {{ .Value }}
Expand Down
2 changes: 1 addition & 1 deletion docs/crds/silence.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Overview

`slience` CRD is used to define policies to mute notifications for a given time. A silence is configured based on a label selector.
`Silence` CRD is used to define policies to mute notifications for a given time. A silence is configured based on a label selector.
If the incoming alert matches the label selector of an active silence, no notifications will be sent out for that alert.

`Silence` can be categorized into 2 types `global` and `tenant` by label like `type = global`, `type = tenant` :
Expand Down
12 changes: 6 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/aws/aws-sdk-go-v2/config v1.15.7
github.com/aws/aws-sdk-go-v2/credentials v1.12.2
github.com/aws/aws-sdk-go-v2/service/sns v1.17.6
github.com/duke-git/lancet/v2 v2.2.9
github.com/emicklei/go-restful v2.16.0+incompatible
github.com/go-chi/chi v4.0.3+incompatible
github.com/go-kit/kit v0.9.0
Expand Down Expand Up @@ -54,7 +55,6 @@ require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/duke-git/lancet/v2 v2.2.9 // indirect
github.com/emicklei/go-restful/v3 v3.9.0 // indirect
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
Expand Down Expand Up @@ -89,16 +89,16 @@ require (
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.24.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/net v0.23.0 // indirect
golang.org/x/oauth2 v0.8.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/term v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/term v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.3.0 // indirect
golang.org/x/tools v0.12.0 // indirect
gomodules.xyz/jsonpatch/v2 v2.3.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.30.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/ini.v1 v1.56.0 // indirect
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
Expand Down
21 changes: 11 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8U
golang.org/x/crypto v0.0.0-20191219195013-becbf705a915/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200510223506-06a226fb4e37/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/exp v0.0.0-20221208152030-732eee02a75a h1:4iLhBPcpqFmylhnkbY3W0ONLUYYkDAW9xMFLfxgsvCw=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
Expand All @@ -266,8 +267,8 @@ golang.org/x/net v0.0.0-20200506145744-7e3656a0809f/go.mod h1:qpuaurCH72eLCgpAm/
golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs=
golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
golang.org/x/oauth2 v0.8.0 h1:6dkIjl3j3LtZ/O3sTgZTMsLKSftL/B8Zgq4huOIIUu8=
golang.org/x/oauth2 v0.8.0/go.mod h1:yr7u4HXZRm1R1kBWqr/xKNqewf0plRYoB7sla+BCIXE=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand All @@ -291,16 +292,16 @@ golang.org/x/sys v0.0.0-20210112080510-489259a85091/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.13.0 h1:bb+I9cTfFazGW51MZqBVmZy7+JEJMouUHTUSKVQLBek=
golang.org/x/term v0.13.0/go.mod h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U=
golang.org/x/term v0.18.0 h1:FcHjZXDMxI8mM3nwhX9HlKop4C0YQvCVCdwYl2wOtE8=
golang.org/x/term v0.18.0/go.mod h1:ILwASektA3OnRv7amZ1xhE/KTR+u50pbXfZ03+6Nx58=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4=
golang.org/x/time v0.3.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
Expand Down Expand Up @@ -330,8 +331,8 @@ google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzi
google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cng=
google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/alecthomas/kingpin.v2 v2.2.6 h1:jMFz6MfLP0/4fUyZle81rXUoxOBFi19VUFKVDOQfozc=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
9 changes: 3 additions & 6 deletions helm/templates/notificationmanagers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ spec:
{{- toYaml .Values.notificationmanager.receivers | nindent 4 }}
defaultConfigSelector:
{{- toYaml .Values.notificationmanager.defaultConfigSelector | nindent 4 }}
{{- if .Values.timezone }}
env:
- name: TZ
value: {{ .Values.timezone }}
{{- end }}
volumeMounts:
{{- toYaml .Values.notificationmanager.volumeMounts | nindent 4 }}
volumes:
Expand Down Expand Up @@ -60,4 +55,6 @@ spec:
annotations:
{{- toYaml .Values.notificationmanager.annotations | nindent 4 }}
labels:
{{- toYaml .Values.notificationmanager.labels | nindent 4 }}
{{- toYaml .Values.notificationmanager.labels | nindent 4 }}
env:
{{- toYaml .Values.notificationmanager.env | nindent 4 }}
2 changes: 1 addition & 1 deletion helm/templates/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ data:
{{ "{{ 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 }} {{ if ne (len .Status) 0 }}{{ .Status }} {{ end }}alerts{{ if gt (len .GroupLabels.SortedPairs) 0 }} for {{ range .GroupLabels.SortedPairs }}{{ .Name | translate }}={{ .Value }} {{ end }}{{ end }}{{ end }}{{ end }}" }}
{{ "{{ define \"nm.subject\" }}{{ .Alerts | len }} {{ if ne (len .Status) 0 }}{{ .Status }} {{ end }}alerts{{ if gt (len .CommonLabels.SortedPairs) 0 }} for {{ range .CommonLabels.SortedPairs }}{{ .Name | translate }}={{ .Value }} {{ end }}{{ end }}{{ end }}" }}
{{ "{{ define \"nm.subject\" -}}{{- $rawStatus := .Status -}}{{- $severity := index .CommonLabels \"severity\" -}}{{- $status := \"\" -}}{{- if eq $rawStatus \"firing\" -}}{{- $status = \"FIRING\" -}}{{- else if eq $rawStatus \"resolved\" -}}{{- $status = \"RESOLVED\" -}}{{- else -}}{{- $status = \"UNKNOWN\" -}}{{- end -}}{{- $numAlerts := len .Alerts -}}{{- if eq $numAlerts 0 -}}Show nothing{{- else if eq $numAlerts 1 -}}{{- range .Alerts -}}{{- $alertType := .Labels.alerttype -}}{{- $alertName := .Labels.alertname -}}{{- $cluster := .Labels.cluster -}}{{- $node := .Labels.node -}}{{- $pod := .Labels.pod -}}{{- $namespace := .Labels.namespace -}}{{- $output := printf \"[%s]\" $status -}}{{- if $severity -}}{{- $output = printf \"%s | severity=%s\" $output $severity -}}{{- end -}}{{- if $alertType -}}{{- $output = printf \"%s | alerttype=%s\" $output $alertType -}}{{- end -}}{{- if $alertName -}}{{- $output = printf \"%s | alertname=%s\" $output $alertName -}}{{- end -}}{{- if $cluster -}}{{- $output = printf \"%s | cluster=%s\" $output $cluster -}}{{- end -}}{{- if $node -}}{{- $output = printf \"%s | node=%s\" $output $node -}}{{- end -}}{{- if $namespace -}}{{- $output = printf \"%s | namespace=%s\" $output $namespace -}}{{- end -}}{{- if $pod -}}{{- $output = printf \"%s | pod=%s\" $output $pod -}}{{- end -}}{{- $output -}}{{- end -}}{{- else -}}{{- $alertType := index .GroupLabels \"alerttype\" -}}{{- $alertName := index .GroupLabels \"alertname\" -}}{{- $cluster := index .GroupLabels \"cluster\" -}}{{- $namespace := index .GroupLabels \"namespace\" -}}{{- $node := index .GroupLabels \"node\" -}}{{- $output := printf \"[%s]\" $status -}}{{- if $severity -}}{{- $output = printf \"%s | severity=%s\" $output $severity -}}{{- end -}}{{- if $alertType -}}{{- $output = printf \"%s | alerttype=%s\" $output $alertType -}}{{- end -}}{{- if $alertName -}}{{- $output = printf \"%s | alertname=%s\" $output $alertName -}}{{- end -}}{{- if $cluster -}}{{- $output = printf \"%s | cluster=%s\" $output $cluster -}}{{- end -}}{{- if $namespace -}}{{- $output = printf \"%s | namespace=%s\" $output $namespace -}}{{- end -}}{{- if $node -}}{{- $output = printf \"%s | node=%s\" $output $node -}}{{- end -}}{{- $output -}}{{- end -}}{{- end -}}" }}
{{ "{{ define \"nm.default.text\" }}{{ range .Alerts }}{{ template \"nm.default.message\" . }}" }}
{{ "{{ range .Labels.SortedPairs }} {{ .Name | translate }}: {{ .Value }}" }}
Expand Down
1 change: 1 addition & 0 deletions pkg/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const (
DiscordContent = "content"
DiscordEmbed = "embed"

Cluster = "cluster"
Namespace = "namespace"

AlertFiring = "firing"
Expand Down
11 changes: 6 additions & 5 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ func (c *Controller) receiverChanged(t *task) {
_ = level.Info(c.logger).Log("msg", "Receiver changed", "op", t.op, "name", receiver.Name)
}

func (c *Controller) tenantIDFromNs(namespace string) ([]string, error) {
func (c *Controller) tenantIDFromNs(cluster, namespace string) ([]string, error) {
tenantIDs := make([]string, 0)
// Use namespace as TenantID directly if tenantSidecar not provided.
if !c.tenantSidecar {
Expand All @@ -514,7 +514,8 @@ func (c *Controller) tenantIDFromNs(namespace string) ([]string, error) {
}

p := make(map[string]string)
p["namespace"] = namespace
p[constants.Cluster] = cluster
p[constants.Namespace] = namespace
u, err := utils.UrlWithParameters(tenantSidecarURL, p)
if err != nil {
return nil, err
Expand All @@ -536,7 +537,7 @@ func (c *Controller) tenantIDFromNs(namespace string) ([]string, error) {
return nil, err
}

_ = level.Debug(c.logger).Log("msg", "get tenants from namespace", "namespace", namespace, "tenant", utils.ArrayToString(res, ","))
_ = level.Debug(c.logger).Log("msg", "get tenants from namespace", "cluster", cluster, "namespace", namespace, "tenant", utils.ArrayToString(res, ","))

return res, nil
}
Expand Down Expand Up @@ -580,13 +581,13 @@ func getMatchedConfig(r internal.Receiver, configs map[string]map[string]interna
}
}

func (c *Controller) RcvsFromNs(namespace *string) []internal.Receiver {
func (c *Controller) RcvsFromNs(cluster string, namespace *string) []internal.Receiver {

// Global receiver should receive all notifications.
tenants := []string{globalTenantID}
if namespace != nil && len(*namespace) > 0 {
// Get all tenants which need to receive the notifications in this namespace.
tenantIDs, err := c.tenantIDFromNs(*namespace)
tenantIDs, err := c.tenantIDFromNs(cluster, *namespace)
if err != nil {
_ = level.Error(c.logger).Log("msg", "get tenantID error", "err", err, "namespace", *namespace)
} else {
Expand Down
17 changes: 12 additions & 5 deletions pkg/route/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package route

import (
"context"
"fmt"
"strings"

"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
Expand Down Expand Up @@ -51,25 +53,30 @@ func (s *routeStage) Exec(ctx context.Context, l log.Logger, data interface{}) (
return ctx, nil, err
}

// Grouping alerts by namespace
// Grouping alerts by cluster and namespace
alertMap := make(map[string][]*template.Alert)
for _, alert := range input {
ns := alert.Labels[constants.Namespace]
as := alertMap[ns]
cluster := alert.Labels[constants.Cluster]
key := fmt.Sprintf("%s|%s", cluster, ns)
as := alertMap[key]
as = append(as, alert)
alertMap[ns] = as
alertMap[key] = as
}

m := make(map[string]*packet)
routePolicy := s.notifierCtl.GetRoutePolicy()
for ns, alerts := range alertMap {
for key, alerts := range alertMap {
flag := false
pair := strings.Split(key, "|")
cluster := pair[0]
ns := pair[1]
var tenantRcvs []internal.Receiver
for _, alert := range alerts {
rcvs := s.rcvsFromRouter(alert, routers)
if routePolicy == RouterPolicyAll || (routePolicy == RouterFirst && len(rcvs) == 0) {
if len(tenantRcvs) == 0 && !flag {
tenantRcvs = s.notifierCtl.RcvsFromNs(&ns)
tenantRcvs = s.notifierCtl.RcvsFromNs(cluster, &ns)
flag = true
}
rcvs = append(rcvs, tenantRcvs...)
Expand Down
5 changes: 5 additions & 0 deletions pkg/webhook/v1/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ func (h *HttpHandler) Alert(w http.ResponseWriter, r *http.Request) {
if v := alert.Labels["cluster"]; v == "" {
alert.Labels["cluster"] = cluster
}

if alert.Labels["alerttype"] == "metric" {
alert.Annotations["alerttime"] = time.Now().Local().String()
}

alert.ID = utils.Hash(alert)
if err := h.alerts.Push(alert); err != nil {
_ = level.Error(h.logger).Log("msg", "push alert error", "error", err.Error())
Expand Down
Loading

0 comments on commit e6ccf34

Please sign in to comment.