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

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

commit 5f469ca3c809f22c3426618b63181ffa91ccc4b2
Author: lburgazzoli <lburgazz...@gmail.com>
AuthorDate: Fri Jun 21 14:11:21 2019 +0200

    Add timeout to maven operations #761
---
 Gopkg.lock                                       |  1 +
 pkg/apis/camel/v1alpha1/common_types.go          |  3 +-
 pkg/apis/camel/v1alpha1/zz_generated.deepcopy.go |  1 +
 pkg/builder/builder_steps.go                     |  6 +--
 pkg/builder/builder_types.go                     |  1 -
 pkg/controller/integrationplatform/initialize.go |  8 ++++
 pkg/trait/camel.go                               |  5 ++-
 pkg/trait/rest-dsl.go                            |  5 ++-
 pkg/util/maven/maven.go                          | 48 +++++++++++++-----------
 pkg/util/maven/maven_types.go                    |  6 ++-
 10 files changed, 51 insertions(+), 33 deletions(-)

diff --git a/Gopkg.lock b/Gopkg.lock
index f5f1eac..4e74fdb 100644
--- a/Gopkg.lock
+++ b/Gopkg.lock
@@ -1173,6 +1173,7 @@
     "k8s.io/api/core/v1",
     "k8s.io/api/extensions/v1beta1",
     "k8s.io/api/rbac/v1",
+    "k8s.io/api/rbac/v1beta1",
     "k8s.io/apimachinery/pkg/api/errors",
     "k8s.io/apimachinery/pkg/api/meta",
     "k8s.io/apimachinery/pkg/api/resource",
