Skip to content

Commit 538d19c

Browse files
authored
feat: Expose method to get manifest report (#853)
* feat: Expose method to get manifest report
1 parent 5c4f666 commit 538d19c

File tree

3 files changed

+100
-81
lines changed

3 files changed

+100
-81
lines changed

diff/diff.go

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,23 @@ func Manifests(oldIndex, newIndex map[string]*manifest.MappingResult, options *O
4242
}
4343

4444
func ManifestsOwnership(oldIndex, newIndex map[string]*manifest.MappingResult, newOwnedReleases map[string]OwnershipDiff, options *Options, to io.Writer) bool {
45+
seenAnyChanges, report, err := generateReport(oldIndex, newIndex, newOwnedReleases, options)
46+
if err != nil {
47+
panic(err)
48+
}
49+
50+
report.print(to)
51+
report.clean()
52+
return seenAnyChanges
53+
}
54+
55+
func ManifestReport(oldIndex, newIndex map[string]*manifest.MappingResult, options *Options) (*Report, error) {
56+
_, report, err := generateReport(oldIndex, newIndex, nil, options)
57+
58+
return report, err
59+
}
60+
61+
func generateReport(oldIndex, newIndex map[string]*manifest.MappingResult, newOwnedReleases map[string]OwnershipDiff, options *Options) (bool, *Report, error) {
4562
report := Report{}
4663
report.setupReportFormat(options.OutputFormat)
4764
var possiblyRemoved []string
@@ -83,26 +100,21 @@ func ManifestsOwnership(oldIndex, newIndex map[string]*manifest.MappingResult, n
83100
doDiff(&report, key, nil, newContent, options)
84101
}
85102

86-
seenAnyChanges := len(report.entries) > 0
103+
seenAnyChanges := len(report.Entries) > 0
87104

88105
report, err := doSuppress(report, options.SuppressedOutputLineRegex)
89-
if err != nil {
90-
panic(err)
91-
}
92106

93-
report.print(to)
94-
report.clean()
95-
return seenAnyChanges
107+
return seenAnyChanges, &report, err
96108
}
97109

98110
func doSuppress(report Report, suppressedOutputLineRegex []string) (Report, error) {
99-
if len(report.entries) == 0 || len(suppressedOutputLineRegex) == 0 {
111+
if len(report.Entries) == 0 || len(suppressedOutputLineRegex) == 0 {
100112
return report, nil
101113
}
102114

103115
filteredReport := Report{}
104116
filteredReport.format = report.format
105-
filteredReport.entries = []ReportEntry{}
117+
filteredReport.Entries = []ReportEntry{}
106118

107119
var suppressOutputRegexes []*regexp.Regexp
108120

@@ -115,11 +127,11 @@ func doSuppress(report Report, suppressedOutputLineRegex []string) (Report, erro
115127
suppressOutputRegexes = append(suppressOutputRegexes, regex)
116128
}
117129

118-
for _, entry := range report.entries {
130+
for _, entry := range report.Entries {
119131
var diffs []difflib.DiffRecord
120132

121133
DIFFS:
122-
for _, diff := range entry.diffs {
134+
for _, diff := range entry.Diffs {
123135
for _, suppressOutputRegex := range suppressOutputRegexes {
124136
if suppressOutputRegex.MatchString(diff.Payload) {
125137
continue DIFFS
@@ -143,11 +155,11 @@ func doSuppress(report Report, suppressedOutputLineRegex []string) (Report, erro
143155
switch {
144156
case containsDiff:
145157
diffRecords = diffs
146-
case entry.changeType == "MODIFY":
147-
entry.changeType = "MODIFY_SUPPRESSED"
158+
case entry.ChangeType == "MODIFY":
159+
entry.ChangeType = "MODIFY_SUPPRESSED"
148160
}
149161

150-
filteredReport.addEntry(entry.key, entry.suppressedKinds, entry.kind, entry.context, diffRecords, entry.changeType)
162+
filteredReport.addEntry(entry.Key, entry.SuppressedKinds, entry.Kind, entry.Context, diffRecords, entry.ChangeType)
151163
}
152164

153165
return filteredReport, nil
@@ -247,9 +259,9 @@ func preHandleSecrets(old, new *manifest.MappingResult) (v1.Secret, v1.Secret, e
247259
if oldSecretDecodeErr != nil {
248260
old.Content = fmt.Sprintf("Error parsing old secret: %s", oldSecretDecodeErr)
249261
} else {
250-
//if we have a Secret containing `stringData`, apply the same
251-
//transformation that the apiserver would do with it (this protects
252-
//stringData keys from being overwritten down below)
262+
// if we have a Secret containing `stringData`, apply the same
263+
// transformation that the apiserver would do with it (this protects
264+
// stringData keys from being overwritten down below)
253265
if len(oldSecret.StringData) > 0 && oldSecret.Data == nil {
254266
oldSecret.Data = make(map[string][]byte, len(oldSecret.StringData))
255267
}
@@ -263,7 +275,7 @@ func preHandleSecrets(old, new *manifest.MappingResult) (v1.Secret, v1.Secret, e
263275
if newSecretDecodeErr != nil {
264276
new.Content = fmt.Sprintf("Error parsing new secret: %s", newSecretDecodeErr)
265277
} else {
266-
//same as above
278+
// same as above
267279
if len(newSecret.StringData) > 0 && newSecret.Data == nil {
268280
newSecret.Data = make(map[string][]byte, len(newSecret.StringData))
269281
}

diff/diff_test.go

Lines changed: 44 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,6 @@ func TestManifests(t *testing.T) {
172172

173173
specBeta := map[string]*manifest.MappingResult{
174174
"default, nginx, Deployment (apps)": {
175-
176175
Name: "default, nginx, Deployment (apps)",
177176
Kind: "Deployment",
178177
Content: `
@@ -181,11 +180,11 @@ kind: Deployment
181180
metadata:
182181
name: nginx
183182
`,
184-
}}
183+
},
184+
}
185185

186186
specRelease := map[string]*manifest.MappingResult{
187187
"default, nginx, Deployment (apps)": {
188-
189188
Name: "default, nginx, Deployment (apps)",
190189
Kind: "Deployment",
191190
Content: `
@@ -194,11 +193,11 @@ kind: Deployment
194193
metadata:
195194
name: nginx
196195
`,
197-
}}
196+
},
197+
}
198198

199199
specReleaseSpec := map[string]*manifest.MappingResult{
200200
"default, nginx, Deployment (apps)": {
201-
202201
Name: "default, nginx, Deployment (apps)",
203202
Kind: "Deployment",
204203
Content: `
@@ -209,11 +208,11 @@ metadata:
209208
spec:
210209
replicas: 3
211210
`,
212-
}}
211+
},
212+
}
213213

214214
specReleaseRenamed := map[string]*manifest.MappingResult{
215215
"default, nginx-renamed, Deployment (apps)": {
216-
217216
Name: "default, nginx-renamed, Deployment (apps)",
218217
Kind: "Deployment",
219218
Content: `
@@ -224,11 +223,11 @@ metadata:
224223
spec:
225224
replicas: 3
226225
`,
227-
}}
226+
},
227+
}
228228

229229
specReleaseRenamedAndUpdated := map[string]*manifest.MappingResult{
230230
"default, nginx-renamed, Deployment (apps)": {
231-
232231
Name: "default, nginx-renamed, Deployment (apps)",
233232
Kind: "Deployment",
234233
Content: `
@@ -239,11 +238,11 @@ metadata:
239238
spec:
240239
replicas: 1
241240
`,
242-
}}
241+
},
242+
}
243243

244244
specReleaseRenamedAndAdded := map[string]*manifest.MappingResult{
245245
"default, nginx-renamed, Deployment (apps)": {
246-
247246
Name: "default, nginx-renamed, Deployment (apps)",
248247
Kind: "Deployment",
249248
Content: `
@@ -257,11 +256,11 @@ spec:
257256
matchLabels:
258257
app: nginx-renamed
259258
`,
260-
}}
259+
},
260+
}
261261

262262
specReleaseKeep := map[string]*manifest.MappingResult{
263263
"default, nginx, Deployment (apps)": {
264-
265264
Name: "default, nginx, Deployment (apps)",
266265
Kind: "Deployment",
267266
Content: `
@@ -273,7 +272,8 @@ annotations:
273272
helm.sh/resource-policy: keep
274273
`,
275274
ResourcePolicy: "keep",
276-
}}
275+
},
276+
}
277277

