@@ -20,7 +20,6 @@ import (
20
20
"context"
21
21
"fmt"
22
22
"io"
23
- "sync"
24
23
"time"
25
24
26
25
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build"
@@ -43,6 +42,11 @@ type ImageDetails struct {
43
42
ID string `yaml:"id,omitempty"`
44
43
}
45
44
45
+ type detailsErr struct {
46
+ details * cachedArtifactDetails
47
+ err error
48
+ }
49
+
46
50
// RetrieveCachedArtifacts checks to see if artifacts are cached, and returns tags for cached images, otherwise a list of images to be built
47
51
func (c * Cache ) RetrieveCachedArtifacts (ctx context.Context , out io.Writer , artifacts []* latest.Artifact ) ([]* latest.Artifact , []build.Artifact , error ) {
48
52
if ! c .useCache {
@@ -52,86 +56,70 @@ func (c *Cache) RetrieveCachedArtifacts(ctx context.Context, out io.Writer, arti
52
56
start := time .Now ()
53
57
color .Default .Fprintln (out , "Checking cache..." )
54
58
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
+
55
71
var (
56
72
needToBuild []* latest.Artifact
57
73
built []build.Artifact
58
-
59
- wg sync.WaitGroup
60
- lock sync.Mutex
61
74
)
62
75
63
- wg .Add (len (artifacts ))
76
+ for i , artifact := range artifacts {
77
+ color .Default .Fprintf (out , " - %s: " , artifact .ImageName )
64
78
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
71
82
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
+ }
74
91
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 )
78
100
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
+ }
81
105
}
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
+ }
85
110
}
86
111
87
- built = append (built , * artifact )
88
- }()
112
+ built = append (built , build.Artifact {
113
+ ImageName : artifact .ImageName ,
114
+ Tag : details .hashTag ,
115
+ })
116
+ }
89
117
}
90
- wg .Wait ()
91
118
92
119
color .Default .Fprintln (out , "Cache check complete in" , time .Since (start ))
93
120
return needToBuild , built , nil
94
121
}
95
122
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
-
135
123
type cachedArtifactDetails struct {
136
124
needsRebuild bool
137
125
needsRetag bool
0 commit comments