Skip to content

Commit 6051a51

Browse files
committed
Check cached artifacts in parallel
Signed-off-by: David Gageot <[email protected]>
1 parent 5b54d97 commit 6051a51

File tree

1 file changed

+51
-63
lines changed

1 file changed

+51
-63
lines changed

pkg/skaffold/build/cache/retrieve.go

+51-63
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"context"
2121
"fmt"
2222
"io"
23-
"sync"
2423
"time"
2524

2625
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build"
@@ -43,6 +42,11 @@ type ImageDetails struct {
4342
ID string `yaml:"id,omitempty"`
4443
}
4544

45+
type detailsErr struct {
46+
details *cachedArtifactDetails
47+
err error
48+
}
49+
4650
// RetrieveCachedArtifacts checks to see if artifacts are cached, and returns tags for cached images, otherwise a list of images to be built
4751
func (c *Cache) RetrieveCachedArtifacts(ctx context.Context, out io.Writer, artifacts []*latest.Artifact) ([]*latest.Artifact, []build.Artifact, error) {
4852
if !c.useCache {
@@ -52,86 +56,70 @@ func (c *Cache) RetrieveCachedArtifacts(ctx context.Context, out io.Writer, arti
5256
start := time.Now()
5357
color.Default.Fprintln(out, "Checking cache...")
5458

59+
detailsErrs := make([]chan detailsErr, len(artifacts))
60+
61+
for i := range artifacts {
62+
detailsErrs[i] = make(chan detailsErr, 1)
63+
64+
i := i
65+
go func() {
66+
details, err := c.retrieveCachedArtifactDetails(ctx, artifacts[i])
67+
detailsErrs[i] <- detailsErr{details: details, err: err}
68+
}()
69+
}
70+
5571
var (
5672
needToBuild []*latest.Artifact
5773
built []build.Artifact
58-
59-
wg sync.WaitGroup
60-
lock sync.Mutex
6174
)
6275

63-
wg.Add(len(artifacts))
76+
for i, artifact := range artifacts {
77+
color.Default.Fprintf(out, " - %s: ", artifact.ImageName)
6478

65-
for _, a := range artifacts {
66-
a := a
67-
go func() {
68-
defer wg.Done()
69-
70-
artifact, err := c.resolveCachedArtifact(ctx, out, a)
79+
select {
80+
case <-ctx.Done():
81+
return nil, nil, context.Canceled
7182

72-
lock.Lock()
73-
defer lock.Unlock()
83+
case d := <-detailsErrs[i]:
84+
details := d.details
85+
err := d.err
86+
if err != nil || details.needsRebuild {
87+
color.Red.Fprintln(out, "Not found. Rebuilding.")
88+
needToBuild = append(needToBuild, artifact)
89+
continue
90+
}
7491

75-
if err != nil {
76-
logrus.Debugf("error retrieving cached artifact for %s: %v\n", a.ImageName, err)
77-
color.Red.Fprintf(out, "Unable to retrieve %s from cache; this image will be rebuilt.\n", a.ImageName)
92+
color.Green.Fprint(out, "Found")
93+
if details.needsRetag {
94+
color.Green.Fprint(out, ". Retagging")
95+
}
96+
if details.needsPush {
97+
color.Green.Fprint(out, ". Pushing.")
98+
}
99+
color.Default.Fprintln(out)
78100

79-
needToBuild = append(needToBuild, a)
80-
return
101+
if details.needsRetag {
102+
if err := c.client.Tag(ctx, details.prebuiltImage, details.hashTag); err != nil {
103+
return nil, nil, errors.Wrap(err, "retagging image")
104+
}
81105
}
82-
if artifact == nil {
83-
needToBuild = append(needToBuild, a)
84-
return
106+
if details.needsPush {
107+
if _, err := c.client.Push(ctx, out, details.hashTag); err != nil {
108+
return nil, nil, errors.Wrap(err, "pushing image")
109+
}
85110
}
86111

87-
built = append(built, *artifact)
88-
}()
112+
built = append(built, build.Artifact{
113+
ImageName: artifact.ImageName,
114+
Tag: details.hashTag,
115+
})
116+
}
89117
}
90-
wg.Wait()
91118

92119
color.Default.Fprintln(out, "Cache check complete in", time.Since(start))
93120
return needToBuild, built, nil
94121
}
95122

96-
func (c *Cache) resolveCachedArtifact(ctx context.Context, out io.Writer, a *latest.Artifact) (*build.Artifact, error) {
97-
details, err := c.retrieveCachedArtifactDetails(ctx, a)
98-
if err != nil {
99-
return nil, errors.Wrap(err, "getting cached artifact details")
100-
}
101-
102-
color.Default.Fprintf(out, " - %s: ", a.ImageName)
103-
104-
if details.needsRebuild {
105-
color.Red.Fprintln(out, "Not found. Rebuilding.")
106-
return nil, nil
107-
}
108-
109-
color.Green.Fprint(out, "Found")
110-
if details.needsRetag {
111-
color.Green.Fprint(out, ". Retagging")
112-
}
113-
if details.needsPush {
114-
color.Green.Fprint(out, ". Pushing.")
115-
}
116-
color.Default.Fprintln(out)
117-
118-
if details.needsRetag {
119-
if err := c.client.Tag(ctx, details.prebuiltImage, details.hashTag); err != nil {
120-
return nil, errors.Wrap(err, "retagging image")
121-
}
122-
}
123-
if details.needsPush {
124-
if _, err := c.client.Push(ctx, out, details.hashTag); err != nil {
125-
return nil, errors.Wrap(err, "pushing image")
126-
}
127-
}
128-
129-
return &build.Artifact{
130-
ImageName: a.ImageName,
131-
Tag: details.hashTag,
132-
}, nil
133-
}
134-
135123
type cachedArtifactDetails struct {
136124
needsRebuild bool
137125
needsRetag bool

0 commit comments

Comments
 (0)