diff --git a/pkg/oc/cli/cmd/importimage.go b/pkg/oc/cli/cmd/importimage.go index f5ed7547b473..60551c69c46f 100644 --- a/pkg/oc/cli/cmd/importimage.go +++ b/pkg/oc/cli/cmd/importimage.go @@ -10,8 +10,7 @@ import ( "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/fields" - "k8s.io/apimachinery/pkg/watch" + "k8s.io/apimachinery/pkg/util/wait" "k8s.io/kubernetes/pkg/api/legacyscheme" kapi "k8s.io/kubernetes/pkg/apis/core" "k8s.io/kubernetes/pkg/kubectl/cmd/templates" @@ -256,8 +255,7 @@ func (o *ImportImageOptions) Run() error { fmt.Fprintln(o.out, "Importing (ctrl+c to stop waiting) ...") - resourceVersion := stream.ResourceVersion - updatedStream, err := o.waitForImport(resourceVersion) + updatedStream, err := o.waitForImport() if err != nil { if _, ok := err.(importError); ok { return err @@ -298,43 +296,26 @@ func (e importError) Error() string { return fmt.Sprintf("unable to import image: %s", e.annotation) } -func (o *ImportImageOptions) waitForImport(resourceVersion string) (*imageapi.ImageStream, error) { - streamWatch, err := o.isClient.Watch(metav1.ListOptions{FieldSelector: fields.OneTermEqualSelector("metadata.name", o.Name).String(), ResourceVersion: resourceVersion}) - if err != nil { - return nil, err - } - defer streamWatch.Stop() - - for { - select { - case event, ok := <-streamWatch.ResultChan(): - if !ok { - return nil, fmt.Errorf("image stream watch ended prematurely") - } - - switch event.Type { - case watch.Modified: - s, ok := event.Object.(*imageapi.ImageStream) - if !ok { - continue - } - annotation, ok := s.Annotations[imageapi.DockerImageRepositoryCheckAnnotation] - if !ok { - continue - } - - if _, err := time.Parse(time.RFC3339, annotation); err == nil { - return s, nil - } - return nil, importError{annotation} +func (o *ImportImageOptions) waitForImport() (*imageapi.ImageStream, error) { + var is *imageapi.ImageStream + err := wait.PollImmediate(1*time.Second, 60*time.Second, func() (bool, error) { + var err error + is, err = o.isClient.Get(o.Name, metav1.GetOptions{}) + if err != nil { + return false, err + } + annotation, ok := is.Annotations[imageapi.DockerImageRepositoryCheckAnnotation] + if !ok { + return false, nil + } - case watch.Deleted: - return nil, fmt.Errorf("the image stream was deleted") - case watch.Error: - return nil, fmt.Errorf("error watching image stream") - } + if _, err := time.Parse(time.RFC3339, annotation); err != nil { + return false, importError{annotation} } - } + + return true, nil + }) + return is, err } func (o *ImportImageOptions) createImageImport() (*imageapi.ImageStream, *imageapi.ImageStreamImport, error) {