-
Notifications
You must be signed in to change notification settings - Fork 81
/
model_alert_channel.go
98 lines (85 loc) 路 2.8 KB
/
model_alert_channel.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright 2022 The kubegems.io 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.
package models
import (
"fmt"
"regexp"
"strconv"
"time"
"github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1alpha1"
"gorm.io/gorm"
"kubegems.io/kubegems/pkg/log"
"kubegems.io/kubegems/pkg/utils/prometheus/channels"
)
var (
defaultChannelName = "kubegems-default-webhook"
DefaultChannel = &AlertChannel{
ID: 1,
Name: defaultChannelName,
ChannelConfig: channels.ChannelConfig{
ChannelIf: &channels.Webhook{
BaseChannel: channels.BaseChannel{
ChannelType: channels.TypeWebhook,
SendResolved: true,
},
URL: channels.KubegemsWebhookURL,
InsecureSkipVerify: true,
},
},
}
DefaultReceiver = DefaultChannel.ChannelConfig.ChannelIf.ToReceiver(DefaultChannel.ReceiverName())
)
// AlertChannel
type AlertChannel struct {
ID uint `gorm:"primarykey" json:"id"`
Name string `gorm:"type:varchar(50)" binding:"min=1,max=50" json:"name"`
ChannelConfig channels.ChannelConfig `json:"channelConfig"`
TenantID *uint `json:"tenantID"` // 鑻ヤ负null锛屽垯琛ㄧず绯荤粺棰勭疆
Tenant *Tenant `gorm:"constraint:OnUpdate:RESTRICT,OnDelete:CASCADE;" json:"tenant,omitempty"`
CreatedAt *time.Time `json:"-"`
UpdatedAt *time.Time `json:"-"`
}
func (c *AlertChannel) ReceiverName() string {
return fmt.Sprintf("%s-id-%d", c.Name, c.ID)
}
func (c *AlertChannel) ToAlertmanagerReceiver() v1alpha1.Receiver {
return c.ChannelConfig.ToReceiver(c.ReceiverName())
}
var receiverNameReg = regexp.MustCompile("(.*)-id-(.*)")
func ChannelIDNameByReceiverName(recName string) (string, uint) {
substrs := receiverNameReg.FindStringSubmatch(recName)
if len(substrs) == 3 {
name := substrs[1]
id, err := strconv.Atoi(substrs[2])
if err != nil {
log.Errorf("channel id %s not valid", id)
}
return name, uint(id)
}
log.Errorf("receiver name %s not valid", recName)
return "", 0
}
func NewChannnelMappler(db *gorm.DB) *channels.ChannelMapper {
allch := []AlertChannel{}
if err := db.Find(&allch).Error; err != nil {
return &channels.ChannelMapper{Err: err}
}
ret := &channels.ChannelMapper{
M: make(map[uint]channels.ChannelIf),
}
for _, v := range allch {
ret.M[v.ID] = v.ChannelConfig.ChannelIf
}
return ret
}