-
Notifications
You must be signed in to change notification settings - Fork 72
/
preview.go
133 lines (114 loc) · 3.63 KB
/
preview.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
package operation
import (
"errors"
"fmt"
"sync"
apiv1 "kusionstack.io/kusion/pkg/apis/core/v1"
v1 "kusionstack.io/kusion/pkg/apis/status/v1"
"kusionstack.io/kusion/pkg/engine/operation/graph"
models "kusionstack.io/kusion/pkg/engine/operation/models"
runtimeinit "kusionstack.io/kusion/pkg/engine/runtime/init"
"kusionstack.io/kusion/pkg/log"
"kusionstack.io/kusion/third_party/terraform/dag"
"kusionstack.io/kusion/third_party/terraform/tfdiags"
)
type PreviewOperation struct {
models.Operation
}
type PreviewRequest struct {
models.Request `json:",inline" yaml:",inline"`
}
type PreviewResponse struct {
Order *models.ChangeOrder
}
// Preview compute all changes between resources in request and the actual infrastructure.
// The whole process is similar to the operation Apply, but the execution of each node is mocked and will not actually invoke the Runtime
func (po *PreviewOperation) Preview(request *PreviewRequest) (rsp *PreviewResponse, s v1.Status) {
o := po.Operation
defer func() {
if e := recover(); e != nil {
log.Error("preview panic:%v", e)
switch x := e.(type) {
case string:
s = v1.NewErrorStatus(fmt.Errorf("preview panic:%s", e))
case error:
s = v1.NewErrorStatus(x)
default:
s = v1.NewErrorStatus(errors.New("unknown panic"))
}
}
}()
if s := validateRequest(&request.Request); v1.IsErr(s) {
return nil, s
}
var (
priorState, resultState *apiv1.State
priorStateResourceIndex map[string]*apiv1.Resource
ag *dag.AcyclicGraph
)
// 1. init & build Indexes
priorState, resultState = po.InitStates(&request.Request)
// Kusion is a multi-runtime system. We initialize runtimes dynamically by resource types
resources := request.Intent.Resources
resources = append(resources, priorState.Resources...)
runtimesMap, s := runtimeinit.Runtimes(resources)
if v1.IsErr(s) {
return nil, s
}
o.RuntimeMap = runtimesMap
switch o.OperationType {
case models.ApplyPreview:
priorStateResourceIndex = priorState.Resources.Index()
ag, s = NewApplyGraph(request.Intent, priorState)
case models.DestroyPreview:
resources := request.Request.Intent.Resources
priorStateResourceIndex = resources.Index()
ag, s = NewDestroyGraph(resources)
}
if v1.IsErr(s) {
return nil, s
}
// copy priorStateResourceIndex into a new map
stateResourceIndex := map[string]*apiv1.Resource{}
for k, v := range priorStateResourceIndex {
stateResourceIndex[k] = v
}
// 2. walk DAG and preview resources
log.Info("walking DAG and preview resources ...")
previewOperation := &PreviewOperation{
Operation: models.Operation{
OperationType: o.OperationType,
StateStorage: o.StateStorage,
CtxResourceIndex: map[string]*apiv1.Resource{},
PriorStateResourceIndex: priorStateResourceIndex,
StateResourceIndex: stateResourceIndex,
IgnoreFields: o.IgnoreFields,
ChangeOrder: o.ChangeOrder,
RuntimeMap: o.RuntimeMap,
Stack: o.Stack,
ResultState: resultState,
Lock: &sync.Mutex{},
},
}
w := &dag.Walker{Callback: previewOperation.previewWalkFun}
w.Update(ag)
// Wait
if diags := w.Wait(); diags.HasErrors() {
return nil, v1.NewErrorStatus(diags.Err())
}
return &PreviewResponse{Order: previewOperation.ChangeOrder}, nil
}
func (po *PreviewOperation) previewWalkFun(v dag.Vertex) (diags tfdiags.Diagnostics) {
var s v1.Status
if v == nil {
return nil
}
if node, ok := v.(graph.ExecutableNode); ok {
s = node.Execute(&po.Operation)
if v1.IsErr(s) {
diags = diags.Append(fmt.Errorf("preview failed.\n%v", s))
return diags
}
}
return nil
}