Skip to content
This repository was archived by the owner on Mar 27, 2024. It is now read-only.

Commit bbb768c

Browse files
committed
Modify image and layer size analyzer
1 parent 4ab93c8 commit bbb768c

13 files changed

+207
-47
lines changed

cmd/root.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ func getImageForName(imageName string) (pkgutil.Image, error) {
208208
}
209209
layers = append(layers, pkgutil.Layer{
210210
FSPath: path,
211+
Digest: digest,
211212
})
212213
elapsed := time.Now().Sub(layerStart)
213214
logrus.Infof("time elapsed retrieving layer: %fs", elapsed.Seconds())
@@ -216,7 +217,11 @@ func getImageForName(imageName string) (pkgutil.Image, error) {
216217
logrus.Infof("time elapsed retrieving image layers: %fs", elapsed.Seconds())
217218
}
218219

219-
path, err := getExtractPathForImage(imageName, img)
220+
imageDigest, err := getImageDigest(img)
221+
if err != nil {
222+
return pkgutil.Image{}, err
223+
}
224+
path, err := getExtractPathForName(pkgutil.RemoveTag(imageName) + "@" + imageDigest.String())
220225
if err != nil {
221226
return pkgutil.Image{}, err
222227
}
@@ -231,19 +236,20 @@ func getImageForName(imageName string) (pkgutil.Image, error) {
231236
Image: img,
232237
Source: imageName,
233238
FSPath: path,
239+
Digest: imageDigest,
234240
Layers: layers,
235241
}, nil
236242
}
237243

