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

相关文章

Java调用DeepSeek API的最佳实践及详细代码示例

《Java调用DeepSeekAPI的最佳实践及详细代码示例》:本文主要介绍如何使用Java调用DeepSeekAPI,包括获取API密钥、添加HTTP客户端依赖、创建HTTP请求、处理响应、... 目录1. 获取API密钥2. 添加HTTP客户端依赖3. 创建HTTP请求4. 处理响应5. 错误处理6.

Android 悬浮窗开发示例((动态权限请求 | 前台服务和通知 | 悬浮窗创建 )

《Android悬浮窗开发示例((动态权限请求|前台服务和通知|悬浮窗创建)》本文介绍了Android悬浮窗的实现效果,包括动态权限请求、前台服务和通知的使用,悬浮窗权限需要动态申请并引导... 目录一、悬浮窗 动态权限请求1、动态请求权限2、悬浮窗权限说明3、检查动态权限4、申请动态权限5、权限设置完毕后

golang内存对齐的项目实践

《golang内存对齐的项目实践》本文主要介绍了golang内存对齐的项目实践,内存对齐不仅有助于提高内存访问效率,还确保了与硬件接口的兼容性,是Go语言编程中不可忽视的重要优化手段,下面就来介绍一下... 目录一、结构体中的字段顺序与内存对齐二、内存对齐的原理与规则三、调整结构体字段顺序优化内存对齐四、内

Deepseek R1模型本地化部署+API接口调用详细教程(释放AI生产力)

《DeepseekR1模型本地化部署+API接口调用详细教程(释放AI生产力)》本文介绍了本地部署DeepSeekR1模型和通过API调用将其集成到VSCode中的过程,作者详细步骤展示了如何下载和... 目录前言一、deepseek R1模型与chatGPT o1系列模型对比二、本地部署步骤1.安装oll

浅析如何使用Swagger生成带权限控制的API文档

《浅析如何使用Swagger生成带权限控制的API文档》当涉及到权限控制时,如何生成既安全又详细的API文档就成了一个关键问题,所以这篇文章小编就来和大家好好聊聊如何用Swagger来生成带有... 目录准备工作配置 Swagger权限控制给 API 加上权限注解查看文档注意事项在咱们的开发工作里,API

Python创建Excel的4种方式小结

《Python创建Excel的4种方式小结》这篇文章主要为大家详细介绍了Python中创建Excel的4种常见方式,文中的示例代码简洁易懂,具有一定的参考价值,感兴趣的小伙伴可以学习一下... 目录库的安装代码1——pandas代码2——openpyxl代码3——xlsxwriterwww.cppcns.c

一分钟带你上手Python调用DeepSeek的API

《一分钟带你上手Python调用DeepSeek的API》最近DeepSeek非常火,作为一枚对前言技术非常关注的程序员来说,自然都想对接DeepSeek的API来体验一把,下面小编就来为大家介绍一下... 目录前言免费体验API-Key申请首次调用API基本概念最小单元推理模型智能体自定义界面总结前言最

JAVA调用Deepseek的api完成基本对话简单代码示例

《JAVA调用Deepseek的api完成基本对话简单代码示例》:本文主要介绍JAVA调用Deepseek的api完成基本对话的相关资料,文中详细讲解了如何获取DeepSeekAPI密钥、添加H... 获取API密钥首先,从DeepSeek平台获取API密钥,用于身份验证。添加HTTP客户端依赖使用Jav

使用Python在Excel中创建和取消数据分组

《使用Python在Excel中创建和取消数据分组》Excel中的分组是一种通过添加层级结构将相邻行或列组织在一起的功能,当分组完成后,用户可以通过折叠或展开数据组来简化数据视图,这篇博客将介绍如何使... 目录引言使用工具python在Excel中创建行和列分组Python在Excel中创建嵌套分组Pyt

通过prometheus监控Tomcat运行状态的操作流程

《通过prometheus监控Tomcat运行状态的操作流程》文章介绍了如何安装和配置Tomcat,并使用Prometheus和TomcatExporter来监控Tomcat的运行状态,文章详细讲解了... 目录Tomcat安装配置以及prometheus监控Tomcat一. 安装并配置tomcat1、安装