forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
rest.go
60 lines (53 loc) · 1.95 KB
/
rest.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package registry
import (
"fmt"
"time"
metainternal "k8s.io/apimachinery/pkg/apis/meta/internalversion"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/watch"
apirequest "k8s.io/apiserver/pkg/endpoints/request"
"k8s.io/apiserver/pkg/registry/rest"
buildapi "github.com/openshift/origin/pkg/build/apis/build"
)
var (
// ErrUnknownBuildPhase is returned for WaitForRunningBuild if an unknown phase is returned.
ErrUnknownBuildPhase = fmt.Errorf("unknown build phase")
ErrBuildDeleted = fmt.Errorf("build was deleted")
)
// WaitForRunningBuild waits until the specified build is no longer New or Pending. Returns true if
// the build ran within timeout, false if it did not, and an error if any other error state occurred.
// The last observed Build state is returned.
func WaitForRunningBuild(watcher rest.Watcher, ctx apirequest.Context, build *buildapi.Build, timeout time.Duration) (*buildapi.Build, bool, error) {
fieldSelector := fields.OneTermEqualSelector("metadata.name", build.Name)
options := &metainternal.ListOptions{FieldSelector: fieldSelector, ResourceVersion: build.ResourceVersion}
w, err := watcher.Watch(ctx, options)
if err != nil {
return build, false, err
}
defer w.Stop()
observed := build
ch := w.ResultChan()
expire := time.After(timeout)
for {
select {
case event := <-ch:
obj, ok := event.Object.(*buildapi.Build)
if !ok {
return observed, false, fmt.Errorf("received unknown object while watching for builds")
}
observed = obj
if event.Type == watch.Deleted {
return observed, false, ErrBuildDeleted
}
switch obj.Status.Phase {
case buildapi.BuildPhaseRunning, buildapi.BuildPhaseComplete, buildapi.BuildPhaseFailed, buildapi.BuildPhaseError, buildapi.BuildPhaseCancelled:
return observed, true, nil
case buildapi.BuildPhaseNew, buildapi.BuildPhasePending:
default:
return observed, false, ErrUnknownBuildPhase
}
case <-expire:
return observed, false, nil
}
}
}