// WaitForABuild waits for a Build object to match either isOK or isFailed conditions. func WaitForABuild(c client.BuildInterface, name string, isOK, isFailed func(*buildapi.Build) bool) error { // wait 2 minutes for build to exist err := wait.Poll(1*time.Second, 2*time.Minute, func() (bool, error) { if _, err := c.Get(name); err != nil { return false, nil } return true, nil }) if err == wait.ErrWaitTimeout { return fmt.Errorf("Timed out waiting for build %q to be created", name) } if err != nil { return err } // wait longer for the build to run to completion err = wait.Poll(5*time.Second, 60*time.Minute, func() (bool, error) { list, err := c.List(kapi.ListOptions{FieldSelector: fields.Set{"name": name}.AsSelector()}) if err != nil { return false, err } for i := range list.Items { if name == list.Items[i].Name && isOK(&list.Items[i]) { return true, nil } if name != list.Items[i].Name || isFailed(&list.Items[i]) { return false, fmt.Errorf("The build %q status is %q", name, list.Items[i].Status.Phase) } } return false, nil }) if err == wait.ErrWaitTimeout { return fmt.Errorf("Timed out waiting for build %q to complete", name) } return err }
// WaitForABuild waits for a Build object to match either isOK or isFailed conditions. func WaitForABuild(c client.BuildInterface, name string, isOK, isFailed func(*buildapi.Build) bool) error { // wait 2 minutes for build to exist err := wait.Poll(1*time.Second, 2*time.Minute, func() (bool, error) { if _, err := c.Get(name); err != nil { return false, nil } return true, nil }) if err != nil { return err } // wait longer for the build to run to completion return wait.Poll(5*time.Second, 20*time.Minute, func() (bool, error) { list, err := c.List(labels.Everything(), fields.Set{"name": name}.AsSelector()) if err != nil { return false, err } for i := range list.Items { if name == list.Items[i].Name && isOK(&list.Items[i]) { return true, nil } if name != list.Items[i].Name || isFailed(&list.Items[i]) { return false, fmt.Errorf("The build %q status is %q", name, &list.Items[i].Status.Phase) } } rv := list.ResourceVersion w, err := c.Watch(labels.Everything(), fields.Set{"name": name}.AsSelector(), rv) if err != nil { return false, err } defer w.Stop() for { val, ok := <-w.ResultChan() if !ok { // reget and re-watch return false, nil } if e, ok := val.Object.(*buildapi.Build); ok { if name == e.Name && isOK(e) { return true, nil } if name != e.Name || isFailed(e) { return false, fmt.Errorf("The build %q status is %q", name, e.Status.Phase) } } } }) }
func retryBuildStatusUpdate(build *api.Build, client client.BuildInterface, sourceRev *api.SourceRevision) error { return kclient.RetryOnConflict(kclient.DefaultBackoff, func() error { // before updating, make sure we are using the latest version of the build latestBuild, err := client.Get(build.Name) if err != nil { // usually this means we failed to get resources due to the missing // privilleges return err } if sourceRev != nil { latestBuild.Spec.Revision = sourceRev latestBuild.ResourceVersion = "" } latestBuild.Status.Reason = build.Status.Reason latestBuild.Status.Message = build.Status.Message if _, err := client.UpdateDetails(latestBuild); err != nil { return err } return nil }) }