Skip to content

Commit

Permalink
oc import-image: poll on IS to respect resourceNames RBAC
Browse files Browse the repository at this point in the history
`watch` doesn't work with RBAC `resourceNames` as well as `get`. Changing
the `waitForImport` to poll a `get` instead.

fixes #13214
  • Loading branch information
Jan Wozniak committed May 10, 2018
1 parent 89d5bfb commit 2724ac5
Showing 1 changed file with 20 additions and 39 deletions.
59 changes: 20 additions & 39 deletions pkg/oc/cli/cmd/importimage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 2724ac5

Please sign in to comment.