@@ -249,151 +249,134 @@ func (o *ImportImageOptions) waitForImport(resourceVersion string) (*imageapi.Im
249
249
}
250
250
251
251
func (o * ImportImageOptions ) createImageImport () (* imageapi.ImageStream , * imageapi.ImageStreamImport , error ) {
252
+ var isi * imageapi.ImageStreamImport
252
253
stream , err := o .isClient .Get (o .Name )
253
- from := o .From
254
- tag := o .Tag
255
-
254
+ // no stream, try creating one
256
255
if err != nil {
257
256
if ! errors .IsNotFound (err ) {
258
257
return nil , nil , err
259
258
}
260
259
if ! o .Confirm {
261
260
return nil , nil , fmt .Errorf ("no image stream named %q exists, pass --confirm to create and import" , o .Name )
262
261
}
263
- from , stream = o .newImageStream ()
264
-
265
- } else {
266
- // the stream already exists
267
- if len (stream .Spec .DockerImageRepository ) == 0 && len (stream .Spec .Tags ) == 0 {
268
- return nil , nil , fmt .Errorf ("image stream has not defined anything to import" )
269
- }
270
-
271
- if o .All {
272
- // importing the entire repository
273
- from , err = o .importAll (stream )
274
- if err != nil {
275
- return nil , nil , err
276
- }
277
- } else {
278
- // importing a single tag
279
- from , err = o .importTag (stream )
280
- if err != nil {
281
- return nil , nil , err
282
- }
283
- }
262
+ stream , isi = o .newImageStream ()
263
+ return stream , isi , nil
284
264
}
285
265
286
- if len ( from ) == 0 {
287
- // catch programmer error
288
- return nil , nil , fmt .Errorf ("unexpected error, from is empty " )
266
+ // the stream already exists
267
+ if len ( stream . Spec . DockerImageRepository ) == 0 && len ( stream . Spec . Tags ) == 0 {
268
+ return nil , nil , fmt .Errorf ("image stream has not defined anything to import " )
289
269
}
290
270
291
- isi := & imageapi.ImageStreamImport {
292
- ObjectMeta : kapi.ObjectMeta {
293
- Name : stream .Name ,
294
- Namespace : o .Namespace ,
295
- ResourceVersion : stream .ResourceVersion ,
296
- },
297
- Spec : imageapi.ImageStreamImportSpec {Import : true },
298
- }
299
- insecureAnnotation := stream .Annotations [imageapi .InsecureRepositoryAnnotation ]
300
- insecure := insecureAnnotation == "true"
301
- // --insecure flag (if provided) takes precedence over insecure annotation
302
- if o .Insecure != nil {
303
- insecure = * o .Insecure
304
- }
305
271
if o .All {
306
- isi .Spec .Repository = & imageapi.RepositoryImportSpec {
307
- From : kapi.ObjectReference {
308
- Kind : "DockerImage" ,
309
- Name : from ,
310
- },
311
- ImportPolicy : imageapi.TagImportPolicy {Insecure : insecure },
272
+ // importing the entire repository
273
+ isi , err = o .importAll (stream )
274
+ if err != nil {
275
+ return nil , nil , err
312
276
}
313
277
} else {
314
- isi .Spec .Images = append (isi .Spec .Images , imageapi.ImageImportSpec {
315
- From : kapi.ObjectReference {
316
- Kind : "DockerImage" ,
317
- Name : from ,
318
- },
319
- To : & kapi.LocalObjectReference {Name : tag },
320
- ImportPolicy : imageapi.TagImportPolicy {Insecure : insecure },
321
- })
278
+ // importing a single tag
279
+ isi , err = o .importTag (stream )
280
+ if err != nil {
281
+ return nil , nil , err
282
+ }
322
283
}
323
284
324
285
return stream , isi , nil
325
286
}
326
287
327
- func (o * ImportImageOptions ) newImageStream () (string , * imageapi.ImageStream ) {
288
+ func (o * ImportImageOptions ) newImageStream () (* imageapi. ImageStream , * imageapi.ImageStreamImport ) {
328
289
from := o .From
329
290
tag := o .Tag
330
291
if len (from ) == 0 {
331
292
from = o .Target
332
293
}
333
294
var stream * imageapi.ImageStream
295
+ // create new ImageStream
334
296
if o .All {
335
297
stream = & imageapi.ImageStream {
336
298
ObjectMeta : kapi.ObjectMeta {Name : o .Name },
337
299
Spec : imageapi.ImageStreamSpec {DockerImageRepository : from },
338
300
}
339
- }
340
- stream = & imageapi.ImageStream {
341
- ObjectMeta : kapi.ObjectMeta {Name : o .Name },
342
- Spec : imageapi.ImageStreamSpec {
343
- Tags : map [string ]imageapi.TagReference {
344
- tag : {
345
- From : & kapi.ObjectReference {
346
- Kind : "DockerImage" ,
347
- Name : from ,
301
+ } else {
302
+ stream = & imageapi.ImageStream {
303
+ ObjectMeta : kapi.ObjectMeta {Name : o .Name },
304
+ Spec : imageapi.ImageStreamSpec {
305
+ Tags : map [string ]imageapi.TagReference {
306
+ tag : {
307
+ From : & kapi.ObjectReference {
308
+ Kind : "DockerImage" ,
309
+ Name : from ,
310
+ },
348
311
},
349
312
},
350
313
},
351
- },
314
+ }
315
+ }
316
+ // and accompanying ImageStreamImport
317
+ var isi * imageapi.ImageStreamImport
318
+ if o .All {
319
+ isi = o .newImageStreamImportAll (stream , from )
320
+ } else {
321
+ isi = o .newImageStreamImportTags (stream , map [string ]string {tag : from })
352
322
}
353
- return from , stream
323
+
324
+ return stream , isi
354
325
}
355
326
356
- func (o * ImportImageOptions ) importAll (stream * imageapi.ImageStream ) (string , error ) {
327
+ func (o * ImportImageOptions ) importAll (stream * imageapi.ImageStream ) (* imageapi. ImageStreamImport , error ) {
357
328
from := o .From
329
+ // update ImageStream appropriately
358
330
if len (from ) == 0 {
359
- if len (stream .Spec .DockerImageRepository ) == 0 {
360
- // FIXME soltysh: support all spec.Tags
361
- return "" , fmt .Errorf ("flag --all is applicable only to images with spec.dockerImageRepository defined" )
331
+ if len (stream .Spec .DockerImageRepository ) != 0 {
332
+ from = stream .Spec .DockerImageRepository
333
+ } else {
334
+ tags := make (map [string ]string )
335
+ for name , tag := range stream .Spec .Tags {
336
+ if tag .From != nil && tag .From .Kind == "DockerImage" {
337
+ tags [name ] = tag .From .Name
338
+ }
339
+ }
340
+ if len (tags ) == 0 {
341
+ return nil , fmt .Errorf ("image stream does not have importable tags (pointing to DockerImage)" )
342
+ }
343
+ return o .newImageStreamImportTags (stream , tags ), nil
362
344
}
363
- from = stream .Spec .DockerImageRepository
364
345
}
365
346
if from != stream .Spec .DockerImageRepository {
366
347
if ! o .Confirm {
367
348
if len (stream .Spec .DockerImageRepository ) == 0 {
368
- return "" , fmt .Errorf ("the image stream does not currently import an entire Docker repository, pass --confirm to update" )
349
+ return nil , fmt .Errorf ("the image stream does not currently import an entire Docker repository, pass --confirm to update" )
369
350
}
370
- return "" , fmt .Errorf ("the image stream has a different import spec %q, pass --confirm to update" , stream .Spec .DockerImageRepository )
351
+ return nil , fmt .Errorf ("the image stream has a different import spec %q, pass --confirm to update" , stream .Spec .DockerImageRepository )
371
352
}
372
353
stream .Spec .DockerImageRepository = from
373
354
}
374
355
375
- return from , nil
356
+ // and create accompanying ImageStreamImport
357
+ return o .newImageStreamImportAll (stream , from ), nil
376
358
}
377
359
378
- func (o * ImportImageOptions ) importTag (stream * imageapi.ImageStream ) (string , error ) {
360
+ func (o * ImportImageOptions ) importTag (stream * imageapi.ImageStream ) (* imageapi. ImageStreamImport , error ) {
379
361
from := o .From
380
362
tag := o .Tag
381
363
// follow any referential tags to the destination
382
364
finalTag , existing , ok , multiple := imageapi .FollowTagReference (stream , tag )
383
365
if ! ok && multiple {
384
- return "" , fmt .Errorf ("tag %q on the image stream is a reference to %q, which does not exist" , tag , finalTag )
366
+ return nil , fmt .Errorf ("tag %q on the image stream is a reference to %q, which does not exist" , tag , finalTag )
385
367
}
386
368
369
+ // update ImageStream appropriately
387
370
if ok {
388
371
// disallow changing an existing tag
389
372
if existing .From == nil || existing .From .Kind != "DockerImage" {
390
- return "" , fmt .Errorf ("tag %q already exists - you must use the 'tag' command if you want to change the source to %q" , tag , from )
373
+ return nil , fmt .Errorf ("tag %q already exists - you must use the 'tag' command if you want to change the source to %q" , tag , from )
391
374
}
392
375
if len (from ) != 0 && from != existing .From .Name {
393
376
if multiple {
394
- return "" , fmt .Errorf ("the tag %q points to the tag %q which points to %q - use the 'tag' command if you want to change the source to %q" , tag , finalTag , existing .From .Name , from )
377
+ return nil , fmt .Errorf ("the tag %q points to the tag %q which points to %q - use the 'tag' command if you want to change the source to %q" , tag , finalTag , existing .From .Name , from )
395
378
}
396
- return "" , fmt .Errorf ("the tag %q points to %q - use the 'tag' command if you want to change the source to %q" , tag , existing .From .Name , from )
379
+ return nil , fmt .Errorf ("the tag %q points to %q - use the 'tag' command if you want to change the source to %q" , tag , existing .From .Name , from )
397
380
}
398
381
399
382
// set the target item to import
@@ -416,7 +399,7 @@ func (o *ImportImageOptions) importTag(stream *imageapi.ImageStream) (string, er
416
399
// if the from is still empty this means there's no such tag defined
417
400
// nor we can't create any from .spec.dockerImageRepository
418
401
if len (from ) == 0 {
419
- return "" , fmt .Errorf ("the tag %q does not exist on the image stream - choose an existing tag to import or use the 'tag' command to create a new tag" , tag )
402
+ return nil , fmt .Errorf ("the tag %q does not exist on the image stream - choose an existing tag to import or use the 'tag' command to create a new tag" , tag )
420
403
}
421
404
existing = & imageapi.TagReference {
422
405
From : & kapi.ObjectReference {
@@ -427,5 +410,53 @@ func (o *ImportImageOptions) importTag(stream *imageapi.ImageStream) (string, er
427
410
}
428
411
stream .Spec .Tags [tag ] = * existing
429
412
430
- return from , nil
413
+ // and create accompanying ImageStreamImport
414
+ return o .newImageStreamImportTags (stream , map [string ]string {tag : from }), nil
415
+ }
416
+
417
+ func (o * ImportImageOptions ) newImageStreamImport (stream * imageapi.ImageStream ) (* imageapi.ImageStreamImport , bool ) {
418
+ isi := & imageapi.ImageStreamImport {
419
+ ObjectMeta : kapi.ObjectMeta {
420
+ Name : stream .Name ,
421
+ Namespace : o .Namespace ,
422
+ ResourceVersion : stream .ResourceVersion ,
423
+ },
424
+ Spec : imageapi.ImageStreamImportSpec {Import : true },
425
+ }
426
+ insecureAnnotation := stream .Annotations [imageapi .InsecureRepositoryAnnotation ]
427
+ insecure := insecureAnnotation == "true"
428
+ // --insecure flag (if provided) takes precedence over insecure annotation
429
+ if o .Insecure != nil {
430
+ insecure = * o .Insecure
431
+ }
432
+
433
+ return isi , insecure
434
+ }
435
+
436
+ func (o * ImportImageOptions ) newImageStreamImportAll (stream * imageapi.ImageStream , from string ) * imageapi.ImageStreamImport {
437
+ isi , insecure := o .newImageStreamImport (stream )
438
+ isi .Spec .Repository = & imageapi.RepositoryImportSpec {
439
+ From : kapi.ObjectReference {
440
+ Kind : "DockerImage" ,
441
+ Name : from ,
442
+ },
443
+ ImportPolicy : imageapi.TagImportPolicy {Insecure : insecure },
444
+ }
445
+
446
+ return isi
447
+ }
448
+
449
+ func (o * ImportImageOptions ) newImageStreamImportTags (stream * imageapi.ImageStream , tags map [string ]string ) * imageapi.ImageStreamImport {
450
+ isi , insecure := o .newImageStreamImport (stream )
451
+ for tag , from := range tags {
452
+ isi .Spec .Images = append (isi .Spec .Images , imageapi.ImageImportSpec {
453
+ From : kapi.ObjectReference {
454
+ Kind : "DockerImage" ,
455
+ Name : from ,
456
+ },
457
+ To : & kapi.LocalObjectReference {Name : tag },
458
+ ImportPolicy : imageapi.TagImportPolicy {Insecure : insecure },
459
+ })
460
+ }
461
+ return isi
431
462
}
0 commit comments