/
main.go
362 lines (302 loc) · 9.81 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
// Copyright (c) 2019 Uber Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"net/http"
"os"
"time"
"github.com/uber/peloton/.gen/peloton/api/v0/respool"
statelesssvc "github.com/uber/peloton/.gen/peloton/api/v1alpha/job/stateless/svc"
podsvc "github.com/uber/peloton/.gen/peloton/api/v1alpha/pod/svc"
watchsvc "github.com/uber/peloton/.gen/peloton/api/v1alpha/watch/svc"
"github.com/uber/peloton/.gen/peloton/private/jobmgrsvc"
"github.com/uber/peloton/.gen/thrift/aurora/api/auroraschedulermanagerserver"
"github.com/uber/peloton/.gen/thrift/aurora/api/readonlyschedulerserver"
"github.com/uber/peloton/pkg/aurorabridge/cache"
auth_impl "github.com/uber/peloton/pkg/auth/impl"
"github.com/uber/peloton/pkg/aurorabridge"
bridgecommon "github.com/uber/peloton/pkg/aurorabridge/common"
"github.com/uber/peloton/pkg/auth"
"github.com/uber/peloton/pkg/common"
"github.com/uber/peloton/pkg/common/buildversion"
"github.com/uber/peloton/pkg/common/config"
"github.com/uber/peloton/pkg/common/health"
"github.com/uber/peloton/pkg/common/leader"
"github.com/uber/peloton/pkg/common/logging"
"github.com/uber/peloton/pkg/common/metrics"
"github.com/uber/peloton/pkg/common/rpc"
"github.com/uber/peloton/pkg/hostmgr/mesos/yarpc/peer"
"github.com/uber/peloton/pkg/middleware/inbound"
"github.com/uber/peloton/pkg/middleware/outbound"
log "github.com/sirupsen/logrus"
"go.uber.org/yarpc"
"go.uber.org/yarpc/api/transport"
"go.uber.org/yarpc/transport/grpc"
kingpin "gopkg.in/alecthomas/kingpin.v2"
)
var (
version string
app = kingpin.New("peloton-aurorabridge", "Peloton Aurora bridge")
enableSentry = app.Flag(
"enable-sentry", "enable logging hook up to sentry").
Default("false").
Envar("ENABLE_SENTRY_LOGGING").
Bool()
cfgFiles = app.Flag(
"config",
"YAML config files (can be provided multiple times to merge configs)").
Short('c').
Required().
ExistingFiles()
electionZkServers = app.Flag(
"election-zk-server",
"Election Zookeeper servers. Specify multiple times for multiple servers "+
"(election.zk_servers override) (set $ELECTION_ZK_SERVERS to override)").
Envar("ELECTION_ZK_SERVERS").
Strings()
datacenter = app.Flag(
"datacenter", "Datacenter name").
Default("").
Envar("DATACENTER").
String()
httpPort = app.Flag(
"http-port", "Aurora Bridge HTTP port (aurorabridge.http_port override) "+
"(set $PORT to override)").
Default("5396").
Envar("HTTP_PORT").
Int()
grpcPort = app.Flag(
"grpc-port", "Aurora Bridge gRPC port (aurorabridge.grpc_port override) "+
"(set $PORT to override)").
Envar("GRPC_PORT").
Int()
respoolPath = app.Flag(
"respool-path", "Aurora Bridge Resource Pool path").
Envar("RESPOOL_PATH").
String()
gpuRespoolPath = app.Flag(
"gpu-respool-path", "Aurora Bridge GPU Resource Pool path").
Envar("GPU_RESPOOL_PATH").
String()
authType = app.Flag(
"auth-type",
"Define the auth type used, default to NOOP").
Default("NOOP").
Envar("AUTH_TYPE").
Enum("NOOP", "BASIC")
authConfigFile = app.Flag(
"auth-config-file",
"config file for the auth feature, which is specific to the auth type used").
Default("").
Envar("AUTH_CONFIG_FILE").
String()
enableInPlace = app.Flag(
"enable-inplace-update", "enable in-place update").
Default("false").
Envar("ENABLE_INPLACE_UPDATE").
Bool()
)
func main() {
app.Version(version)
app.HelpFlag.Short('h')
kingpin.MustParse(app.Parse(os.Args[1:]))
log.SetFormatter(
&logging.LogFieldFormatter{
Formatter: &log.JSONFormatter{},
Fields: log.Fields{
common.AppLogField: app.Name,
},
},
)
var cfg Config
if err := config.Parse(&cfg, *cfgFiles...); err != nil {
log.Fatalf("Error parsing yaml config: %s", err)
}
if *enableSentry {
logging.ConfigureSentry(&cfg.SentryConfig)
}
if len(*electionZkServers) > 0 {
cfg.Election.ZKServers = *electionZkServers
}
if *httpPort != 0 {
cfg.HTTPPort = *httpPort
}
if *grpcPort != 0 {
cfg.GRPCPort = *grpcPort
}
if len(*respoolPath) > 0 {
cfg.RespoolLoader.RespoolPath = *respoolPath
}
if len(*gpuRespoolPath) > 0 {
cfg.RespoolLoader.GPURespoolPath = *gpuRespoolPath
}
// Parse and setup peloton auth
if len(*authType) != 0 {
cfg.Auth.AuthType = auth.Type(*authType)
cfg.Auth.Path = *authConfigFile
}
if *enableInPlace {
cfg.ServiceHandler.EnableInPlace = true
}
initialLevel := log.InfoLevel
if cfg.Debug {
initialLevel = log.DebugLevel
}
log.SetLevel(initialLevel)
log.WithField("config", cfg).Info("Loaded AuroraBridge configuration")
rootScope, scopeCloser, mux := metrics.InitMetricScope(
&cfg.Metrics,
common.PelotonAuroraBridge,
metrics.TallyFlushInterval,
)
defer scopeCloser.Close()
mux.HandleFunc(
logging.LevelOverwrite,
logging.LevelOverwriteHandler(initialLevel))
mux.HandleFunc(buildversion.Get, buildversion.Handler(version))
// Create both HTTP and GRPC inbounds
inbounds := rpc.NewAuroraBridgeInbounds(
cfg.HTTPPort,
cfg.GRPCPort, // dummy grpc port for aurora bridge
mux)
discovery, err := leader.NewZkServiceDiscovery(
cfg.Election.ZKServers, cfg.Election.Root)
if err != nil {
log.WithError(err).
Fatal("Could not create zk service discovery")
}
clientRecvOption := grpc.ClientMaxRecvMsgSize(cfg.EventPublisher.GRPCMsgSize)
serverRecvOption := grpc.ServerMaxRecvMsgSize(cfg.EventPublisher.GRPCMsgSize)
t := grpc.NewTransport(
clientRecvOption,
serverRecvOption,
)
outbounds := yarpc.Outbounds{
common.PelotonJobManager: transport.Outbounds{
Unary: t.NewOutbound(
peer.NewPeerChooser(t, 1*time.Second, discovery.GetAppURL, common.JobManagerRole),
),
Stream: t.NewOutbound(
peer.NewPeerChooser(t, 1*time.Second, discovery.GetAppURL, common.JobManagerRole),
),
},
common.PelotonResourceManager: transport.Outbounds{
Unary: t.NewOutbound(
peer.NewPeerChooser(t, 1*time.Second, discovery.GetAppURL, common.ResourceManagerRole),
),
},
}
securityManager, err := auth_impl.CreateNewSecurityManager(&cfg.Auth)
if err != nil {
log.WithError(err).
Fatal("Could not enable security feature")
}
rateLimitMiddleware, err := inbound.NewRateLimitInboundMiddleware(cfg.RateLimit)
if err != nil {
log.WithError(err).
Fatal("Could not create rate limit middleware")
}
authInboundMiddleware := inbound.NewAuthInboundMiddleware(securityManager)
securityClient, err := auth_impl.CreateNewSecurityClient(&cfg.Auth)
if err != nil {
log.WithError(err).
Fatal("Could not establish secure inter-component communication")
}
authOutboundMiddleware := outbound.NewAuthOutboundMiddleware(securityClient)
dispatcher := yarpc.NewDispatcher(yarpc.Config{
Name: common.PelotonAuroraBridge,
Inbounds: inbounds,
Outbounds: outbounds,
Metrics: yarpc.MetricsConfig{
Tally: rootScope,
},
InboundMiddleware: yarpc.InboundMiddleware{
Unary: yarpc.UnaryInboundMiddleware(rateLimitMiddleware, authInboundMiddleware),
Stream: yarpc.StreamInboundMiddleware(rateLimitMiddleware, authInboundMiddleware),
Oneway: yarpc.OnewayInboundMiddleware(rateLimitMiddleware, authInboundMiddleware),
},
OutboundMiddleware: yarpc.OutboundMiddleware{
Unary: authOutboundMiddleware,
Stream: authOutboundMiddleware,
Oneway: authOutboundMiddleware,
},
})
jobClient := statelesssvc.NewJobServiceYARPCClient(
dispatcher.ClientConfig(common.PelotonJobManager))
jobmgrClient := jobmgrsvc.NewJobManagerServiceYARPCClient(
dispatcher.ClientConfig(common.PelotonJobManager))
podClient := podsvc.NewPodServiceYARPCClient(
dispatcher.ClientConfig(common.PelotonJobManager))
respoolClient := respool.NewResourceManagerYARPCClient(
dispatcher.ClientConfig(common.PelotonResourceManager))
watchClient := watchsvc.NewWatchServiceYARPCClient(
dispatcher.ClientConfig(common.PelotonJobManager))
// Start the dispatcher before we register the aurorabridge handler, since we'll
// need to make some outbound requests to get things setup.
if err := dispatcher.Start(); err != nil {
log.Fatalf("Could not start rpc server: %v", err)
}
eventPublisher := aurorabridge.NewEventPublisher(
cfg.EventPublisher.KafkaURL,
jobClient,
podClient,
watchClient,
&http.Client{},
cfg.EventPublisher.PublishEvents,
)
server, err := aurorabridge.NewServer(
cfg.HTTPPort,
cfg.Election,
eventPublisher,
common.PelotonAuroraBridgeRole,
)
if err != nil {
log.Fatalf("Unable to create server: %v", err)
}
candidate, err := leader.NewCandidate(
cfg.Election,
rootScope,
common.PelotonAuroraBridgeRole,
server,
)
if err != nil {
log.Fatalf("Unable to create leader candidate: %v", err)
}
respoolLoader := aurorabridge.NewRespoolLoader(cfg.RespoolLoader, respoolClient)
handler, err := aurorabridge.NewServiceHandler(
cfg.ServiceHandler,
rootScope,
jobClient,
jobmgrClient,
podClient,
respoolLoader,
bridgecommon.RandomImpl{},
cache.NewJobIDCache(),
)
if err != nil {
log.Fatalf("Unable to create service handler: %v", err)
}
dispatcher.Register(auroraschedulermanagerserver.New(handler))
dispatcher.Register(readonlyschedulerserver.New(handler))
if err := candidate.Start(); err != nil {
log.Fatalf("Unable to start leader candidate: %v", err)
}
defer candidate.Stop()
log.WithFields(log.Fields{
"httpPort": cfg.HTTPPort,
}).Info("Started Aurora Bridge")
// we can *honestly* say the server is booted up now
health.InitHeartbeat(rootScope, cfg.Health, candidate)
select {}
}