@@ -21,6 +21,7 @@ import (
21
21
"encoding/json"
22
22
"fmt"
23
23
"io"
24
+ "strings"
24
25
"time"
25
26
26
27
cstorage "cloud.google.com/go/storage"
@@ -30,6 +31,8 @@ import (
30
31
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/v1alpha2"
31
32
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
32
33
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/version"
34
+ "github.com/docker/distribution/reference"
35
+ "github.com/docker/docker/registry"
33
36
"github.com/pkg/errors"
34
37
"github.com/sirupsen/logrus"
35
38
"golang.org/x/oauth2/google"
@@ -116,13 +119,18 @@ func (cb *GoogleCloudBuilder) buildArtifact(ctx context.Context, out io.Writer,
116
119
}
117
120
logrus .Debugf ("Build args: %s" , buildArgs )
118
121
119
- cbBucket := fmt .Sprintf ("%s%s" , cb .ProjectID , constants .GCSBucketSuffix )
120
- buildObject := fmt .Sprintf ("source/%s-%s.tar.gz" , cb .ProjectID , util .RandomID ())
122
+ projectID , err := cb .guessProjectID (artifact )
123
+ if err != nil {
124
+ return "" , errors .Wrap (err , "getting projectID" )
125
+ }
126
+
127
+ cbBucket := fmt .Sprintf ("%s%s" , projectID , constants .GCSBucketSuffix )
128
+ buildObject := fmt .Sprintf ("source/%s-%s.tar.gz" , projectID , util .RandomID ())
121
129
122
- if err := cb .createBucketIfNotExists (ctx , cbBucket ); err != nil {
130
+ if err := cb .createBucketIfNotExists (ctx , projectID , cbBucket ); err != nil {
123
131
return "" , errors .Wrap (err , "creating bucket if not exists" )
124
132
}
125
- if err := cb .checkBucketProjectCorrect (ctx , cbBucket ); err != nil {
133
+ if err := cb .checkBucketProjectCorrect (ctx , projectID , cbBucket ); err != nil {
126
134
return "" , errors .Wrap (err , "checking bucket is in correct project" )
127
135
}
128
136
@@ -133,7 +141,7 @@ func (cb *GoogleCloudBuilder) buildArtifact(ctx context.Context, out io.Writer,
133
141
134
142
args := append ([]string {"build" , "--tag" , artifact .ImageName , "-f" , artifact .DockerArtifact .DockerfilePath }, buildArgs ... )
135
143
args = append (args , "." )
136
- call := cbclient .Projects .Builds .Create (cb . ProjectID , & cloudbuild.Build {
144
+ call := cbclient .Projects .Builds .Create (projectID , & cloudbuild.Build {
137
145
LogsBucket : cbBucket ,
138
146
Source : & cloudbuild.Source {
139
147
StorageSource : & cloudbuild.StorageSource {
@@ -169,7 +177,7 @@ func (cb *GoogleCloudBuilder) buildArtifact(ctx context.Context, out io.Writer,
169
177
watch:
170
178
for {
171
179
logrus .Debugf ("current offset %d" , offset )
172
- b , err := cbclient .Projects .Builds .Get (cb . ProjectID , remoteID ).Do ()
180
+ b , err := cbclient .Projects .Builds .Get (projectID , remoteID ).Do ()
173
181
if err != nil {
174
182
return "" , errors .Wrap (err , "getting build status" )
175
183
}
@@ -226,6 +234,35 @@ watch:
226
234
return newTag , nil
227
235
}
228
236
237
+ func (cb * GoogleCloudBuilder ) guessProjectID (artifact * v1alpha2.Artifact ) (string , error ) {
238
+ if cb .ProjectID != "" {
239
+ return cb .ProjectID , nil
240
+ }
241
+
242
+ ref , err := reference .ParseNormalizedNamed (artifact .ImageName )
243
+ if err != nil {
244
+ return "" , errors .Wrap (err , "parsing image name for registry" )
245
+ }
246
+
247
+ repoInfo , err := registry .ParseRepositoryInfo (ref )
248
+ if err != nil {
249
+ return "" , err
250
+ }
251
+
252
+ index := repoInfo .Index
253
+ if ! index .Official {
254
+ switch index .Name {
255
+ case "gcr.io" , "us.gcr.io" , "eu.gcr.io" , "asia.gcr.io" , "staging-k8s.gcr.io" :
256
+ parts := strings .Split (repoInfo .Name .String (), "/" )
257
+ if len (parts ) >= 2 {
258
+ return parts [1 ], nil
259
+ }
260
+ }
261
+ }
262
+
263
+ return "" , fmt .Errorf ("unable to guess GCP projectID from image name [%s]" , artifact .ImageName )
264
+ }
265
+
229
266
func getBuildID (op * cloudbuild.Operation ) (string , error ) {
230
267
if op .Metadata == nil {
231
268
return "" , errors .New ("missing Metadata in operation" )
@@ -272,12 +309,12 @@ func (cb *GoogleCloudBuilder) getLogs(ctx context.Context, offset int64, bucket,
272
309
return r , nil
273
310
}
274
311
275
- func (cb * GoogleCloudBuilder ) checkBucketProjectCorrect (ctx context.Context , bucket string ) error {
312
+ func (cb * GoogleCloudBuilder ) checkBucketProjectCorrect (ctx context.Context , projectID , bucket string ) error {
276
313
c , err := cstorage .NewClient (ctx )
277
314
if err != nil {
278
315
return errors .Wrap (err , "getting storage client" )
279
316
}
280
- it := c .Buckets (ctx , cb . ProjectID )
317
+ it := c .Buckets (ctx , projectID )
281
318
// Set the prefix to the bucket we're looking for to only return that bucket and buckets with that prefix
282
319
// that we'll filter further later on
283
320
it .Prefix = bucket
@@ -297,7 +334,7 @@ func (cb *GoogleCloudBuilder) checkBucketProjectCorrect(ctx context.Context, buc
297
334
298
335
}
299
336
300
- func (cb * GoogleCloudBuilder ) createBucketIfNotExists (ctx context.Context , bucket string ) error {
337
+ func (cb * GoogleCloudBuilder ) createBucketIfNotExists (ctx context.Context , projectID , bucket string ) error {
301
338
c , err := cstorage .NewClient (ctx )
302
339
if err != nil {
303
340
return errors .Wrap (err , "getting storage client" )
@@ -315,11 +352,11 @@ func (cb *GoogleCloudBuilder) createBucketIfNotExists(ctx context.Context, bucke
315
352
return errors .Wrapf (err , "getting bucket %s" , bucket )
316
353
}
317
354
318
- if err := c .Bucket (bucket ).Create (ctx , cb . ProjectID , & cstorage.BucketAttrs {
355
+ if err := c .Bucket (bucket ).Create (ctx , projectID , & cstorage.BucketAttrs {
319
356
Name : bucket ,
320
357
}); err != nil {
321
358
return err
322
359
}
323
- logrus .Debugf ("Created bucket %s in %s" , bucket , cb . ProjectID )
360
+ logrus .Debugf ("Created bucket %s in %s" , bucket , projectID )
324
361
return nil
325
362
}
0 commit comments