-
Notifications
You must be signed in to change notification settings - Fork 78
/
preview.go
138 lines (115 loc) · 3.51 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
134
135
136
137
138
package operation
import (
"errors"
"fmt"
"sync"
opsmodels "kusionstack.io/kusion/pkg/engine/operation/models"
"kusionstack.io/kusion/pkg/engine/operation/graph"
"kusionstack.io/kusion/pkg/engine/operation/types"
"kusionstack.io/kusion/pkg/engine/models"
"github.com/hashicorp/terraform/dag"
"github.com/hashicorp/terraform/tfdiags"
"kusionstack.io/kusion/pkg/engine/states"
"kusionstack.io/kusion/pkg/log"
"kusionstack.io/kusion/pkg/status"
)
type PreviewOperation struct {
opsmodels.Operation
}
type PreviewRequest struct {
opsmodels.Request `json:",inline" yaml:",inline"`
}
type PreviewResponse struct {
Order *opsmodels.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 status.Status) {
o := po.Operation
defer func() {
if e := recover(); e != nil {
log.Error("preview panic:%v", e)
switch x := e.(type) {
case string:
s = status.NewErrorStatus(fmt.Errorf("preview panic:%s", e))
case error:
s = status.NewErrorStatus(x)
default:
s = status.NewErrorStatus(errors.New("unknown panic"))
}
}
}()
if s := validateRequest(&request.Request); status.IsErr(s) {
return nil, s
}
var (
priorState, resultState *states.State
priorStateResourceIndex map[string]*models.Resource
ag *dag.AcyclicGraph
)
// 1. init & build Indexes
priorState, resultState = po.InitStates(&request.Request)
switch o.OperationType {
case types.ApplyPreview:
priorStateResourceIndex = priorState.Resources.Index()
ag, s = NewApplyGraph(request.Spec, priorState)
case types.DestroyPreview:
resources := request.Request.Spec.Resources
priorStateResourceIndex = resources.Index()
ag, s = NewDestroyGraph(resources)
}
if status.IsErr(s) {
return nil, s
}
// 2. walk DAG and preview resources
log.Info("walking DAG and preview resources ...")
previewOperation := &PreviewOperation{
Operation: opsmodels.Operation{
OperationType: o.OperationType,
StateStorage: o.StateStorage,
CtxResourceIndex: map[string]*models.Resource{},
PriorStateResourceIndex: priorStateResourceIndex,
StateResourceIndex: priorStateResourceIndex,
ChangeOrder: o.ChangeOrder,
Runtime: o.Runtime, // preview need get the latest spec from runtime
ResultState: resultState,
Lock: &sync.Mutex{},
},
}
w := &dag.Walker{Callback: previewOperation.previewWalkFun}
w.Update(ag)
// Wait
if diags := w.Wait(); diags.HasErrors() {
return nil, status.NewErrorStatus(diags.Err())
}
return &PreviewResponse{Order: previewOperation.ChangeOrder}, nil
}
func (po *PreviewOperation) previewWalkFun(v dag.Vertex) (diags tfdiags.Diagnostics) {
var s status.Status
if v == nil {
return nil
}
defer func() {
if e := recover(); e != nil {
log.Errorf("previewWalkFun panic:%v", e)
var err error
switch x := e.(type) {
case string:
err = fmt.Errorf("previewWalkFun panic:%s", e)
case error:
err = x
default:
err = errors.New("unknown panic")
}
s = status.NewErrorStatus(err)
}
}()
if node, ok := v.(graph.ExecutableNode); ok {
s = node.Execute(&po.Operation)
if status.IsErr(s) {
diags = diags.Append(fmt.Errorf("node execute failed.\n%v", s))
return diags
}
}
return nil
}