-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
192 lines (165 loc) · 4.75 KB
/
main.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package main
import (
"errors"
"log"
"net"
"os"
"strconv"
"time"
dockerClient "github.com/docker/docker/client"
consul "github.com/hashicorp/consul/api"
pb "github.com/opencopilot/agent/agent"
pbHealth "github.com/opencopilot/agent/health"
"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
"github.com/grpc-ecosystem/go-grpc-middleware"
"github.com/grpc-ecosystem/go-grpc-middleware/logging/zap"
"github.com/grpc-ecosystem/go-grpc-middleware/recovery"
"github.com/grpc-ecosystem/go-grpc-middleware/tags"
)
var (
// InstanceID is the identifier of this agent/device
InstanceID = os.Getenv("INSTANCE_ID")
// ConfigDir is the config directory of opencopilot on the host
ConfigDir = os.Getenv("CONFIG_DIR")
)
const (
port = 50051
)
func servePublicGRPC(server *server) {
lis, err := net.Listen("tcp", ":"+strconv.Itoa(port))
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
logger, err := zap.NewProduction()
defer logger.Sync()
if err != nil {
log.Fatalf("failed to setup logger: %v", err)
}
// TODO: TLS for gRPC connection to outside world
// creds, err := credentials.NewServerTLSFromFile("server.crt", "server.key")
// if err != nil {
// log.Fatalf("failed to load credentials: %v", err)
// }
s := grpc.NewServer(
// grpc.Creds(creds),
grpc.StreamInterceptor(grpc_middleware.ChainStreamServer(
grpc_ctxtags.StreamServerInterceptor(grpc_ctxtags.WithFieldExtractor(grpc_ctxtags.CodeGenRequestFieldExtractor)),
grpc_zap.StreamServerInterceptor(logger),
grpc_recovery.StreamServerInterceptor(),
)),
grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(
grpc_ctxtags.UnaryServerInterceptor(grpc_ctxtags.WithFieldExtractor(grpc_ctxtags.CodeGenRequestFieldExtractor)),
grpc_zap.UnaryServerInterceptor(logger),
grpc_recovery.UnaryServerInterceptor(),
)),
)
pb.RegisterAgentServer(s, server)
pbHealth.RegisterHealthServer(s, server)
// Register reflection service on gRPC server.
reflection.Register(s)
s.Serve(lis)
if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}
func servePrivateGRPC(server *server) {
lis, err := net.Listen("tcp", "127.0.0.1:50050")
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
s := grpc.NewServer()
pb.RegisterAgentServer(s, server)
// Register reflection service on gRPC server.
reflection.Register(s)
s.Serve(lis)
if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}
func watchConfigTree(agent *Agent, queue chan consul.KVPairs) {
kv := agent.consulCli.KV()
var prevIndex uint64
for {
kvs, queryMeta, err := kv.List("instances/"+InstanceID+"/services/", &consul.QueryOptions{
WaitIndex: prevIndex,
})
if err != nil {
log.Fatal(err)
}
lastIndex := queryMeta.LastIndex
if prevIndex != lastIndex {
queue <- kvs
prevIndex = lastIndex
}
}
}
func pollConfigTree(agent *Agent, queue chan consul.KVPairs, interval time.Duration) {
kv := agent.consulCli.KV()
for {
kvs, _, err := kv.List("instances/"+InstanceID+"/services", nil)
if err != nil {
log.Fatal(err)
}
select {
case queue <- kvs:
default:
// something is already in the queue, lets not add to
}
time.Sleep(interval)
}
}
func registerService(consulCli *consul.Client) {
agent := consulCli.Agent()
err := agent.ServiceRegister(&consul.AgentServiceRegistration{
ID: InstanceID,
Name: "opencopilot-agent",
Port: port,
Check: &consul.AgentServiceCheck{
CheckID: "agent-grpc",
Name: "Agent gRPC Health Check",
GRPC: "127.0.0.1:" + strconv.Itoa(port),
Interval: "10s",
},
})
if err != nil {
log.Fatal(err)
}
}
func main() {
if InstanceID == "" {
panic(errors.New("No instance ID specified"))
}
consulClientConfig := consul.DefaultConfig()
if os.Getenv("ENV") == "dev" {
consulClientConfig.Address = "host.docker.internal:8500"
}
consulCli, err := consul.NewClient(consulClientConfig)
if err != nil {
log.Fatalf("failed to initialize consul client")
}
dockerCli, err := dockerClient.NewClientWithOpts(dockerClient.WithVersion("1.37"))
if err != nil {
log.Fatalf("failed to initialize docker client")
}
server := &server{
dockerCli: dockerCli,
consulCli: consulCli,
}
agent := server.ToAgent()
queue := make(chan consul.KVPairs, 1)
log.Println("starting to watch Consul KV...")
go watchConfigTree(agent, queue)
log.Println("starting public gRPC...")
go servePublicGRPC(server)
log.Println("starting private gRPC...")
go servePrivateGRPC(server)
log.Println("registering service...")
registerService(consulCli)
log.Println("starting to poll Consul KV...")
interval, _ := time.ParseDuration("15s") // Move this to an ENV var?
go pollConfigTree(agent, queue, interval)
log.Println("starting config handler...")
agent.startConfigHandler(queue)
}