|
| 1 | +/* |
| 2 | +Copyright 2018 Google, Inc. All rights reserved. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package differs |
| 18 | + |
| 19 | +import ( |
| 20 | + "strconv" |
| 21 | + |
| 22 | + pkgutil "github.com/GoogleContainerTools/container-diff/pkg/util" |
| 23 | + "github.com/GoogleContainerTools/container-diff/util" |
| 24 | +) |
| 25 | + |
| 26 | +type SizeAnalyzer struct { |
| 27 | +} |
| 28 | + |
| 29 | +func (a SizeAnalyzer) Name() string { |
| 30 | + return "SizeAnalyzer" |
| 31 | +} |
| 32 | + |
| 33 | +// SizeDiff diffs two images and compares their size |
| 34 | +func (a SizeAnalyzer) Diff(image1, image2 pkgutil.Image) (util.Result, error) { |
| 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 | + }) |
| 44 | + } |
| 45 | + |
| 46 | + return &util.SizeDiffResult{ |
| 47 | + Image1: image1.Source, |
| 48 | + Image2: image2.Source, |
| 49 | + DiffType: "Size", |
| 50 | + Diff: diff, |
| 51 | + }, nil |
| 52 | +} |
| 53 | + |
| 54 | +func (a SizeAnalyzer) Analyze(image pkgutil.Image) (util.Result, error) { |
| 55 | + entries := []util.SizeEntry{ |
| 56 | + { |
| 57 | + Name: image.Source, |
| 58 | + Digest: image.Digest, |
| 59 | + Size: pkgutil.GetSize(image.FSPath), |
| 60 | + }, |
| 61 | + } |
| 62 | + |
| 63 | + return &util.SizeAnalyzeResult{ |
| 64 | + Image: image.Source, |
| 65 | + AnalyzeType: "Size", |
| 66 | + Analysis: entries, |
| 67 | + }, nil |
| 68 | +} |
| 69 | + |
| 70 | +type SizeLayerAnalyzer struct { |
| 71 | +} |
| 72 | + |
| 73 | +func (a SizeLayerAnalyzer) Name() string { |
| 74 | + return "SizeLayerAnalyzer" |
| 75 | +} |
| 76 | + |
| 77 | +// SizeLayerDiff diffs the layers of two images and compares their size |
| 78 | +func (a SizeLayerAnalyzer) Diff(image1, image2 pkgutil.Image) (util.Result, error) { |
| 79 | + var layerDiffs []util.SizeDiff |
| 80 | + |
| 81 | + maxLayer := len(image1.Layers) |
| 82 | + if len(image2.Layers) > maxLayer { |
| 83 | + maxLayer = len(image2.Layers) |
| 84 | + } |
| 85 | + |
| 86 | + for index := 0; index < maxLayer; index++ { |
| 87 | + var size1, size2 int64 = -1, -1 |
| 88 | + if index < len(image1.Layers) { |
| 89 | + size1 = pkgutil.GetSize(image1.Layers[index].FSPath) |
| 90 | + } |
| 91 | + if index < len(image2.Layers) { |
| 92 | + size2 = pkgutil.GetSize(image2.Layers[index].FSPath) |
| 93 | + } |
| 94 | + |
| 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) |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + return &util.SizeLayerDiffResult{ |
| 106 | + Image1: image1.Source, |
| 107 | + Image2: image2.Source, |
| 108 | + DiffType: "SizeLayer", |
| 109 | + Diff: layerDiffs, |
| 110 | + }, nil |
| 111 | +} |
| 112 | + |
| 113 | +func (a SizeLayerAnalyzer) Analyze(image pkgutil.Image) (util.Result, error) { |
| 114 | + var entries []util.SizeEntry |
| 115 | + for index, layer := range image.Layers { |
| 116 | + entry := util.SizeEntry{ |
| 117 | + Name: strconv.Itoa(index), |
| 118 | + Digest: layer.Digest, |
| 119 | + Size: pkgutil.GetSize(layer.FSPath), |
| 120 | + } |
| 121 | + entries = append(entries, entry) |
| 122 | + } |
| 123 | + |
| 124 | + return &util.SizeLayerAnalyzeResult{ |
| 125 | + Image: image.Source, |
| 126 | + AnalyzeType: "SizeLayer", |
| 127 | + Analysis: entries, |
| 128 | + }, nil |
| 129 | +} |
0 commit comments