本文主要是介绍golang调用prometheus-operator api创建PromtheusRule,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
prometheus-operator使用PrometheusRule来代替了规则文件。每个告警规则对应一个PrometheusRule对象。所有的PrometheusRule对象会被Prometheus-Operator转换为规则文件挂载在promtheus pod内部的 /etc/prometheus/rules/prometheus-k8s-rulefiles-0 目录下。
package apiimport ("errors""fmt"operatorV1 "github.com/coreos/prometheus-operator/pkg/apis/monitoring/v1""github.com/coreos/prometheus-operator/pkg/client/versioned"jsoniter "github.com/json-iterator/go"metav1 "k8s.io/apimachinery/pkg/apis/meta/v1""k8s.io/apimachinery/pkg/fields""k8s.io/apimachinery/pkg/types""k8s.io/klog"
)const ApiVersion = "monitoring.coreos.com/v1"// ================创建告警规则================
// 查询告警规则是否存在
func GetPrometheusRule(clientSet *versioned.Clientset, namespace string, alertName string) (*operatorV1.PrometheusRule, error) {return clientSet.MonitoringV1().PrometheusRules(namespace).Get(alertName, metav1.GetOptions{})
}// 根据ns查询告警规则
func GetRulesByClusterNs(clientSet *versioned.Clientset, monitorNs, sign, clusterId string) (*operatorV1.PrometheusRuleList, error) {return clientSet.MonitoringV1().PrometheusRules(monitorNs).List(metav1.ListOptions{LabelSelector: fields.SelectorFromSet(fields.Set(map[string]string{"sign": sign,"cluster": clusterId,})).String(),})
}// sign, 如果是pod,那么对应pod的namespace,如果是宿主机,那么对应宿主机ip
func CreateRule(clientSet *versioned.Clientset, groups []operatorV1.RuleGroup, monitorNs, sign, cluster, alertName string, showLog bool) (*operatorV1.PrometheusRule, error) {if showLog {klog.Infof("received create PrometheusRule in namespace:%s, name:%s", monitorNs, alertName)}pr := &operatorV1.PrometheusRule{TypeMeta: metav1.TypeMeta{APIVersion: ApiVersion,Kind: "PrometheusRule",},ObjectMeta: metav1.ObjectMeta{Name: alertName,Namespace: monitorNs,Labels: map[string]string{"prometheus": "k8s","role": "alert-rules","sign": sign,"cluster": cluster,},},Spec: operatorV1.PrometheusRuleSpec{Groups: groups,},}return clientSet.MonitoringV1().PrometheusRules(monitorNs).Create(pr)
}// 修改告警规则
func PatchRule(clientSet *versioned.Clientset, groups []operatorV1.RuleGroup, namespace, ruleName string, showLog bool) (*operatorV1.PrometheusRule, error) {if showLog {klog.Infof("received patch PrometheusRule in namespace:%s, name:%s", namespace, ruleName)}patch := PathRule{Metadata: Metadata{Namespace: namespace,Name: ruleName,},Spec: operatorV1.PrometheusRuleSpec{Groups: groups,},}data, err := jsoniter.Marshal(patch)if err != nil {return nil, errors.New(fmt.Sprintf("update %s rule, but format err:%s ", ruleName, err.Error()))}return clientSet.MonitoringV1().PrometheusRules(namespace).Patch(ruleName, types.MergePatchType, data)
}// 删除PrometheusRule
func DeleteRule(clientSet *versioned.Clientset, namespace, name string) error {return clientSet.MonitoringV1().PrometheusRules(namespace).Delete(name, &metav1.DeleteOptions{})
}type PathRule struct {Metadata Metadata `json:"metadata"`Spec operatorV1.PrometheusRuleSpec `json:"spec"`
}type Metadata struct {Namespace string `json:"namespace"`Name string `json:"name"`
}
这篇关于golang调用prometheus-operator api创建PromtheusRule的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!