-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.go
92 lines (84 loc) · 2.24 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package server
import (
"context"
"fmt"
"net"
"github.com/LTitan/Mebius/apis/v1alpha1"
mcontext "github.com/LTitan/Mebius/pkg/context"
"github.com/LTitan/Mebius/pkg/factory"
"github.com/LTitan/Mebius/pkg/options"
"github.com/LTitan/Mebius/pkg/protos"
"github.com/LTitan/Mebius/pkg/protos/types"
"github.com/spf13/cobra"
"google.golang.org/grpc"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/klog/v2"
)
type RawServer struct {
factory.FrameworkInterface
opts *options.GlobalOption
}
func NewServer(opts *options.GlobalOption) factory.Application {
return &RawServer{
opts: opts,
FrameworkInterface: factory.NewBaseFramework(opts),
}
}
func (rs *RawServer) GetMachine(ctx context.Context, req *types.ExampleRequest) (resp *types.ExampleResponse, err error) {
resp = &types.ExampleResponse{
Content: "this is a grpc server",
}
return
}
func (rs *RawServer) Heartbeat(ctx context.Context, req *types.HeartbeatRequest) (resp *types.HeartbeatResponse, err error) {
klog.Infof("%+v", req.String())
resp = &types.HeartbeatResponse{
Code: 0,
}
machine, err := rs.GetMebiusClientSet().MebiusV1alpha1().Machines("default").Get(ctx, req.Name, metav1.GetOptions{})
if err != nil {
resp.Code = 4
resp.Message = err.Error()
err = nil
return
}
resp.Data = []*v1alpha1.Machine{
machine,
}
return
}
func (rs *RawServer) RegisterCommand() {
cmd := &cobra.Command{
Use: "server",
Short: "mebius grpc server",
Long: "mebius grpc server",
RunE: func(cmd *cobra.Command, args []string) error {
return rs.Run()
},
}
rs.opts.GetCommand().AddCommand(cmd)
}
func (rs *RawServer) Run() error {
// TODO: Add some grpc middleware
ctx, cancel := mcontext.WithWaitGroup(context.Background()).WithCancel()
defer cancel()
server := grpc.NewServer(
grpc.MaxRecvMsgSize(rs.opts.MaxRecvByteSize),
grpc.MaxSendMsgSize(rs.opts.MaxSendByteSize),
)
if err := rs.Init(); err != nil {
return err
}
klog.Infof("start grpc server, listen on *:%d", rs.opts.Sever().Port)
protos.RegisterServerServer(server, rs)
listen, err := net.Listen("tcp", fmt.Sprintf(":%d", rs.opts.Sever().Port))
if err != nil {
return err
}
if err := server.Serve(listen); err != nil {
return err
}
<-ctx.Done()
server.Stop()
return nil
}