You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by lb...@apache.org on 2019/03/13 17:59:19 UTC

[camel-k] branch master updated: Timeout in S2I build #536

This is an automated email from the ASF dual-hosted git repository.

lburgazzoli pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel-k.git


The following commit(s) were added to refs/heads/master by this push:
     new eaed6b9  Timeout in S2I build #536
eaed6b9 is described below

commit eaed6b9fc41f91d8679b2edaa347f4f6b751341d
Author: lburgazzoli <lb...@gmail.com>
AuthorDate: Wed Mar 13 14:12:21 2019 +0100

    Timeout in S2I build #536
---
 .../camel/v1alpha1/integrationplatform_types.go    |  1 +
 pkg/builder/builder_steps_test.go                  | 54 ++++++++++++++++++++++
 pkg/builder/s2i/publisher.go                       |  4 +-
 pkg/cmd/install.go                                 | 12 +++++
 pkg/controller/integrationplatform/initialize.go   |  5 ++
 pkg/util/kubernetes/wait.go                        | 13 ++++--
 test/builder_integration_test.go                   | 19 +++++---
 7 files changed, 96 insertions(+), 12 deletions(-)

diff --git a/pkg/apis/camel/v1alpha1/integrationplatform_types.go b/pkg/apis/camel/v1alpha1/integrationplatform_types.go
index 41e2daf..134017f 100644
--- a/pkg/apis/camel/v1alpha1/integrationplatform_types.go
+++ b/pkg/apis/camel/v1alpha1/integrationplatform_types.go
@@ -79,6 +79,7 @@ type IntegrationPlatformBuildSpec struct {
 	LocalRepository string                                  `json:"localRepository,omitempty"`
 	Repositories    []string                                `json:"repositories,omitempty"`
 	Registry        IntegrationPlatformRegistrySpec         `json:"registry,omitempty"`
+	Timeout         metav1.Duration                         `json:"timeout,omitempty"`
 }
 
 // IntegrationPlatformRegistrySpec --
diff --git a/pkg/builder/builder_steps_test.go b/pkg/builder/builder_steps_test.go
index f85a80e..e85bc55 100644
--- a/pkg/builder/builder_steps_test.go
+++ b/pkg/builder/builder_steps_test.go
@@ -18,9 +18,12 @@ limitations under the License.
 package builder
 
 import (
+	"errors"
+	"sync"
 	"testing"
 
 	"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
+	"github.com/apache/camel-k/pkg/util/cancellable"
 	"github.com/apache/camel-k/pkg/util/defaults"
 	"github.com/apache/camel-k/pkg/util/maven"
 	"github.com/apache/camel-k/pkg/util/test"
@@ -292,3 +295,54 @@ func TestSanitizeDependencies(t *testing.T) {
 		Type:       "jar",
 	})
 }
+
+func TestFailure(t *testing.T) {
+	catalog, err := test.DefaultCatalog()
+	assert.Nil(t, err)
+
+	b := New(nil, "ns")
+
+	var wg sync.WaitGroup
+	wg.Add(1)
+
+	r := Request{
+		C:              cancellable.NewContext(),
+		Catalog:        catalog,
+		RuntimeVersion: defaults.RuntimeVersion,
+		Steps: []Step{
+			NewStep("step1", InitPhase, func(i *Context) error {
+				return nil
+			}),
+			NewStep("step2", ApplicationPublishPhase, func(i *Context) error {
+				return errors.New("an error")
+			}),
+		},
+		Platform: v1alpha1.IntegrationPlatformSpec{
+			Build: v1alpha1.IntegrationPlatformBuildSpec{
+				CamelVersion: catalog.Version,
+			},
+		},
+	}
+
+	var res *Result
+
+	b.Submit(r, func(result *Result) {
+		switch result.Status {
+		case StatusError:
+			res = result
+			wg.Done()
+		case StatusCompleted:
+			res = result
+			wg.Done()
+		case StatusInterrupted:
+			res = result
+			wg.Done()
+		}
+
+	})
+
+	wg.Wait()
+
+	assert.NotNil(t, res)
+	assert.Equal(t, StatusError, res.Status)
+}
diff --git a/pkg/builder/s2i/publisher.go b/pkg/builder/s2i/publisher.go
index 96d80b7..010a2d5 100644
--- a/pkg/builder/s2i/publisher.go
+++ b/pkg/builder/s2i/publisher.go
@@ -19,7 +19,6 @@ package s2i
 
 import (
 	"io/ioutil"
-	"time"
 
 	"k8s.io/apimachinery/pkg/util/json"
 	k8sclient "sigs.k8s.io/controller-runtime/pkg/client"
@@ -151,7 +150,8 @@ func Publisher(ctx *builder.Context) error {
 			}
 		}
 		return false, nil
