golang调用prometheus-operator api创建PromtheusRule

2024-05-25 04:58

本文主要是介绍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的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1000547

相关文章

PHP应用中处理限流和API节流的最佳实践

《PHP应用中处理限流和API节流的最佳实践》限流和API节流对于确保Web应用程序的可靠性、安全性和可扩展性至关重要,本文将详细介绍PHP应用中处理限流和API节流的最佳实践,下面就来和小编一起学习... 目录限流的重要性在 php 中实施限流的最佳实践使用集中式存储进行状态管理(如 Redis)采用滑动

Spring创建Bean的八种主要方式详解

《Spring创建Bean的八种主要方式详解》Spring(尤其是SpringBoot)提供了多种方式来让容器创建和管理Bean,@Component、@Configuration+@Bean、@En... 目录引言一、Spring 创建 Bean 的 8 种主要方式1. @Component 及其衍生注解

Java调用Python脚本实现HelloWorld的示例详解

《Java调用Python脚本实现HelloWorld的示例详解》作为程序员,我们经常会遇到需要在Java项目中调用Python脚本的场景,下面我们来看看如何从基础到进阶,一步步实现Java与Pyth... 目录一、环境准备二、基础调用:使用 Runtime.exec()2.1 实现步骤2.2 代码解析三、

MySQL 数据库表操作完全指南:创建、读取、更新与删除实战

《MySQL数据库表操作完全指南:创建、读取、更新与删除实战》本文系统讲解MySQL表的增删查改(CURD)操作,涵盖创建、更新、查询、删除及插入查询结果,也是贯穿各类项目开发全流程的基础数据交互原... 目录mysql系列前言一、Create(创建)并插入数据1.1 单行数据 + 全列插入1.2 多行数据

Python如何调用另一个类的方法和属性

《Python如何调用另一个类的方法和属性》在Python面向对象编程中,类与类之间的交互是非常常见的场景,本文将详细介绍在Python中一个类如何调用另一个类的方法和属性,大家可以根据需要进行选择... 目录一、前言二、基本调用方式通过实例化调用通过类继承调用三、高级调用方式通过组合方式调用通过类方法/静

C#控制台程序同步调用WebApi实现方式

《C#控制台程序同步调用WebApi实现方式》控制台程序作为Job时,需同步调用WebApi以确保获取返回结果后执行后续操作,否则会引发TaskCanceledException异常,同步处理可避免异... 目录同步调用WebApi方法Cls001类里面的写法总结控制台程序一般当作Job使用,有时候需要控制

Go语言使用net/http构建一个RESTful API的示例代码

《Go语言使用net/http构建一个RESTfulAPI的示例代码》Go的标准库net/http提供了构建Web服务所需的强大功能,虽然众多第三方框架(如Gin、Echo)已经封装了很多功能,但... 目录引言一、什么是 RESTful API?二、实战目标:用户信息管理 API三、代码实现1. 用户数据

Python用Flask封装API及调用详解

《Python用Flask封装API及调用详解》本文介绍Flask的优势(轻量、灵活、易扩展),对比GET/POST表单/JSON请求方式,涵盖错误处理、开发建议及生产环境部署注意事项... 目录一、Flask的优势一、基础设置二、GET请求方式服务端代码客户端调用三、POST表单方式服务端代码客户端调用四

MySQL 临时表创建与使用详细说明

《MySQL临时表创建与使用详细说明》MySQL临时表是存储在内存或磁盘的临时数据表,会话结束时自动销毁,适合存储中间计算结果或临时数据集,其名称以#开头(如#TempTable),本文给大家介绍M... 目录mysql 临时表详细说明1.定义2.核心特性3.创建与使用4.典型应用场景5.生命周期管理6.注

MySQL的触发器全解析(创建、查看触发器)

《MySQL的触发器全解析(创建、查看触发器)》MySQL触发器是与表关联的存储程序,当INSERT/UPDATE/DELETE事件发生时自动执行,用于维护数据一致性、日志记录和校验,优点包括自动执行... 目录触发器的概念:创建触www.chinasem.cn发器:查看触发器:查看当前数据库的所有触发器的定