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

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

commit bc20fe15d64d7d923a49193201b00867007737d1
Author: Antonin Stefanutti <[email protected]>
AuthorDate: Fri Oct 25 11:56:06 2019 +0200

    chore(quarkus): Factorize Camel catalog runtime generation logic
---
 pkg/trait/camel.go             | 106 +----------------------------------------
 pkg/trait/camel_test.go        |  25 ----------
 pkg/trait/util.go              |  10 ++++
 pkg/util/camel/catalog.go      |  88 ++++++++++++++++++++++++++++++++++
 pkg/util/camel/catalog_test.go |  47 ++++++++++++++++++
 5 files changed, 147 insertions(+), 129 deletions(-)

diff --git a/pkg/trait/camel.go b/pkg/trait/camel.go
index 9a8281a..2a07b37 100644
--- a/pkg/trait/camel.go
+++ b/pkg/trait/camel.go
@@ -19,23 +19,14 @@ package trait
 
 import (
        "fmt"
-       "io/ioutil"
-       "os"
-       "path"
-       "regexp"
        "strings"
 
-       yaml2 "gopkg.in/yaml.v2"
-
        "github.com/pkg/errors"
 
        k8serrors "k8s.io/apimachinery/pkg/api/errors"
 
        "github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
        "github.com/apache/camel-k/pkg/util/camel"
-       "github.com/apache/camel-k/pkg/util/defaults"
-       "github.com/apache/camel-k/pkg/util/kubernetes"
-       "github.com/apache/camel-k/pkg/util/maven"
 )
 
 type camelTrait struct {
@@ -112,17 +103,8 @@ func (t *camelTrait) loadOrCreateCatalog(e *Environment, 
camelVersion string, ru
                // if the catalog is not found in the cluster, try to create it 
if
                // the required versions (camel and runtime) are not expressed 
as
                // semver constraints
-               cvHasFixedVersion, err := 
regexp.MatchString(`^(\d+)\.(\d+)\.([\w-\.]+)$`, camelVersion)
-               if err != nil {
-                       return err
-               }
-               rvHasFixedVersion, err := 
regexp.MatchString(`^(\d+)\.(\d+)\.([\w-\.]+)$`, runtimeVersion)
-               if err != nil {
-                       return err
-               }
-
-               if cvHasFixedVersion && rvHasFixedVersion {
-                       c, err = t.generateCatalog(e, camelVersion, 
runtimeVersion)
+               if exactVersionRegexp.MatchString(camelVersion) && 
exactVersionRegexp.MatchString(runtimeVersion) {
+                       c, err = camel.GenerateCatalog(e.C, e.Client, ns, 
e.Platform.Spec.Build.Maven, camelVersion, runtimeVersion)
                        if err != nil {
                                return err
                        }
@@ -150,90 +132,6 @@ func (t *camelTrait) loadOrCreateCatalog(e *Environment, 
camelVersion string, ru
        return nil
 }
 
-func (t *camelTrait) generateCatalog(e *Environment, camelVersion string, 
runtimeVersion string) (*camel.RuntimeCatalog, error) {
-       root := os.TempDir()
-       tmpDir, err := ioutil.TempDir(root, "camel-catalog")
-       if err != nil {
-               return nil, err
-       }
-
-       defer os.RemoveAll(tmpDir)
-
-       if err := os.MkdirAll(tmpDir, os.ModePerm); err != nil {
-               return nil, err
-       }
-
-       project := t.generateMavenProject(camelVersion, runtimeVersion)
-
-       mc := maven.NewContext(tmpDir, project)
-       mc.LocalRepository = e.Platform.Spec.Build.Maven.LocalRepository
-       mc.Timeout = e.Platform.Spec.Build.Maven.Timeout.Duration
-       mc.AddSystemProperty("catalog.path", tmpDir)
-       mc.AddSystemProperty("catalog.file", "catalog.yaml")
-
-       ns := e.DetermineNamespace()
-       if ns == "" {
-               return nil, errors.New("unable to determine namespace")
-       }
-
-       settings, err := kubernetes.ResolveValueSource(e.C, e.Client, ns, 
&e.Platform.Spec.Build.Maven.Settings)
-       if err != nil {
-               return nil, err
-       }
-       if settings != "" {
-               mc.SettingsContent = []byte(settings)
-       }
-
-       err = maven.Run(mc)
-       if err != nil {
-               return nil, err
-       }
-
-       content, err := ioutil.ReadFile(path.Join(tmpDir, "catalog.yaml"))
-       if err != nil {
-               return nil, err
-       }
-
-       catalog := v1alpha1.CamelCatalog{}
-       if err := yaml2.Unmarshal(content, &catalog); err != nil {
-               return nil, err
-       }
-
-       return camel.NewRuntimeCatalog(catalog.Spec), nil
-}
-
-func (t *camelTrait) generateMavenProject(camelVersion string, runtimeVersion 
string) maven.Project {
-       p := maven.NewProjectWithGAV("org.apache.camel.k.integration", 
"camel-k-catalog-generator", defaults.Version)
-
-       p.Build = &maven.Build{
-               DefaultGoal: "generate-resources",
-               Plugins: []maven.Plugin{
-                       {
-                               GroupID:    "org.apache.camel.k",
-                               ArtifactID: "camel-k-maven-plugin",
-                               Version:    runtimeVersion,
-                               Executions: []maven.Execution{
-                                       {
-                                               ID: "generate-catalog",
-                                               Goals: []string{
-                                                       "generate-catalog",
-                                               },
-                                       },
-                               },
-                               Dependencies: []maven.Dependency{
-                                       {
-                                               GroupID:    "org.apache.camel",
-                                               ArtifactID: "camel-catalog",
-                                               Version:    camelVersion,
-                                       },
-                               },
-                       },
-               },
-       }
-
-       return p
-}
-
 func (t *camelTrait) determineCamelVersion(e *Environment) string {
        if t.Version != "" {
                return t.Version
diff --git a/pkg/trait/camel_test.go b/pkg/trait/camel_test.go
index 24c5032..49f594d 100644
--- a/pkg/trait/camel_test.go
+++ b/pkg/trait/camel_test.go
@@ -69,31 +69,6 @@ func 
TestApplyCamelTraitWithoutEnvironmentCatalogAndUnmatchableVersionFails(t *t
        assert.Equal(t, "unable to find catalog matching version requirement: 
camel=Unmatchable version, runtime=0.0.1", err.Error())
 }
 
-func TestCamelTraitGenerateMavenProjectSucceeds(t *testing.T) {
-       trait, _ := createNominalCamelTest()
-
-       mvnProject := trait.generateMavenProject("1.23.0", "1.0.0")
-       assert.NotNil(t, mvnProject)
-       assert.Equal(t, "org.apache.camel.k.integration", mvnProject.GroupID)
-       assert.Equal(t, "camel-k-catalog-generator", mvnProject.ArtifactID)
-       assert.NotNil(t, mvnProject.Build)
-       assert.Equal(t, "generate-resources", mvnProject.Build.DefaultGoal)
-       assert.NotNil(t, mvnProject.Build.Plugins)
-       assert.Len(t, mvnProject.Build.Plugins, 1)
-       assert.Equal(t, "org.apache.camel.k", 
mvnProject.Build.Plugins[0].GroupID)
-       assert.Equal(t, "camel-k-maven-plugin", 
mvnProject.Build.Plugins[0].ArtifactID)
-       assert.NotNil(t, mvnProject.Build.Plugins[0].Executions)
-       assert.Len(t, mvnProject.Build.Plugins[0].Executions, 1)
-       assert.Equal(t, "generate-catalog", 
mvnProject.Build.Plugins[0].Executions[0].ID)
-       assert.NotNil(t, mvnProject.Build.Plugins[0].Executions[0].Goals)
-       assert.Len(t, mvnProject.Build.Plugins[0].Executions[0].Goals, 1)
-       assert.Equal(t, "generate-catalog", 
mvnProject.Build.Plugins[0].Executions[0].Goals[0])
-       assert.NotNil(t, mvnProject.Build.Plugins[0].Dependencies)
-       assert.Len(t, mvnProject.Build.Plugins[0].Dependencies, 1)
-       assert.Equal(t, "org.apache.camel", 
mvnProject.Build.Plugins[0].Dependencies[0].GroupID)
-       assert.Equal(t, "camel-catalog", 
mvnProject.Build.Plugins[0].Dependencies[0].ArtifactID)
-}
-
 func createNominalCamelTest() (*camelTrait, *Environment) {
        client, _ := test.NewFakeClient()
 
diff --git a/pkg/trait/util.go b/pkg/trait/util.go
index da5b20a..9c3e12c 100644
--- a/pkg/trait/util.go
+++ b/pkg/trait/util.go
@@ -33,6 +33,16 @@ import (
        k8sclient "sigs.k8s.io/controller-runtime/pkg/client"
 )
 
+var exactVersionRegexp regexp.Regexp
+
+func init() {
+       r, err := regexp.Compile(`^(\d+)\.(\d+)\.([\w-\.]+)$`)
+       if err != nil {
+               panic(err)
+       }
+       exactVersionRegexp = *r
+}
+
 // GetIntegrationKit retrieves the kit set on the integration
 func GetIntegrationKit(ctx context.Context, c client.Client, integration 
*v1alpha1.Integration) (*v1alpha1.IntegrationKit, error) {
        if integration.Status.Kit == "" {
diff --git a/pkg/util/camel/catalog.go b/pkg/util/camel/catalog.go
index adcf510..4505334 100644
--- a/pkg/util/camel/catalog.go
+++ b/pkg/util/camel/catalog.go
@@ -18,13 +18,21 @@ limitations under the License.
 package camel
 
 import (
+       "context"
+       "io/ioutil"
+       "os"
+       "path"
        "strings"
 
        yaml2 "gopkg.in/yaml.v2"
 
+       k8sclient "sigs.k8s.io/controller-runtime/pkg/client"
+
        "github.com/apache/camel-k/deploy"
        "github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
        "github.com/apache/camel-k/pkg/util/defaults"
+       "github.com/apache/camel-k/pkg/util/kubernetes"
+       "github.com/apache/camel-k/pkg/util/maven"
 )
 
 // DefaultCatalog --
@@ -57,3 +65,83 @@ func catalogForRuntimeProvider(provider interface{}) 
(*RuntimeCatalog, error) {
 
        return findBestMatch(catalogs, defaults.DefaultCamelVersion, 
defaults.DefaultRuntimeVersion, provider)
 }
+
+// GenerateCatalog --
+func GenerateCatalog(ctx context.Context, client k8sclient.Reader, namespace 
string, mvn v1alpha1.MavenSpec, camelVersion string, runtimeVersion string) 
(*RuntimeCatalog, error) {
+       root := os.TempDir()
+       tmpDir, err := ioutil.TempDir(root, "camel-catalog")
+       if err != nil {
+               return nil, err
+       }
+
+       defer os.RemoveAll(tmpDir)
+
+       if err := os.MkdirAll(tmpDir, os.ModePerm); err != nil {
+               return nil, err
+       }
+
+       project := generateMavenProject(camelVersion, runtimeVersion)
+
+       mc := maven.NewContext(tmpDir, project)
+       mc.LocalRepository = mvn.LocalRepository
+       mc.Timeout = mvn.Timeout.Duration
+       mc.AddSystemProperty("catalog.path", tmpDir)
+       mc.AddSystemProperty("catalog.file", "catalog.yaml")
+
+       settings, err := kubernetes.ResolveValueSource(ctx, client, namespace, 
&mvn.Settings)
+       if err != nil {
+               return nil, err
+       }
+       if settings != "" {
+               mc.SettingsContent = []byte(settings)
+       }
+
+       err = maven.Run(mc)
+       if err != nil {
+               return nil, err
+       }
+
+       content, err := ioutil.ReadFile(path.Join(tmpDir, "catalog.yaml"))
+       if err != nil {
+               return nil, err
+       }
+
+       catalog := v1alpha1.CamelCatalog{}
+       if err := yaml2.Unmarshal(content, &catalog); err != nil {
+               return nil, err
+       }
+
+       return NewRuntimeCatalog(catalog.Spec), nil
+}
+
+func generateMavenProject(camelVersion string, runtimeVersion string) 
maven.Project {
+       p := maven.NewProjectWithGAV("org.apache.camel.k.integration", 
"camel-k-catalog-generator", defaults.Version)
+
+       p.Build = &maven.Build{
+               DefaultGoal: "generate-resources",
+               Plugins: []maven.Plugin{
+                       {
+                               GroupID:    "org.apache.camel.k",
+                               ArtifactID: "camel-k-maven-plugin",
+                               Version:    runtimeVersion,
+                               Executions: []maven.Execution{
+                                       {
+                                               ID: "generate-catalog",
+                                               Goals: []string{
+                                                       "generate-catalog",
+                                               },
+                                       },
+                               },
+                               Dependencies: []maven.Dependency{
+                                       {
+                                               GroupID:    "org.apache.camel",
+                                               ArtifactID: "camel-catalog",
+                                               Version:    camelVersion,
+                                       },
+                               },
+                       },
+               },
+       }
+
+       return p
+}
diff --git a/pkg/util/camel/catalog_test.go b/pkg/util/camel/catalog_test.go
new file mode 100644
index 0000000..47ab71f
--- /dev/null
+++ b/pkg/util/camel/catalog_test.go
@@ -0,0 +1,47 @@
+/*
+Licensed to the Apache Software Foundation (ASF) under one or more
+contributor license agreements.  See the NOTICE file distributed with
+this work for additional information regarding copyright ownership.
+The ASF licenses this file to You under the Apache License, Version 2.0
+(the "License"); you may not use this file except in compliance with
+the License.  You may obtain a copy of the License at
+
+   http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package camel
+
+import (
+       "testing"
+
+       "github.com/stretchr/testify/assert"
+)
+
+func TestCamelTraitGenerateMavenProjectSucceeds(t *testing.T) {
+       mvnProject := generateMavenProject("1.23.0", "1.0.0", nil)
+       assert.NotNil(t, mvnProject)
+       assert.Equal(t, "org.apache.camel.k.integration", mvnProject.GroupID)
+       assert.Equal(t, "camel-k-catalog-generator", mvnProject.ArtifactID)
+       assert.NotNil(t, mvnProject.Build)
+       assert.Equal(t, "generate-resources", mvnProject.Build.DefaultGoal)
+       assert.NotNil(t, mvnProject.Build.Plugins)
+       assert.Len(t, mvnProject.Build.Plugins, 1)
+       assert.Equal(t, "org.apache.camel.k", 
mvnProject.Build.Plugins[0].GroupID)
+       assert.Equal(t, "camel-k-maven-plugin", 
mvnProject.Build.Plugins[0].ArtifactID)
+       assert.NotNil(t, mvnProject.Build.Plugins[0].Executions)
+       assert.Len(t, mvnProject.Build.Plugins[0].Executions, 1)
+       assert.Equal(t, "generate-catalog", 
mvnProject.Build.Plugins[0].Executions[0].ID)
+       assert.NotNil(t, mvnProject.Build.Plugins[0].Executions[0].Goals)
+       assert.Len(t, mvnProject.Build.Plugins[0].Executions[0].Goals, 1)
+       assert.Equal(t, "generate-catalog", 
mvnProject.Build.Plugins[0].Executions[0].Goals[0])
+       assert.NotNil(t, mvnProject.Build.Plugins[0].Dependencies)
+       assert.Len(t, mvnProject.Build.Plugins[0].Dependencies, 1)
+       assert.Equal(t, "org.apache.camel", 
mvnProject.Build.Plugins[0].Dependencies[0].GroupID)
+       assert.Equal(t, "camel-catalog", 
mvnProject.Build.Plugins[0].Dependencies[0].ArtifactID)
+}

Reply via email to