-
Notifications
You must be signed in to change notification settings - Fork 0
/
statesyncer.go
143 lines (119 loc) · 3.97 KB
/
statesyncer.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package statesyncer
import (
"context"
"time"
"github.com/rancher/norman/controller"
"github.com/rancher/rancher/pkg/controllers/user/alert/manager"
"github.com/rancher/rancher/pkg/ticker"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/config"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/labels"
)
func StartStateSyncer(ctx context.Context, cluster *config.UserContext, manager *manager.Manager) {
s := &StateSyncer{
clusterAlertClient: cluster.Management.Management.ClusterAlerts(cluster.ClusterName),
projectAlertClient: cluster.Management.Management.ProjectAlerts(""),
alertManager: manager,
clusterName: cluster.ClusterName,
}
go s.watch(ctx, 10*time.Second)
}
func (s *StateSyncer) watch(ctx context.Context, interval time.Duration) {
for range ticker.Context(ctx, interval) {
s.syncState()
}
}
type StateSyncer struct {
clusterAlertClient v3.ClusterAlertInterface
projectAlertClient v3.ProjectAlertInterface
alertManager *manager.Manager
clusterName string
}
//synchronize the state between alert CRD and alertmanager.
func (s *StateSyncer) syncState() error {
if s.alertManager.IsDeploy == false {
return nil
}
apiAlerts, err := s.alertManager.GetAlertList()
if err == nil {
clusterAlerts, err := s.clusterAlertClient.Controller().Lister().List("", labels.NewSelector())
if err != nil {
return err
}
projectAlerts, err := s.projectAlertClient.Controller().Lister().List("", labels.NewSelector())
if err != nil {
return err
}
pAlerts := []*v3.ProjectAlert{}
for _, alert := range projectAlerts {
if controller.ObjectInCluster(s.clusterName, alert) {
pAlerts = append(pAlerts, alert)
}
}
for _, alert := range clusterAlerts {
alertID := alert.Namespace + "-" + alert.Name
state := s.alertManager.GetState(alertID, apiAlerts)
needUpdate := s.doSync(alertID, alert.Status.AlertState, state)
if needUpdate {
alert.Status.AlertState = state
_, err := s.clusterAlertClient.Update(alert)
if err != nil {
logrus.Errorf("Error occurred while updating alert state : %v", err)
}
}
}
for _, alert := range pAlerts {
alertID := alert.Namespace + "-" + alert.Name
state := s.alertManager.GetState(alertID, apiAlerts)
needUpdate := s.doSync(alertID, alert.Status.AlertState, state)
if needUpdate {
alert.Status.AlertState = state
_, err := s.projectAlertClient.Update(alert)
if err != nil {
logrus.Errorf("Error occurred while updating alert state and time: %v", err)
}
}
}
}
return err
}
//The curState is the state in the CRD status,
//The newState is the state in alert manager side
func (s *StateSyncer) doSync(alertID, curState, newState string) (needUpdate bool) {
if curState == "inactive" {
return false
}
//only take ation when the state is not the same
if newState != curState {
//the alert is muted by user (curState == muted), but it already went away in alertmanager side (newState == active)
//then we need to remove the silence rule and update the state in CRD
if curState == "muted" && newState == "active" {
err := s.alertManager.RemoveSilenceRule(alertID)
if err != nil {
logrus.Errorf("Error occurred while remove silence : %v", err)
}
return true
}
//the alert is unmuted by user, but it is still muted in alertmanager side
//need to remove the silence rule, but do not have to update the CRD
if curState == "alerting" && newState == "muted" {
err := s.alertManager.RemoveSilenceRule(alertID)
if err != nil {
logrus.Errorf("Error occurred while remove silence : %v", err)
}
return false
}
//the alert is muted by user, but it is still alerting in alertmanager side
//need to add silence rule to alertmanager
if curState == "muted" && newState == "alerting" {
err := s.alertManager.AddSilenceRule(alertID)
if err != nil {
logrus.Errorf("Error occurred while remove silence : %v", err)
}
return false
}
return true
}
return false
}