-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiffer.go
More file actions
218 lines (193 loc) · 6.03 KB
/
differ.go
File metadata and controls
218 lines (193 loc) · 6.03 KB
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
package platform
import (
"crypto/sha256"
"encoding/json"
"fmt"
"time"
"github.com/GoCodeAlone/workflow/interfaces"
)
// ComputePlan compares desired ResourceSpecs against current ResourceStates
// and returns a Plan with the minimal set of ordered actions needed to
// reconcile them. Creates and updates are ordered by DependsOn (dependencies
// first); deletes are ordered in reverse dependency order.
//
// Returns an error if the DependsOn graph contains a cycle.
func ComputePlan(desired []interfaces.ResourceSpec, current []interfaces.ResourceState) (interfaces.IaCPlan, error) {
// Index current state by resource name.
currentMap := make(map[string]interfaces.ResourceState, len(current))
for _, rs := range current {
currentMap[rs.Name] = rs
}
// Index desired specs by name for delete detection.
desiredMap := make(map[string]interfaces.ResourceSpec, len(desired))
for _, spec := range desired {
desiredMap[spec.Name] = spec
}
var creates, updates, deletes []interfaces.PlanAction
// Creates and updates: iterate desired in stable order.
for _, spec := range desired {
hash := configHash(spec.Config)
if rs, exists := currentMap[spec.Name]; !exists {
creates = append(creates, interfaces.PlanAction{
Action: "create",
Resource: spec,
})
} else if rs.ConfigHash != hash {
rsCopy := rs
updates = append(updates, interfaces.PlanAction{
Action: "update",
Resource: spec,
Current: &rsCopy,
})
}
// No change: skip.
}
// Deletes: resources in current that are not in desired.
for _, rs := range current {
if _, exists := desiredMap[rs.Name]; !exists {
rsCopy := rs
spec := interfaces.ResourceSpec{
Name: rs.Name,
Type: rs.Type,
DependsOn: rs.Dependencies,
}
deletes = append(deletes, interfaces.PlanAction{
Action: "delete",
Resource: spec,
Current: &rsCopy,
})
}
}
// Topological sort: creates and updates in dependency order (deps first).
sorted, err := topoSort(creates, updates, desired)
if err != nil {
return interfaces.IaCPlan{}, err
}
// Deletes in reverse dependency order (dependents deleted before deps).
sortedDeletes, err := reverseTopoSort(deletes)
if err != nil {
return interfaces.IaCPlan{}, err
}
actions := append(sorted, sortedDeletes...)
return interfaces.IaCPlan{
ID: planID(),
Actions: actions,
CreatedAt: time.Now().UTC(),
}, nil
}
// configHash returns a deterministic SHA-256 hex hash of a config map.
// json.Marshal error is intentionally ignored: map[string]any always marshals.
func configHash(config map[string]any) string {
if len(config) == 0 {
return ""
}
data, _ := json.Marshal(config) // map[string]any is always marshalable
return fmt.Sprintf("%x", sha256.Sum256(data))
}
// planID generates a simple unique plan ID based on current time.
func planID() string {
return fmt.Sprintf("plan-%d", time.Now().UnixNano())
}
// topoSort returns creates and updates ordered so that a resource's
// dependencies appear before itself. Iteration order is seeded from
// desiredSpecs to ensure deterministic output for independent resources.
// Returns an error if a dependency cycle is detected.
func topoSort(creates, updates []interfaces.PlanAction, desiredSpecs []interfaces.ResourceSpec) ([]interfaces.PlanAction, error) {
// Build a map of name → DependsOn from desired specs.
deps := make(map[string][]string, len(desiredSpecs))
for _, s := range desiredSpecs {
deps[s.Name] = s.DependsOn
}
// Collect all actions into a map by resource name.
actionMap := make(map[string]interfaces.PlanAction)
for _, a := range creates {
actionMap[a.Resource.Name] = a
}
for _, a := range updates {
actionMap[a.Resource.Name] = a
}
visited := make(map[string]bool)
inStack := make(map[string]bool) // cycle detection
var result []interfaces.PlanAction
var visit func(name string) error
visit = func(name string) error {
if inStack[name] {
return fmt.Errorf("dependency cycle detected involving resource %q", name)
}
if visited[name] {
return nil
}
inStack[name] = true
for _, dep := range deps[name] {
if err := visit(dep); err != nil {
return err
}
}
inStack[name] = false
visited[name] = true
if action, ok := actionMap[name]; ok {
result = append(result, action)
}
return nil
}
// Seed DFS from desiredSpecs to guarantee deterministic ordering.
for _, s := range desiredSpecs {
if _, ok := actionMap[s.Name]; ok {
if err := visit(s.Name); err != nil {
return nil, err
}
}
}
return result, nil
}
// reverseTopoSort returns deletes in reverse dependency order so that
// dependent resources are deleted before the resources they depend on.
// Returns an error if a dependency cycle is detected.
func reverseTopoSort(deletes []interfaces.PlanAction) ([]interfaces.PlanAction, error) {
if len(deletes) == 0 {
return nil, nil
}
// Build deps map from DependsOn on the resource spec.
deps := make(map[string][]string, len(deletes))
actionMap := make(map[string]interfaces.PlanAction, len(deletes))
for _, a := range deletes {
deps[a.Resource.Name] = a.Resource.DependsOn
actionMap[a.Resource.Name] = a
}
visited := make(map[string]bool)
inStack := make(map[string]bool) // cycle detection
var forward []interfaces.PlanAction
var visit func(name string) error
visit = func(name string) error {
if inStack[name] {
return fmt.Errorf("dependency cycle detected involving resource %q", name)
}
if visited[name] {
return nil
}
inStack[name] = true
for _, dep := range deps[name] {
if err := visit(dep); err != nil {
return err
}
}
inStack[name] = false
visited[name] = true
if action, ok := actionMap[name]; ok {
forward = append(forward, action)
}
return nil
}
// Seed DFS from the stable delete-action order.
for _, a := range deletes {
if err := visit(a.Resource.Name); err != nil {
return nil, err
}
}
// Reverse the order: deps-first → dependents-first for deletion.
result := make([]interfaces.PlanAction, len(forward))
for i, a := range forward {
result[len(forward)-1-i] = a
}
return result, nil
}