diff --git a/pkg/apis/camel/v1alpha1/common_types.go 
b/pkg/apis/camel/v1alpha1/common_types.go
index 4a927dd..00a45d1 100644
--- a/pkg/apis/camel/v1alpha1/common_types.go
+++ b/pkg/apis/camel/v1alpha1/common_types.go
@@ -75,7 +75,8 @@ type Configurable interface {
 
 // MavenSpec --
 type MavenSpec struct {
-       Settings ValueSource `json:"settings,omitempty"`
+       Settings ValueSource     `json:"settings,omitempty"`
+       Timeout  metav1.Duration `json:"timeout,omitempty"`
 }
 
 // ValueSource --
diff --git a/pkg/apis/camel/v1alpha1/zz_generated.deepcopy.go 
b/pkg/apis/camel/v1alpha1/zz_generated.deepcopy.go
index 60e0f48..a0f5962 100644
--- a/pkg/apis/camel/v1alpha1/zz_generated.deepcopy.go
+++ b/pkg/apis/camel/v1alpha1/zz_generated.deepcopy.go
@@ -902,6 +902,7 @@ func (in *IntegrationStatus) DeepCopy() *IntegrationStatus {
 func (in *MavenSpec) DeepCopyInto(out *MavenSpec) {
        *out = *in
        in.Settings.DeepCopyInto(&out.Settings)
+       out.Timeout = in.Timeout
        return
 }
 
diff --git a/pkg/builder/builder_steps.go b/pkg/builder/builder_steps.go
index 32d0890..1d3499a 100644
--- a/pkg/builder/builder_steps.go
+++ b/pkg/builder/builder_steps.go
@@ -264,9 +264,9 @@ func sanitizeDependencies(ctx *Context) error {
 
 func computeDependencies(ctx *Context) error {
        mc := maven.NewContext(path.Join(ctx.Path, "maven"), ctx.Maven.Project)
-       mc.Settings = ctx.Maven.Settings
-       mc.SettingsData = ctx.Maven.SettingsData
-       
mc.AddArguments(maven.ExtraOptions(ctx.Build.Platform.Build.LocalRepository)...)
+       mc.SettingsContent = ctx.Maven.SettingsData
+       mc.LocalRepository = ctx.Build.Platform.Build.LocalRepository
+       mc.Timeout = ctx.Build.Platform.Build.Maven.Timeout.Duration
        
mc.AddArgumentf("org.apache.camel.k:camel-k-maven-plugin:%s:generate-dependency-list",
 ctx.Build.RuntimeVersion)
 
        if err := maven.Run(mc); err != nil {
diff --git a/pkg/builder/builder_types.go b/pkg/builder/builder_types.go
index 1ad1a19..7574b07 100644
--- a/pkg/builder/builder_types.go
+++ b/pkg/builder/builder_types.go
@@ -115,7 +115,6 @@ type Context struct {
 
        Maven struct {
                Project      maven.Project
-               Settings     *maven.Settings
                SettingsData []byte
        }
 }
diff --git a/pkg/controller/integrationplatform/initialize.go 
b/pkg/controller/integrationplatform/initialize.go
index 6976165..64eeb93 100644
--- a/pkg/controller/integrationplatform/initialize.go
+++ b/pkg/controller/integrationplatform/initialize.go
@@ -132,12 +132,20 @@ func (action *initializeAction) Handle(ctx 
context.Context, ip *v1alpha1.Integra
        if target.Spec.Build.PersistentVolumeClaim == "" {
                target.Spec.Build.PersistentVolumeClaim = target.Name
        }
+       if target.Spec.Build.Maven.Timeout.Duration == 0 {
+               target.Spec.Build.Maven.Timeout.Duration = 2 * 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)
+       action.L.Infof("Maven Timeout set to %s", 
target.Spec.Build.Maven.Timeout.Duration)
+
+       if target.Spec.Build.Maven.Timeout.Duration != 0 {
+               action.L.Infof("Maven Timeout set to %s", 
target.Spec.Build.Maven.Timeout.Duration)
+       }
 
        err = action.client.Update(ctx, target)
        if err != nil {
diff --git a/pkg/trait/camel.go b/pkg/trait/camel.go
index f5564e9..03e0b25 100644
--- a/pkg/trait/camel.go
+++ b/pkg/trait/camel.go
@@ -142,7 +142,8 @@ func (t *camelTrait) GenerateCatalog(e *Environment, 
version string) (*camel.Run
        }
 
        mc := maven.NewContext(tmpDir, project)
-       
mc.AddArguments(maven.ExtraOptions(e.Platform.Spec.Build.LocalRepository)...)
+       mc.LocalRepository = e.Platform.Spec.Build.LocalRepository
+       mc.Timeout = e.Platform.Spec.Build.Maven.Timeout.Duration
        mc.AddSystemProperty("catalog.path", tmpDir)
        mc.AddSystemProperty("catalog.file", "catalog.yaml")
 
@@ -156,7 +157,7 @@ func (t *camelTrait) GenerateCatalog(e *Environment, 
version string) (*camel.Run
                return nil, err
        }
        if settings != "" {
-               mc.SettingsData = []byte(settings)
+               mc.SettingsContent = []byte(settings)
        }
 
        err = maven.Run(mc)
diff --git a/pkg/trait/rest-dsl.go b/pkg/trait/rest-dsl.go
index 6e9778d..ab1789d 100644
--- a/pkg/trait/rest-dsl.go
+++ b/pkg/trait/rest-dsl.go
@@ -113,7 +113,8 @@ func (t *restDslTrait) Apply(e *Environment) error {
                }
 
                mc := maven.NewContext(tmpDir, project)
-               
mc.AddArguments(maven.ExtraOptions(e.Platform.Spec.Build.LocalRepository)...)
+               mc.LocalRepository = e.Platform.Spec.Build.LocalRepository
+               mc.Timeout = e.Platform.Spec.Build.Maven.Timeout.Duration
                mc.AddArgument("-Dopenapi.spec=" + in)
                mc.AddArgument("-Ddsl.out=" + out)
 
@@ -122,7 +123,7 @@ func (t *restDslTrait) Apply(e *Environment) error {
                        return err
                }
                if settings != "" {
-                       mc.SettingsData = []byte(settings)
+                       mc.SettingsContent = []byte(settings)
                }
 
                err = maven.Run(mc)
diff --git a/pkg/util/maven/maven.go b/pkg/util/maven/maven.go
index 3e28d18..757df37 100644
--- a/pkg/util/maven/maven.go
+++ b/pkg/util/maven/maven.go
@@ -18,9 +18,11 @@ limitations under the License.
 package maven
 
 import (
+       "context"
        "fmt"
        "io"
        "io/ioutil"
+       "math"
        "os"
        "os/exec"
        "path"
@@ -42,12 +44,8 @@ func GenerateProjectStructure(context Context) error {
                return err
        }
 
-       if context.SettingsData != nil {
-               if err := util.WriteFileWithContent(context.Path, 
"settings.xml", context.SettingsData); err != nil {
-                       return err
-               }
-       } else if context.Settings != nil {
-               if err := 
util.WriteFileWithBytesMarshallerContent(context.Path, "settings.xml", 
context.Settings); err != nil {
+       if context.SettingsContent != nil {
+               if err := util.WriteFileWithContent(context.Path, 
"settings.xml", context.SettingsContent); err != nil {
                        return err
                }
        }
@@ -81,8 +79,8 @@ func GenerateProjectStructure(context Context) error {
 }
 
 // Run --
-func Run(context Context) error {
-       if err := GenerateProjectStructure(context); err != nil {
+func Run(ctx Context) error {
+       if err := GenerateProjectStructure(ctx); err != nil {
                return err
        }
 
@@ -91,9 +89,15 @@ func Run(context Context) error {
                mvnCmd = c
        }
 
-       args := append(context.AdditionalArguments, "--batch-mode")
+       args := append(ctx.AdditionalArguments, "--batch-mode")
+
+       if ctx.LocalRepository == "" {
+               args = append(ctx.AdditionalArguments, "-Dcamel.noop=true")
+       } else if _, err := os.Stat(ctx.LocalRepository); err == nil {
+               args = append(ctx.AdditionalArguments, 
"-Dmaven.repo.local="+ctx.LocalRepository)
+       }
 
-       settingsPath := path.Join(context.Path, "settings.xml")
+       settingsPath := path.Join(ctx.Path, "settings.xml")
        settingsExists, err := util.FileExists(settingsPath)
        if err != nil {
                return err
@@ -103,12 +107,20 @@ func Run(context Context) error {
                args = append(args, "--settings", settingsPath)
        }
 
-       cmd := exec.Command(mvnCmd, args...)
-       cmd.Dir = context.Path
-       cmd.Stdout = os.Stdout
+       timeout := ctx.Timeout
+       if timeout == 0 {
+               timeout = math.MaxInt64
+       }
+
+       c, cancel := context.WithTimeout(context.Background(), timeout)
+       defer cancel()
+
+       cmd := exec.CommandContext(c, mvnCmd, args...)
+       cmd.Dir = ctx.Path
        cmd.Stderr = os.Stderr
+       cmd.Stdout = os.Stdout
 
-       Log.Infof("execute: %s", strings.Join(cmd.Args, " "))
+       Log.WithValues("timeout", timeout.String()).Infof("executing: %s", 
strings.Join(cmd.Args, " "))
 
        return cmd.Run()
 }
@@ -150,11 +162,3 @@ func ParseGAV(gav string) (Dependency, error) {
 
        return dep, nil
 }
-
-// ExtraOptions --
-func ExtraOptions(localRepo string) []string {
-       if _, err := os.Stat(localRepo); err == nil {
-               return []string{"-Dmaven.repo.local=" + localRepo}
-       }
-       return []string{"-Dcamel.noop=true"}
-}
diff --git a/pkg/util/maven/maven_types.go b/pkg/util/maven/maven_types.go
index c292b13..26e5b3c 100644
--- a/pkg/util/maven/maven_types.go
+++ b/pkg/util/maven/maven_types.go
@@ -21,6 +21,7 @@ import (
        "bytes"
        "encoding/xml"
        "fmt"
+       "time"
 )
 
 // Repository --
@@ -102,10 +103,11 @@ func NewContext(buildDir string, project Project) Context 
{
 type Context struct {
        Path                string
        Project             Project
-       Settings            *Settings
-       SettingsData        []byte
+       SettingsContent     []byte
        AdditionalArguments []string
        AdditionalEntries   map[string]interface{}
+       Timeout             time.Duration
+       LocalRepository     string
 }
 
 // AddEntry --

Reply via email to