-
Notifications
You must be signed in to change notification settings - Fork 322
Expand file tree
/
Copy pathstructured.go
More file actions
386 lines (348 loc) · 8.9 KB
/
structured.go
File metadata and controls
386 lines (348 loc) · 8.9 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
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
package diff
import (
"bytes"
"encoding/json"
"fmt"
"reflect"
"sort"
"strconv"
"strings"
jsonpatch "github.com/evanphx/json-patch/v5"
"sigs.k8s.io/yaml"
"github.com/databus23/helm-diff/v3/manifest"
)
// StructuredEntry captures machine-readable diff information for a resource.
type StructuredEntry struct {
APIVersion string `json:"apiVersion,omitempty"`
Kind string `json:"kind,omitempty"`
Namespace string `json:"namespace,omitempty"`
Name string `json:"name,omitempty"`
ChangeType string `json:"changeType,omitempty"`
ResourceStatus ResourceStatus `json:"resourceStatus"`
Changes []FieldChange `json:"changes,omitempty"`
ChangesSuppressed bool `json:"changesSuppressed,omitempty"`
}
// ResourceStatus indicates whether manifests existed before or after the diff.
type ResourceStatus struct {
OldExists bool `json:"oldExists"`
NewExists bool `json:"newExists"`
}
// FieldChange stores a JSON-Pointer path and the change that occurred.
type FieldChange struct {
Path string `json:"path,omitempty"`
Field string `json:"field,omitempty"`
Change string `json:"change"`
OldValue interface{} `json:"oldValue,omitempty"`
NewValue interface{} `json:"newValue,omitempty"`
}
func buildStructuredEntry(key, changeType, kind string, suppressedKinds []string, oldContent, newContent *manifest.MappingResult) (*StructuredEntry, error) {
entry := &StructuredEntry{
ChangeType: changeType,
ResourceStatus: ResourceStatus{
OldExists: manifestExists(oldContent),
NewExists: manifestExists(newContent),
},
}
isSuppressed := containsKind(suppressedKinds, kind)
entry.ChangesSuppressed = isSuppressed
oldJSON, oldObj, err := manifestToJSON(oldContent)
if err != nil {
return nil, fmt.Errorf("convert old manifest: %w", err)
}
newJSON, newObj, err := manifestToJSON(newContent)
if err != nil {
return nil, fmt.Errorf("convert new manifest: %w", err)
}
entry.populateMetadata(key, oldObj, newObj)
if isSuppressed {
return entry, nil
}
if changeType == "MODIFY" && oldJSON != nil && newJSON != nil {
changes, err := calculateFieldChanges(oldJSON, newJSON)
if err != nil {
return nil, err
}
entry.Changes = changes
}
return entry, nil
}
func manifestExists(m *manifest.MappingResult) bool {
return m != nil && strings.TrimSpace(m.Content) != ""
}
func manifestToJSON(m *manifest.MappingResult) ([]byte, map[string]interface{}, error) {
if m == nil || strings.TrimSpace(m.Content) == "" {
return nil, nil, nil
}
jsonBytes, err := yaml.YAMLToJSON([]byte(m.Content))
if err != nil {
return nil, nil, err
}
if len(jsonBytes) == 0 || string(jsonBytes) == "null" {
return jsonBytes, nil, nil
}
var obj map[string]interface{}
if err := json.Unmarshal(jsonBytes, &obj); err != nil {
return nil, nil, err
}
return jsonBytes, obj, nil
}
func (e *StructuredEntry) populateMetadata(key string, objects ...map[string]interface{}) {
for _, obj := range objects {
if obj == nil {
continue
}
if e.APIVersion == "" {
if v, ok := obj["apiVersion"].(string); ok {
e.APIVersion = v
}
}
if e.Kind == "" {
if v, ok := obj["kind"].(string); ok {
e.Kind = v
}
}
if meta, ok := obj["metadata"].(map[string]interface{}); ok {
if e.Name == "" {
if v, ok := meta["name"].(string); ok {
e.Name = v
}
}
if e.Namespace == "" {
if v, ok := meta["namespace"].(string); ok {
e.Namespace = v
}
}
}
}
if e.Kind == "" || e.Name == "" || e.Namespace == "" || e.APIVersion == "" {
templateData := ReportTemplateSpec{}
if err := templateData.loadFromKey(key); err == nil {
if e.Kind == "" {
e.Kind = templateData.Kind
}
if e.Name == "" {
e.Name = templateData.Name
}
if e.Namespace == "" {
e.Namespace = templateData.Namespace
}
if e.APIVersion == "" {
e.APIVersion = templateData.API
}
}
}
}
func calculateFieldChanges(oldJSON, newJSON []byte) ([]FieldChange, error) {
patchBytes, err := jsonpatch.CreateMergePatch(oldJSON, newJSON)
if err != nil {
return nil, err
}
trimmed := bytes.TrimSpace(patchBytes)
if len(trimmed) == 0 || bytes.Equal(trimmed, []byte("{}")) {
return nil, nil
}
var patch interface{}
if err := json.Unmarshal(patchBytes, &patch); err != nil {
return nil, err
}
var oldDoc interface{}
if len(oldJSON) > 0 {
if err := json.Unmarshal(oldJSON, &oldDoc); err != nil {
return nil, err
}
}
var newDoc interface{}
if len(newJSON) > 0 {
if err := json.Unmarshal(newJSON, &newDoc); err != nil {
return nil, err
}
}
var changes []FieldChange
if err := walkPatch(&changes, nil, patch, oldDoc, newDoc); err != nil {
return nil, err
}
return changes, nil
}
func walkPatch(changes *[]FieldChange, tokens []string, patchNode, oldNode, newNode interface{}) error {
switch typed := patchNode.(type) {
case map[string]interface{}:
if len(typed) == 0 {
return nil
}
oldMap, _ := oldNode.(map[string]interface{})
newMap, _ := newNode.(map[string]interface{})
keys := make([]string, 0, len(typed))
for key := range typed {
keys = append(keys, key)
}
sort.Strings(keys)
for _, key := range keys {
var oldChild interface{}
var newChild interface{}
if oldMap != nil {
oldChild = oldMap[key]
}
if newMap != nil {
newChild = newMap[key]
}
if err := walkPatch(changes, append(tokens, key), typed[key], oldChild, newChild); err != nil {
return err
}
}
case []interface{}:
return diffArrayNodes(changes, tokens, oldNode, newNode)
default:
path, field := splitTokens(tokens)
change := FieldChange{
Path: path,
Field: field,
}
if patchNode == nil {
change.Change = "remove"
change.OldValue = oldNode
} else {
if oldNode == nil {
change.Change = "add"
change.NewValue = newNode
} else {
if reflect.DeepEqual(oldNode, newNode) {
return nil
}
change.Change = "replace"
change.OldValue = oldNode
change.NewValue = newNode
}
}
*changes = append(*changes, change)
}
return nil
}
func diffArrayNodes(changes *[]FieldChange, tokens []string, oldNode, newNode interface{}) error {
oldArr, _ := oldNode.([]interface{})
newArr, _ := newNode.([]interface{})
maxLen := len(oldArr)
if len(newArr) > maxLen {
maxLen = len(newArr)
}
for i := 0; i < maxLen; i++ {
next := append(tokens, strconv.Itoa(i))
var oldVal interface{}
var newVal interface{}
if i < len(oldArr) {
oldVal = oldArr[i]
}
if i < len(newArr) {
newVal = newArr[i]
}
switch {
case oldVal != nil && newVal != nil:
if reflect.DeepEqual(oldVal, newVal) {
continue
}
subPatch, err := createNodePatch(oldVal, newVal)
if err != nil {
return err
}
if subPatch == nil {
path, field := splitTokens(next)
*changes = append(*changes, FieldChange{
Path: path,
Field: field,
Change: "replace",
OldValue: oldVal,
NewValue: newVal,
})
continue
}
if err := walkPatch(changes, next, subPatch, oldVal, newVal); err != nil {
return err
}
case oldVal != nil:
path, field := splitTokens(next)
*changes = append(*changes, FieldChange{
Path: path,
Field: field,
Change: "remove",
OldValue: oldVal,
})
case newVal != nil:
path, field := splitTokens(next)
*changes = append(*changes, FieldChange{
Path: path,
Field: field,
Change: "add",
NewValue: newVal,
})
}
}
return nil
}
func createNodePatch(oldNode, newNode interface{}) (interface{}, error) {
oldJSON, err := json.Marshal(oldNode)
if err != nil {
return nil, err
}
newJSON, err := json.Marshal(newNode)
if err != nil {
return nil, err
}
patchBytes, err := jsonpatch.CreateMergePatch(oldJSON, newJSON)
if err != nil {
return nil, err
}
trimmed := bytes.TrimSpace(patchBytes)
if len(trimmed) == 0 || bytes.Equal(trimmed, []byte("{}")) {
return nil, nil
}
var patch interface{}
if err := json.Unmarshal(patchBytes, &patch); err != nil {
return nil, err
}
return patch, nil
}
func splitTokens(tokens []string) (string, string) {
if len(tokens) == 0 {
return "", ""
}
return formatPath(tokens[:len(tokens)-1]), tokens[len(tokens)-1]
}
func containsKind(list []string, target string) bool {
for _, item := range list {
if item == target {
return true
}
}
return false
}
func formatPath(tokens []string) string {
if len(tokens) == 0 {
return ""
}
segments := []string{}
for _, token := range tokens {
if token == "" {
continue
}
if isArrayIndex(token) {
if len(segments) == 0 {
segments = append(segments, "["+token+"]")
} else {
segments[len(segments)-1] = segments[len(segments)-1] + "[" + token + "]"
}
continue
}
segments = append(segments, token)
}
return strings.Join(segments, ".")
}
func isArrayIndex(token string) bool {
if token == "" {
return false
}
for _, r := range token {
if r < '0' || r > '9' {
return false
}
}
return true
}