forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
rest.go
59 lines (52 loc) · 1.78 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
package registry
import (
"fmt"
"time"
kapi "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/rest"
"k8s.io/kubernetes/pkg/fields"
"k8s.io/kubernetes/pkg/watch"
"github.com/openshift/origin/pkg/build/api"
)
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 kapi.Context, build *api.Build, timeout time.Duration) (*api.Build, bool, error) {
fieldSelector := fields.OneTermEqualSelector("metadata.name", build.Name)
options := &kapi.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.(*api.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 api.BuildPhaseRunning, api.BuildPhaseComplete, api.BuildPhaseFailed, api.BuildPhaseError, api.BuildPhaseCancelled:
return observed, true, nil
case api.BuildPhaseNew, api.BuildPhasePending:
default:
return observed, false, ErrUnknownBuildPhase
}
case <-expire:
return observed, false, nil
}
}
}