-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
528 lines (431 loc) · 13.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
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
package hclconfig
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"strings"
"sync"
"sync/atomic"
"github.com/hashicorp/errwrap"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/jumppad-labs/hclconfig/errors"
"github.com/jumppad-labs/hclconfig/resources"
"github.com/jumppad-labs/hclconfig/types"
"github.com/silas/dag"
)
// Config defines the stack config
type Config struct {
Resources []types.Resource `json:"resources"`
contexts map[types.Resource]*hcl.EvalContext
bodies map[types.Resource]*hclsyntax.Body
sync sync.Mutex
}
// ResourceNotFoundError is thrown when a resource could not be found
type ResourceNotFoundError struct {
Name string
}
func (e ResourceNotFoundError) Error() string {
return fmt.Sprintf("resource not found: %s", e.Name)
}
// ResourceExistsError is thrown when a resource already exists in the resource list
type ResourceExistsError struct {
Name string
}
func (e ResourceExistsError) Error() string {
return fmt.Sprintf("resource already exists: %s", e.Name)
}
// New creates a new Config
func NewConfig() *Config {
c := &Config{
Resources: []types.Resource{},
contexts: map[types.Resource]*hcl.EvalContext{},
bodies: map[types.Resource]*hclsyntax.Body{},
sync: sync.Mutex{},
}
return c
}
// FindResource returns the resource for the given name
// name is defined with the convention: resource.[type].[name]
// the keyword "resource" is a required component in the path to allow
// names of resources to contain "." and to enable easy separate of
// module parts.
//
// "module" is an optional path parameter: module.[module_name].resource.[type].[name]
// and is required when searching for resources that have the Module flag set.
//
// If a resource can not be found, resource will be null and an
// error will be returned
//
// e.g. to find a cluster named k3s
// r, err := c.FindResource("resource.cluster.k3s")
//
// e.g. to find a cluster named k3s in the module module1
// r, err := c.FindResource("module.module1.resource.cluster.k3s")
func (c *Config) FindResource(path string) (types.Resource, error) {
c.sync.Lock()
defer c.sync.Unlock()
return c.findResource(path)
}
// local version of FindResource that does not lock the config
func (c *Config) findResource(path string) (types.Resource, error) {
fqdn, err := resources.ParseFQRN(path)
if err != nil {
return nil, err
}
// this is an internal error and should not happen unless there is an issue with a provider
// there was, hence why we are here
if c.Resources == nil {
return nil, fmt.Errorf("unable to find resources, reference to parent config does not exist. Ensure that the object has been added to the config: `config.Info.AddChild(type)`")
}
for _, r := range c.Resources {
if r.Metadata().Module == fqdn.Module &&
r.Metadata().Type == fqdn.Type &&
r.Metadata().Name == fqdn.Resource {
return r, nil
}
}
return nil, ResourceNotFoundError{path}
}
func (c *Config) FindRelativeResource(path string, parentModule string) (types.Resource, error) {
c.sync.Lock()
defer c.sync.Unlock()
fqdn, err := resources.ParseFQRN(path)
if err != nil {
return nil, err
}
if parentModule != "" {
mod := fmt.Sprintf("%s.%s", parentModule, fqdn.Module)
// fqdn.Module could be nil
mod = strings.Trim(mod, ".")
fqdn.Module = mod
}
r, err := c.findResource(fqdn.String())
if err != nil {
return nil, err
}
return r, nil
}
// FindResourcesByType returns the resources from the given type
func (c *Config) FindResourcesByType(t string) ([]types.Resource, error) {
c.sync.Lock()
defer c.sync.Unlock()
res := []types.Resource{}
for _, r := range c.Resources {
if r.Metadata().Type == t {
res = append(res, r)
}
}
if len(res) > 0 {
return res, nil
}
return nil, ResourceNotFoundError{t}
}
// FindModuleResources returns an array of resources for the given module
// if includeSubModules is true then resources in any submodules
// are also returned
// if includeSubModules is false only the resources defined in the given module are returned
func (c *Config) FindModuleResources(module string, includeSubModules bool) ([]types.Resource, error) {
c.sync.Lock()
defer c.sync.Unlock()
fqdn, err := resources.ParseFQRN(module)
if err != nil {
return nil, err
}
if fqdn.Type != resources.TypeModule {
return nil, fmt.Errorf("resource %s is not a module reference", module)
}
moduleString := fmt.Sprintf("%s.%s", fqdn.Module, fqdn.Resource)
moduleString = strings.TrimPrefix(moduleString, ".")
resources := []types.Resource{}
for _, r := range c.Resources {
match := false
if includeSubModules && strings.HasPrefix(r.Metadata().Module, moduleString) {
match = true
}
if !includeSubModules && r.Metadata().Module == moduleString {
match = true
}
if match {
resources = append(resources, r)
}
}
if len(resources) > 0 {
return resources, nil
}
return nil, ResourceNotFoundError{fqdn.Module}
}
// ResourceCount defines the number of resources in a config
func (c *Config) ResourceCount() int {
return len(c.Resources)
}
// AppendResourcesFromConfig adds the resources in the given config to
// this config. If a resources all ready exists a ResourceExistsError
// error is returned
func (c *Config) AppendResourcesFromConfig(new *Config) error {
c.sync.Lock()
defer c.sync.Unlock()
for _, r := range new.Resources {
fqdn := resources.FQRNFromResource(r).String()
// does the resource already exist?
if _, err := c.findResource(fqdn); err == nil {
return ResourceExistsError{Name: fqdn}
}
// we need to add the context and the body from the other resource
// so we can use it when parsing
c.addResource(r, new.contexts[r], new.bodies[r])
}
return nil
}
// AppendResource adds a given resource to the resource list
// if the resource already exists an error will be returned
func (c *Config) AppendResource(r types.Resource) error {
c.sync.Lock()
defer c.sync.Unlock()
return c.addResource(r, nil, nil)
}
// ToJSON converts the config to a serializable json string
// to unmarshal the output of this method back into a config you can use
// the Parser.UnmarshalJSON method
func (c *Config) ToJSON() ([]byte, error) {
buf := bytes.NewBuffer([]byte{})
enc := json.NewEncoder(buf)
enc.SetIndent("", " ")
err := enc.Encode(c)
if err != nil {
return nil, fmt.Errorf("unable to encode config: %s", err)
}
return buf.Bytes(), nil
}
// ResourceDiff is a container for resources that have changed between
// two different configurations
type ResourceDiff struct {
// Resources that have been added to the configuration
Added []types.Resource
// Resources that have been updated after the parse step, typically this is
// any change to the resource definition, but does not include changes to referenced
// resources
// It is possible that a resource is in both ParseUpdated and ProcessUpdated
ParseUpdated []types.Resource
// Resources that have been updated after the process step, typically this includes
// any changes to referenced resources
// It is possible that a resource is in both ParseUpdated and ProcessUpdated
ProcessedUpdated []types.Resource
// Resources that have been removed from the configuration
Removed []types.Resource
// Resources that have not changed
Unchanged []types.Resource
}
// Diff compares the current configuration to the provided configuration and
// returns resources that have changed between the two configurations
func (c *Config) Diff(o *Config) (*ResourceDiff, error) {
var new []types.Resource
var parseChanged []types.Resource
var processChanged []types.Resource
var removed []types.Resource
var unchanged []types.Resource
for _, r := range o.Resources {
// does the resource exist
cr, err := c.findResource(r.Metadata().ID)
// check if the resource has been found
if err != nil {
// resource does not exist
new = append(new, r)
continue
}
// check if the resource has changed
if cr.Metadata().Checksum.Parsed != r.Metadata().Checksum.Parsed {
// resource has changes rebuild
parseChanged = append(parseChanged, r)
continue
}
if cr.Metadata().Checksum.Processed != r.Metadata().Checksum.Processed {
// resource has changes rebuild
processChanged = append(processChanged, r)
continue
}
}
// check if there are resources in the state that are no longer
// in the config
for _, r := range c.Resources {
found := false
for _, r2 := range o.Resources {
if r.Metadata().ID == r2.Metadata().ID {
found = true
break
}
}
if !found {
removed = append(removed, r)
}
}
// now add any unchanged resources
for _, r := range c.Resources {
found := false
for _, r2 := range new {
if r.Metadata().ID == r2.Metadata().ID {
found = true
break
}
}
for _, r2 := range parseChanged {
if r.Metadata().ID == r2.Metadata().ID {
found = true
break
}
}
for _, r2 := range processChanged {
if r.Metadata().ID == r2.Metadata().ID {
found = true
break
}
}
for _, r2 := range removed {
if r.Metadata().ID == r2.Metadata().ID {
found = true
break
}
}
if !found {
unchanged = append(unchanged, r)
}
}
return &ResourceDiff{
Added: new,
Removed: removed,
ParseUpdated: parseChanged,
ProcessedUpdated: processChanged,
Unchanged: unchanged,
}, nil
}
func (c *Config) RemoveResource(rf types.Resource) error {
c.sync.Lock()
defer c.sync.Unlock()
pos := -1
for i, r := range c.Resources {
if rf.Metadata().Name == r.Metadata().Name &&
rf.Metadata().Type == r.Metadata().Type &&
rf.Metadata().Module == r.Metadata().Module {
pos = i
break
}
}
// found the resource remove from the collection
// preserve order
if pos > -1 {
c.Resources = append(c.Resources[:pos], c.Resources[pos+1:]...)
// clean up the context and body
delete(c.contexts, rf)
delete(c.bodies, rf)
return nil
}
return ResourceNotFoundError{}
}
// WalkCallback is called with the resource when the graph processes that particular node
type WalkCallback func(r types.Resource) error
// Walk creates a Directed Acyclic Graph for the configuration resources depending on their
// links and references. All the resources defined in the graph are traversed and
// the provided callback is executed for every resource in the graph.
//
// Any error returned from the ProcessCallback function halts execution of any other
// callback for resources in the graph.
//
// Specifying the reverse option to 'true' causes the graph to be traversed in reverse
// order.
func (c *Config) Walk(wf WalkCallback, reverse bool) error {
// We need to ensure that Process does not execute the callback when
// any other callback returns an error.
// Unfortunately returning an error with tfdiags does not stop the walk
hasError := atomic.Bool{}
pe := errors.NewConfigError()
errs := c.walk(
func(v dag.Vertex) (diags dag.Diagnostics) {
r, ok := v.(types.Resource)
// not a resource skip, this should never happen
if !ok {
panic("an item has been added to the graph that is not a resource")
}
// if this is the root module or is disabled skip
if (r.Metadata().Type == resources.TypeRoot || r.Metadata().Type == resources.TypeModule) || r.GetDisabled() {
return nil
}
// call the callback only if a previous error has not occurred
if hasError.Load() {
return nil
}
err := wf(r)
if err != nil {
// set the global error mutex to stop further processing
hasError.Store(true)
return diags.Append(err)
}
return nil
},
reverse,
)
for _, e := range errs {
pe.AppendError(e)
}
if len(pe.Errors) > 0 {
return pe
}
return nil
}
// Until parse is called the HCL configuration is not deserialized into
// the structs. We have to do this using a graph as some inputs depend on
// outputs from other resources, therefore we need to process this is strict order
func (c *Config) walk(wf dag.WalkFunc, reverse bool) []error {
// build the graph
d, err := doYaLikeDAGs(c)
if err != nil {
return []error{err}
}
// reduce the graph nodes to unique instances
d.TransitiveReduction()
// validate the dependency graph is ok
err = d.Validate()
if err != nil {
return []error{fmt.Errorf("unable to validate dependency graph: %w", err)}
}
// define the walker callback that will be called for every node in the graph
w := dag.Walker{}
w.Callback = wf
w.Reverse = reverse
// update the dag and process the nodes
log.SetOutput(io.Discard)
errs := []error{}
w.Update(d)
diags := w.Wait()
if diags.HasErrors() {
errs = append(errs, diags.Err().(errwrap.Wrapper).WrappedErrors()...)
return errs
}
return nil
}
func (c *Config) addResource(r types.Resource, ctx *hcl.EvalContext, b *hclsyntax.Body) error {
fqdn := resources.FQRNFromResource(r)
// set the ID
r.Metadata().ID = fqdn.String()
rf, err := c.findResource(fqdn.String())
if err == nil && rf != nil {
return ResourceExistsError{r.Metadata().Name}
}
c.Resources = append(c.Resources, r)
c.contexts[r] = ctx
c.bodies[r] = b
return nil
}
func (c *Config) getContext(rf types.Resource) (*hcl.EvalContext, error) {
if ctx, ok := c.contexts[rf]; ok {
return ctx, nil
}
return nil, ResourceNotFoundError{}
}
func (c *Config) getBody(rf types.Resource) (*hclsyntax.Body, error) {
if b, ok := c.bodies[rf]; ok {
return b, nil
}
return nil, ResourceNotFoundError{}
}