From 43474fef685fef91b180354dcc840890de9be0c2 Mon Sep 17 00:00:00 2001 From: Jan Wozniak Date: Thu, 10 May 2018 11:33:35 +0200 Subject: [PATCH] oc import-image: poll on IS to respect resourceNames RBAC `watch` doesn't work with RBAC `resourceNames` as well as `get`. Changing the `waitForImport` to poll a `get` instead. fixes https://github.com/openshift/origin/issues/13214 --- pkg/oc/cli/cmd/importimage.go | 55 +++++++++++++---------------------- 1 file changed, 20 insertions(+), 35 deletions(-) diff --git a/pkg/oc/cli/cmd/importimage.go b/pkg/oc/cli/cmd/importimage.go index f5ed7547b473..d77ddc91ddc4 100644 --- a/pkg/oc/cli/cmd/importimage.go +++ b/pkg/oc/cli/cmd/importimage.go @@ -3,6 +3,7 @@ package cmd import ( "fmt" "io" + "k8s.io/apimachinery/pkg/util/wait" "strings" "time" @@ -10,8 +11,6 @@ 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/kubernetes/pkg/api/legacyscheme" kapi "k8s.io/kubernetes/pkg/apis/core" "k8s.io/kubernetes/pkg/kubectl/cmd/templates" @@ -299,42 +298,28 @@ func (e importError) Error() string { } 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") + var is *imageapi.ImageStream + err := wait.Poll(1*time.Second, 60*time.Second, func() (bool, error) { + var err error + is, err = o.isClient.Get(o.Name, metav1.GetOptions{ResourceVersion: resourceVersion}) + if err != nil { + if errors.IsNotFound(err) { + return false, nil } + return false, err + } + annotation, ok := is.Annotations[imageapi.DockerImageRepositoryCheckAnnotation] + if !ok { + return false, nil + } - 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} - - 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) {