-	}, 5*time.Minute)
+	}, ctx.Request.Platform.Build.Timeout.Duration)
+
 	if err != nil {
 		return err
 	}
diff --git a/pkg/cmd/install.go b/pkg/cmd/install.go
index b4a2722..2f2e366 100644
--- a/pkg/cmd/install.go
+++ b/pkg/cmd/install.go
@@ -20,6 +20,7 @@ package cmd
 import (
 	"fmt"
 	"strings"
+	"time"
 
 	"github.com/apache/camel-k/deploy"
 	"github.com/apache/camel-k/pkg/apis"
@@ -35,6 +36,7 @@ import (
 	"github.com/apache/camel-k/pkg/util/kubernetes"
 	"github.com/pkg/errors"
 	"github.com/spf13/cobra"
+
 	k8serrors "k8s.io/apimachinery/pkg/api/errors"
 )
 
@@ -68,6 +70,7 @@ func newCmdInstall(rootCmdOptions *RootCmdOptions) *cobra.Command {
 	cmd.Flags().StringVar(&impl.runtimeVersion, "runtime-version", "", "Set the camel-k runtime version")
 	cmd.Flags().StringVar(&impl.baseImage, "base-image", "", "Set the base image used to run integrations")
 	cmd.Flags().StringSliceVar(&impl.contexts, "context", nil, "Add a camel context to build at startup, by default all known contexts are built")
+	cmd.Flags().StringVar(&impl.buildTimeout, "build-timeout", "", "Set how long the build process can last")
 
 	// completion support
 	configureBashAnnotationForFlag(
@@ -93,6 +96,7 @@ type installCmdOptions struct {
 	runtimeVersion       string
 	baseImage            string
 	localRepository      string
+	buildTimeout         string
 	repositories         []string
 	properties           []string
 	contexts             []string
@@ -182,6 +186,14 @@ func (o *installCmdOptions) install(_ *cobra.Command, _ []string) error {
 		if o.baseImage != "" {
 			platform.Spec.Build.BaseImage = o.baseImage
 		}
+		if o.buildTimeout != "" {
+			d, err := time.ParseDuration(o.buildTimeout)
+			if err != nil {
+				return err
+			}
+
+			platform.Spec.Build.Timeout.Duration = d
+		}
 
 		platform.Spec.Resources.Contexts = o.contexts
 
diff --git a/pkg/controller/integrationplatform/initialize.go b/pkg/controller/integrationplatform/initialize.go
index fe84775..59aead4 100644
--- a/pkg/controller/integrationplatform/initialize.go
+++ b/pkg/controller/integrationplatform/initialize.go
@@ -19,6 +19,7 @@ package integrationplatform
 
 import (
 	"context"
+	"time"
 
 	"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
 	"github.com/apache/camel-k/pkg/platform"
@@ -104,11 +105,15 @@ func (action *initializeAction) Handle(ctx context.Context, ip *v1alpha1.Integra
 	if target.Spec.Build.LocalRepository == "" {
 		target.Spec.Build.LocalRepository = defaults.LocalRepository
 	}
+	if target.Spec.Build.Timeout.Duration == 0 {
+		target.Spec.Build.Timeout.Duration = 5 * time.Minute
+	}
 
 	action.L.Infof("CamelVersion set to %s", target.Spec.Build.CamelVersion)
 	action.L.Infof("RuntimeVersion set to %s", target.Spec.Build.RuntimeVersion)
 	action.L.Infof("BaseImage set to %s", target.Spec.Build.BaseImage)
 	action.L.Infof("LocalRepository set to %s", target.Spec.Build.LocalRepository)
+	action.L.Infof("Timeout set to %s", target.Spec.Build.Timeout)
 
 	for i, r := range target.Spec.Build.Repositories {
 		if i == 0 {
diff --git a/pkg/util/kubernetes/wait.go b/pkg/util/kubernetes/wait.go
index e4895d7..5c6129b 100644
--- a/pkg/util/kubernetes/wait.go
+++ b/pkg/util/kubernetes/wait.go
@@ -24,6 +24,8 @@ import (
 	"github.com/apache/camel-k/pkg/client"
 	"github.com/pkg/errors"
 	"k8s.io/apimachinery/pkg/runtime"
+
+	k8serrors "k8s.io/apimachinery/pkg/api/errors"
 	k8sclient "sigs.k8s.io/controller-runtime/pkg/client"
 )
 
@@ -47,14 +49,19 @@ func WaitCondition(ctx context.Context, c client.Client, obj runtime.Object, con
 	for start.Add(maxDuration).After(time.Now()) {
 		err := c.Get(ctx, key, obj)
 		if err != nil {
-			time.Sleep(sleepTime)
-			continue
+			if k8serrors.IsNotFound(err) {
+				time.Sleep(sleepTime)
+				continue
+			}
+
+			return err
 		}
 
 		satisfied, err := condition(obj)
 		if err != nil {
 			return errors.Wrap(err, "error while evaluating condition")
-		} else if !satisfied {
+		}
+		if !satisfied {
 			time.Sleep(sleepTime)
 			continue
 		}
diff --git a/test/builder_integration_test.go b/test/builder_integration_test.go
index 293724c..bad83a3 100644
--- a/test/builder_integration_test.go
+++ b/test/builder_integration_test.go
@@ -26,17 +26,16 @@ import (
 	"testing"
 	"time"
 
+	"github.com/apache/camel-k/pkg/util/cancellable"
 	"github.com/apache/camel-k/pkg/util/defaults"
-
 	"github.com/apache/camel-k/pkg/util/test"
 
-	"github.com/apache/camel-k/pkg/util/cancellable"
-
-	"k8s.io/apimachinery/pkg/apis/meta/v1"
-
 	"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
 	"github.com/apache/camel-k/pkg/builder"
 	"github.com/apache/camel-k/pkg/builder/s2i"
+
+	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
+
 	"github.com/stretchr/testify/assert"
 )
 
@@ -67,7 +66,7 @@ func TestBuildManagerBuild(t *testing.T) {
 		C:              cancellable.NewContext(),
 		Catalog:        catalog,
 		RuntimeVersion: defaults.RuntimeVersion,
-		Meta: v1.ObjectMeta{
+		Meta: metav1.ObjectMeta{
 			Name:            "man-test",
 			ResourceVersion: "1",
 		},
@@ -76,6 +75,9 @@ func TestBuildManagerBuild(t *testing.T) {
 				CamelVersion:   catalog.Version,
 				RuntimeVersion: defaults.RuntimeVersion,
 				BaseImage:      "docker.io/fabric8/s2i-java:3.0-java8",
+				Timeout: metav1.Duration{
+					Duration: 5 * time.Minute,
+				},
 			},
 		},
 		Dependencies: []string{
@@ -115,7 +117,7 @@ func TestBuildManagerFailedBuild(t *testing.T) {
 		C:              cancellable.NewContext(),
 		Catalog:        catalog,
 		RuntimeVersion: defaults.RuntimeVersion,
-		Meta: v1.ObjectMeta{
+		Meta: metav1.ObjectMeta{
 			Name:            "man-test",
 			ResourceVersion: "1",
 		},
@@ -124,6 +126,9 @@ func TestBuildManagerFailedBuild(t *testing.T) {
 				CamelVersion:   catalog.Version,
 				RuntimeVersion: defaults.RuntimeVersion,
 				BaseImage:      "docker.io/fabric8/s2i-java:3.0-java8",
+				Timeout: metav1.Duration{
+					Duration: 5 * time.Minute,
+				},
 			},
 		},
 		Dependencies: []string{