pcm-kubernetes/internal/cron/profilecron.go

55 lines
1.7 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package cron
import (
"context"
"github.com/zeromicro/go-zero/core/logx"
"gitlink.org.cn/jcce-pcm/pcm-coordinator/rpc/client/pcmcore"
"gitlink.org.cn/jcce-pcm/pcm-participant-kubernetes/internal/pkg"
"gitlink.org.cn/jcce-pcm/pcm-participant-kubernetes/internal/svc"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/util/flowcontrol"
)
// 同步配置
func SyncProfile(svc *svc.ServiceContext) {
//从c端获取ListParticipantResp信息
req := pcmcore.ParticipantTenant{}
resp, err := svc.ParticipantRpc.ListPhyInformation(context.Background(), &req)
if err != nil {
logx.Errorf("同步配置失败获取ListParticipantResp信息失败err:%v", err)
panic(err)
}
//遍历resp信息
for _, v := range resp.ParticipantPhys {
//过滤掉不是sealos的信息
if v.Type == "CLOUD" {
client := CreateClient(v.Address, v.Token)
pkg.KClients[v.Name] = &pkg.KubernetesClient{ClientSet: client.ClientSet, DynamicClient: client.DynamicClient}
}
}
//初始化客户端
logx.Infof("同步配置成功,====客户端信息:%v", pkg.KClients)
}
func CreateClient(server, token string) *pkg.KubernetesClient {
restConfig := &rest.Config{
Host: server,
RateLimiter: flowcontrol.NewTokenBucketRateLimiter(1000, 1000),
BearerToken: token,
TLSClientConfig: rest.TLSClientConfig{
Insecure: true,
},
}
clientSet, err := kubernetes.NewForConfig(restConfig)
if err != nil {
logx.Errorf("Failed to create Kubernetes client for %s: %v\n", server, err)
}
dynamicClient, err := dynamic.NewForConfig(restConfig)
if err != nil {
logx.Errorf("Failed to create Kubernetes client for %s: %v\n", server, err)
}
return &pkg.KubernetesClient{ClientSet: clientSet, DynamicClient: dynamicClient}
}