forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 1
/
prometheus_operator.go
195 lines (168 loc) · 5.75 KB
/
prometheus_operator.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package manager
import (
"context"
"fmt"
"reflect"
"sort"
"strings"
monitoringv1 "github.com/coreos/prometheus-operator/pkg/client/monitoring/v1"
monitorutil "github.com/rancher/rancher/pkg/monitoring"
"github.com/rancher/types/apis/core/v1"
"github.com/rancher/types/apis/management.cattle.io/v3"
rmonitoringv1 "github.com/rancher/types/apis/monitoring.coreos.com/v1"
"github.com/rancher/types/config"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
)
var (
ComparisonEqual = "equal"
ComparisonNotEqual = "not-equal"
ComparisonGreaterThan = "greater-than"
ComparisonLessThan = "less-than"
ComparisonGreaterOrEqual = "greater-or-equal"
ComparisonLessOrEqual = "less-or-equal"
)
var (
comparisonMap = map[string]string{
ComparisonEqual: "==",
ComparisonNotEqual: "!=",
ComparisonGreaterThan: ">",
ComparisonLessThan: "<",
ComparisonGreaterOrEqual: ">=",
ComparisonLessOrEqual: "<=",
}
)
type PromOperatorCRDManager struct {
clusterName string
namespaces v1.NamespaceInterface
prometheusRules rmonitoringv1.PrometheusRuleInterface
}
func NewPrometheusCRDManager(ctx context.Context, cluster *config.UserContext) *PromOperatorCRDManager {
return &PromOperatorCRDManager{
clusterName: cluster.ClusterName,
namespaces: cluster.Core.Namespaces(metav1.NamespaceAll),
prometheusRules: cluster.Monitoring.PrometheusRules(metav1.NamespaceAll),
}
}
func (c *PromOperatorCRDManager) GetDefaultPrometheusRule(namespace, name string) *monitoringv1.PrometheusRule {
return &monitoringv1.PrometheusRule{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Labels: map[string]string{
monitorutil.CattlePrometheusRuleLabelKey: monitorutil.CattleAlertingPrometheusRuleLabelValue,
},
},
}
}
func (c *PromOperatorCRDManager) DeletePrometheusRule(namespace, name string) error {
if err := c.prometheusRules.DeleteNamespaced(namespace, name, &metav1.DeleteOptions{}); err != nil && !apierrors.IsNotFound(err) {
return fmt.Errorf("delete prometheus rule %s:%s failed, %v", namespace, name, err)
}
return nil
}
func (c *PromOperatorCRDManager) SyncPrometheusRule(promRule *monitoringv1.PrometheusRule) error {
if len(promRule.Spec.Groups) == 0 {
return nil
}
ns := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: promRule.Namespace,
},
}
if _, err := c.namespaces.Create(ns); err != nil && !apierrors.IsAlreadyExists(err) {
return fmt.Errorf("get namespace %s for prometheus rule %s:%s failed, %v", ns.Name, promRule.Namespace, promRule.Name, err)
}
old, err := c.prometheusRules.GetNamespaced(promRule.Namespace, promRule.Name, metav1.GetOptions{})
if err != nil {
if !apierrors.IsNotFound(err) {
return fmt.Errorf("get prometheus rule %s:%s failed, %v", promRule.Namespace, promRule.Name, err)
}
if _, err = c.prometheusRules.Create(promRule); err != nil && !apierrors.IsAlreadyExists(err) {
return fmt.Errorf("create prometheus rule %s:%s failed, %v", promRule.Namespace, promRule.Name, err)
}
return nil
}
sortedNewGroups := promRule.Spec.Groups
sortedOldGroups := old.Spec.Groups
sortGroups(sortedNewGroups)
sortGroups(sortedOldGroups)
if !reflect.DeepEqual(sortedOldGroups, sortedNewGroups) {
updated := old.DeepCopy()
updated.Spec.Groups = sortedNewGroups
if _, err = c.prometheusRules.Update(updated); err != nil {
return fmt.Errorf("update prometheus rule %s:%s failed, %v", updated.Namespace, updated.Name, err)
}
}
return nil
}
func (c *PromOperatorCRDManager) AddRuleGroup(promRule *monitoringv1.PrometheusRule, ruleGroup monitoringv1.RuleGroup) {
if promRule.Spec.Groups == nil {
promRule.Spec.Groups = []monitoringv1.RuleGroup{ruleGroup}
return
}
for _, v := range promRule.Spec.Groups {
if v.Name == ruleGroup.Name {
v = ruleGroup
return
}
}
promRule.Spec.Groups = append(promRule.Spec.Groups, ruleGroup)
}
func (c *PromOperatorCRDManager) GetRuleGroup(name string) *monitoringv1.RuleGroup {
return &monitoringv1.RuleGroup{
Name: name,
}
}
func (c *PromOperatorCRDManager) AddRule(ruleGroup *monitoringv1.RuleGroup, rule monitoringv1.Rule) {
ruleGroup.Rules = append(ruleGroup.Rules, rule)
}
func Metric2Rule(groupID, ruleID, serverity, displayName, clusterName, projectName string, metric *v3.MetricRule) monitoringv1.Rule {
expr := getExpr(metric.Expression, metric.Comparison, metric.ThresholdValue)
comp := strings.Replace(metric.Comparison, "-", " ", -1)
labels := map[string]string{
"alert_type": "metric",
"alert_name": displayName,
"group_id": groupID,
"cluster_name": clusterName,
"rule_id": ruleID,
"severity": serverity,
"duration": metric.Duration,
"expression": expr,
"threshold_value": fmt.Sprintf("%v", metric.ThresholdValue),
"comparison": comp,
}
annotation := map[string]string{
"current_value": "{{ .Value }}",
}
if projectName != "" {
labels["project_name"] = projectName
labels["level"] = "project"
}
return monitoringv1.Rule{
Alert: displayName,
Expr: intstr.FromString(expr),
For: metric.Duration,
Labels: labels,
Annotations: annotation,
}
}
func getExpr(expr, comparison string, thresholdValue float64) string {
if comparison != "" {
return fmt.Sprintf("%s%s%v", expr, comparisonMap[comparison], thresholdValue)
}
return expr
}
func sortGroups(groups []monitoringv1.RuleGroup) {
for _, v := range groups {
sortedRules := v.Rules
sort.Slice(sortedRules, func(k, l int) bool {
return sortedRules[k].Labels["rule_id"] < sortedRules[l].Labels["rule_id"]
})
}
sort.Slice(groups, func(i, j int) bool {
return groups[i].Name < groups[j].Name
})
}