Skip to content

Commit

Permalink
support for semver catalog resolution #414
Browse files Browse the repository at this point in the history
  • Loading branch information
lburgazzoli committed Feb 10, 2019
1 parent 40b7bdc commit 006c77a
Show file tree
Hide file tree
Showing 14 changed files with 1,186 additions and 43 deletions.
9 changes: 9 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 6 additions & 8 deletions pkg/builder/builder_steps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ import (

"github.com/apache/camel-k/pkg/util/test"

"github.com/apache/camel-k/pkg/util/defaults"

"github.com/apache/camel-k/pkg/util/maven"
"github.com/apache/camel-k/version"

Expand All @@ -41,7 +39,7 @@ func TestGenerateJvmProject(t *testing.T) {
Catalog: catalog,
Platform: v1alpha1.IntegrationPlatformSpec{
Build: v1alpha1.IntegrationPlatformBuildSpec{
CamelVersion: defaults.CamelVersion,
CamelVersion: catalog.Version,
},
},
Dependencies: []string{
Expand All @@ -58,7 +56,7 @@ func TestGenerateJvmProject(t *testing.T) {
assert.Equal(t, 1, len(ctx.Project.DependencyManagement.Dependencies))
assert.Equal(t, "org.apache.camel", ctx.Project.DependencyManagement.Dependencies[0].GroupID)
assert.Equal(t, "camel-bom", ctx.Project.DependencyManagement.Dependencies[0].ArtifactID)
assert.Equal(t, defaults.CamelVersion, ctx.Project.DependencyManagement.Dependencies[0].Version)
assert.Equal(t, catalog.Version, ctx.Project.DependencyManagement.Dependencies[0].Version)
assert.Equal(t, "pom", ctx.Project.DependencyManagement.Dependencies[0].Type)
assert.Equal(t, "import", ctx.Project.DependencyManagement.Dependencies[0].Scope)

Expand All @@ -84,7 +82,7 @@ func TestGenerateGroovyProject(t *testing.T) {
Catalog: catalog,
Platform: v1alpha1.IntegrationPlatformSpec{
Build: v1alpha1.IntegrationPlatformBuildSpec{
CamelVersion: defaults.CamelVersion,
CamelVersion: catalog.Version,
},
},
Dependencies: []string{
Expand All @@ -101,7 +99,7 @@ func TestGenerateGroovyProject(t *testing.T) {
assert.Equal(t, 1, len(ctx.Project.DependencyManagement.Dependencies))
assert.Equal(t, "org.apache.camel", ctx.Project.DependencyManagement.Dependencies[0].GroupID)
assert.Equal(t, "camel-bom", ctx.Project.DependencyManagement.Dependencies[0].ArtifactID)
assert.Equal(t, defaults.CamelVersion, ctx.Project.DependencyManagement.Dependencies[0].Version)
assert.Equal(t, catalog.Version, ctx.Project.DependencyManagement.Dependencies[0].Version)
assert.Equal(t, "pom", ctx.Project.DependencyManagement.Dependencies[0].Type)
assert.Equal(t, "import", ctx.Project.DependencyManagement.Dependencies[0].Scope)

Expand Down Expand Up @@ -138,7 +136,7 @@ func TestGenerateProjectWithRepositories(t *testing.T) {
Catalog: catalog,
Platform: v1alpha1.IntegrationPlatformSpec{
Build: v1alpha1.IntegrationPlatformBuildSpec{
CamelVersion: defaults.CamelVersion,
CamelVersion: catalog.Version,
},
},
Repositories: []string{
Expand All @@ -156,7 +154,7 @@ func TestGenerateProjectWithRepositories(t *testing.T) {
assert.Equal(t, 1, len(ctx.Project.DependencyManagement.Dependencies))
assert.Equal(t, "org.apache.camel", ctx.Project.DependencyManagement.Dependencies[0].GroupID)
assert.Equal(t, "camel-bom", ctx.Project.DependencyManagement.Dependencies[0].ArtifactID)
assert.Equal(t, defaults.CamelVersion, ctx.Project.DependencyManagement.Dependencies[0].Version)
assert.Equal(t, catalog.Version, ctx.Project.DependencyManagement.Dependencies[0].Version)
assert.Equal(t, "pom", ctx.Project.DependencyManagement.Dependencies[0].Type)
assert.Equal(t, "import", ctx.Project.DependencyManagement.Dependencies[0].Scope)

Expand Down
26 changes: 5 additions & 21 deletions pkg/util/camel/camel_runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package camel

import (
"context"
"fmt"
"sync"

"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
Expand Down Expand Up @@ -50,34 +49,19 @@ func (r *Runtime) LoadCatalog(ctx context.Context, client client.Client, namespa
return &c, nil
}

var c *RuntimeCatalog
var catalog *RuntimeCatalog
var err error

// try with the exact match
c, err = r.doLoadCatalog(ctx, client, namespace, version)
list := v1alpha1.NewCamelCatalogList()
err = client.List(ctx, &k8sclient.ListOptions{Namespace: namespace}, &list)
if err != nil {
return nil, err
}
if c != nil {
r.catalogs[version] = *c
return c, nil
}

return nil, fmt.Errorf("unable to find a camel catalog for version: %s", version)
}

func (r *Runtime) doLoadCatalog(ctx context.Context, client client.Client, namespace string, version string) (*RuntimeCatalog, error) {
list := v1alpha1.NewCamelCatalogList()
err := client.List(ctx, &k8sclient.ListOptions{Namespace: namespace}, &list)
catalog, err = FindBestMatch(version, list.Items)
if err != nil {
return nil, err
}

for _, c := range list.Items {
if c.Spec.Version == version {
return NewRuntimeCatalog(c.Spec), nil
}
}

return nil, nil
return catalog, nil
}
65 changes: 65 additions & 0 deletions pkg/util/camel/camel_util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
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 (
"fmt"
"sort"

"github.com/Masterminds/semver"
"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
)

// FindBestMatch --
func FindBestMatch(constraint string, catalogs []v1alpha1.CamelCatalog) (*RuntimeCatalog, error) {
ref, err := semver.NewConstraint(constraint)
if err != nil {
fmt.Printf("Error parsing version: %s\n", err.Error())
}

versions := make([]*semver.Version, 0)

for _, catalog := range catalogs {
v, err := semver.NewVersion(catalog.Spec.Version)
if err != nil {
return nil, err
}

versions = append(versions, v)
}

sort.Sort(
sort.Reverse(semver.Collection(versions)),
)

for _, v := range versions {
ver := v

if ref.Check(ver) {
for _, catalog := range catalogs {
if catalog.Spec.Version == ver.Original() {
return NewRuntimeCatalog(catalog.Spec), nil
}
}

}
}

return nil, fmt.Errorf("unable to find default catalog from embedded resources")

}
77 changes: 77 additions & 0 deletions pkg/util/camel/camel_util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
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"

"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
)

func TestFindBestMatch(t *testing.T) {
catalogs := []v1alpha1.CamelCatalog{
{
Spec: v1alpha1.CamelCatalogSpec{Version: "2.23.0"},
},
{
Spec: v1alpha1.CamelCatalogSpec{Version: "2.23.1"},
},
}

c, err := FindBestMatch("~2.23.x", catalogs)
assert.Nil(t, err)
assert.NotNil(t, c)
assert.Equal(t, "2.23.1", c.Version)
}

func TestFindExactMatch(t *testing.T) {
catalogs := []v1alpha1.CamelCatalog{
{
Spec: v1alpha1.CamelCatalogSpec{Version: "2.23.0"},
},
{
Spec: v1alpha1.CamelCatalogSpec{Version: "2.23.1"},
},
}

c, err := FindBestMatch("2.23.0", catalogs)
assert.Nil(t, err)
assert.NotNil(t, c)
assert.Equal(t, "2.23.0", c.Version)
}

func TestFindRangeMatch(t *testing.T) {
catalogs := []v1alpha1.CamelCatalog{
{
Spec: v1alpha1.CamelCatalogSpec{Version: "2.23.0"},
},
{
Spec: v1alpha1.CamelCatalogSpec{Version: "2.23.1"},
},
{
Spec: v1alpha1.CamelCatalogSpec{Version: "2.23.2"},
},
}

c, err := FindBestMatch(">= 2.23.0, < 2.23.2", catalogs)
assert.Nil(t, err)
assert.NotNil(t, c)
assert.Equal(t, "2.23.1", c.Version)
}
2 changes: 1 addition & 1 deletion pkg/util/defaults/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package defaults

const (
// CamelVersion --
CamelVersion = "2.23.1"
CamelVersion = "~2.23.x"

// BaseImage --
BaseImage = "fabric8/s2i-java:3.0-java8"
Expand Down
23 changes: 13 additions & 10 deletions pkg/util/test/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,29 @@ limitations under the License.
package test

import (
"fmt"
"strings"

"github.com/apache/camel-k/deploy"
"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"
yaml "gopkg.in/yaml.v2"
"gopkg.in/yaml.v2"
)

// DefaultCatalog --
func DefaultCatalog() (*camel.RuntimeCatalog, error) {
data, ok := deploy.Resources["camel-catalog-"+defaults.CamelVersion+".yaml"]
if !ok {
return nil, fmt.Errorf("unable to find default catalog from embedded resources")
}
catalogs := make([]v1alpha1.CamelCatalog, 0)

for name, content := range deploy.Resources {
if strings.HasPrefix(name, "camel-catalog-") {
var c v1alpha1.CamelCatalog
if err := yaml.Unmarshal([]byte(content), &c); err != nil {
return nil, err
}

var c v1alpha1.CamelCatalog
if err := yaml.Unmarshal([]byte(data), &c); err != nil {
return nil, err
catalogs = append(catalogs, c)
}
}

return camel.NewRuntimeCatalog(c.Spec), nil
return camel.FindBestMatch(defaults.CamelVersion, catalogs)
}
2 changes: 2 additions & 0 deletions pkg/util/test/catalog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ func TestRuntimeContainsEmbeddedArtifacts(t *testing.T) {
catalog, err := DefaultCatalog()
assert.Nil(t, err)

assert.Equal(t, "2.23.1", catalog.Version)

artifact := catalog.GetArtifactByScheme("knative")
assert.Equal(t, 1, len(artifact.Schemes))
assert.Equal(t, "org.apache.camel.k", artifact.GroupID)
Expand Down
5 changes: 2 additions & 3 deletions test/builder_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"github.com/apache/camel-k/pkg/util/test"

"github.com/apache/camel-k/pkg/util/cancellable"
"github.com/apache/camel-k/pkg/util/defaults"

"k8s.io/apimachinery/pkg/apis/meta/v1"

Expand Down Expand Up @@ -71,7 +70,7 @@ func TestBuildManagerBuild(t *testing.T) {
},
Platform: v1alpha1.IntegrationPlatformSpec{
Build: v1alpha1.IntegrationPlatformBuildSpec{
CamelVersion: defaults.CamelVersion,
CamelVersion: catalog.Version,
BaseImage: "docker.io/fabric8/s2i-java:3.0-java8",
},
},
Expand Down Expand Up @@ -117,7 +116,7 @@ func TestBuildManagerFailedBuild(t *testing.T) {
},
Platform: v1alpha1.IntegrationPlatformSpec{
Build: v1alpha1.IntegrationPlatformBuildSpec{
CamelVersion: defaults.CamelVersion,
CamelVersion: catalog.Version,
BaseImage: "docker.io/fabric8/s2i-java:3.0-java8",
},
},
Expand Down
20 changes: 20 additions & 0 deletions vendor/github.com/Masterminds/semver/LICENSE.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 006c77a

Please sign in to comment.