Browse code

Retry build logs in start build when waiting for build

Michal Fojtik authored on 2015/12/11 18:10:18
Showing 2 changed files
... ...
@@ -30,6 +30,7 @@ import (
30 30
 	osutil "github.com/openshift/origin/pkg/cmd/util"
31 31
 	"github.com/openshift/origin/pkg/cmd/util/clientcmd"
32 32
 	"github.com/openshift/origin/pkg/generate/git"
33
+	"github.com/openshift/origin/pkg/util/errors"
33 34
 	"github.com/openshift/source-to-image/pkg/tar"
34 35
 )
35 36
 
... ...
@@ -251,19 +252,33 @@ func RunStartBuild(f *clientcmd.Factory, in io.Reader, out io.Writer, cmd *cobra
251 251
 	if follow {
252 252
 		wg.Add(1)
253 253
 		go func() {
254
-			defer wg.Done()
254
+			// if --wait option is set, then don't wait for logs to finish streaming
255
+			// but wait for the build to reach its final state
256
+			if waitForComplete {
257
+				wg.Done()
258
+			} else {
259
+				defer wg.Done()
260
+			}
255 261
 			opts := buildapi.BuildLogOptions{
256 262
 				Follow: true,
257 263
 				NoWait: false,
258 264
 			}
259
-			rd, err := client.BuildLogs(namespace).Get(newBuild.Name, opts).Stream()
260
-			if err != nil {
261
-				fmt.Fprintf(cmd.Out(), "error getting logs: %v\n", err)
262
-				return
263
-			}
264
-			defer rd.Close()
265
-			if _, err = io.Copy(out, rd); err != nil {
266
-				fmt.Fprintf(cmd.Out(), "error streaming logs: %v\n", err)
265
+			for {
266
+				rd, err := client.BuildLogs(namespace).Get(newBuild.Name, opts).Stream()
267
+				if err != nil {
268
+					// if --wait options is set, then retry the connection to build logs
269
+					// when we hit the timeout.
270
+					if waitForComplete && errors.IsTimeoutErr(err) {
271
+						continue
272
+					}
273
+					fmt.Fprintf(cmd.Out(), "error getting logs: %v\n", err)
274
+					return
275
+				}
276
+				defer rd.Close()
277
+				if _, err = io.Copy(out, rd); err != nil {
278
+					fmt.Fprintf(cmd.Out(), "error streaming logs: %v\n", err)
279
+				}
280
+				break
267 281
 			}
268 282
 		}()
269 283
 	}
... ...
@@ -2,7 +2,10 @@ package errors
2 2
 
3 3
 import "strings"
4 4
 
5
-import kapierrors "k8s.io/kubernetes/pkg/api/errors"
5
+import (
6
+	kapierrors "k8s.io/kubernetes/pkg/api/errors"
7
+	"k8s.io/kubernetes/pkg/api/unversioned"
8
+)
6 9
 
7 10
 // TolerateNotFoundError tolerates 'not found' errors
8 11
 func TolerateNotFoundError(err error) error {
... ...
@@ -25,3 +28,12 @@ func ErrorToSentence(err error) string {
25 25
 	}
26 26
 	return msg
27 27
 }
28
+
29
+// IsTimeoutErr returns true if the error indicates timeout
30
+func IsTimeoutErr(err error) bool {
31
+	e, ok := err.(*kapierrors.StatusError)
32
+	if !ok {
33
+		return false
34
+	}
35
+	return e.ErrStatus.Reason == unversioned.StatusReasonTimeout
36
+}