forked from nudtpc/pcm-kubernetes
91 lines
2.7 KiB
Go
91 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/jcce-pcm/pcm-coordinator/rpc/client/participantservice"
|
|
"gitlink.org.cn/jcce-pcm/pcm-coordinator/rpc/pcmCore"
|
|
"gitlink.org.cn/jcce-pcm/pcm-participant-kubernetes/internal/config"
|
|
"gitlink.org.cn/jcce-pcm/pcm-participant-kubernetes/internal/cron"
|
|
"gitlink.org.cn/jcce-pcm/pcm-participant-kubernetes/internal/server"
|
|
"gitlink.org.cn/jcce-pcm/pcm-participant-kubernetes/internal/svc"
|
|
"gitlink.org.cn/jcce-pcm/pcm-participant-kubernetes/internal/tracker"
|
|
"gitlink.org.cn/jcce-pcm/pcm-participant-kubernetes/kubernetes"
|
|
"gitlink.org.cn/jcce-pcm/utils/tool"
|
|
"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)
|
|
// 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 := tool.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{}
|
|
tool.Convert(config.Participant, &req)
|
|
req.ParticipantId = participantId
|
|
nodePhyInfo := []*pcmCore.NodePhyInfo{}
|
|
tool.Convert(&phyInfos, &nodePhyInfo)
|
|
req.NodeInfo = nodePhyInfo
|
|
req.LabelInfo = labels
|
|
|
|
resp, err := participantService.RegisterParticipant(context.Background(), &req)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
// 更新本地配置文件ParticipantId
|
|
tool.UpdateParticipantId(*configFile, strconv.FormatInt(resp.ParticipantId, 10))
|
|
}
|