-
Notifications
You must be signed in to change notification settings - Fork 66
/
config.go
480 lines (438 loc) · 15.2 KB
/
config.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package repository
import (
"os"
"path/filepath"
"strings"
"encoding/json"
"github.com/pkg/errors"
"github.com/uber/zanzibar/codegen"
zanzibar "github.com/uber/zanzibar/runtime"
)
// Config stores configuration for a gateway.
type Config struct {
ID string
Repository string
Team string
Tier int
ThriftRootDir string
PackageRoot string
ManagedThriftFolder string
GenCodePackage string
TargetGenDir string
ClientConfigDir string
EndpointConfigDir string
MiddlewareConfigFile string
// Maps endpointID to configuration.
Endpoints map[string]*EndpointConfig
// Maps clientID to configuration.
Clients map[string]*ClientConfig
ThriftServices ThriftServiceMap
Middlewares map[string]interface{}
RawMiddlewares map[string]*codegen.MiddlewareConfig
}
// ThriftServiceMap maps thrift file -> service name -> *ThriftService
type ThriftServiceMap map[string]map[string]*ThriftService
// EndpointConfig stores configuration for an endpoint.
type EndpointConfig struct {
ID string `json:"endpointId"`
Type ProtocolType `json:"endpointType"`
HandleID string `json:"handleId"`
ThriftFile string `json:"thriftFile"`
ThriftFileSha string `json:"thriftFileSha,omitempty"`
ThriftMethodName string `json:"thriftMethodName"`
WorkflowType string `json:"workflowType"`
ClientID string `json:"clientId"`
ClientMethod string `json:"clientMethod"`
TestFixtures map[string]*codegen.EndpointTestFixture `json:"testFixtures"`
Middlewares []*codegen.MiddlewareSpec `json:"middlewares"`
ReqHeaderMap map[string]string `json:"reqHeaderMap"`
ResHeaderMap map[string]string `json:"resHeaderMap"`
}
// ClientConfig stores configuration for an client.
type ClientConfig struct {
Name string `json:"name"`
Type ProtocolType `json:"type"`
ThriftFile string `json:"thriftFile,omitempty"`
ServiceName string `json:"serviceName,omitempty"`
ExposedMethods map[string]string `json:"exposedMethods,omitempty"`
IP string `json:"ip,omitempty"`
Port int64 `json:"port,omitempty"`
Timeout int64 `json:"clientTimeout,omitempty"`
TimeoutPerAttempt int64 `json:"clientTimeoutPerAttempt,omitempty"`,
RoutingKey string `json:"routingKey"`
}
// ThriftService is a service defined in Thrift file.
type ThriftService struct {
Name string
Path string
Methods []ThriftMethod
}
// ThriftMethod is a method defined in a Thrift Service.
type ThriftMethod struct {
Name string
Type ProtocolType
}
// ThriftMeta is the meta about a thrift file.
type ThriftMeta struct {
// relative path under thrift root directory.
Path string `json:"path"`
// committed version
Version string `json:"version"`
// content of the thrift file
Content string `json:"content,omitempty"`
}
// ProtocolType represents tranportation protocol type.
type ProtocolType string
const (
gatewayConfigFile = "gateway.json"
productionCfgJSONPath = "config/production.json"
clientConfigFileName = "client-config.json"
clientModuleFileName = "clients-config.json"
endpointConfigFileName = "endpoint-config.json"
)
const (
// HTTP type
HTTP ProtocolType = "http"
// TCHANNEL type
TCHANNEL ProtocolType = "tchannel"
// CUSTOM type
CUSTOM ProtocolType = "custom"
// UNKNOWN type
UNKNOWN ProtocolType = "unknown"
)
// GatewayConfig returns the cached gateway configuration of the repository if
// the repository has not been updated for a certain time.
func (r *Repository) GatewayConfig() (*Config, error) {
r.RLock()
curCfg, curCfgError := r.gatewayConfig, r.gatewayConfigError
r.RUnlock()
if (curCfg != nil || curCfgError != nil) && !r.Update() {
return curCfg, curCfgError
}
return r.LatestGatewayConfig()
}
// LatestGatewayConfig returns the configuration of current repository.
func (r *Repository) LatestGatewayConfig() (*Config, error) {
// The newCfg won't be changed once created.
newCfg, newCfgError := r.newGatewayConfig()
r.Lock()
r.gatewayConfig, r.gatewayConfigError = newCfg, newCfgError
r.Unlock()
return newCfg, newCfgError
}
// newGatewayConfig regenerates the configuration for a repository.
func (r *Repository) newGatewayConfig() (configuration *Config, cfgErr error) {
defer func() {
if p := recover(); p != nil {
cfgErr = errors.Errorf(
"panic when getting configuration for gateway %s: %+v",
r.LocalDir(), p,
)
}
}()
// Get the read lock for reading the content of repository.
r.RLock()
defer r.RUnlock()
configDir := r.absPath(r.LocalDir())
cfg := zanzibar.NewStaticConfigOrDie([]*zanzibar.ConfigOption{
zanzibar.ConfigFilePath(filepath.Join(configDir, gatewayConfigFile)),
}, nil)
config := &Config{
ID: cfg.MustGetString("gatewayName"),
Repository: r.remote,
ThriftRootDir: cfg.MustGetString("thriftRootDir"),
PackageRoot: cfg.MustGetString("packageRoot"),
ManagedThriftFolder: cfg.MustGetString("managedThriftFolder"),
GenCodePackage: cfg.MustGetString("genCodePackage"),
TargetGenDir: cfg.MustGetString("targetGenDir"),
ClientConfigDir: cfg.MustGetString("clientConfig"),
EndpointConfigDir: cfg.MustGetString("endpointConfig"),
MiddlewareConfigFile: cfg.MustGetString("middlewareConfig"),
}
pkgHelper, err := codegen.NewPackageHelper(
config.PackageRoot,
config.ManagedThriftFolder,
configDir,
config.MiddlewareConfigFile,
config.ThriftRootDir,
config.GenCodePackage,
config.TargetGenDir,
"",
)
if err != nil {
return nil, errors.Wrapf(err, "failed to create package helper")
}
moduleSystem, err := codegen.NewDefaultModuleSystem(pkgHelper)
if err != nil {
return nil, errors.Wrapf(err, "failed to create module system")
}
moduleInstances, err := moduleSystem.ResolveModules(
pkgHelper.PackageRoot(),
configDir,
pkgHelper.CodeGenTargetPath(),
)
if err != nil {
return nil, errors.Wrapf(err, "failed to resolve module instances")
}
gatewaySpec, err := codegen.NewGatewaySpec(
moduleInstances,
pkgHelper,
configDir,
config.EndpointConfigDir,
config.MiddlewareConfigFile,
config.ID,
)
if err != nil {
return nil, errors.Wrap(err, "failed to read gateway spec")
}
if config.ThriftServices, err = r.thriftservices(config.ThriftRootDir, pkgHelper); err != nil {
return nil, errors.Wrap(err, "failed to read thrift services")
}
if config.Clients, err = r.clientConfigs(config.ThriftRootDir, gatewaySpec); err != nil {
return nil, errors.Wrap(err, "failed to read client configuration")
}
config.Endpoints = r.endpointConfigs(config.ThriftRootDir, gatewaySpec)
if config.Middlewares, config.RawMiddlewares, err = r.middlewareConfigs(
config.MiddlewareConfigFile,
); err != nil {
return nil, errors.Wrap(err, "fail to read middleware configuration")
}
return config, nil
}
func (r *Repository) clientConfigs(
thriftRootDir string,
gatewaySpec *codegen.GatewaySpec,
) (map[string]*ClientConfig, error) {
cfgs := make(map[string]*ClientConfig, len(gatewaySpec.ClientModules))
productionCfgJSON := map[string]interface{}{}
path := r.absPath(productionCfgJSONPath)
err := readJSONFile(path, &productionCfgJSON)
if err != nil {
return nil, err
}
for _, spec := range gatewaySpec.ClientModules {
cfgs[spec.ClientID], err = clientConfig(spec, productionCfgJSON)
if err != nil {
return nil, errors.Wrapf(err, "failed to parse config %q for client %q", path, spec.ClientID)
}
if cfgs[spec.ClientID].Type != CUSTOM {
cfgs[spec.ClientID].ThriftFile = r.relativePath(thriftRootDir, spec.ThriftFile)
}
}
return cfgs, nil
}
func clientConfig(spec *codegen.ClientSpec, productionCfgJSON map[string]interface{}) (*ClientConfig, error) {
clientConfig := &ClientConfig{
Name: spec.ClientID,
Type: ProtocolTypeFromString(spec.ClientType),
ExposedMethods: spec.ExposedMethods,
}
// TODO(zw): handle configurations of a customized client.
if clientConfig.Type != HTTP && clientConfig.Type != TCHANNEL {
return clientConfig, nil
}
var prefix string
if spec.SidecarRouter != "" {
prefix = "sidecarRouter." + spec.SidecarRouter + "." + string(clientConfig.Type) + "."
} else {
prefix = "clients." + spec.ClientID + "."
}
var err error
clientConfig.IP, err = convStrVal(productionCfgJSON, prefix+"ip")
if err != nil {
return nil, err
}
clientConfig.Port, err = convInt64Val(productionCfgJSON, prefix+"port")
if err != nil {
return nil, err
}
if clientConfig.Type == HTTP {
return clientConfig, nil
}
// tchannel related fields.
prefix = "clients." + spec.ClientID + "."
clientConfig.ServiceName, err = convStrVal(productionCfgJSON, prefix+"serviceName")
if err != nil {
return nil, err
}
clientConfig.Timeout, err = convInt64Val(productionCfgJSON, prefix+"timeout")
if err != nil {
return nil, err
}
clientConfig.TimeoutPerAttempt, err = convInt64Val(productionCfgJSON, prefix+"timeoutPerAttempt")
if err != nil {
return nil, err
}
return clientConfig, nil
}
func convStrVal(m map[string]interface{}, key string) (string, error) {
if _, ok := m[key]; !ok {
return "", errors.Errorf("key %q is not found", key)
}
val, ok := m[key].(string)
if !ok {
return "", errors.Errorf("key %q is not string, but %T", key, m[key])
}
return val, nil
}
func convInt64Val(m map[string]interface{}, key string) (int64, error) {
if _, ok := m[key]; !ok {
return 0, errors.Errorf("key %q is not found", key)
}
val, ok := m[key].(float64)
if !ok {
return 0, errors.Errorf("key %q is not int, but %T", key, m[key])
}
return int64(val), nil
}
func (r *Repository) endpointConfigs(thriftRootDir string, gatewaySpec *codegen.GatewaySpec) map[string]*EndpointConfig {
cfgs := make(map[string]*EndpointConfig, len(gatewaySpec.EndpointModules))
for _, spec := range gatewaySpec.EndpointModules {
endpointID := spec.EndpointID + "." + spec.HandleID
cfgs[endpointID] = &EndpointConfig{
ID: spec.EndpointID,
Type: ProtocolTypeFromString(spec.EndpointType),
HandleID: spec.HandleID,
ThriftFile: r.relativePath(thriftRootDir, spec.ThriftFile),
ThriftMethodName: spec.ThriftServiceName + "::" + spec.ThriftMethodName,
WorkflowType: spec.WorkflowType,
ClientID: spec.ClientID,
ClientMethod: spec.ClientMethod,
// TODO(zw): add test fixtures and middleware config.
}
}
return cfgs
}
func (r *Repository) middlewareConfigs(
middlewareConfigFile string,
) (map[string]interface{}, map[string]*codegen.MiddlewareConfig, error) {
middlewares := make(map[string]*codegen.MiddlewareConfig)
cfgs := make(map[string]interface{})
var rawCfgs map[string][]*codegen.MiddlewareConfig
if middlewareConfigFile == "" {
return cfgs, middlewares, nil
}
bytes, err := r.ReadFile(middlewareConfigFile)
if err != nil {
return nil, nil, err
}
if len(bytes) == 0 {
return nil, nil, errors.New("middlewares file is an empty file")
}
err = json.Unmarshal(bytes, &rawCfgs)
if err != nil {
return nil, nil, err
}
if _, ok := rawCfgs["middlewares"]; !ok {
return nil, nil, errors.New(
"middlewares config is missing root object property \"middlewares\"",
)
}
for _, mc := range rawCfgs["middlewares"] {
schemaFile := mc.SchemaFile
bytes, err := r.ReadFile(schemaFile)
if err != nil {
return nil, nil, errors.Wrapf(
err, "could not read schemaFile(%s)", schemaFile,
)
}
rawData := map[string]interface{}{}
err = json.Unmarshal(bytes, &rawData)
if err != nil {
return nil, nil, errors.Wrapf(
err, "could not parse schemaFile(%s)", schemaFile,
)
}
cfgs[mc.Name] = rawData
middlewares[mc.Name] = mc
}
return cfgs, middlewares, nil
}
func (r *Repository) thriftservices(thriftRootDir string, packageHelper *codegen.PackageHelper) (ThriftServiceMap, error) {
idlMap := make(ThriftServiceMap)
walkFn := func(path string, info os.FileInfo, err error) error {
if info.IsDir() || filepath.Ext(path) != ".thrift" {
return nil
}
if err != nil {
return err
}
// Parse service module as tchannel service.
mspec, err := codegen.NewModuleSpec(path, false, false, packageHelper)
if err != nil {
return errors.Wrapf(err, "failed to genenerate module spec for thrift %s", path)
}
serviceType := TCHANNEL
// Parse HTTP annotations.
if _, err := codegen.NewModuleSpec(path, true, false, packageHelper); err == nil {
serviceType = HTTP
}
relativePath := r.relativePath(thriftRootDir, path)
idlMap[relativePath] = map[string]*ThriftService{}
for _, service := range mspec.Services {
tservice := &ThriftService{
Name: service.Name,
Path: relativePath,
}
tservice.Methods = make([]ThriftMethod, len(service.Methods))
for i, method := range service.Methods {
tservice.Methods[i].Name = method.Name
tservice.Methods[i].Type = serviceType
}
idlMap[relativePath][service.Name] = tservice
}
return nil
}
if err := filepath.Walk(r.absPath(thriftRootDir), walkFn); err != nil {
return nil, errors.Wrapf(err, "failed to traverse IDL dir")
}
return idlMap, nil
}
func (r *Repository) absPath(path string) string {
if filepath.IsAbs(path) {
return path
}
path, err := filepath.Abs(filepath.Join(r.localDir, path))
if err != nil {
return err.Error()
}
return path
}
func (r *Repository) relativePath(rootDir string, filePath string) string {
rootAbsDir := r.absPath(rootDir)
fileAbsPath := r.absPath(filePath)
relative := strings.TrimPrefix(fileAbsPath, rootAbsDir)
return strings.TrimPrefix(relative, "/")
}
// ProtocolTypeFromString converts a string to ProtocolType.
func ProtocolTypeFromString(str string) ProtocolType {
switch str {
case "http":
return HTTP
case "tchannel":
return TCHANNEL
case "custom":
return CUSTOM
default:
return UNKNOWN
}
}