custom alerting use the same API group and flagset to alerting

Signed-off-by: junotx <junotx@126.com>
This commit is contained in:
junotx
2021-01-12 16:43:13 +08:00
parent 6f9d306368
commit 514fec7eb4
21 changed files with 261 additions and 294 deletions

View File

@@ -1,4 +1,4 @@
package customalerting
package alerting
import (
"context"
@@ -13,11 +13,11 @@ import (
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/util/intstr"
coreinformersv1 "k8s.io/client-go/informers/core/v1"
"kubesphere.io/kubesphere/pkg/api/customalerting/v1alpha1"
"kubesphere.io/kubesphere/pkg/api/alerting/v2alpha1"
"kubesphere.io/kubesphere/pkg/constants"
"kubesphere.io/kubesphere/pkg/informers"
"kubesphere.io/kubesphere/pkg/models/customalerting/rules"
"kubesphere.io/kubesphere/pkg/simple/client/customalerting"
"kubesphere.io/kubesphere/pkg/models/alerting/rules"
"kubesphere.io/kubesphere/pkg/simple/client/alerting"
)
const (
@@ -41,36 +41,36 @@ var (
type Operator interface {
// ListCustomAlertingRules lists the custom alerting rules.
ListCustomAlertingRules(ctx context.Context, namespace string,
queryParams *v1alpha1.AlertingRuleQueryParams) (*v1alpha1.GettableAlertingRuleList, error)
queryParams *v2alpha1.AlertingRuleQueryParams) (*v2alpha1.GettableAlertingRuleList, error)
// ListCustomRulesAlerts lists the alerts of the custom alerting rules.
ListCustomRulesAlerts(ctx context.Context, namespace string,
queryParams *v1alpha1.AlertQueryParams) (*v1alpha1.AlertList, error)
queryParams *v2alpha1.AlertQueryParams) (*v2alpha1.AlertList, error)
// GetCustomAlertingRule gets the custom alerting rule with the given name.
GetCustomAlertingRule(ctx context.Context, namespace, ruleName string) (*v1alpha1.GettableAlertingRule, error)
GetCustomAlertingRule(ctx context.Context, namespace, ruleName string) (*v2alpha1.GettableAlertingRule, error)
// ListCustomRuleAlerts lists the alerts of the custom alerting rule with the given name.
ListCustomRuleAlerts(ctx context.Context, namespace, ruleName string) ([]*v1alpha1.Alert, error)
ListCustomRuleAlerts(ctx context.Context, namespace, ruleName string) ([]*v2alpha1.Alert, error)
// CreateCustomAlertingRule creates a custom alerting rule.
CreateCustomAlertingRule(ctx context.Context, namespace string, rule *v1alpha1.PostableAlertingRule) error
CreateCustomAlertingRule(ctx context.Context, namespace string, rule *v2alpha1.PostableAlertingRule) error
// UpdateCustomAlertingRule updates the custom alerting rule with the given name.
UpdateCustomAlertingRule(ctx context.Context, namespace, ruleName string, rule *v1alpha1.PostableAlertingRule) error
UpdateCustomAlertingRule(ctx context.Context, namespace, ruleName string, rule *v2alpha1.PostableAlertingRule) error
// DeleteCustomAlertingRule deletes the custom alerting rule with the given name.
DeleteCustomAlertingRule(ctx context.Context, namespace, ruleName string) error
// ListBuiltinAlertingRules lists the builtin(non-custom) alerting rules
ListBuiltinAlertingRules(ctx context.Context,
queryParams *v1alpha1.AlertingRuleQueryParams) (*v1alpha1.GettableAlertingRuleList, error)
queryParams *v2alpha1.AlertingRuleQueryParams) (*v2alpha1.GettableAlertingRuleList, error)
// ListBuiltinRulesAlerts lists the alerts of the builtin(non-custom) alerting rules
ListBuiltinRulesAlerts(ctx context.Context,
queryParams *v1alpha1.AlertQueryParams) (*v1alpha1.AlertList, error)
queryParams *v2alpha1.AlertQueryParams) (*v2alpha1.AlertList, error)
// GetBuiltinAlertingRule gets the builtin(non-custom) alerting rule with the given id
GetBuiltinAlertingRule(ctx context.Context, ruleId string) (*v1alpha1.GettableAlertingRule, error)
GetBuiltinAlertingRule(ctx context.Context, ruleId string) (*v2alpha1.GettableAlertingRule, error)
// ListBuiltinRuleAlerts lists the alerts of the builtin(non-custom) alerting rule with the given id
ListBuiltinRuleAlerts(ctx context.Context, ruleId string) ([]*v1alpha1.Alert, error)
ListBuiltinRuleAlerts(ctx context.Context, ruleId string) ([]*v2alpha1.Alert, error)
}
func NewOperator(informers informers.InformerFactory,
promResourceClient promresourcesclient.Interface, ruleClient customalerting.RuleClient,
option *customalerting.Options) Operator {
promResourceClient promresourcesclient.Interface, ruleClient alerting.RuleClient,
option *alerting.Options) Operator {
o := operator{
namespaceInformer: informers.KubernetesSharedInformerFactory().Core().V1().Namespaces(),
@@ -101,7 +101,7 @@ func NewOperator(informers informers.InformerFactory,
}
type operator struct {
ruleClient customalerting.RuleClient
ruleClient alerting.RuleClient
promResourceClient promresourcesclient.Interface
@@ -117,14 +117,14 @@ type operator struct {
}
func (o *operator) ListCustomAlertingRules(ctx context.Context, namespace string,
queryParams *v1alpha1.AlertingRuleQueryParams) (*v1alpha1.GettableAlertingRuleList, error) {
queryParams *v2alpha1.AlertingRuleQueryParams) (*v2alpha1.GettableAlertingRuleList, error) {
var level v1alpha1.RuleLevel
var level v2alpha1.RuleLevel
if namespace == "" {
namespace = rulerNamespace
level = v1alpha1.RuleLevelCluster
level = v2alpha1.RuleLevelCluster
} else {
level = v1alpha1.RuleLevelNamespace
level = v2alpha1.RuleLevelNamespace
}
ruleNamespace, err := o.namespaceInformer.Lister().Get(namespace)
@@ -141,14 +141,14 @@ func (o *operator) ListCustomAlertingRules(ctx context.Context, namespace string
}
func (o *operator) ListCustomRulesAlerts(ctx context.Context, namespace string,
queryParams *v1alpha1.AlertQueryParams) (*v1alpha1.AlertList, error) {
queryParams *v2alpha1.AlertQueryParams) (*v2alpha1.AlertList, error) {
var level v1alpha1.RuleLevel
var level v2alpha1.RuleLevel
if namespace == "" {
namespace = rulerNamespace
level = v1alpha1.RuleLevelCluster
level = v2alpha1.RuleLevelCluster
} else {
level = v1alpha1.RuleLevelNamespace
level = v2alpha1.RuleLevelNamespace
}
ruleNamespace, err := o.namespaceInformer.Lister().Get(namespace)
@@ -165,14 +165,14 @@ func (o *operator) ListCustomRulesAlerts(ctx context.Context, namespace string,
}
func (o *operator) GetCustomAlertingRule(ctx context.Context, namespace, ruleName string) (
*v1alpha1.GettableAlertingRule, error) {
*v2alpha1.GettableAlertingRule, error) {
var level v1alpha1.RuleLevel
var level v2alpha1.RuleLevel
if namespace == "" {
namespace = rulerNamespace
level = v1alpha1.RuleLevelCluster
level = v2alpha1.RuleLevelCluster
} else {
level = v1alpha1.RuleLevelNamespace
level = v2alpha1.RuleLevelNamespace
}
ruleNamespace, err := o.namespaceInformer.Lister().Get(namespace)
@@ -184,20 +184,20 @@ func (o *operator) GetCustomAlertingRule(ctx context.Context, namespace, ruleNam
}
func (o *operator) ListCustomRuleAlerts(ctx context.Context, namespace, ruleName string) (
[]*v1alpha1.Alert, error) {
[]*v2alpha1.Alert, error) {
rule, err := o.GetCustomAlertingRule(ctx, namespace, ruleName)
if err != nil {
return nil, err
}
if rule == nil {
return nil, v1alpha1.ErrAlertingRuleNotFound
return nil, v2alpha1.ErrAlertingRuleNotFound
}
return rule.Alerts, nil
}
func (o *operator) ListBuiltinAlertingRules(ctx context.Context,
queryParams *v1alpha1.AlertingRuleQueryParams) (*v1alpha1.GettableAlertingRuleList, error) {
queryParams *v2alpha1.AlertingRuleQueryParams) (*v2alpha1.GettableAlertingRuleList, error) {
alertingRules, err := o.listBuiltinAlertingRules(ctx)
if err != nil {
@@ -208,7 +208,7 @@ func (o *operator) ListBuiltinAlertingRules(ctx context.Context,
}
func (o *operator) ListBuiltinRulesAlerts(ctx context.Context,
queryParams *v1alpha1.AlertQueryParams) (*v1alpha1.AlertList, error) {
queryParams *v2alpha1.AlertQueryParams) (*v2alpha1.AlertList, error) {
alertingRules, err := o.listBuiltinAlertingRules(ctx)
if err != nil {
return nil, err
@@ -218,24 +218,24 @@ func (o *operator) ListBuiltinRulesAlerts(ctx context.Context,
}
func (o *operator) GetBuiltinAlertingRule(ctx context.Context, ruleId string) (
*v1alpha1.GettableAlertingRule, error) {
*v2alpha1.GettableAlertingRule, error) {
return o.getBuiltinAlertingRule(ctx, ruleId)
}
func (o *operator) ListBuiltinRuleAlerts(ctx context.Context, ruleId string) ([]*v1alpha1.Alert, error) {
func (o *operator) ListBuiltinRuleAlerts(ctx context.Context, ruleId string) ([]*v2alpha1.Alert, error) {
rule, err := o.getBuiltinAlertingRule(ctx, ruleId)
if err != nil {
return nil, err
}
if rule == nil {
return nil, v1alpha1.ErrAlertingRuleNotFound
return nil, v2alpha1.ErrAlertingRuleNotFound
}
return rule.Alerts, nil
}
func (o *operator) ListClusterAlertingRules(ctx context.Context, customFlag string,
queryParams *v1alpha1.AlertingRuleQueryParams) (*v1alpha1.GettableAlertingRuleList, error) {
queryParams *v2alpha1.AlertingRuleQueryParams) (*v2alpha1.GettableAlertingRuleList, error) {
namespace := rulerNamespace
ruleNamespace, err := o.namespaceInformer.Lister().Get(namespace)
@@ -243,7 +243,7 @@ func (o *operator) ListClusterAlertingRules(ctx context.Context, customFlag stri
return nil, err
}
alertingRules, err := o.listCustomAlertingRules(ctx, ruleNamespace, v1alpha1.RuleLevelCluster)
alertingRules, err := o.listCustomAlertingRules(ctx, ruleNamespace, v2alpha1.RuleLevelCluster)
if err != nil {
return nil, err
}
@@ -252,7 +252,7 @@ func (o *operator) ListClusterAlertingRules(ctx context.Context, customFlag stri
}
func (o *operator) ListClusterRulesAlerts(ctx context.Context,
queryParams *v1alpha1.AlertQueryParams) (*v1alpha1.AlertList, error) {
queryParams *v2alpha1.AlertQueryParams) (*v2alpha1.AlertList, error) {
namespace := rulerNamespace
ruleNamespace, err := o.namespaceInformer.Lister().Get(namespace)
@@ -260,7 +260,7 @@ func (o *operator) ListClusterRulesAlerts(ctx context.Context,
return nil, err
}
alertingRules, err := o.listCustomAlertingRules(ctx, ruleNamespace, v1alpha1.RuleLevelCluster)
alertingRules, err := o.listCustomAlertingRules(ctx, ruleNamespace, v2alpha1.RuleLevelCluster)
if err != nil {
return nil, err
}
@@ -269,14 +269,14 @@ func (o *operator) ListClusterRulesAlerts(ctx context.Context,
}
func (o *operator) listCustomAlertingRules(ctx context.Context, ruleNamespace *corev1.Namespace,
level v1alpha1.RuleLevel) ([]*v1alpha1.GettableAlertingRule, error) {
level v2alpha1.RuleLevel) ([]*v2alpha1.GettableAlertingRule, error) {
ruler, err := o.getThanosRuler()
if err != nil {
return nil, err
}
if ruler == nil {
return nil, v1alpha1.ErrThanosRulerNotEnabled
return nil, v2alpha1.ErrThanosRulerNotEnabled
}
resourceRulesMap, err := o.resourceRuleCache.ListRules(ruler, ruleNamespace,
@@ -298,14 +298,14 @@ func (o *operator) listCustomAlertingRules(ctx context.Context, ruleNamespace *c
}
func (o *operator) getCustomAlertingRule(ctx context.Context, ruleNamespace *corev1.Namespace,
ruleName string, level v1alpha1.RuleLevel) (*v1alpha1.GettableAlertingRule, error) {
ruleName string, level v2alpha1.RuleLevel) (*v2alpha1.GettableAlertingRule, error) {
ruler, err := o.getThanosRuler()
if err != nil {
return nil, err
}
if ruler == nil {
return nil, v1alpha1.ErrThanosRulerNotEnabled
return nil, v2alpha1.ErrThanosRulerNotEnabled
}
resourceRule, err := o.resourceRuleCache.GetRule(ruler, ruleNamespace,
@@ -314,7 +314,7 @@ func (o *operator) getCustomAlertingRule(ctx context.Context, ruleNamespace *cor
return nil, err
}
if resourceRule == nil {
return nil, v1alpha1.ErrAlertingRuleNotFound
return nil, v2alpha1.ErrAlertingRuleNotFound
}
ruleGroups, err := o.ruleClient.ThanosRules(ctx)
@@ -330,7 +330,7 @@ func (o *operator) getCustomAlertingRule(ctx context.Context, ruleNamespace *cor
}
func (o *operator) listBuiltinAlertingRules(ctx context.Context) (
[]*v1alpha1.GettableAlertingRule, error) {
[]*v2alpha1.GettableAlertingRule, error) {
ruler, err := o.getPrometheusRuler()
if err != nil {
@@ -344,8 +344,8 @@ func (o *operator) listBuiltinAlertingRules(ctx context.Context) (
if ruler == nil {
// for out-cluster prometheus
return rules.ParseAlertingRules(ruleGroups, false, v1alpha1.RuleLevelCluster,
func(group, id string, rule *customalerting.AlertingRule) bool {
return rules.ParseAlertingRules(ruleGroups, false, v2alpha1.RuleLevelCluster,
func(group, id string, rule *alerting.AlertingRule) bool {
return true
})
}
@@ -364,11 +364,11 @@ func (o *operator) listBuiltinAlertingRules(ctx context.Context) (
return rules.GetAlertingRulesStatus(ruleNamespace.Name, &rules.ResourceRuleChunk{
ResourceRulesMap: resourceRulesMap,
Custom: false,
Level: v1alpha1.RuleLevelCluster,
Level: v2alpha1.RuleLevelCluster,
}, ruleGroups, ruler.ExternalLabels())
}
func (o *operator) getBuiltinAlertingRule(ctx context.Context, ruleId string) (*v1alpha1.GettableAlertingRule, error) {
func (o *operator) getBuiltinAlertingRule(ctx context.Context, ruleId string) (*v2alpha1.GettableAlertingRule, error) {
ruler, err := o.getPrometheusRuler()
if err != nil {
@@ -382,18 +382,18 @@ func (o *operator) getBuiltinAlertingRule(ctx context.Context, ruleId string) (*
if ruler == nil {
// for out-cluster prometheus
alertingRules, err := rules.ParseAlertingRules(ruleGroups, false, v1alpha1.RuleLevelCluster,
func(group, id string, rule *customalerting.AlertingRule) bool {
alertingRules, err := rules.ParseAlertingRules(ruleGroups, false, v2alpha1.RuleLevelCluster,
func(group, id string, rule *alerting.AlertingRule) bool {
return ruleId == id
})
if err != nil {
return nil, err
}
if len(alertingRules) == 0 {
return nil, v1alpha1.ErrAlertingRuleNotFound
return nil, v2alpha1.ErrAlertingRuleNotFound
}
sort.Slice(alertingRules, func(i, j int) bool {
return v1alpha1.AlertingRuleIdCompare(alertingRules[i].Id, alertingRules[j].Id)
return v2alpha1.AlertingRuleIdCompare(alertingRules[i].Id, alertingRules[j].Id)
})
return alertingRules[0], nil
}
@@ -410,28 +410,28 @@ func (o *operator) getBuiltinAlertingRule(ctx context.Context, ruleId string) (*
}
if resourceRule == nil {
return nil, v1alpha1.ErrAlertingRuleNotFound
return nil, v2alpha1.ErrAlertingRuleNotFound
}
return rules.GetAlertingRuleStatus(ruleNamespace.Name, &rules.ResourceRule{
ResourceRuleItem: *resourceRule,
Custom: false,
Level: v1alpha1.RuleLevelCluster,
Level: v2alpha1.RuleLevelCluster,
}, ruleGroups, ruler.ExternalLabels())
}
func (o *operator) CreateCustomAlertingRule(ctx context.Context, namespace string,
rule *v1alpha1.PostableAlertingRule) error {
rule *v2alpha1.PostableAlertingRule) error {
ruler, err := o.getThanosRuler()
if err != nil {
return err
}
if ruler == nil {
return v1alpha1.ErrThanosRulerNotEnabled
return v2alpha1.ErrThanosRulerNotEnabled
}
var (
level v1alpha1.RuleLevel
level v2alpha1.RuleLevel
ruleResourceLabels = make(map[string]string)
)
for k, v := range o.thanosRuleResourceLabels {
@@ -439,9 +439,9 @@ func (o *operator) CreateCustomAlertingRule(ctx context.Context, namespace strin
}
if namespace == "" {
namespace = rulerNamespace
level = v1alpha1.RuleLevelCluster
level = v2alpha1.RuleLevelCluster
} else {
level = v1alpha1.RuleLevelNamespace
level = v2alpha1.RuleLevelNamespace
expr, err := rules.InjectExprNamespaceLabel(rule.Query, namespace)
if err != nil {
return err
@@ -461,7 +461,7 @@ func (o *operator) CreateCustomAlertingRule(ctx context.Context, namespace strin
return err
}
if resourceRule != nil {
return v1alpha1.ErrAlertingRuleAlreadyExists
return v2alpha1.ErrAlertingRuleAlreadyExists
}
return ruler.AddAlertingRule(ctx, ruleNamespace, extraRuleResourceSelector,
@@ -469,7 +469,7 @@ func (o *operator) CreateCustomAlertingRule(ctx context.Context, namespace strin
}
func (o *operator) UpdateCustomAlertingRule(ctx context.Context, namespace, name string,
rule *v1alpha1.PostableAlertingRule) error {
rule *v2alpha1.PostableAlertingRule) error {
rule.Name = name
@@ -478,11 +478,11 @@ func (o *operator) UpdateCustomAlertingRule(ctx context.Context, namespace, name
return err
}
if ruler == nil {
return v1alpha1.ErrThanosRulerNotEnabled
return v2alpha1.ErrThanosRulerNotEnabled
}
var (
level v1alpha1.RuleLevel
level v2alpha1.RuleLevel
ruleResourceLabels = make(map[string]string)
)
for k, v := range o.thanosRuleResourceLabels {
@@ -490,9 +490,9 @@ func (o *operator) UpdateCustomAlertingRule(ctx context.Context, namespace, name
}
if namespace == "" {
namespace = rulerNamespace
level = v1alpha1.RuleLevelCluster
level = v2alpha1.RuleLevelCluster
} else {
level = v1alpha1.RuleLevelNamespace
level = v2alpha1.RuleLevelNamespace
expr, err := rules.InjectExprNamespaceLabel(rule.Query, namespace)
if err != nil {
return err
@@ -512,7 +512,7 @@ func (o *operator) UpdateCustomAlertingRule(ctx context.Context, namespace, name
return err
}
if resourceRule == nil {
return v1alpha1.ErrAlertingRuleNotFound
return v2alpha1.ErrAlertingRuleNotFound
}
return ruler.UpdateAlertingRule(ctx, ruleNamespace, extraRuleResourceSelector,
@@ -525,17 +525,17 @@ func (o *operator) DeleteCustomAlertingRule(ctx context.Context, namespace, name
return err
}
if ruler == nil {
return v1alpha1.ErrThanosRulerNotEnabled
return v2alpha1.ErrThanosRulerNotEnabled
}
var (
level v1alpha1.RuleLevel
level v2alpha1.RuleLevel
)
if namespace == "" {
namespace = rulerNamespace
level = v1alpha1.RuleLevelCluster
level = v2alpha1.RuleLevelCluster
} else {
level = v1alpha1.RuleLevelNamespace
level = v2alpha1.RuleLevelNamespace
}
ruleNamespace, err := o.namespaceInformer.Lister().Get(namespace)
@@ -549,7 +549,7 @@ func (o *operator) DeleteCustomAlertingRule(ctx context.Context, namespace, name
return err
}
if resourceRule == nil {
return v1alpha1.ErrAlertingRuleNotFound
return v2alpha1.ErrAlertingRuleNotFound
}
return ruler.DeleteAlertingRule(ctx, ruleNamespace, extraRuleResourceSelector, resourceRule.Group, name)
@@ -591,7 +591,7 @@ func (o *operator) getThanosRuler() (rules.Ruler, error) {
return rules.NewThanosRuler(thanosrulers[0], o.ruleResourceInformer, o.promResourceClient), nil
}
func parseToPrometheusRule(rule *v1alpha1.PostableAlertingRule) *promresourcesv1.Rule {
func parseToPrometheusRule(rule *v2alpha1.PostableAlertingRule) *promresourcesv1.Rule {
return &promresourcesv1.Rule{
Alert: rule.Name,
Expr: intstr.FromString(rule.Query),
@@ -601,28 +601,28 @@ func parseToPrometheusRule(rule *v1alpha1.PostableAlertingRule) *promresourcesv1
}
}
func pageAlertingRules(alertingRules []*v1alpha1.GettableAlertingRule,
queryParams *v1alpha1.AlertingRuleQueryParams) *v1alpha1.GettableAlertingRuleList {
func pageAlertingRules(alertingRules []*v2alpha1.GettableAlertingRule,
queryParams *v2alpha1.AlertingRuleQueryParams) *v2alpha1.GettableAlertingRuleList {
alertingRules = queryParams.Filter(alertingRules)
queryParams.Sort(alertingRules)
return &v1alpha1.GettableAlertingRuleList{
return &v2alpha1.GettableAlertingRuleList{
Total: len(alertingRules),
Items: queryParams.Sub(alertingRules),
}
}
func pageAlerts(alertingRules []*v1alpha1.GettableAlertingRule,
queryParams *v1alpha1.AlertQueryParams) *v1alpha1.AlertList {
func pageAlerts(alertingRules []*v2alpha1.GettableAlertingRule,
queryParams *v2alpha1.AlertQueryParams) *v2alpha1.AlertList {
var alerts []*v1alpha1.Alert
var alerts []*v2alpha1.Alert
for _, rule := range alertingRules {
alerts = append(alerts, queryParams.Filter(rule.Alerts)...)
}
queryParams.Sort(alerts)
return &v1alpha1.AlertList{
return &v2alpha1.AlertList{
Total: len(alerts),
Items: queryParams.Sub(alerts),
}

View File

@@ -9,7 +9,7 @@ import (
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/tools/cache"
"kubesphere.io/kubesphere/pkg/api/customalerting/v1alpha1"
"kubesphere.io/kubesphere/pkg/api/alerting/v2alpha1"
"kubesphere.io/kubesphere/pkg/server/errors"
)
@@ -150,7 +150,7 @@ func (c *RuleCache) GetRule(ruler Ruler, ruleNamespace *corev1.Namespace,
} else if l > 1 {
// guarantees the stability of the get operations.
sort.Slice(rules, func(i, j int) bool {
return v1alpha1.AlertingRuleIdCompare(rules[i].Id, rules[j].Id)
return v2alpha1.AlertingRuleIdCompare(rules[i].Id, rules[j].Id)
})
}
return rules[0], nil

View File

@@ -2,7 +2,7 @@ package rules
import (
promresourcesv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
"kubesphere.io/kubesphere/pkg/api/customalerting/v1alpha1"
"kubesphere.io/kubesphere/pkg/api/alerting/v2alpha1"
)
type ResourceRuleCollection struct {
@@ -19,13 +19,13 @@ type ResourceRuleItem struct {
}
type ResourceRule struct {
Level v1alpha1.RuleLevel
Level v2alpha1.RuleLevel
Custom bool
ResourceRuleItem
}
type ResourceRuleChunk struct {
Level v1alpha1.RuleLevel
Level v2alpha1.RuleLevel
Custom bool
ResourceRulesMap map[string]*ResourceRuleCollection
}

View File

@@ -13,7 +13,7 @@ import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"kubesphere.io/kubesphere/pkg/api/customalerting/v1alpha1"
"kubesphere.io/kubesphere/pkg/api/alerting/v2alpha1"
)
const (
@@ -438,7 +438,7 @@ func (r *ThanosRuler) UpdateAlertingRule(ctx context.Context, ruleNamespace *cor
}
if !found {
return v1alpha1.ErrAlertingRuleNotFound
return v2alpha1.ErrAlertingRuleNotFound
}
if !success {
@@ -478,7 +478,7 @@ func (r *ThanosRuler) DeleteAlertingRule(ctx context.Context, ruleNamespace *cor
}
}
if !success {
return v1alpha1.ErrAlertingRuleNotFound
return v2alpha1.ErrAlertingRuleNotFound
}
return nil
}

View File

@@ -1,6 +1,7 @@
package rules
import (
"kubesphere.io/kubesphere/pkg/simple/client/alerting"
"path/filepath"
"sort"
"strings"
@@ -14,8 +15,7 @@ import (
"github.com/prometheus/prometheus/promql/parser"
"github.com/prometheus/prometheus/rules"
"k8s.io/klog"
"kubesphere.io/kubesphere/pkg/api/customalerting/v1alpha1"
"kubesphere.io/kubesphere/pkg/simple/client/customalerting"
"kubesphere.io/kubesphere/pkg/api/alerting/v2alpha1"
)
const (
@@ -92,7 +92,7 @@ func GenResourceRuleIdIgnoreFormat(group string, rule *promresourcesv1.Rule) str
return prommodel.Fingerprint(prommodel.LabelsToSignature(lbls)).String()
}
func GenEndpointRuleId(group string, epRule *customalerting.AlertingRule,
func GenEndpointRuleId(group string, epRule *alerting.AlertingRule,
externalLabels func() map[string]string) (string, error) {
query, err := FormatExpr(epRule.Query)
if err != nil {
@@ -127,13 +127,13 @@ func GenEndpointRuleId(group string, epRule *customalerting.AlertingRule,
// GetAlertingRulesStatus mix rules from prometheusrule custom resources and rules from endpoints.
// Use rules from prometheusrule custom resources as the main reference.
func GetAlertingRulesStatus(ruleNamespace string, ruleChunk *ResourceRuleChunk, epRuleGroups []*customalerting.RuleGroup,
extLabels func() map[string]string) ([]*v1alpha1.GettableAlertingRule, error) {
func GetAlertingRulesStatus(ruleNamespace string, ruleChunk *ResourceRuleChunk, epRuleGroups []*alerting.RuleGroup,
extLabels func() map[string]string) ([]*v2alpha1.GettableAlertingRule, error) {
var (
idEpRules = make(map[string]*customalerting.AlertingRule)
idEpRules = make(map[string]*alerting.AlertingRule)
nameIds = make(map[string][]string)
ret []*v1alpha1.GettableAlertingRule
ret []*v2alpha1.GettableAlertingRule
)
for _, group := range epRuleGroups {
fileShort := strings.TrimSuffix(filepath.Base(group.File), filepath.Ext(group.File))
@@ -169,7 +169,7 @@ func GetAlertingRulesStatus(ruleNamespace string, ruleChunk *ResourceRuleChunk,
if l := len(rrArr); l > 0 {
if l > 1 {
sort.Slice(rrArr, func(i, j int) bool {
return v1alpha1.AlertingRuleIdCompare(rrArr[i].Id, rrArr[j].Id)
return v2alpha1.AlertingRuleIdCompare(rrArr[i].Id, rrArr[j].Id)
})
}
resRule := rrArr[0]
@@ -181,7 +181,7 @@ func GetAlertingRulesStatus(ruleNamespace string, ruleChunk *ResourceRuleChunk,
}
} else {
// guarantee the ids of the builtin alerting rules not to be repeated
var m = make(map[string]*v1alpha1.GettableAlertingRule)
var m = make(map[string]*v2alpha1.GettableAlertingRule)
for _, resourceRules := range ruleChunk.ResourceRulesMap {
for id, rule := range resourceRules.IdRules {
if r := getAlertingRuleStatus(rule, idEpRules[id], ruleChunk.Custom, ruleChunk.Level); r != nil {
@@ -197,14 +197,14 @@ func GetAlertingRulesStatus(ruleNamespace string, ruleChunk *ResourceRuleChunk,
return ret, nil
}
func GetAlertingRuleStatus(ruleNamespace string, rule *ResourceRule, epRuleGroups []*customalerting.RuleGroup,
extLabels func() map[string]string) (*v1alpha1.GettableAlertingRule, error) {
func GetAlertingRuleStatus(ruleNamespace string, rule *ResourceRule, epRuleGroups []*alerting.RuleGroup,
extLabels func() map[string]string) (*v2alpha1.GettableAlertingRule, error) {
if rule == nil || rule.Rule == nil {
return nil, nil
}
var epRules = make(map[string]*customalerting.AlertingRule)
var epRules = make(map[string]*alerting.AlertingRule)
for _, group := range epRuleGroups {
fileShort := strings.TrimSuffix(filepath.Base(group.File), filepath.Ext(group.File))
if !strings.HasPrefix(fileShort, ruleNamespace+"-") {
@@ -224,7 +224,7 @@ func GetAlertingRuleStatus(ruleNamespace string, rule *ResourceRule, epRuleGroup
}
}
}
var epRule *customalerting.AlertingRule
var epRule *alerting.AlertingRule
if rule.Custom {
// guarantees the stability of the get operations.
var ids []string
@@ -234,7 +234,7 @@ func GetAlertingRuleStatus(ruleNamespace string, rule *ResourceRule, epRuleGroup
if l := len(ids); l > 0 {
if l > 1 {
sort.Slice(ids, func(i, j int) bool {
return v1alpha1.AlertingRuleIdCompare(ids[i], ids[j])
return v2alpha1.AlertingRuleIdCompare(ids[i], ids[j])
})
}
epRule = epRules[ids[0]]
@@ -246,15 +246,15 @@ func GetAlertingRuleStatus(ruleNamespace string, rule *ResourceRule, epRuleGroup
return getAlertingRuleStatus(&rule.ResourceRuleItem, epRule, rule.Custom, rule.Level), nil
}
func getAlertingRuleStatus(resRule *ResourceRuleItem, epRule *customalerting.AlertingRule,
custom bool, level v1alpha1.RuleLevel) *v1alpha1.GettableAlertingRule {
func getAlertingRuleStatus(resRule *ResourceRuleItem, epRule *alerting.AlertingRule,
custom bool, level v2alpha1.RuleLevel) *v2alpha1.GettableAlertingRule {
if resRule == nil || resRule.Rule == nil {
return nil
}
rule := v1alpha1.GettableAlertingRule{
AlertingRule: v1alpha1.AlertingRule{
rule := v2alpha1.GettableAlertingRule{
AlertingRule: v2alpha1.AlertingRule{
Id: resRule.Id,
Name: resRule.Rule.Alert,
Query: resRule.Rule.Expr.String(),
@@ -289,7 +289,7 @@ func getAlertingRuleStatus(resRule *ResourceRuleItem, epRule *customalerting.Ale
rule.State = aState
}
}
rule.Alerts = append(rule.Alerts, &v1alpha1.Alert{
rule.Alerts = append(rule.Alerts, &v2alpha1.Alert{
ActiveAt: a.ActiveAt,
Labels: a.Labels,
Annotations: a.Annotations,
@@ -304,10 +304,10 @@ func getAlertingRuleStatus(resRule *ResourceRuleItem, epRule *customalerting.Ale
return &rule
}
func ParseAlertingRules(epRuleGroups []*customalerting.RuleGroup, custom bool, level v1alpha1.RuleLevel,
filterFunc func(group, ruleId string, rule *customalerting.AlertingRule) bool) ([]*v1alpha1.GettableAlertingRule, error) {
func ParseAlertingRules(epRuleGroups []*alerting.RuleGroup, custom bool, level v2alpha1.RuleLevel,
filterFunc func(group, ruleId string, rule *alerting.AlertingRule) bool) ([]*v2alpha1.GettableAlertingRule, error) {
var ret []*v1alpha1.GettableAlertingRule
var ret []*v2alpha1.GettableAlertingRule
for _, g := range epRuleGroups {
for _, r := range g.Rules {
id, err := GenEndpointRuleId(g.Name, r, nil)
@@ -315,8 +315,8 @@ func ParseAlertingRules(epRuleGroups []*customalerting.RuleGroup, custom bool, l
return nil, err
}
if filterFunc(g.Name, id, r) {
rule := &v1alpha1.GettableAlertingRule{
AlertingRule: v1alpha1.AlertingRule{
rule := &v2alpha1.GettableAlertingRule{
AlertingRule: v2alpha1.AlertingRule{
Id: id,
Name: r.Name,
Query: r.Query,
@@ -344,7 +344,7 @@ func ParseAlertingRules(epRuleGroups []*customalerting.RuleGroup, custom bool, l
rule.State = aState
}
}
rule.Alerts = append(rule.Alerts, &v1alpha1.Alert{
rule.Alerts = append(rule.Alerts, &v2alpha1.Alert{
ActiveAt: a.ActiveAt,
Labels: a.Labels,
Annotations: a.Annotations,

View File

@@ -1,14 +1,14 @@
package rules
import (
"kubesphere.io/kubesphere/pkg/simple/client/alerting"
"testing"
"github.com/google/go-cmp/cmp"
promresourcesv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
"github.com/prometheus/prometheus/rules"
"k8s.io/apimachinery/pkg/util/intstr"
"kubesphere.io/kubesphere/pkg/api/customalerting/v1alpha1"
"kubesphere.io/kubesphere/pkg/simple/client/customalerting"
"kubesphere.io/kubesphere/pkg/api/alerting/v2alpha1"
)
func TestGetAlertingRulesStatus(t *testing.T) {
@@ -16,14 +16,14 @@ func TestGetAlertingRulesStatus(t *testing.T) {
description string
ruleNamespace string
resourceRuleChunk *ResourceRuleChunk
ruleGroups []*customalerting.RuleGroup
ruleGroups []*alerting.RuleGroup
extLabels func() map[string]string
expected []*v1alpha1.GettableAlertingRule
expected []*v2alpha1.GettableAlertingRule
}{{
description: "get alerting rules status",
ruleNamespace: "test",
resourceRuleChunk: &ResourceRuleChunk{
Level: v1alpha1.RuleLevelNamespace,
Level: v2alpha1.RuleLevelNamespace,
Custom: true,
ResourceRulesMap: map[string]*ResourceRuleCollection{
"custom-alerting-rule-jqbgn": &ResourceRuleCollection{
@@ -47,10 +47,10 @@ func TestGetAlertingRulesStatus(t *testing.T) {
},
},
},
ruleGroups: []*customalerting.RuleGroup{{
ruleGroups: []*alerting.RuleGroup{{
Name: "alerting.custom.defaults",
File: "/etc/thanos/rules/thanos-ruler-thanos-ruler-rulefiles-0/test-custom-alerting-rule-jqbgn.yaml",
Rules: []*customalerting.AlertingRule{{
Rules: []*alerting.AlertingRule{{
Name: "TestCPUUsageHigh",
Query: `namespace:workload_cpu_usage:sum{namespace="test"} > 1`,
Duration: 60,
@@ -62,8 +62,8 @@ func TestGetAlertingRulesStatus(t *testing.T) {
},
}},
}},
expected: []*v1alpha1.GettableAlertingRule{{
AlertingRule: v1alpha1.AlertingRule{
expected: []*v2alpha1.GettableAlertingRule{{
AlertingRule: v2alpha1.AlertingRule{
Id: "ca7f09e76954e67c",
Name: "TestCPUUsageHigh",
Query: `namespace:workload_cpu_usage:sum{namespace="test"} > 1`,