@@ -183,40 +183,56 @@ func (bc *BuildPodController) HandlePod(pod *kapi.Pod) error {
183
183
build .Status .Message = ""
184
184
}
185
185
}
186
+
187
+ needsUpdate := false
186
188
// Update the build object when it progress to a next state or the reason for
187
- // the current state changed.
188
- if (! common .HasBuildPodNameAnnotation (build ) || build .Status .Phase != nextStatus || build .Status .Phase == buildapi .BuildPhaseFailed ) || ! buildutil .IsBuildComplete (build ) {
189
+ // the current state changed. Do not touch builds that are complete
190
+ // because we'd potentially be overwriting build state information set by the
191
+ // build pod directly.
192
+ if (! common .HasBuildPodNameAnnotation (build ) || build .Status .Phase != nextStatus ) && ! buildutil .IsBuildComplete (build ) {
193
+ needsUpdate = true
189
194
common .SetBuildPodNameAnnotation (build , pod .Name )
190
195
reason := ""
191
196
if len (build .Status .Reason ) > 0 {
192
197
reason = " (" + string (build .Status .Reason ) + ")"
193
198
}
194
199
glog .V (4 ).Infof ("Updating build %s/%s status %s -> %s%s" , build .Namespace , build .Name , build .Status .Phase , nextStatus , reason )
195
200
build .Status .Phase = nextStatus
196
-
197
- if buildutil .IsBuildComplete (build ) {
198
- common .SetBuildCompletionTimeAndDuration (build )
199
- switch build .Status .Phase {
200
- case buildapi .BuildPhaseComplete :
201
- bc .recorder .Eventf (build , kapi .EventTypeNormal , buildapi .BuildCompletedEventReason , fmt .Sprintf (buildapi .BuildCompletedEventMessage , build .Namespace , build .Name ))
202
- case buildapi .BuildPhaseError , buildapi .BuildPhaseFailed :
203
- bc .recorder .Eventf (build , kapi .EventTypeNormal , buildapi .BuildFailedEventReason , fmt .Sprintf (buildapi .BuildFailedEventMessage , build .Namespace , build .Name ))
204
- }
205
- }
206
201
if build .Status .Phase == buildapi .BuildPhaseRunning {
207
202
now := unversioned .Now ()
208
203
build .Status .StartTimestamp = & now
209
204
bc .recorder .Eventf (build , kapi .EventTypeNormal , buildapi .BuildRunningEventReason , fmt .Sprintf (buildapi .BuildRunningEventMessage , build .Namespace , build .Name ))
210
205
}
206
+ }
207
+
208
+ // we're going to get pod relist events for completed/failed pods,
209
+ // there's no reason to re-update the build and rerun
210
+ // HandleBuildCompletion if we've already done it for this
211
+ // build previously.
212
+ buildWasComplete := build .Status .CompletionTimestamp != nil
213
+ if ! buildWasComplete && buildutil .IsBuildComplete (build ) && build .Status .Phase != buildapi .BuildPhaseCancelled {
214
+ needsUpdate = common .SetBuildCompletionTimeAndDuration (build )
215
+ }
216
+ if needsUpdate {
211
217
if err := bc .buildUpdater .Update (build .Namespace , build ); err != nil {
212
218
return fmt .Errorf ("failed to update build %s/%s: %v" , build .Namespace , build .Name , err )
213
219
}
214
220
glog .V (4 ).Infof ("Build %s/%s status was updated to %s" , build .Namespace , build .Name , build .Status .Phase )
215
-
216
- if buildutil .IsBuildComplete (build ) {
217
- common .HandleBuildCompletion (build , bc .runPolicies )
218
- }
219
221
}
222
+ // if the build was not previously marked complete but it's complete now,
223
+ // handle completion for it. otherwise ignore it because we've already
224
+ // handled its completion previously.
225
+ if ! buildWasComplete && buildutil .IsBuildComplete (build ) {
226
+ common .SetBuildCompletionTimeAndDuration (build )
227
+ switch build .Status .Phase {
228
+ case buildapi .BuildPhaseComplete :
229
+ bc .recorder .Eventf (build , kapi .EventTypeNormal , buildapi .BuildCompletedEventReason , fmt .Sprintf (buildapi .BuildCompletedEventMessage , build .Namespace , build .Name ))
230
+ case buildapi .BuildPhaseError , buildapi .BuildPhaseFailed :
231
+ bc .recorder .Eventf (build , kapi .EventTypeNormal , buildapi .BuildFailedEventReason , fmt .Sprintf (buildapi .BuildFailedEventMessage , build .Namespace , build .Name ))
232
+ }
233
+ common .HandleBuildCompletion (build , bc .runPolicies )
234
+ }
235
+
220
236
return nil
221
237
}
222
238
@@ -260,6 +276,7 @@ func (bc *BuildPodController) HandleBuildPodDeletion(pod *kapi.Pod) error {
260
276
if err := bc .buildUpdater .Update (build .Namespace , build ); err != nil {
261
277
return fmt .Errorf ("Failed to update build %s/%s: %v" , build .Namespace , build .Name , err )
262
278
}
279
+ common .HandleBuildCompletion (build , bc .runPolicies )
263
280
}
264
281
return nil
265
282
}
0 commit comments