Browse code

Ensure build pod name annotation is set

Cesar Wong authored on 2016/07/26 02:08:01
Showing 2 changed files
... ...
@@ -205,10 +205,7 @@ func (bc *BuildController) nextBuildPhase(build *buildapi.Build) error {
205 205
 		build.Status.Reason = buildapi.StatusReasonCannotCreateBuildPod
206 206
 		return fmt.Errorf("failed to create build pod: %v", err)
207 207
 	}
208
-	if build.Annotations == nil {
209
-		build.Annotations = make(map[string]string)
210
-	}
211
-	build.Annotations[buildapi.BuildPodNameAnnotation] = podSpec.Name
208
+	setBuildPodNameAnnotation(build, podSpec.Name)
212 209
 	glog.V(4).Infof("Created pod for build: %#v", podSpec)
213 210
 
214 211
 	// Set the build phase, which will be persisted.
... ...
@@ -326,7 +323,8 @@ func (bc *BuildPodController) HandlePod(pod *kapi.Pod) error {
326 326
 
327 327
 	// Update the build object when it progress to a next state or the reason for
328 328
 	// the current state changed.
329
-	if build.Status.Phase != nextStatus && !buildutil.IsBuildComplete(build) {
329
+	if (!hasBuildPodNameAnnotation(build) || build.Status.Phase != nextStatus) && !buildutil.IsBuildComplete(build) {
330
+		setBuildPodNameAnnotation(build, pod.Name)
330 331
 		reason := ""
331 332
 		if len(build.Status.Reason) > 0 {
332 333
 			reason = " (" + string(build.Status.Reason) + ")"
... ...
@@ -441,3 +439,20 @@ func buildKey(pod *kapi.Pod) *buildapi.Build {
441 441
 		},
442 442
 	}
443 443
 }
444
+
445
+func hasBuildPodNameAnnotation(build *buildapi.Build) bool {
446
+	if build.Annotations == nil {
447
+		return false
448
+	}
449
+	_, hasAnnotation := build.Annotations[buildapi.BuildPodNameAnnotation]
450
+	return hasAnnotation
451
+}
452
+
453
+func setBuildPodNameAnnotation(build *buildapi.Build, podName string) {
454
+	if build.Annotations == nil {
455
+		build.Annotations = map[string]string{}
456
+	}
457
+	if _, hasAnnotation := build.Annotations[buildapi.BuildPodNameAnnotation]; !hasAnnotation {
458
+		build.Annotations[buildapi.BuildPodNameAnnotation] = podName
459
+	}
460
+}
... ...
@@ -542,7 +542,9 @@ func TestHandlePod(t *testing.T) {
542 542
 		if build.Status.Phase != tc.outStatus {
543 543
 			t.Errorf("(%d) Expected %s, got %s!", i, tc.outStatus, build.Status.Phase)
544 544
 		}
545
-
545
+		if tc.inStatus != buildapi.BuildPhaseCancelled && tc.inStatus != buildapi.BuildPhaseComplete && !hasBuildPodNameAnnotation(build) {
546
+			t.Errorf("(%d) Build does not have pod name annotation.", i)
547
+		}
546 548
 		if tc.startTimestamp == nil && build.Status.StartTimestamp != nil {
547 549
 			t.Errorf("(%d) Expected nil start timestamp, got %v!", i, build.Status.StartTimestamp)
548 550
 		}