278278
t.Run("OnChange", func(t *testing.T) {
279279
var buf1 bytes.Buffer
@@ -603,7 +603,8 @@ data:
603603
key2: dmFsdWUy
604604
key3: dmFsdWUz
605605
`,
606-
}}
606+
},
607+
}
607608

608609
specSecretWithByteDataChanged := map[string]*manifest.MappingResult{
609610
"default, foobar, Secret (v1)": {
@@ -620,7 +621,8 @@ data:
620621
key2: dmFsdWUy
621622
key4: dmFsdWU0
622623
`,
623-
}}
624+
},
625+
}
624626

625627
specSecretWithStringData := map[string]*manifest.MappingResult{
626628
"default, foobar, Secret (v1)": {
@@ -637,7 +639,8 @@ stringData:
637639
key2: value2
638640
key3: value3
639641
`,
640-
}}
642+
},
643+
}
641644

642645
specSecretWithStringDataChanged := map[string]*manifest.MappingResult{
643646
"default, foobar, Secret (v1)": {
@@ -654,17 +657,18 @@ stringData:
654657
key2: value2
655658
key4: value4
656659
`,
657-
}}
660+
},
661+
}
658662

659663
t.Run("OnChangeSecretWithByteData", func(t *testing.T) {
660664
var buf1 bytes.Buffer
661-
diffOptions := Options{"diff", 10, false, false, false, []string{}, 0.5, []string{}} //NOTE: ShowSecrets = false
665+
diffOptions := Options{"diff", 10, false, false, false, []string{}, 0.5, []string{}} // NOTE: ShowSecrets = false
662666

663667
if changesSeen := Manifests(specSecretWithByteData, specSecretWithByteDataChanged, &diffOptions, &buf1); !changesSeen {
664668
t.Error("Unexpected return value from Manifests: Expected the return value to be `true` to indicate that it has seen any change(s), but was `false`")
665669
}
666670

667-
//TODO: Why is there no empty line between the header and the start of the diff, like in the other diffs?
671+
// TODO: Why is there no empty line between the header and the start of the diff, like in the other diffs?
668672
require.Equal(t, `default, foobar, Secret (v1) has changed:
669673
apiVersion: v1
670674
kind: Secret
@@ -683,7 +687,7 @@ stringData:
683687

684688
t.Run("OnChangeSecretWithStringData", func(t *testing.T) {
685689
var buf1 bytes.Buffer
686-
diffOptions := Options{"diff", 10, false, false, false, []string{}, 0.5, []string{}} //NOTE: ShowSecrets = false
690+
diffOptions := Options{"diff", 10, false, false, false, []string{}, 0.5, []string{}} // NOTE: ShowSecrets = false
687691

688692
if changesSeen := Manifests(specSecretWithStringData, specSecretWithStringDataChanged, &diffOptions, &buf1); !changesSeen {
689693
t.Error("Unexpected return value from Manifests: Expected the return value to be `true` to indicate that it has seen any change(s), but was `false`")
@@ -722,53 +726,53 @@ func TestDoSuppress(t *testing.T) {
722726
{
723727
name: "simple",
724728
input: Report{
725-
entries: []ReportEntry{
729+
Entries: []ReportEntry{
726730
{
727-
diffs: diffStrings("hello: world", "hello: world2", false),
731+
Diffs: diffStrings("hello: world", "hello: world2", false),
728732
},
729733
},
730734
},
731735
supressRegex: []string{},
732736
expected: Report{
733-
entries: []ReportEntry{
737+
Entries: []ReportEntry{
734738
{
735-
diffs: diffStrings("hello: world", "hello: world2", false),
739+
Diffs: diffStrings("hello: world", "hello: world2", false),
736740
},
737741
},
738742
},
739743
},
740744
{
741745
name: "ignore all",
742746
input: Report{
743-
entries: []ReportEntry{
747+
Entries: []ReportEntry{
744748
{
745-
diffs: diffStrings("hello: world", "hello: world2", false),
749+
Diffs: diffStrings("hello: world", "hello: world2", false),
746750
},
747751
},
748752
},
749753
supressRegex: []string{".*world2?"},
750754
expected: Report{
751-
entries: []ReportEntry{
755+
Entries: []ReportEntry{
752756
{
753-
diffs: []difflib.DiffRecord{},
757+
Diffs: []difflib.DiffRecord{},
754758
},
755759
},
756760
},
757761
},
758762
{
759763
name: "ignore partial",
760764
input: Report{
761-
entries: []ReportEntry{
765+
Entries: []ReportEntry{
762766
{
763-
diffs: diffStrings("hello: world", "hello: world2", false),
767+
Diffs: diffStrings("hello: world", "hello: world2", false),
764768
},
765769
},
766770
},
767771
supressRegex: []string{".*world2"},
768772
expected: Report{
769-
entries: []ReportEntry{
773+
Entries: []ReportEntry{
770774
{
771-
diffs: []difflib.DiffRecord{
775+
Diffs: []difflib.DiffRecord{
772776
{
773777
Payload: "hello: world",
774778
Delta: difflib.LeftOnly,
@@ -803,11 +807,12 @@ metadata:
803807
data:
804808
key1: value1
805809
`,
806-
}}
810+
},
811+
}
807812

