@@ -4,26 +4,21 @@ import (
4
4
"fmt"
5
5
"io"
6
6
"strings"
7
- "time"
8
7
9
8
"github.com/spf13/cobra"
10
9
11
10
"k8s.io/apimachinery/pkg/api/errors"
12
11
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
13
- "k8s.io/apimachinery/pkg/fields"
14
- "k8s.io/apimachinery/pkg/watch"
15
12
"k8s.io/kubernetes/pkg/api/legacyscheme"
16
13
kapi "k8s.io/kubernetes/pkg/apis/core"
17
14
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
18
15
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
19
- kprinters "k8s.io/kubernetes/pkg/printers"
20
16
21
17
imageapiv1 "github.com/openshift/api/image/v1"
22
18
imageapi "github.com/openshift/origin/pkg/image/apis/image"
23
19
imageclientinternal "github.com/openshift/origin/pkg/image/generated/internalclientset"
24
20
imageclient "github.com/openshift/origin/pkg/image/generated/internalclientset/typed/image/internalversion"
25
21
"github.com/openshift/origin/pkg/oc/cli/describe"
26
- quotautil "github.com/openshift/origin/pkg/quota/util"
27
22
)
28
23
29
24
var (
@@ -170,53 +165,36 @@ func (o *ImportImageOptions) Run() error {
170
165
return err
171
166
}
172
167
173
- // Attempt the new, direct import path
174
168
result , err := o .imageClient .ImageStreamImports (isi .Namespace ).Create (isi )
175
- err = TransformUnsupportedError (err )
176
- switch {
177
- case err == imageapi .ErrImageStreamImportUnsupported :
178
- case err != nil :
169
+ if err != nil {
179
170
return err
180
- default :
181
- if o .DryRun {
182
- if wasError (result ) {
183
- fmt .Fprintf (o .errout , "The dry-run import completed with errors.\n \n " )
184
- } else {
185
- fmt .Fprint (o .out , "The dry-run import completed successfully.\n \n " )
186
- }
171
+ }
172
+
173
+ if o .DryRun {
174
+ if wasError (result ) {
175
+ fmt .Fprintf (o .errout , "The dry-run import completed with errors.\n \n " )
187
176
} else {
188
- if wasError (result ) {
189
- fmt .Fprintf (o .errout , "The import completed with errors.\n \n " )
190
- } else {
191
- fmt .Fprint (o .out , "The import completed successfully.\n \n " )
192
- }
177
+ fmt .Fprint (o .out , "The dry-run import completed successfully.\n \n " )
193
178
}
194
-
195
- if result .Status .Import != nil {
196
- // TODO: dry-run doesn't return an image stream, so we have to display partial results
197
- info , err := describe .DescribeImageStream (result .Status .Import )
198
- if err != nil {
199
- return err
200
- }
201
- fmt .Fprintln (o .out , info )
179
+ } else {
180
+ if wasError (result ) {
181
+ fmt .Fprintf (o .errout , "The import completed with errors.\n \n " )
182
+ } else {
183
+ fmt .Fprint (o .out , "The import completed successfully.\n \n " )
202
184
}
185
+ }
203
186
204
- if repo := result .Status .Repository ; repo != nil {
205
- for _ , image := range repo .Images {
206
- if image .Image != nil {
207
- info , err := describe .DescribeImage (image .Image , imageapi .JoinImageStreamTag (stream .Name , image .Tag ))
208
- if err != nil {
209
- fmt .Fprintf (o .errout , "error: tag %s failed: %v\n " , image .Tag , err )
210
- } else {
211
- fmt .Fprintln (o .out , info )
212
- }
213
- } else {
214
- fmt .Fprintf (o .errout , "error: repository tag %s failed: %v\n " , image .Tag , image .Status .Message )
215
- }
216
- }
187
+ if result .Status .Import != nil {
188
+ // TODO: dry-run doesn't return an image stream, so we have to display partial results
189
+ info , err := describe .DescribeImageStream (result .Status .Import )
190
+ if err != nil {
191
+ return err
217
192
}
193
+ fmt .Fprintln (o .out , info )
194
+ }
218
195
219
- for _ , image := range result .Status .Images {
196
+ if repo := result .Status .Repository ; repo != nil {
197
+ for _ , image := range repo .Images {
220
198
if image .Image != nil {
221
199
info , err := describe .DescribeImage (image .Image , imageapi .JoinImageStreamTag (stream .Name , image .Tag ))
222
200
if err != nil {
@@ -225,54 +203,27 @@ func (o *ImportImageOptions) Run() error {
225
203
fmt .Fprintln (o .out , info )
226
204
}
227
205
} else {
228
- fmt .Fprintf (o .errout , "error: tag %s failed: %v\n " , image .Tag , image .Status .Message )
206
+ fmt .Fprintf (o .errout , "error: repository tag %s failed: %v\n " , image .Tag , image .Status .Message )
229
207
}
230
208
}
231
-
232
- if r := result .Status .Repository ; r != nil && len (r .AdditionalTags ) > 0 {
233
- fmt .Fprintf (o .out , "\n info: The remote repository contained %d additional tags which were not imported: %s\n " , len (r .AdditionalTags ), strings .Join (r .AdditionalTags , ", " ))
234
- }
235
- return nil
236
- }
237
-
238
- // Legacy path, remove when support for older importers is removed
239
- delete (stream .Annotations , imageapi .DockerImageRepositoryCheckAnnotation )
240
- if o .Insecure != nil && * o .Insecure {
241
- if stream .Annotations == nil {
242
- stream .Annotations = make (map [string ]string )
243
- }
244
- stream .Annotations [imageapi .InsecureRepositoryAnnotation ] = "true"
245
- }
246
-
247
- if stream .CreationTimestamp .IsZero () {
248
- stream , err = o .isClient .Create (stream )
249
- } else {
250
- stream , err = o .isClient .Update (stream )
251
209
}
252
- if err != nil {
253
- return err
254
- }
255
-
256
- fmt .Fprintln (o .out , "Importing (ctrl+c to stop waiting) ..." )
257
210
258
- resourceVersion := stream .ResourceVersion
259
- updatedStream , err := o .waitForImport (resourceVersion )
260
- if err != nil {
261
- if _ , ok := err .(importError ); ok {
262
- return err
211
+ for _ , image := range result .Status .Images {
212
+ if image .Image != nil {
213
+ info , err := describe .DescribeImage (image .Image , imageapi .JoinImageStreamTag (stream .Name , image .Tag ))
214
+ if err != nil {
215
+ fmt .Fprintf (o .errout , "error: tag %s failed: %v\n " , image .Tag , err )
216
+ } else {
217
+ fmt .Fprintln (o .out , info )
218
+ }
219
+ } else {
220
+ fmt .Fprintf (o .errout , "error: tag %s failed: %v\n " , image .Tag , image .Status .Message )
263
221
}
264
- return fmt .Errorf ("unable to determine if the import completed successfully - please run '%s describe -n %s imagestream/%s' to see if the tags were updated as expected: %v" , o .CommandName , stream .Namespace , stream .Name , err )
265
222
}
266
223
267
- fmt .Fprint (o .out , "The import completed successfully.\n \n " )
268
-
269
- d := describe.ImageStreamDescriber {ImageClient : o .imageClient }
270
- info , err := d .Describe (updatedStream .Namespace , updatedStream .Name , kprinters.DescriberSettings {})
271
- if err != nil {
272
- return err
224
+ if r := result .Status .Repository ; r != nil && len (r .AdditionalTags ) > 0 {
225
+ fmt .Fprintf (o .out , "\n info: The remote repository contained %d additional tags which were not imported: %s\n " , len (r .AdditionalTags ), strings .Join (r .AdditionalTags , ", " ))
273
226
}
274
-
275
- fmt .Fprintln (o .out , info )
276
227
return nil
277
228
}
278
229
@@ -297,45 +248,6 @@ func (e importError) Error() string {
297
248
return fmt .Sprintf ("unable to import image: %s" , e .annotation )
298
249
}
299
250
300
- func (o * ImportImageOptions ) waitForImport (resourceVersion string ) (* imageapi.ImageStream , error ) {
301
- streamWatch , err := o .isClient .Watch (metav1.ListOptions {FieldSelector : fields .OneTermEqualSelector ("metadata.name" , o .Name ).String (), ResourceVersion : resourceVersion })
302
- if err != nil {
303
- return nil , err
304
- }
305
- defer streamWatch .Stop ()
306
-
307
- for {
308
- select {
309
- case event , ok := <- streamWatch .ResultChan ():
310
- if ! ok {
311
- return nil , fmt .Errorf ("image stream watch ended prematurely" )
312
- }
313
-
314
- switch event .Type {
315
- case watch .Modified :
316
- s , ok := event .Object .(* imageapi.ImageStream )
317
- if ! ok {
318
- continue
319
- }
320
- annotation , ok := s .Annotations [imageapi .DockerImageRepositoryCheckAnnotation ]
321
- if ! ok {
322
- continue
323
- }
324
-
325
- if _ , err := time .Parse (time .RFC3339 , annotation ); err == nil {
326
- return s , nil
327
- }
328
- return nil , importError {annotation }
329
-
330
- case watch .Deleted :
331
- return nil , fmt .Errorf ("the image stream was deleted" )
332
- case watch .Error :
333
- return nil , fmt .Errorf ("error watching image stream" )
334
- }
335
- }
336
- }
337
- }
338
-
339
251
func (o * ImportImageOptions ) createImageImport () (* imageapi.ImageStream , * imageapi.ImageStreamImport , error ) {
340
252
var isi * imageapi.ImageStreamImport
341
253
stream , err := o .isClient .Get (o .Name , metav1.GetOptions {})
@@ -593,27 +505,3 @@ func (o *ImportImageOptions) newImageStreamImportTags(stream *imageapi.ImageStre
593
505
}
594
506
return isi
595
507
}
596
-
597
- // TransformUnsupportedError converts specific error conditions to unsupported
598
- func TransformUnsupportedError (err error ) error {
599
- if err == nil {
600
- return nil
601
- }
602
- if errors .IsNotFound (err ) {
603
- status , ok := err .(errors.APIStatus )
604
- if ! ok {
605
- return imageapi .ErrImageStreamImportUnsupported
606
- }
607
- if status .Status ().Details == nil || status .Status ().Details .Kind == "" {
608
- return imageapi .ErrImageStreamImportUnsupported
609
- }
610
- }
611
- // The ImageStreamImport resource exists in v1.1.1 of origin but is not yet
612
- // enabled by policy. A create request will return a Forbidden(403) error.
613
- // We want to return ErrImageStreamImportUnsupported to allow fallback behavior
614
- // in clients.
615
- if errors .IsForbidden (err ) && ! quotautil .IsErrorQuotaExceeded (err ) {
616
- return imageapi .ErrImageStreamImportUnsupported
617
- }
618
- return err
619
- }
0 commit comments