Skip to content

Commit a3ebb95

Browse files
committed
Merge pull request #166 from aledbf/check-exit-code
fix(builder): if exit code from build pod is not 0 it must return an error
2 parents 13f07b1 + 26c577e commit a3ebb95

2 files changed

Lines changed: 49 additions & 11 deletions

File tree

pkg/gitreceive/build.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,24 @@ func build(conf *Config, s3Client *s3.S3, kubeClient *client.Client, builderKey,
190190
if err != nil {
191191
return fmt.Errorf("fetching builder logs (%s)", err)
192192
}
193-
log.Debug("size of logs streamed %v", size)
193+
log.Debug("size of streamed logs %v", size)
194+
195+
// check the state and exit code of the build pod.
196+
// if the code is not 0 return error
197+
if err := waitForPodEnd(kubeClient, newPod.Namespace, newPod.Name, conf.BuilderPodTickDuration(), conf.BuilderPodWaitDuration()); err != nil {
198+
return fmt.Errorf("error getting builder pod status (%s)", err)
199+
}
200+
buildPod, err := kubeClient.Pods(newPod.Namespace).Get(newPod.Name)
201+
if err != nil {
202+
return fmt.Errorf("error getting builder pod status (%s)", err)
203+
}
204+
205+
for _, containerStatus := range buildPod.Status.ContainerStatuses {
206+
state := containerStatus.State.Terminated
207+
if state.ExitCode != 0 {
208+
return fmt.Errorf("Stopping build.")
209+
}
210+
}
194211

195212
// poll the s3 server to ensure the slug exists
196213
err = wait.PollImmediate(conf.ObjectStorageTickDuration(), conf.ObjectStorageWaitDuration(), func() (bool, error) {

pkg/gitreceive/k8s_util.go

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,37 @@ func addEnvToPod(pod api.Pod, key, value string) {
147147

148148
// waitForPod waits for a pod in state running or failed
149149
func waitForPod(c *client.Client, ns, podName string, interval, timeout time.Duration) error {
150+
condition := func(pod *api.Pod) (bool, error) {
151+
if pod.Status.Phase == api.PodRunning {
152+
return true, nil
153+
}
154+
if pod.Status.Phase == api.PodFailed {
155+
return true, fmt.Errorf("Giving up; pod went into failed status: \n%s", fmt.Sprintf("%#v", pod))
156+
}
157+
return false, nil
158+
}
159+
160+
return waitForPodCondition(c, ns, podName, condition, interval, timeout)
161+
}
162+
163+
// waitForPodEnd waits for a pod in state succeeded or failed
164+
func waitForPodEnd(c *client.Client, ns, podName string, interval, timeout time.Duration) error {
165+
condition := func(pod *api.Pod) (bool, error) {
166+
if pod.Status.Phase == api.PodSucceeded {
167+
return true, nil
168+
}
169+
if pod.Status.Phase == api.PodFailed {
170+
return true, nil
171+
}
172+
return false, nil
173+
}
174+
175+
return waitForPodCondition(c, ns, podName, condition, interval, timeout)
176+
}
177+
178+
// waitForPodCondition waits for a pod in state defined by a condition (func)
179+
func waitForPodCondition(c *client.Client, ns, podName string, condition func(pod *api.Pod) (bool, error),
180+
interval, timeout time.Duration) error {
150181
return wait.PollImmediate(interval, timeout, func() (bool, error) {
151182
pod, err := c.Pods(ns).Get(podName)
152183
if err != nil {
@@ -155,16 +186,6 @@ func waitForPod(c *client.Client, ns, podName string, interval, timeout time.Dur
155186
}
156187
}
157188

158-
condition := func(pod *api.Pod) (bool, error) {
159-
if pod.Status.Phase == api.PodRunning {
160-
return true, nil
161-
}
162-
if pod.Status.Phase == api.PodFailed {
163-
return true, fmt.Errorf("Giving up; pod went into failed status: \n%s", fmt.Sprintf("%#v", pod))
164-
}
165-
return false, nil
166-
}
167-
168189
done, err := condition(pod)
169190
if done {
170191
return true, nil

0 commit comments

Comments
 (0)