package k8s import ( "encoding/json" "fmt" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/common/conf" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/util" "strings" monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1" ) var ( PrometheusRuleGroup = "monitoring.beagle.io" // kubectl api-resources | grep -i prome PrometheusRuleVersion = "v1" PrometheusRuleKind = "PrometheusRule" Namespace = "beagle-monitoring" PrometheusRuleApiVersion = PrometheusRuleGroup + "/" + PrometheusRuleVersion PrometheusRuleName = strings.ToLower(PrometheusRuleKind) + "s." + PrometheusRuleGroup PrometheusRuleNamePrefix = "beagle-prometheus-so-operation-api-rules" // beagle-monitoring beagle-prometheus-prometheus-operator 43d ) var AlertDefLabels = map[string]string{ "app": "prometheus", "app.bd-apaas.com/cluster-component": "monitoring", "prometheus-operator": "monitoring", "release": "beagle-prometheus", } // GetPrometheusRuleName 获取规则CRD名称 func GetPrometheusRuleName(alertRulesId string) string { return fmt.Sprintf("%s-%s", PrometheusRuleNamePrefix, alertRulesId) } // GetPrometheusRuleGroupName 获取规则组名称 func GetPrometheusRuleGroupName(alertName string, groupInterval string) string { return fmt.Sprintf("%s-group-%s", alertName, groupInterval) } type PrometheusRule struct { Header map[string]string } func (p PrometheusRule) Create(pRule *monitoringv1.PrometheusRule) error { k8sSvc := K8sSvc{Header: p.Header} c := &Content{Kind: PrometheusRuleKind, ApiVersion: PrometheusRuleApiVersion, Metadata: pRule.ObjectMeta, Spec: pRule.Spec} _, err := k8sSvc.SendFile(c) return err } func (p PrometheusRule) Delete(namespace string, name string) error { delUrl := fmt.Sprintf("%s/kubernetes/api/v1/_raw/%s/namespace/%s/name/%s", conf.Options.AweRestURL, PrometheusRuleName, namespace, name) _, err := util.ProxySendRes("DELETE", delUrl, "", p.Header) return err } func (p PrometheusRule) Update(pRule *monitoringv1.PrometheusRule) error { updateUrl := fmt.Sprintf("%s/kubernetes/api/v1/_raw/%s/namespace/%s/name/%s", conf.Options.AweRestURL, PrometheusRuleName, pRule.Namespace, pRule.Name) body, _ := json.Marshal(pRule) p.Header["Content-Type"] = "application/json" _, err := util.ProxySendRes("PUT", updateUrl, string(body), p.Header) return err } func (p PrometheusRule) Get(namespace string, name string) (obj *monitoringv1.PrometheusRule, err error) { getUrl := fmt.Sprintf("%s/kubernetes/api/v1/_raw/%s/namespace/%s/name/%s", conf.Options.AweRestURL, PrometheusRuleName, namespace, name) res, err := util.ProxySendRes("GET", getUrl, "", p.Header) if err != nil { return } err = json.Unmarshal(res, &obj) return }