-
Notifications
You must be signed in to change notification settings - Fork 4
/
config.go
558 lines (525 loc) · 14.9 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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
// Copyright 2018 GRAIL, Inc. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
// Package infra provides cloud infrastructure management for Go
// programs. The package includes facilities for configuring,
// provisioning, and migrating cloud infrastructure that is used by a Go
// program. You can think of infra as a simple, embedded version of
// Terraform, combined with a self-contained dependency injection
// framework.
//
// Infrastructure managed by this package is exposed through a
// configuration. Configurations specify which providers should be used
// for which infrastructure component; configurations also store
// provider configuration and migration state. Users instantiate typed
// values directly from the configuration: the details of configuration
// and of managing dependencies between infrastructure components is
// handled by the config object itself. Configurations are marshaled and
// must be stored by the user.
//
// Infrastructure migration is handled by maintaining a set of versions
// for each provider; migrations perform side-effects and can modify the
// configuration accordingly (e.g., to store identifiers used by the
// cloud infrastructure provider).
package infra
import (
"flag"
"fmt"
"log"
"reflect"
"runtime"
"strings"
yaml "gopkg.in/yaml.v2"
)
// A Schema defines a mapping between configuration keys and the
// types of values provided by those configuration keys. For example,
// the key "cluster" may provide values of the type
// "cluster.Interface". Schemas themselves are represented by strings
// to zero values of the mapped type. Interface types should use
// a pointer to a zero value. The following schema defines a mapping
// between to two interface types and a value type.
//
// type Cluster interface { ... }
// type BlobStore interface { ... }
// type User string
//
// var schema = infra.Schema{
// "cluster": new(Cluster),
// "repository": new(BlobStore),
// "user": User(""),
// }
//
// Schemas must be bijective: multiple keys cannot map to the same
// type.
type Schema map[string]interface{}
// Make builds a new configuration based on the Schema s with the
// provided configuration keys. Make ensures that the configuration
// is well-formed: that there are no dependency cycles and that all
// needed dependencies are satisfied. Make panics if the schema is
// not a bijection.
//
// Make performs all necessary type checking, ensuring that the
// schema is valid and that the configured providers are
// type-compatible with the keys laid out in the schema. Besides
// exact matches, where the schema type matches the provider type
// exactly, the following type conversions are allowable:
//
// - the provider type is assignable to the schema type (e.g., the
// schema type is an interface that is implemented by the
// provider); or
// - the provider type is a struct (or pointer to struct) that
// contains an embedded field that is assignable to the schema
// type.
func (s Schema) Make(keys Keys) (Config, error) {
keys = keys.Clone()
config := Config{
Keys: keys,
schema: s,
types: s.types(),
versions: make(map[string]int),
instances: make(map[reflect.Type]*instance),
}
config.typeset = make([]reflect.Type, 0, len(config.types))
for k := range config.types {
config.typeset = append(config.typeset, k)
}
if v := keys["versions"]; v != nil {
if err := remarshal(v, config.versions); err != nil {
return Config{}, err
}
}
if err := config.build(); err != nil {
return Config{}, err
}
return config, nil
}
// Unmarshal unmarshals the configuration keys in the YAML-formatted
// byte buffer p. The configuration is then initialized with Make.
func (s Schema) Unmarshal(p []byte) (Config, error) {
keys := make(Keys)
if err := yaml.Unmarshal(p, keys); err != nil {
return Config{}, err
}
return s.Make(keys)
}
func (s Schema) types() map[reflect.Type]string {
types := make(map[reflect.Type]string)
for k, zero := range s {
typ := reflect.TypeOf(zero)
if typ.Kind() == reflect.Ptr && typ.Elem().Kind() == reflect.Interface {
typ = typ.Elem()
}
if _, ok := types[typ]; ok {
log.Panicf("bindings not bijective for type %v", typ)
}
types[typ] = k
}
return types
}
// A Config manages a concrete configuration of infrastructure
// providers. Configs are instantiated from a Schema, which also
// performs validation. Configurations are responsible for mapping
// and configuring concrete instances into the types specified by the
// schema.
type Config struct {
Keys
schema Schema
types map[reflect.Type]string
instances map[reflect.Type]*instance
order []*instance
typeset []reflect.Type
versions map[string]int
}
// Flag is a provider flag.
type Flag struct {
// Name of the flag
Name string
// Default Value if non empty.
DefaultValue string
// Help is the help text for the flag.
Help string
}
// Usage contains the usage information of the provider
type Usage struct {
// Name of the provider.
Name string
// Usage information of the provider.
Usage string
// Args to the provider.
Args []Flag
}
// Help returns Usages, organized by schema keys.
func (c Config) Help() map[string][]Usage {
usage := make(map[string][]Usage)
for typ, key := range c.types {
for k, p := range providers {
field, ok := assign(p.Type(), typ)
if !ok {
continue
}
inst := p.New(c, field)
flags := inst.Flags()
u := Usage{Name: k, Usage: inst.Help()}
flags.VisitAll(func(f *flag.Flag) {
arg := Flag{f.Name, f.DefValue, f.Usage}
u.Args = append(u.Args, arg)
})
usage[key] = append(usage[key], u)
}
}
return usage
}
// Instance stores the configuration-managed instance into the
// provided pointer. Instance panics if ptr is not pointer-typed.
// Instance returns an error if no providers are configured for the
// requested type, or if the provider's initialization failed.
func (c Config) Instance(ptr interface{}) error {
vptr := reflect.ValueOf(ptr)
if vptr.Kind() != reflect.Ptr {
panic("infra.Instance: non-pointer argument")
}
typ, err := assignUnique(vptr.Type().Elem(), c.typeset)
if err != nil {
return err
}
inst := c.instances[typ]
if inst == nil {
_, file, line, _ := runtime.Caller(1)
return fmt.Errorf("no providers for type %s (%s:%d)", vptr.Type().Elem(), file, line)
}
value := inst.Value()
// If we get an instance, it's guaranteed to have well-formed dependencies.
if err := inst.Init(); err != nil {
return err
}
value = c.getValue(inst, vptr.Type().Elem())
vptr.Elem().Set(value)
return nil
}
func (c Config) getValue(inst *instance, typ reflect.Type) reflect.Value {
value := inst.Value()
ptyp := inst.val.Type()
pval := inst.val
for ptyp.Kind() == reflect.Ptr {
ptyp = ptyp.Elem()
pval = inst.val.Elem()
value = inst.val
}
if ptyp.Kind() == reflect.Struct {
for i := 0; i < ptyp.NumField(); i++ {
f := ptyp.Field(i)
// Skip non-embedded fields and ones that are not exported.
if !f.Anonymous || f.PkgPath != "" {
continue
}
if f.Type.AssignableTo(typ) {
value = pval.Field(i)
break
}
}
}
return value
}
// Must stores the configuration-managed instance into the provider
// pointer, as in Instance. Must fails fatally if any errors occur.
func (c Config) Must(ptr interface{}) {
if err := c.Instance(ptr); err != nil {
log.Fatal(err)
}
}
// Marshal marshals the configuration's using YAML and returns the
// marshaled content. The configuration can thus be persisted and
// restored with Schema.Unmarshal. If instances is true, then the
// instance configuration is marshaled as well, so that they may be
// restored.
func (c Config) Marshal(instances bool) ([]byte, error) {
keys := c.Keys.Clone()
keys["versions"] = c.versions
if instances {
// Make sure that reachable providers with instance configs are
// initialized.
for _, inst := range c.order {
if !inst.HasInstanceConfig() {
continue
}
if err := inst.Init(); err != nil {
return nil, err
}
}
} else {
delete(keys, "instances")
}
return yaml.Marshal(keys)
}
// Setup performs any required provider setup actions implied by this
// configuration. The configuration may be marshaled in the process
// and the caller should (re-)marshal the configuration after setup
// completes.
func (c Config) Setup() error {
for _, inst := range c.order {
impl := inst.Impl()
if version, ok := c.versions[impl]; ok && version >= inst.Version() {
continue
}
if err := inst.Setup(); err != nil {
return fmt.Errorf("setup %s: %v", inst.Impl(), err)
}
c.versions[impl] = inst.Version()
}
return nil
}
func (c Config) provider(key string) (p *provider, name string, err error) {
args, ok, err := c.Keys.String(key)
if err != nil {
return nil, "", err
}
if !ok {
return nil, "", nil
}
argv := strings.SplitN(args, ",", 2)
return lookup(argv[0]), argv[0], nil
}
func (c Config) args(key string) []string {
args, ok, err := c.Keys.String(key)
if err != nil {
panic(err)
}
if !ok || args == "" {
return nil
}
return strings.Split(args, ",")[1:]
}
func assignUnique(src reflect.Type, dsts []reflect.Type) (reflect.Type, error) {
var matches []reflect.Type
for _, dst := range dsts {
_, ok := assign(src, dst)
if ok {
matches = append(matches, dst)
}
}
switch {
case len(matches) == 0:
return nil, fmt.Errorf("no providers for type %v", src)
case len(matches) > 1:
return nil, fmt.Errorf("multiple providers for type %v: %v", src, matches)
}
return matches[0], nil
}
func assign(src, dst reflect.Type) (string, bool) {
if src.AssignableTo(dst) {
return "", true
}
// See if we can promote any anonymous fields.
ptyp := src
for ptyp.Kind() == reflect.Ptr {
ptyp = ptyp.Elem()
}
if ptyp.Kind() == reflect.Struct {
for i := 0; i < ptyp.NumField(); i++ {
f := ptyp.Field(i)
// Skip non-embedded fields and ones that are not exported.
if !f.Anonymous || f.PkgPath != "" {
continue
}
if f.Type.AssignableTo(dst) {
return f.Name, true
}
}
}
return "", false
}
func (c *Config) build() error {
graph := make(topoSorter)
// TODO(marius): we could separate out a schema check as a
// separate phase, so that we are guaranteed that this never
// fails.
instanceConfigs, _, err := c.Keys.Keys("instances")
if err != nil {
return err
}
if instanceConfigs == nil {
instanceConfigs = make(Keys)
}
for typ, key := range c.types {
p, impl, err := c.provider(key)
if err != nil {
return err
}
if p == nil {
if impl != "" {
pkg := impl
pkg = strings.TrimRightFunc(pkg, func(r rune) bool { return r != '.' })
pkg = strings.TrimRight(pkg, ".")
return fmt.Errorf("%s: no provider named %s (is package %s linked into the binary?)", key, impl, pkg)
}
// Ignore missing providers. They only matter if they're
// going to be used when instantiating values later on.
continue
}
field, ok := assign(p.Type(), typ)
if !ok {
return fmt.Errorf("provider implements type %s, which is incompatible to the bound type %s", p.Type(), typ)
}
inst := p.New(*c, field)
flags := inst.Flags()
for _, arg := range c.args(key) {
var (
kv = strings.SplitN(arg, "=", 2)
err error
)
switch len(kv) {
case 1:
err = flags.Set(kv[0], "") // ok for booleans
case 2:
err = flags.Set(kv[0], kv[1])
}
if err != nil {
return fmt.Errorf("provider %s flag %s: %v", impl, kv[0], err)
}
}
if src, dst := c.Value(impl), inst.Config(); src != nil && dst != nil {
if err := remarshal(src, dst); err != nil {
return err
}
}
if config := inst.Config(); config != nil {
c.Keys[impl] = config
}
// TODO(marius): support multiple instances per provider by naming
// these differently.
if src, dst := instanceConfigs.Value(impl), inst.InstanceConfig(); src != nil && dst != nil {
if err := remarshal(src, dst); err != nil {
return err
}
}
if instanceConfig := inst.InstanceConfig(); instanceConfig != nil {
instanceConfigs[impl] = instanceConfig
}
c.instances[typ] = inst
}
c.Keys["instances"] = instanceConfigs
for _, src := range c.instances {
graph.Add(src, nil)
for _, typ := range src.RequiresInit() {
typ, err = assignUnique(typ, c.typeset)
if err != nil {
return err
}
dst, ok := c.instances[typ]
if !ok {
log.Fatalf("%v Init requires %v: unspecified", src.name, typ)
}
graph.Add(src, dst)
}
for _, typ := range src.RequiresSetup() {
typ, err = assignUnique(typ, c.typeset)
if err != nil {
return err
}
dst, ok := c.instances[typ]
if !ok {
log.Fatalf("%v Setup requires %v: unspecified", src.name, typ.Name())
}
graph.Add(src, dst)
}
}
if cycle := graph.Cycle(); cycle != nil {
strs := make([]string, len(cycle))
for i := range strs {
strs[i] = cycle[i].Impl()
}
return fmt.Errorf("dependency cycle: %s", strings.Join(strs, "<-"))
}
c.order = graph.Sort()
return nil
}
// Keys holds the toplevel configuration keys as managed
// by a Keys. Each config instance defines a provider for this
// type to be used by other providers that may need to access
// the raw configuration (e.g., common config values).
type Keys map[string]interface{}
var typeOfKeys = reflect.TypeOf(Keys{})
// Value returns the value associated with the provided key.
func (k Keys) Value(key string) interface{} {
return k[key]
}
// Keys returns the Keys value of the provided key.
func (k Keys) Keys(key string) (Keys, bool, error) {
v, ok := k[key]
if !ok {
return nil, false, nil
}
raw, ok := v.(map[interface{}]interface{})
if !ok {
return nil, false, fmt.Errorf("%v not proper key: %v", key, reflect.TypeOf(v))
}
keys := make(Keys)
for k, v := range raw {
kstr, ok := k.(string)
if !ok {
return nil, false, fmt.Errorf("%v not proper2 key", key)
}
keys[kstr] = v
}
return keys, true, nil
}
// String returns the string value of the provided key.
func (k Keys) String(key string) (string, bool, error) {
v, ok := k[key]
if !ok {
return "", false, nil
}
s, ok := v.(string)
if !ok {
return "", false, fmt.Errorf("%v not string key", key)
}
return s, true, nil
}
// Int returns the integer value of the provided key.
func (k Keys) Int(key string) (int, bool, error) {
v, ok := k[key]
if !ok {
return 0, false, nil
}
s, ok := v.(int)
if !ok {
return 0, false, fmt.Errorf("%v not int key", key)
}
return s, true, nil
}
// Clone returns a deeply-copied version of keys.
func (k Keys) Clone() Keys {
return deepcopy(k).(Keys)
}
func remarshal(src, dst interface{}) error {
b, err := yaml.Marshal(src)
if err != nil {
return err
}
err = yaml.Unmarshal(b, dst)
return yaml.Unmarshal(b, dst)
}
func deepcopy(v interface{}) interface{} {
switch w := v.(type) {
case Keys:
copy := make(Keys)
for k, v := range w {
copy[k] = deepcopy(v)
}
return copy
case map[string]interface{}:
copy := make(map[string]interface{})
for k, v := range w {
copy[k] = deepcopy(v)
}
return copy
case map[interface{}]interface{}:
copy := make(map[interface{}]interface{})
for k, v := range w {
copy[k] = deepcopy(v)
}
return copy
default:
return v
}
}