golang调用k8s API操作servicemonitor

go 获取k8s servicemonitor

package test
import (
	"context"
	"fmt"
	monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
	"k8s.io/apimachinery/pkg/runtime"
	"k8s.io/client-go/kubernetes/scheme"

	"k8s.io/client-go/rest"
	"k8s.io/client-go/tools/clientcmd"
	"os"
	"testing"
)

func TestAddEndpoints(t *testing.T) {
	_ = monitoringv1.AddToScheme(scheme.Scheme)
	// 加载 Kubeconfig
	kubeconfig := os.Getenv("KUBECONFIG")
	if kubeconfig == "" {
		kubeconfig = "E:/kubeconfig" // 替换为你的 kubeconfig 路径
	}
	config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
	if err != nil {
		panic(err.Error())
	}
	
	config.GroupVersion = &monitoringv1.SchemeGroupVersion
	config.APIPath = "/apis"
	config.NegotiatedSerializer = scheme.Codecs

	restClient, err := rest.RESTClientFor(config)
	if err != nil {
		fmt.Println(err)
	}

	// 获取现有的 ServiceMonitor
	svcMonitorClient := restClient.
		Get().
		Resource("servicemonitors").
		Namespace("cloudos-dbaas-nosql").    // 替换为你的命名空间
		Name("redis-monitor-servicemonitor") // 替换为你的 ServiceMonitor 名称

	svcMonitor := &monitoringv1.ServiceMonitor{}
	err = svcMonitorClient.Do(context.Background()).Into(svcMonitor)
	if err != nil {
		panic(err.Error())
	}

	fmt.Println(svcMonitor)
}

go 向servicemonitor中添加endpoints

package test

import (
	"context"
	"fmt"
	monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
	"k8s.io/client-go/kubernetes/scheme"

	"k8s.io/client-go/rest"
	"k8s.io/client-go/tools/clientcmd"
	"os"
	"testing"
)


func TestAddEndpoints(t *testing.T) {
	_ = monitoringv1.AddToScheme(scheme.Scheme)

	// 加载 Kubeconfig
	kubeconfig := os.Getenv("KUBECONFIG")
	if kubeconfig == "" {
		kubeconfig = "E:/kubeconfig" // 替换为你的 kubeconfig 路径
	}
	config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
	if err != nil {
		panic(err.Error())
	}
	config.GroupVersion = &monitoringv1.SchemeGroupVersion
	config.APIPath = "/apis"
	config.NegotiatedSerializer = scheme.Codecs

	restClient, err := rest.RESTClientFor(config)
	if err != nil {
		fmt.Println(err)
	}
	// 获取现有的 ServiceMonitor
	svcMonitorClient := restClient.
		Get().
		Resource("servicemonitors").
		Namespace("cloudos-dbaas-nosql").    // 替换为你的命名空间
		Name("redis-monitor-servicemonitor") // 替换为你的 ServiceMonitor 名称

	svcMonitor := &monitoringv1.ServiceMonitor{}
	err = svcMonitorClient.Do(context.Background()).Into(svcMonitor)
	if err != nil {
		panic(err.Error())
	}

	// 添加新的 endpoints
	newEndpoint := monitoringv1.Endpoint{
		Port:     "http",     // 替换为你的服务端口名
		Path:     "/metrics", // 替换为你的指标路径
		Interval: "30s",
		Scheme:   "HTTP",
	}

	// 添加新 endpoints
	svcMonitor.Spec.Endpoints = append(svcMonitor.Spec.Endpoints, newEndpoint)
	// 更新 ServiceMonitor
	err = restClient.
		Put().
		Resource("servicemonitors").
		Namespace(svcMonitor.Namespace). // 替换为你的命名空间
		Name(svcMonitor.Name).
		Body(svcMonitor).
		Do(context.Background()).Into(svcMonitor)
	if err != nil {
		panic(err.Error())
	}

	fmt.Printf("Updated ServiceMonitor %s with new endpoints\n", err)
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yiqian1989

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值