238-
func getExtractPathForImage(imageName string, image v1.Image) (string, error) {
244+
func getImageDigest(image v1.Image) (digest v1.Hash, err error) {
239245
start := time.Now()
240-
digest, err := image.Digest()
246+
digest, err = image.Digest()
241247
if err != nil {
242-
return "", err
248+
return digest, err
243249
}
244250
elapsed := time.Now().Sub(start)
245251
logrus.Infof("time elapsed retrieving image digest: %fs", elapsed.Seconds())
246-
return getExtractPathForName(pkgutil.RemoveTag(imageName) + "@" + digest.String())
252+
return digest, nil
247253
}
248254

249255
func getExtractPathForName(name string) (string, error) {

differs/size_diff.go

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,15 @@ func (a SizeAnalyzer) Name() string {
3232

3333
// SizeDiff diffs two images and compares their size
3434
func (a SizeAnalyzer) Diff(image1, image2 pkgutil.Image) (util.Result, error) {
35-
diff := []util.EntryDiff{
36-
{
37-
Name: "*",
38-
Size1: pkgutil.GetSize(image1.FSPath),
39-
Size2: pkgutil.GetSize(image2.FSPath),
40-
},
35+
diff := []util.SizeDiff{}
36+
size1 := pkgutil.GetSize(image1.FSPath)
37+
size2 := pkgutil.GetSize(image2.FSPath)
38+
39+
if size1 != size2 {
40+
diff = append(diff, util.SizeDiff{
41+
Size1: size1,
42+
Size2: size2,
43+
})
4144
}
4245

4346
return &util.SizeDiffResult{
@@ -49,10 +52,11 @@ func (a SizeAnalyzer) Diff(image1, image2 pkgutil.Image) (util.Result, error) {
4952
}
5053

5154
func (a SizeAnalyzer) Analyze(image pkgutil.Image) (util.Result, error) {
52-
entries := []pkgutil.DirectoryEntry{
55+
entries := []util.SizeEntry{
5356
{
54-
Name: "*",
55-
Size: pkgutil.GetSize(image.FSPath),
57+
Name: image.Source,
58+
Digest: image.Digest,
59+
Size: pkgutil.GetSize(image.FSPath),
5660
},
5761
}
5862

@@ -72,7 +76,7 @@ func (a SizeLayerAnalyzer) Name() string {
7276

7377
// SizeLayerDiff diffs the layers of two images and compares their size
7478
func (a SizeLayerAnalyzer) Diff(image1, image2 pkgutil.Image) (util.Result, error) {
75-
var layerDiffs []util.EntryDiff
79+
var layerDiffs []util.SizeDiff
7680

7781
maxLayer := len(image1.Layers)
7882
if len(image2.Layers) > maxLayer {
@@ -88,15 +92,17 @@ func (a SizeLayerAnalyzer) Diff(image1, image2 pkgutil.Image) (util.Result, erro
8892
size2 = pkgutil.GetSize(image2.Layers[index].FSPath)
8993
}
9094

91-
diff := util.EntryDiff{
92-
Name: strconv.Itoa(index),
93-
Size1: size1,
94-
Size2: size2,
95+
if size1 != size2 {
96+
diff := util.SizeDiff{
97+
Name: strconv.Itoa(index),
98+
Size1: size1,
99+
Size2: size2,
100+
}
101+
layerDiffs = append(layerDiffs, diff)
95102
}
96-
layerDiffs = append(layerDiffs, diff)
97103
}
98104

99-
return &util.SizeDiffResult{
105+
return &util.SizeLayerDiffResult{
100106
Image1: image1.Source,
101107
Image2: image2.Source,
102108
DiffType: "SizeLayer",
@@ -105,16 +111,17 @@ func (a SizeLayerAnalyzer) Diff(image1, image2 pkgutil.Image) (util.Result, erro
105111
}
106112

107113
func (a SizeLayerAnalyzer) Analyze(image pkgutil.Image) (util.Result, error) {
108-
var entries []pkgutil.DirectoryEntry
114+
var entries []util.SizeEntry
109115
for index, layer := range image.Layers {
110-
entry := pkgutil.DirectoryEntry{
111-
Name: strconv.Itoa(index),
112-
Size: pkgutil.GetSize(layer.FSPath),
116+
entry := util.SizeEntry{
117+
Name: strconv.Itoa(index),
118+
Digest: layer.Digest,
119+
Size: pkgutil.GetSize(layer.FSPath),
113120
}
114121
entries = append(entries, entry)
115122
}
116123

117-
return &util.SizeAnalyzeResult{
124+
return &util.SizeLayerAnalyzeResult{
118125
Image: image.Source,
119126
AnalyzeType: "SizeLayer",
120127
Analysis: entries,

pkg/util/image_utils.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,14 @@ const tagRegexStr = ".*:([^/]+$)"
3737

3838
type Layer struct {
3939
FSPath string
40+
Digest v1.Hash
4041
}
4142

4243
type Image struct {
4344
Image v1.Image
4445
Source string
4546
FSPath string
47+
Digest v1.Hash
4648
Layers []Layer
4749
}
4850

tests/size_analysis_expected.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"AnalyzeType": "Size",
55
"Analysis": [
66
{
7-
"Name": "*",
7+
"Name": "gcr.io/gcp-runtimes/diff-base",
8+
"Digest": "sha256:d4e51212be9fffca8d3573a35ac12a69d050c3f65f706e053d24f41317762cca",
89
"Size": 383043168
910
}
1011
]

tests/size_diff_expected.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"DiffType": "Size",
66
"Diff": [
77
{
8-
"Name": "*",
8+
"Name": "",
99
"Size1": 19,
1010
"Size2": 15
1111
}

tests/size_layer_analysis_expected.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@
55
"Analysis": [
66
{
77
"Name": "0",
8+
"Digest": "sha256:74a9fa414ff7212ca555a1024826b94e27ec6a436fd971f8e6c2a909b23630a2",
89
"Size": 6
910
},
1011
{
1112
"Name": "1",
13+
"Digest": "sha256:e39d9b94d5c118b34cce2f4af7efd4c7ed2553e697d7f676e24c4091e42a4998",
1214
"Size": 7
1315
},
1416
{
1517
"Name": "2",
18+
"Digest": "sha256:bda36409f35ed9e71049d22abdce890d6335056ca15bfed9f323be69b0607e24",
1619
"Size": 6
1720
}
1821
]

tests/size_layer_diff_expected.json

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@
44
"Image2": "gcr.io/gcp-runtimes/diff-layer-modified",
55
"DiffType": "SizeLayer",
66
"Diff": [
7-
{
8-
"Name": "0",
9-
"Size1": 6,
10-
"Size2": 6
11-
},
127
{
138
"Name": "1",
149
"Size1": 7,

util/analyze_output_utils.go

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -344,32 +344,65 @@ func (r FileLayerAnalyzeResult) OutputText(analyzeType string, format string) er
344344
type SizeAnalyzeResult AnalyzeResult
345345

346346
func (r SizeAnalyzeResult) OutputStruct() interface{} {
347-
analysis, valid := r.Analysis.([]util.DirectoryEntry)
347+
analysis, valid := r.Analysis.([]SizeEntry)
348348
if !valid {
349-
logrus.Error("Unexpected structure of Analysis. Should be of type []DirectoryEntry")
349+
logrus.Error("Unexpected structure of Analysis. Should be of type []SizeEntry")
350350
return errors.New("Could not output SizeAnalyzer analysis result")
351351
}
352352
r.Analysis = analysis
353353
return r
354354
}
355355

356356
func (r SizeAnalyzeResult) OutputText(analyzeType string, format string) error {
357-
analysis, valid := r.Analysis.([]util.DirectoryEntry)
357+
analysis, valid := r.Analysis.([]SizeEntry)
358358
if !valid {
359-
logrus.Error("Unexpected structure of Analysis. Should be of type []DirectoryEntry")
359+
logrus.Error("Unexpected structure of Analysis. Should be of type []SizeEntry")
360360
return errors.New("Could not output SizeAnalyzer analysis result")
361361
}
362362

363-
strAnalysis := stringifyDirectoryEntries(analysis)
363+
strAnalysis := stringifySizeEntries(analysis)
364364

365365
strResult := struct {
366366
Image string
367367
AnalyzeType string
368-
Analysis []StrDirectoryEntry
368+
Analysis []StrSizeEntry
369369
}{
370370
Image: r.Image,
371371
AnalyzeType: r.AnalyzeType,
372372
Analysis: strAnalysis,
373373
}
374374
return TemplateOutputFromFormat(strResult, "SizeAnalyze", format)
375375
}
376+
377+
type SizeLayerAnalyzeResult AnalyzeResult
378+
379+
func (r SizeLayerAnalyzeResult) OutputStruct() interface{} {
380+
analysis, valid := r.Analysis.([]SizeEntry)
381+
if !valid {
382+
logrus.Error("Unexpected structure of Analysis. Should be of type []SizeEntry")
383+
return errors.New("Could not output SizeLayerAnalyzer analysis result")
384+
}
385+
r.Analysis = analysis
386+
return r
387+
}
388+
389+
func (r SizeLayerAnalyzeResult) OutputText(analyzeType string, format string) error {
390+
analysis, valid := r.Analysis.([]SizeEntry)
391+
if !valid {
392+
logrus.Error("Unexpected structure of Analysis. Should be of type []SizeEntry")
393+
return errors.New("Could not output SizeLayerAnalyzer analysis result")
394+
}
395+
396+
strAnalysis := stringifySizeEntries(analysis)
397+
398+
strResult := struct {
399+
Image string
400+
AnalyzeType string
401+
Analysis []StrSizeEntry
402+
}{
403+
Image: r.Image,
404+
AnalyzeType: r.AnalyzeType,
405+
Analysis: strAnalysis,
406+
}
407+
return TemplateOutputFromFormat(strResult, "SizeLayerAnalyze", format)
408+
}

util/diff_output_utils.go

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -300,9 +300,9 @@ func (r DirDiffResult) OutputText(diffType string, format string) error {
300300
type SizeDiffResult DiffResult
301301

302302
func (r SizeDiffResult) OutputStruct() interface{} {
303-
diff, valid := r.Diff.([]EntryDiff)
303+
diff, valid := r.Diff.([]SizeDiff)
304304
if !valid {
305-
logrus.Error("Unexpected structure of Diff. Should be of type []EntryDiff")
305+
logrus.Error("Unexpected structure of Diff. Should be of type []SizeDiff")
306306
return errors.New("Could not output SizeAnalyzer diff result")
307307
}
308308

@@ -311,19 +311,19 @@ func (r SizeDiffResult) OutputStruct() interface{} {
311311
}
312312

313313
func (r SizeDiffResult) OutputText(diffType string, format string) error {
314-
diff, valid := r.Diff.([]EntryDiff)
314+
diff, valid := r.Diff.([]SizeDiff)
315315
if !valid {
316-
logrus.Error("Unexpected structure of Diff. Should be of type []EntryDiff")
316+
logrus.Error("Unexpected structure of Diff. Should be of type []SizeDiff")
317317
return errors.New("Could not output SizeAnalyzer diff result")
318318
}
319319

320-
strDiff := stringifyEntryDiffs(diff)
320+
strDiff := stringifySizeDiffs(diff)
321321

322322
strResult := struct {
323323
Image1 string
324324
Image2 string
325325
DiffType string
326-
Diff []StrEntryDiff
326+
Diff []StrSizeDiff
327327
}{
328328
Image1: r.Image1,
329329
Image2: r.Image2,
@@ -333,6 +333,42 @@ func (r SizeDiffResult) OutputText(diffType string, format string) error {
333333
return TemplateOutputFromFormat(strResult, "SizeDiff", format)
334334
}
335335

336+
type SizeLayerDiffResult DiffResult
337+
338+
func (r SizeLayerDiffResult) OutputStruct() interface{} {
339+
diff, valid := r.Diff.([]SizeDiff)
340+
if !valid {
341+
logrus.Error("Unexpected structure of Diff. Should be of type []SizeDiff")
342+
return errors.New("Could not output SizeLayerAnalyzer diff result")
343+
}
344+
345+
r.Diff = diff
346+
return r
347+
}
348+
349+
func (r SizeLayerDiffResult) OutputText(diffType string, format string) error {
350+
diff, valid := r.Diff.([]SizeDiff)
351+
if !valid {
352+
logrus.Error("Unexpected structure of Diff. Should be of type []SizeDiff")
353+
return errors.New("Could not output SizeLayerAnalyzer diff result")
354+
}
355+
356+
strDiff := stringifySizeDiffs(diff)
357+
358+
strResult := struct {
359+
Image1 string
360+
Image2 string
361+
DiffType string
362+
Diff []StrSizeDiff
363+
}{
364+
Image1: r.Image1,
365+
Image2: r.Image2,
366+
DiffType: r.DiffType,
367+
Diff: strDiff,
368+
}
369+
return TemplateOutputFromFormat(strResult, "SizeLayerDiff", format)
370+
}
371+
336372
type MultipleDirDiffResult DiffResult
337373

338374
func (r MultipleDirDiffResult) OutputStruct() interface{} {

util/format_utils.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ var templates = map[string]string{
4040
"FileAnalyze": FileAnalysisOutput,
4141
"FileLayerAnalyze": FileLayerAnalysisOutput,
4242
"SizeAnalyze": SizeAnalysisOutput,
43+
"SizeLayerAnalyze": SizeLayerAnalysisOutput,
4344
"SizeDiff": SizeDiffOutput,
45+
"SizeLayerDiff": SizeLayerDiffOutput,
4446
"MultiVersionPackageAnalyze": MultiVersionPackageOutput,
4547
"SingleVersionPackageAnalyze": SingleVersionPackageOutput,
4648
"SingleVersionPackageLayerAnalyze": SingleVersionPackageLayerOutput,

util/output_text_utils.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,31 @@ func stringifyEntryDiffs(entries []EntryDiff) (strEntries []StrEntryDiff) {
121121
}
122122
return
123123
}
124+
125+
type StrSizeEntry struct {
126+
Name string
127+
Digest string
128+
Size string
129+
}
130+
131+
func stringifySizeEntries(entries []SizeEntry) (strEntries []StrSizeEntry) {
132+
for _, entry := range entries {
133+
strEntry := StrSizeEntry{Name: entry.Name, Digest: entry.Digest.String(), Size: stringifySize(entry.Size)}
134+
strEntries = append(strEntries, strEntry)
135+
}
136+
return
137+
}
138+
139+
type StrSizeDiff struct {
140+
Name string
141+
Size1 string
142+
Size2 string
143+
}
144+
145+
func stringifySizeDiffs(entries []SizeDiff) (strEntries []StrSizeDiff) {
146+
for _, entry := range entries {
147+
strEntry := StrSizeDiff{Name: entry.Name, Size1: stringifySize(entry.Size1), Size2: stringifySize(entry.Size2)}
148+
strEntries = append(strEntries, strEntry)
149+
}
150+
return
151+
}

0 commit comments

Comments
 (0)