808813
t.Run("OnChangeOwnershipWithoutSpecChange", func(t *testing.T) {
809814
var buf1 bytes.Buffer
810-
diffOptions := Options{"diff", 10, false, true, false, []string{}, 0.5, []string{}} //NOTE: ShowSecrets = false
815+
diffOptions := Options{"diff", 10, false, true, false, []string{}, 0.5, []string{}} // NOTE: ShowSecrets = false
811816

812817
newOwnedReleases := map[string]OwnershipDiff{
813818
"default, foobar, ConfigMap (v1)": {
@@ -827,7 +832,7 @@ data:
827832

828833
t.Run("OnChangeOwnershipWithSpecChange", func(t *testing.T) {
829834
var buf1 bytes.Buffer
830-
diffOptions := Options{"diff", 10, false, true, false, []string{}, 0.5, []string{}} //NOTE: ShowSecrets = false
835+
diffOptions := Options{"diff", 10, false, true, false, []string{}, 0.5, []string{}} // NOTE: ShowSecrets = false
831836

832837
specNew := map[string]*manifest.MappingResult{
833838
"default, foobar, ConfigMap (v1)": {
@@ -841,7 +846,8 @@ metadata:
841846
data:
842847
key1: newValue1
843848
`,
844-
}}
849+
},
850+
}
845851

846852
newOwnedReleases := map[string]OwnershipDiff{
847853
"default, foobar, ConfigMap (v1)": {
@@ -869,6 +875,7 @@ default, foobar, ConfigMap (v1) has changed:
869875
`, buf1.String())
870876
})
871877
}
878+
872879
func TestDecodeSecrets(t *testing.T) {
873880
ansi.DisableColors(true)
874881

0 commit comments

Comments
 (0)