forked from databus23/helm-diff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiff.go
466 lines (400 loc) · 13.1 KB
/
diff.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
package diff
import (
"bytes"
"fmt"
"io"
"math"
"regexp"
"sort"
"strings"
"github.com/aryann/difflib"
"github.com/mgutz/ansi"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime/serializer/json"
"k8s.io/apimachinery/pkg/util/yaml"
"k8s.io/client-go/kubernetes/scheme"
"github.com/databus23/helm-diff/v3/manifest"
)
// Options are all the options to be passed to generate a diff
type Options struct {
OutputFormat string
OutputContext int
StripTrailingCR bool
ShowSecrets bool
SuppressedKinds []string
FindRenames float32
SuppressedOutputLineRegex []string
}
type OwnershipDiff struct {
OldRelease string
NewRelease string
}
// Manifests diff on manifests
func Manifests(oldIndex, newIndex map[string]*manifest.MappingResult, options *Options, to io.Writer) bool {
return ManifestsOwnership(oldIndex, newIndex, nil, options, to)
}
func ManifestsOwnership(oldIndex, newIndex map[string]*manifest.MappingResult, newOwnedReleases map[string]OwnershipDiff, options *Options, to io.Writer) bool {
report := Report{}
report.setupReportFormat(options.OutputFormat)
var possiblyRemoved []string
for name, diff := range newOwnedReleases {
diff := diffStrings(diff.OldRelease, diff.NewRelease, true)
report.addEntry(name, options.SuppressedKinds, "", 0, diff, "OWNERSHIP")
}
for _, key := range sortedKeys(oldIndex) {
oldContent := oldIndex[key]
if newContent, ok := newIndex[key]; ok {
// modified?
doDiff(&report, key, oldContent, newContent, options)
} else {
possiblyRemoved = append(possiblyRemoved, key)
}
}
var possiblyAdded []string
for _, key := range sortedKeys(newIndex) {
if _, ok := oldIndex[key]; !ok {
possiblyAdded = append(possiblyAdded, key)
}
}
removed, added := contentSearch(&report, possiblyRemoved, oldIndex, possiblyAdded, newIndex, options)
for _, key := range removed {
oldContent := oldIndex[key]
if oldContent.ResourcePolicy != "keep" {
doDiff(&report, key, oldContent, nil, options)
}
}
for _, key := range added {
newContent := newIndex[key]
doDiff(&report, key, nil, newContent, options)
}
seenAnyChanges := len(report.entries) > 0
report, err := doSuppress(report, options.SuppressedOutputLineRegex)
if err != nil {
panic(err)
}
report.print(to)
report.clean()
return seenAnyChanges
}
func doSuppress(report Report, suppressedOutputLineRegex []string) (Report, error) {
if len(report.entries) == 0 || len(suppressedOutputLineRegex) == 0 {
return report, nil
}
filteredReport := Report{}
filteredReport.format = report.format
filteredReport.entries = []ReportEntry{}
var suppressOutputRegexes []*regexp.Regexp
for _, suppressOutputRegex := range suppressedOutputLineRegex {
regex, err := regexp.Compile(suppressOutputRegex)
if err != nil {
return Report{}, err
}
suppressOutputRegexes = append(suppressOutputRegexes, regex)
}
for _, entry := range report.entries {
var diffs []difflib.DiffRecord
DIFFS:
for _, diff := range entry.diffs {
for _, suppressOutputRegex := range suppressOutputRegexes {
if suppressOutputRegex.MatchString(diff.Payload) {
continue DIFFS
}
}
diffs = append(diffs, diff)
}
containsDiff := false
// Add entry to the report, if diffs are present.
for _, diff := range diffs {
if diff.Delta.String() != " " {
containsDiff = true
break
}
}
if containsDiff {
filteredReport.addEntry(entry.key, entry.suppressedKinds, entry.kind, entry.context, diffs, entry.changeType)
} else {
filteredReport.addEntry(entry.key, entry.suppressedKinds, entry.kind, entry.context, []difflib.DiffRecord{}, entry.changeType)
}
}
return filteredReport, nil
}
func actualChanges(diff []difflib.DiffRecord) int {
changes := 0
for _, record := range diff {
if record.Delta != difflib.Common {
changes++
}
}
return changes
}
func contentSearch(report *Report, possiblyRemoved []string, oldIndex map[string]*manifest.MappingResult, possiblyAdded []string, newIndex map[string]*manifest.MappingResult, options *Options) ([]string, []string) {
if options.FindRenames <= 0 {
return possiblyRemoved, possiblyAdded
}
var removed []string
for _, removedKey := range possiblyRemoved {
oldContent := oldIndex[removedKey]
var smallestKey string
var smallestFraction float32 = math.MaxFloat32
for _, addedKey := range possiblyAdded {
newContent := newIndex[addedKey]
if oldContent.Kind != newContent.Kind {
continue
}
if !options.ShowSecrets {
redactSecrets(oldContent, newContent)
}
diff := diffMappingResults(oldContent, newContent, options.StripTrailingCR)
delta := actualChanges(diff)
if delta == 0 || len(diff) == 0 {
continue // Should never happen, but better safe than sorry
}
fraction := float32(delta) / float32(len(diff))
if fraction > 0 && fraction < smallestFraction {
smallestKey = addedKey
smallestFraction = fraction
}
}
if smallestFraction < options.FindRenames {
index := sort.SearchStrings(possiblyAdded, smallestKey)
possiblyAdded = append(possiblyAdded[:index], possiblyAdded[index+1:]...)
newContent := newIndex[smallestKey]
doDiff(report, removedKey, oldContent, newContent, options)
} else {
removed = append(removed, removedKey)
}
}
return removed, possiblyAdded
}
func doDiff(report *Report, key string, oldContent *manifest.MappingResult, newContent *manifest.MappingResult, options *Options) {
if oldContent != nil && newContent != nil && oldContent.Content == newContent.Content {
return
}
if !options.ShowSecrets {
redactSecrets(oldContent, newContent)
}
if oldContent == nil {
emptyMapping := &manifest.MappingResult{}
diffs := diffMappingResults(emptyMapping, newContent, options.StripTrailingCR)
report.addEntry(key, options.SuppressedKinds, newContent.Kind, options.OutputContext, diffs, "ADD")
} else if newContent == nil {
emptyMapping := &manifest.MappingResult{}
diffs := diffMappingResults(oldContent, emptyMapping, options.StripTrailingCR)
report.addEntry(key, options.SuppressedKinds, oldContent.Kind, options.OutputContext, diffs, "REMOVE")
} else {
diffs := diffMappingResults(oldContent, newContent, options.StripTrailingCR)
if actualChanges(diffs) > 0 {
report.addEntry(key, options.SuppressedKinds, oldContent.Kind, options.OutputContext, diffs, "MODIFY")
}
}
}
func redactSecrets(old, new *manifest.MappingResult) {
if (old != nil && old.Kind != "Secret") || (new != nil && new.Kind != "Secret") {
return
}
serializer := json.NewYAMLSerializer(json.DefaultMetaFactory, scheme.Scheme,
scheme.Scheme)
var oldSecret, newSecret v1.Secret
if old != nil {
if err := yaml.NewYAMLToJSONDecoder(bytes.NewBufferString(old.Content)).Decode(&oldSecret); err != nil {
old.Content = fmt.Sprintf("Error parsing old secret: %s", err)
}
//if we have a Secret containing `stringData`, apply the same
//transformation that the apiserver would do with it (this protects
//stringData keys from being overwritten down below)
if len(oldSecret.StringData) > 0 && oldSecret.Data == nil {
oldSecret.Data = make(map[string][]byte, len(oldSecret.StringData))
}
for k, v := range oldSecret.StringData {
oldSecret.Data[k] = []byte(v)
}
}
if new != nil {
if err := yaml.NewYAMLToJSONDecoder(bytes.NewBufferString(new.Content)).Decode(&newSecret); err != nil {
new.Content = fmt.Sprintf("Error parsing new secret: %s", err)
}
//same as above
if len(newSecret.StringData) > 0 && newSecret.Data == nil {
newSecret.Data = make(map[string][]byte, len(newSecret.StringData))
}
for k, v := range newSecret.StringData {
newSecret.Data[k] = []byte(v)
}
}
if old != nil {
oldSecret.StringData = make(map[string]string, len(oldSecret.Data))
for k, v := range oldSecret.Data {
if new != nil && bytes.Equal(v, newSecret.Data[k]) {
oldSecret.StringData[k] = fmt.Sprintf("REDACTED # (%d bytes)", len(v))
} else {
oldSecret.StringData[k] = fmt.Sprintf("-------- # (%d bytes)", len(v))
}
}
}
if new != nil {
newSecret.StringData = make(map[string]string, len(newSecret.Data))
for k, v := range newSecret.Data {
if old != nil && bytes.Equal(v, oldSecret.Data[k]) {
newSecret.StringData[k] = fmt.Sprintf("REDACTED # (%d bytes)", len(v))
} else {
newSecret.StringData[k] = fmt.Sprintf("++++++++ # (%d bytes)", len(v))
}
}
}
// remove Data field now that we are using StringData for serialization
var buf bytes.Buffer
if old != nil {
oldSecret.Data = nil
if err := serializer.Encode(&oldSecret, &buf); err != nil {
new.Content = fmt.Sprintf("Error encoding new secret: %s", err)
}
old.Content = getComment(old.Content) + strings.Replace(strings.Replace(buf.String(), "stringData", "data", 1), " creationTimestamp: null\n", "", 1)
buf.Reset() //reuse buffer for new secret
}
if new != nil {
newSecret.Data = nil
if err := serializer.Encode(&newSecret, &buf); err != nil {
new.Content = fmt.Sprintf("Error encoding new secret: %s", err)
}
new.Content = getComment(new.Content) + strings.Replace(strings.Replace(buf.String(), "stringData", "data", 1), " creationTimestamp: null\n", "", 1)
}
}
// return the first line of a string if its a comment.
// This gives as the # Source: lines from the rendering
func getComment(s string) string {
i := strings.Index(s, "\n")
if i < 0 || !strings.HasPrefix(s, "#") {
return ""
}
return s[:i+1]
}
// Releases reindex the content based on the template names and pass it to Manifests
func Releases(oldIndex, newIndex map[string]*manifest.MappingResult, options *Options, to io.Writer) bool {
oldIndex = reIndexForRelease(oldIndex)
newIndex = reIndexForRelease(newIndex)
return Manifests(oldIndex, newIndex, options, to)
}
func diffMappingResults(oldContent *manifest.MappingResult, newContent *manifest.MappingResult, stripTrailingCR bool) []difflib.DiffRecord {
return diffStrings(oldContent.Content, newContent.Content, stripTrailingCR)
}
func diffStrings(before, after string, stripTrailingCR bool) []difflib.DiffRecord {
return difflib.Diff(split(before, stripTrailingCR), split(after, stripTrailingCR))
}
func split(value string, stripTrailingCR bool) []string {
const sep = "\n"
split := strings.Split(value, sep)
if !stripTrailingCR {
return split
}
var stripped []string
for _, s := range split {
stripped = append(stripped, strings.TrimSuffix(s, "\r"))
}
return stripped
}
func printDiffRecords(suppressedKinds []string, kind string, context int, diffs []difflib.DiffRecord, to io.Writer) {
for _, ckind := range suppressedKinds {
if ckind == kind {
str := fmt.Sprintf("+ Changes suppressed on sensitive content of type %s\n", kind)
_, _ = fmt.Fprint(to, ansi.Color(str, "yellow"))
return
}
}
if context >= 0 {
distances := calculateDistances(diffs)
omitting := false
for i, diff := range diffs {
if distances[i] > context {
if !omitting {
_, _ = fmt.Fprintln(to, "...")
omitting = true
}
} else {
omitting = false
printDiffRecord(diff, to)
}
}
} else {
for _, diff := range diffs {
printDiffRecord(diff, to)
}
}
}
func printDiffRecord(diff difflib.DiffRecord, to io.Writer) {
text := diff.Payload
switch diff.Delta {
case difflib.RightOnly:
_, _ = fmt.Fprintf(to, "%s\n", ansi.Color("+ "+text, "green"))
case difflib.LeftOnly:
_, _ = fmt.Fprintf(to, "%s\n", ansi.Color("- "+text, "red"))
case difflib.Common:
if text == "" {
_, _ = fmt.Fprintln(to)
} else {
_, _ = fmt.Fprintf(to, "%s\n", " "+text)
}
}
}
// Calculate distance of every diff-line to the closest change
func calculateDistances(diffs []difflib.DiffRecord) map[int]int {
distances := map[int]int{}
// Iterate forwards through diffs, set 'distance' based on closest 'change' before this line
change := -1
for i, diff := range diffs {
if diff.Delta != difflib.Common {
change = i
}
distance := math.MaxInt32
if change != -1 {
distance = i - change
}
distances[i] = distance
}
// Iterate backwards through diffs, reduce 'distance' based on closest 'change' after this line
change = -1
for i := len(diffs) - 1; i >= 0; i-- {
diff := diffs[i]
if diff.Delta != difflib.Common {
change = i
}
if change != -1 {
distance := change - i
if distance < distances[i] {
distances[i] = distance
}
}
}
return distances
}
// reIndexForRelease based on template names
func reIndexForRelease(index map[string]*manifest.MappingResult) map[string]*manifest.MappingResult {
// sort the index to iterate map in the same order
var keys []string
for key := range index {
keys = append(keys, key)
}
sort.Strings(keys)
// holds number of object in a single file
count := make(map[string]int)
newIndex := make(map[string]*manifest.MappingResult)
for key := range keys {
str := strings.Replace(strings.Split(index[keys[key]].Content, "\n")[0], "# Source: ", "", 1)
if _, ok := newIndex[str]; ok {
count[str]++
str += fmt.Sprintf(" %d", count[str])
newIndex[str] = index[keys[key]]
} else {
newIndex[str] = index[keys[key]]
count[str]++
}
}
return newIndex
}
func sortedKeys(manifests map[string]*manifest.MappingResult) []string {
var keys []string
for key := range manifests {
keys = append(keys, key)
}
sort.Strings(keys)
return keys
}