6
6
7
7
kapi "k8s.io/kubernetes/pkg/api"
8
8
"k8s.io/kubernetes/pkg/api/rest"
9
+ "k8s.io/kubernetes/pkg/api/unversioned"
9
10
"k8s.io/kubernetes/pkg/fields"
10
11
11
12
"github.com/openshift/origin/pkg/build/api"
@@ -20,33 +21,44 @@ var ErrUnknownBuildPhase = fmt.Errorf("unknown build phase")
20
21
func WaitForRunningBuild (watcher rest.Watcher , ctx kapi.Context , build * api.Build , timeout time.Duration ) (* api.Build , bool , error ) {
21
22
fieldSelector := fields .OneTermEqualSelector ("metadata.name" , build .Name )
22
23
options := & kapi.ListOptions {FieldSelector : fieldSelector , ResourceVersion : build .ResourceVersion }
23
- w , err := watcher .Watch (ctx , options )
24
- if err != nil {
25
- return nil , false , err
26
- }
27
- defer w .Stop ()
28
24
29
- ch := w .ResultChan ()
30
- observed := build
31
25
expire := time .After (timeout )
32
- for {
33
- select {
34
- case event := <- ch :
35
- obj , ok := event .Object .(* api.Build )
36
- if ! ok {
37
- return observed , false , fmt .Errorf ("received unknown object while watching for builds" )
38
- }
39
- observed = obj
40
26
41
- switch obj .Status .Phase {
42
- case api .BuildPhaseRunning , api .BuildPhaseComplete , api .BuildPhaseFailed , api .BuildPhaseError , api .BuildPhaseCancelled :
43
- return observed , true , nil
44
- case api .BuildPhaseNew , api .BuildPhasePending :
45
- default :
46
- return observed , false , ErrUnknownBuildPhase
27
+ watcherLoop:
28
+ for {
29
+ w , err := watcher .Watch (ctx , options )
30
+ if err != nil {
31
+ return nil , false , err
32
+ }
33
+ defer w .Stop ()
34
+ ch := w .ResultChan ()
35
+ eventLoop:
36
+ for {
37
+ select {
38
+ case event := <- ch :
39
+ switch observed := event .Object .(type ) {
40
+ case * api.Build :
41
+ switch observed .Status .Phase {
42
+ case api .BuildPhaseRunning , api .BuildPhaseComplete , api .BuildPhaseFailed , api .BuildPhaseError , api .BuildPhaseCancelled :
43
+ // Build has started, return it.
44
+ return observed , true , nil
45
+ case api .BuildPhaseNew , api .BuildPhasePending :
46
+ // Build haven't started yet, continue waiting for more events.
47
+ continue eventLoop
48
+ default :
49
+ // Build has an unknown phase.
50
+ return observed , false , ErrUnknownBuildPhase
51
+ }
52
+ case * unversioned.Status :
53
+ if observed .Reason == unversioned .StatusReasonExpired {
54
+ // The watcher expired, need to start over with a new watcher.
55
+ continue watcherLoop
56
+ }
57
+ }
58
+ return build , false , fmt .Errorf ("received unknown object while watching for builds: %v" , event .Object )
59
+ case <- expire :
60
+ return build , false , nil
47
61
}
48
- case <- expire :
49
- return observed , false , nil
50
62
}
51
63
}
52
64
}
0 commit comments