Skip to content

Commit 7032e62

Browse files
authored
speed up structure test (#54)
- Skip `mutate.Extract` if there's only one layer (for our images, there will only be one layer). `mutate.Extract` is wasteful and blocks if there's only one layer. I've tested this works with a multi-layer image as well. - Stop reading the tar stream if all the file checks we plan to run have run. Signed-off-by: Jason Hall <[email protected]>
1 parent 4da7c11 commit 7032e62

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

pkg/structure/structure.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,25 @@ type File struct {
6666
}
6767

6868
func (f FilesCondition) Check(i v1.Image) error {
69-
rc := mutate.Extract(i)
69+
ls, err := i.Layers()
70+
if err != nil {
71+
return err
72+
}
73+
var rc io.ReadCloser
74+
// If there's only one layer, we don't need to extract it.
75+
if len(ls) == 1 {
76+
rc, err = ls[0].Uncompressed()
77+
if err != nil {
78+
return err
79+
}
80+
} else {
81+
rc = mutate.Extract(i)
82+
}
83+
7084
defer rc.Close()
7185
tr := tar.NewReader(rc)
7286
errs := []error{}
87+
L:
7388
for {
7489
hdr, err := tr.Next()
7590
if err == io.EOF {
@@ -100,6 +115,17 @@ func (f FilesCondition) Check(i v1.Image) error {
100115
Regex: f.Want[hdr.Name].Regex,
101116
ran: true,
102117
}
118+
119+
// If all the checks have run, we can stop early.
120+
// This might not be strictly correct, since tar files can have multiple
121+
// files with the same name, and the last one wins; in practice, this is
122+
// unlikely to be a problem, and the optimization is worth it.
123+
for _, f := range f.Want {
124+
if !f.ran {
125+
continue L
126+
}
127+
}
128+
break
103129
}
104130
for path, f := range f.Want {
105131
if !f.ran {

0 commit comments

Comments
 (0)