pcm-kubernetes/kubernetes.go

92 lines
2.7 KiB
Go

package main
import (
"context"
"flag"
"fmt"
"github.com/zeromicro/go-zero/core/conf"
"github.com/zeromicro/go-zero/core/service"
"github.com/zeromicro/go-zero/zrpc"
"gitlink.org.cn/JointCloud/pcm-coordinator/pkg/utils"
"gitlink.org.cn/JointCloud/pcm-coordinator/rpc/client/participantservice"
"gitlink.org.cn/JointCloud/pcm-coordinator/rpc/pcmCore"
"gitlink.org.cn/JointCloud/pcm-kubernetes/internal/config"
"gitlink.org.cn/JointCloud/pcm-kubernetes/internal/cron"
"gitlink.org.cn/JointCloud/pcm-kubernetes/internal/server"
"gitlink.org.cn/JointCloud/pcm-kubernetes/internal/svc"
"gitlink.org.cn/JointCloud/pcm-kubernetes/internal/tracker"
"gitlink.org.cn/JointCloud/pcm-kubernetes/kubernetes"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
k8s "k8s.io/client-go/kubernetes"
"strconv"
)
var configFile = flag.String("f", "etc/kubernetes.yaml", "the config file")
func main() {
flag.Parse()
var c config.Config
conf.MustLoad(*configFile, &c)
ctx := svc.NewServiceContext(c)
s := zrpc.MustNewServer(c.RpcServerConf, func(grpcServer *grpc.Server) {
kubernetes.RegisterKubernetesServer(grpcServer, server.NewKubernetesServer(ctx))
if c.Mode == service.DevMode || c.Mode == service.TestMode {
reflection.Register(grpcServer)
}
})
defer s.Stop()
// 启动并添加定时任务
ctx.Cron.Start()
cron.AddCronGroup(ctx)
// 推送p端静态信息
PushParticipantInfo(ctx.Config, ctx.ParticipantRpc, ctx.ClientSet)
fmt.Printf("Starting rpc server at %s...\n", c.ListenOn)
cron.SyncProfile(ctx)
// todo 测试
//Operate(ctx.ClientSet)
cron.SyncProfile(svc.NewServiceContext(c))
s.Start()
}
// PushParticipantInfo 推送p端静态信息
func PushParticipantInfo(config config.Config, participantService participantservice.ParticipantService, k8sClient *k8s.Clientset) {
participantId, err := utils.GetParticipantId(*configFile)
if err != nil {
return
}
// 获取节点静态信息
phyInfos, err := tracker.NodesStaticInfo(k8sClient)
if err != nil {
return
}
// 从配置文件中读取participant标签信息
var labels []*pcmCore.ParticipantLabel
for k, v := range config.Participant.Labels {
labels = append(labels, &pcmCore.ParticipantLabel{
Key: k,
Value: v,
})
}
// 封装请求入参
req := participantservice.ParticipantPhyReq{}
utils.Convert(config.Participant, &req)
req.ParticipantId = participantId
nodePhyInfo := []*pcmCore.NodePhyInfo{}
utils.Convert(&phyInfos, &nodePhyInfo)
req.NodeInfo = nodePhyInfo
req.LabelInfo = labels
resp, err := participantService.RegisterParticipant(context.Background(), &req)
if err != nil {
return
}
// 更新本地配置文件ParticipantId
utils.UpdateParticipantId(*configFile, strconv.FormatInt(resp.ParticipantId, 10))
}