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
commit 59bebae1d9f9d2414979a2c18f9e8ab581c42977 Author: lburgazzoli <lburgazz...@gmail.com> AuthorDate: Tue Oct 8 16:27:44 2019 +0200 Include camel-k-runtime version in camel-catalog #959 --- assets/json-schema/CamelCatalog.json | 4 + assets/json-schema/IntegrationKit.json | 2 +- build/maven/pom-catalog.xml | 8 +- deploy/crd-camel-catalog.yaml | 8 +- deploy/resources.go | 8 +- go.mod | 2 + pkg/apis/camel/v1alpha1/camelcatalog_types.go | 5 +- pkg/builder/builder.go | 2 +- pkg/builder/builder_steps.go | 4 +- pkg/builder/builder_steps_test.go | 35 +++--- pkg/builder/builder_test.go | 3 +- pkg/builder/builder_utils.go | 4 +- pkg/builder/builder_utils_test.go | 5 +- pkg/controller/integrationplatform/initialize.go | 2 +- pkg/trait/camel.go | 31 +++-- pkg/trait/camel_test.go | 2 +- pkg/util/camel/camel.go | 42 ------- pkg/util/camel/camel_runtime.go | 30 ++--- pkg/util/camel/camel_types.go | 58 +++++++++ pkg/util/camel/camel_util.go | 90 +++++++------- pkg/util/camel/camel_util_test.go | 147 +++++++++++++++++------ pkg/util/defaults/defaults.go | 10 +- pkg/util/test/catalog.go | 2 +- pkg/util/test/catalog_test.go | 4 +- script/Makefile | 11 +- script/build_catalog.sh | 1 - 26 files changed, 319 insertions(+), 201 deletions(-) diff --git a/assets/json-schema/CamelCatalog.json b/assets/json-schema/CamelCatalog.json index 1266aa1..b8e37a6 100644 --- a/assets/json-schema/CamelCatalog.json +++ b/assets/json-schema/CamelCatalog.json @@ -159,6 +159,7 @@ "CamelCatalogSpec": { "required": [ "version", + "runtimeVersion", "artifacts" ], "properties": { @@ -171,6 +172,9 @@ }, "type": "object" }, + "runtimeVersion": { + "type": "string" + }, "version": { "type": "string" } diff --git a/assets/json-schema/IntegrationKit.json b/assets/json-schema/IntegrationKit.json index 94c94f1..0815ea8 100644 --- a/assets/json-schema/IntegrationKit.json +++ b/assets/json-schema/IntegrationKit.json @@ -441,4 +441,4 @@ "type": "object" } } -} +} \ No newline at end of file diff --git a/build/maven/pom-catalog.xml b/build/maven/pom-catalog.xml index db52c6d..acef746 100644 --- a/build/maven/pom-catalog.xml +++ b/build/maven/pom-catalog.xml @@ -46,7 +46,7 @@ <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-catalog</artifactId> - <version>${catalog.version}</version> + <version>${camel.version}</version> </dependency> </dependencies> </plugin> @@ -56,7 +56,6 @@ <repositories> <repository> <id>apache.snapshots</id> - <name>Apache Development Snapshot Repository</name> <url>https://repository.apache.org/content/repositories/snapshots/</url> <releases> <enabled>false</enabled> @@ -80,7 +79,6 @@ <pluginRepositories> <pluginRepository> <id>apache.snapshots</id> - <name>Apache Development Snapshot Repository</name> <url>https://repository.apache.org/content/repositories/snapshots/</url> <releases> <enabled>false</enabled> @@ -101,4 +99,8 @@ </pluginRepository> </pluginRepositories> + + + + </project> diff --git a/deploy/crd-camel-catalog.yaml b/deploy/crd-camel-catalog.yaml index 858bd0f..b57a08f 100644 --- a/deploy/crd-camel-catalog.yaml +++ b/deploy/crd-camel-catalog.yaml @@ -35,8 +35,12 @@ spec: subresources: status: {} additionalPrinterColumns: - - name: Version + - name: Camel Version type: string - description: The Catalog version + description: The Camel version JSONPath: .spec.version + - name: Runtime Version + type: string + description: The Camel K Runtime version + JSONPath: .spec.runtimeVersion diff --git a/deploy/resources.go b/deploy/resources.go index 2a59ae0..5b7c171 100644 --- a/deploy/resources.go +++ b/deploy/resources.go @@ -2531,10 +2531,14 @@ spec: subresources: status: {} additionalPrinterColumns: - - name: Version + - name: Camel Version type: string - description: The Catalog version + description: The Camel version JSONPath: .spec.version + - name: Runtime Version + type: string + description: The Camel K Runtime version + JSONPath: .spec.runtimeVersion ` diff --git a/go.mod b/go.mod index bedbd73..583473d 100644 --- a/go.mod +++ b/go.mod @@ -51,3 +51,5 @@ replace ( ) replace git.apache.org/thrift.git => github.com/apache/thrift v0.0.0-20180902110319-2566ecd5d999 + +go 1.13 diff --git a/pkg/apis/camel/v1alpha1/camelcatalog_types.go b/pkg/apis/camel/v1alpha1/camelcatalog_types.go index e91210d..d778fb5 100644 --- a/pkg/apis/camel/v1alpha1/camelcatalog_types.go +++ b/pkg/apis/camel/v1alpha1/camelcatalog_types.go @@ -53,8 +53,9 @@ type CamelArtifact struct { // CamelCatalogSpec defines the desired state of CamelCatalog type CamelCatalogSpec struct { - Version string `json:"version" yaml:"version"` - Artifacts map[string]CamelArtifact `json:"artifacts" yaml:"artifacts"` + Version string `json:"version" yaml:"version"` + RuntimeVersion string `json:"runtimeVersion" yaml:"runtimeVersion"` + Artifacts map[string]CamelArtifact `json:"artifacts" yaml:"artifacts"` } // CamelCatalogStatus defines the observed state of CamelCatalog diff --git a/pkg/builder/builder.go b/pkg/builder/builder.go index 7327d97..872c3df 100644 --- a/pkg/builder/builder.go +++ b/pkg/builder/builder.go @@ -71,7 +71,7 @@ func (b *defaultBuilder) Build(build v1alpha1.BuildSpec) v1alpha1.BuildStatus { defer os.RemoveAll(builderPath) - catalog, err := camel.Catalog(b.ctx, b.client, build.Meta.Namespace, build.CamelVersion) + catalog, err := camel.LoadCatalog(b.ctx, b.client, build.Meta.Namespace, build.CamelVersion, build.RuntimeVersion) if err != nil { log.Error(err, "Error while loading Camel catalog") diff --git a/pkg/builder/builder_steps.go b/pkg/builder/builder_steps.go index 81cd13a..cb886f0 100644 --- a/pkg/builder/builder_steps.go +++ b/pkg/builder/builder_steps.go @@ -248,7 +248,7 @@ func computeDependencies(ctx *Context) error { 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) + mc.AddArgumentf("org.apache.camel.k:camel-k-maven-plugin:%s:generate-dependency-list", ctx.Catalog.RuntimeVersion) if err := maven.Run(mc); err != nil { return errors.Wrap(err, "failure while determining classpath") @@ -390,7 +390,7 @@ func listPublishedImages(context *Context) ([]publishedImage, error) { if kit.Status.CamelVersion != context.Catalog.Version { continue } - if kit.Status.RuntimeVersion != context.Build.RuntimeVersion { + if kit.Status.RuntimeVersion != context.Catalog.RuntimeVersion { continue } if kit.Status.Phase != v1alpha1.IntegrationKitPhaseReady || kit.Labels == nil { diff --git a/pkg/builder/builder_steps_test.go b/pkg/builder/builder_steps_test.go index 3210dfa..e7da61a 100644 --- a/pkg/builder/builder_steps_test.go +++ b/pkg/builder/builder_steps_test.go @@ -25,7 +25,6 @@ import ( "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" @@ -58,7 +57,7 @@ func TestGenerateJvmProject(t *testing.T) { ctx := Context{ Catalog: catalog, Build: v1alpha1.BuildSpec{ - RuntimeVersion: defaults.RuntimeVersion, + RuntimeVersion: catalog.RuntimeVersion, Platform: v1alpha1.IntegrationPlatformSpec{ Build: v1alpha1.IntegrationPlatformBuildSpec{ CamelVersion: catalog.Version, @@ -89,7 +88,7 @@ func TestGenerateJvmProject(t *testing.T) { { GroupID: "org.apache.camel.k", ArtifactID: "camel-k-runtime-bom", - Version: defaults.RuntimeVersion, + Version: catalog.RuntimeVersion, Type: "pom", Scope: "import", }, @@ -147,7 +146,7 @@ func TestMavenSettingsFromConfigMap(t *testing.T) { Client: c, Namespace: "ns", Build: v1alpha1.BuildSpec{ - RuntimeVersion: defaults.RuntimeVersion, + RuntimeVersion: catalog.RuntimeVersion, Platform: v1alpha1.IntegrationPlatformSpec{ Build: v1alpha1.IntegrationPlatformBuildSpec{ CamelVersion: catalog.Version, @@ -199,7 +198,7 @@ func TestMavenSettingsFromSecret(t *testing.T) { Client: c, Namespace: "ns", Build: v1alpha1.BuildSpec{ - RuntimeVersion: defaults.RuntimeVersion, + RuntimeVersion: catalog.RuntimeVersion, Platform: v1alpha1.IntegrationPlatformSpec{ Build: v1alpha1.IntegrationPlatformBuildSpec{ CamelVersion: catalog.Version, @@ -231,7 +230,7 @@ func TestGenerateGroovyProject(t *testing.T) { ctx := Context{ Catalog: catalog, Build: v1alpha1.BuildSpec{ - RuntimeVersion: defaults.RuntimeVersion, + RuntimeVersion: catalog.RuntimeVersion, Platform: v1alpha1.IntegrationPlatformSpec{ Build: v1alpha1.IntegrationPlatformBuildSpec{ CamelVersion: catalog.Version, @@ -263,7 +262,7 @@ func TestGenerateGroovyProject(t *testing.T) { { GroupID: "org.apache.camel.k", ArtifactID: "camel-k-runtime-bom", - Version: defaults.RuntimeVersion, + Version: catalog.RuntimeVersion, Type: "pom", Scope: "import", }, @@ -292,15 +291,15 @@ func TestGenerateGroovyProject(t *testing.T) { }, { GroupID: "org.apache.camel", - ArtifactID: "camel-properties", + ArtifactID: "camel-groovy", }, { GroupID: "org.apache.camel", - ArtifactID: "camel-endpointdsl", + ArtifactID: "camel-properties", }, { GroupID: "org.apache.camel", - ArtifactID: "camel-groovy", + ArtifactID: "camel-endpointdsl", }, }, ) @@ -313,7 +312,7 @@ func TestSanitizeDependencies(t *testing.T) { ctx := Context{ Catalog: catalog, Build: v1alpha1.BuildSpec{ - RuntimeVersion: defaults.RuntimeVersion, + RuntimeVersion: catalog.RuntimeVersion, Platform: v1alpha1.IntegrationPlatformSpec{ Build: v1alpha1.IntegrationPlatformBuildSpec{ CamelVersion: catalog.Version, @@ -372,9 +371,10 @@ func TestListPublishedImages(t *testing.T) { }, }, Status: v1alpha1.IntegrationKitStatus{ - Phase: v1alpha1.IntegrationKitPhaseError, - Image: "image-1", - CamelVersion: catalog.Version, + Phase: v1alpha1.IntegrationKitPhaseError, + Image: "image-1", + CamelVersion: catalog.Version, + RuntimeVersion: catalog.RuntimeVersion, }, }, &v1alpha1.IntegrationKit{ @@ -390,9 +390,10 @@ func TestListPublishedImages(t *testing.T) { }, }, Status: v1alpha1.IntegrationKitStatus{ - Phase: v1alpha1.IntegrationKitPhaseReady, - Image: "image-2", - CamelVersion: catalog.Version, + Phase: v1alpha1.IntegrationKitPhaseReady, + Image: "image-2", + CamelVersion: catalog.Version, + RuntimeVersion: catalog.RuntimeVersion, }, }, ) diff --git a/pkg/builder/builder_test.go b/pkg/builder/builder_test.go index 97ce186..ca899ae 100644 --- a/pkg/builder/builder_test.go +++ b/pkg/builder/builder_test.go @@ -22,7 +22,6 @@ import ( "testing" "github.com/apache/camel-k/pkg/apis/camel/v1alpha1" - "github.com/apache/camel-k/pkg/util/defaults" "github.com/apache/camel-k/pkg/util/test" "github.com/stretchr/testify/assert" @@ -58,7 +57,7 @@ func TestFailure(t *testing.T) { steps.Step1, steps.Step2, ), - RuntimeVersion: defaults.RuntimeVersion, + RuntimeVersion: catalog.RuntimeVersion, Platform: v1alpha1.IntegrationPlatformSpec{ Build: v1alpha1.IntegrationPlatformBuildSpec{ CamelVersion: catalog.Version, diff --git a/pkg/builder/builder_utils.go b/pkg/builder/builder_utils.go index 6d236a4..e6516dd 100644 --- a/pkg/builder/builder_utils.go +++ b/pkg/builder/builder_utils.go @@ -51,7 +51,7 @@ func NewMavenProject(ctx *Context) (maven.Project, error) { // Catalog // if ctx.Catalog == nil { - c, err := camel.Catalog(ctx.C, ctx.Client, ctx.Namespace, ctx.Build.Platform.Build.CamelVersion) + c, err := camel.LoadCatalog(ctx.C, ctx.Client, ctx.Namespace, ctx.Build.Platform.Build.CamelVersion, ctx.Build.Platform.Build.RuntimeVersion) if err != nil { return maven.Project{}, err } @@ -77,7 +77,7 @@ func NewMavenProject(ctx *Context) (maven.Project, error) { p.DependencyManagement.Dependencies = append(p.DependencyManagement.Dependencies, maven.Dependency{ GroupID: "org.apache.camel.k", ArtifactID: "camel-k-runtime-bom", - Version: ctx.Build.RuntimeVersion, + Version: ctx.Catalog.RuntimeVersion, Type: "pom", Scope: "import", }) diff --git a/pkg/builder/builder_utils_test.go b/pkg/builder/builder_utils_test.go index 0fc39d1..951d035 100644 --- a/pkg/builder/builder_utils_test.go +++ b/pkg/builder/builder_utils_test.go @@ -21,7 +21,6 @@ import ( "testing" "github.com/apache/camel-k/pkg/apis/camel/v1alpha1" - "github.com/apache/camel-k/pkg/util/defaults" "github.com/apache/camel-k/pkg/util/maven" "github.com/apache/camel-k/pkg/util/test" @@ -35,7 +34,7 @@ func TestNewProject(t *testing.T) { ctx := Context{ Catalog: catalog, Build: v1alpha1.BuildSpec{ - RuntimeVersion: defaults.RuntimeVersion, + RuntimeVersion: catalog.RuntimeVersion, Platform: v1alpha1.IntegrationPlatformSpec{ Build: v1alpha1.IntegrationPlatformBuildSpec{ CamelVersion: catalog.Version, @@ -65,7 +64,7 @@ func TestNewProject(t *testing.T) { { GroupID: "org.apache.camel.k", ArtifactID: "camel-k-runtime-bom", - Version: defaults.RuntimeVersion, + Version: catalog.RuntimeVersion, Type: "pom", Scope: "import", }, diff --git a/pkg/controller/integrationplatform/initialize.go b/pkg/controller/integrationplatform/initialize.go index c76c5a2..a2e4178 100644 --- a/pkg/controller/integrationplatform/initialize.go +++ b/pkg/controller/integrationplatform/initialize.go @@ -181,7 +181,7 @@ func (action *initializeAction) setDefaults(ctx context.Context, platform *v1alp platform.Spec.Build.CamelVersion = defaults.CamelVersionConstraint } if platform.Spec.Build.RuntimeVersion == "" { - platform.Spec.Build.RuntimeVersion = defaults.RuntimeVersion + platform.Spec.Build.RuntimeVersion = defaults.RuntimeVersionConstraint } if platform.Spec.Build.BaseImage == "" { platform.Spec.Build.BaseImage = defaults.BaseImage diff --git a/pkg/trait/camel.go b/pkg/trait/camel.go index 7cfbc9d..b776d9c 100644 --- a/pkg/trait/camel.go +++ b/pkg/trait/camel.go @@ -73,31 +73,38 @@ func (t *camelTrait) Apply(e *Environment) error { } if e.CamelCatalog == nil { - c, err := camel.Catalog(e.C, e.Client, ns, cv) + c, err := camel.LoadCatalog(e.C, e.Client, ns, cv, rv) if err != nil { return err } if c == nil { - // if the catalog is not found in the cluster, try to create it if the - // required version is not set using semver constraints - matched, err := regexp.MatchString(`^(\d+)\.(\d+)\.([\w-\.]+)$`, cv) + // 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-\.]+)$`, cv) if err != nil { return err } - if matched { - c, err = t.GenerateCatalog(e, cv) + rvHasFixedVersion, err := regexp.MatchString(`^(\d+)\.(\d+)\.([\w-\.]+)$`, rv) + if err != nil { + return err + } + + if cvHasFixedVersion && rvHasFixedVersion { + c, err = t.GenerateCatalog(e, cv, rv) if err != nil { return err } // sanitize catalog name - catalogName := "camel-catalog-" + strings.ToLower(cv) + catalogName := "camel-catalog-" + strings.ToLower(cv+"-"+rv) cx := v1alpha1.NewCamelCatalogWithSpecs(ns, catalogName, c.CamelCatalogSpec) cx.Labels = make(map[string]string) cx.Labels["app"] = "camel-k" cx.Labels["camel.apache.org/catalog.version"] = cv cx.Labels["camel.apache.org/catalog.loader.version"] = cv + cx.Labels["camel.apache.org/runtime.version"] = rv cx.Labels["camel.apache.org/catalog.generated"] = True err = e.Client.Create(e.C, &cx) @@ -129,7 +136,7 @@ func (t *camelTrait) Apply(e *Environment) error { } // GenerateCatalog -- -func (t *camelTrait) GenerateCatalog(e *Environment, version string) (*camel.RuntimeCatalog, error) { +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 { @@ -142,7 +149,7 @@ func (t *camelTrait) GenerateCatalog(e *Environment, version string) (*camel.Run return nil, err } - project, err := t.GenerateMavenProject(version) + project, err := t.GenerateMavenProject(camelVersion, runtimeVersion) if err != nil { return nil, err } @@ -185,7 +192,7 @@ func (t *camelTrait) GenerateCatalog(e *Environment, version string) (*camel.Run } // GenerateCatalogMavenProject -- -func (t *camelTrait) GenerateMavenProject(version string) (maven.Project, error) { +func (t *camelTrait) GenerateMavenProject(camelVersion string, runtimeVersion string) (maven.Project, error) { p := maven.NewProjectWithGAV("org.apache.camel.k.integration", "camel-k-catalog-generator", defaults.Version) p.Build = &maven.Build{ DefaultGoal: "generate-resources", @@ -193,7 +200,7 @@ func (t *camelTrait) GenerateMavenProject(version string) (maven.Project, error) { GroupID: "org.apache.camel.k", ArtifactID: "camel-k-maven-plugin", - Version: defaults.RuntimeVersion, + Version: runtimeVersion, Executions: []maven.Execution{ { ID: "generate-catalog", @@ -206,7 +213,7 @@ func (t *camelTrait) GenerateMavenProject(version string) (maven.Project, error) { GroupID: "org.apache.camel", ArtifactID: "camel-catalog", - Version: version, + Version: camelVersion, }, }, }, diff --git a/pkg/trait/camel_test.go b/pkg/trait/camel_test.go index ea2483a..08d5af3 100644 --- a/pkg/trait/camel_test.go +++ b/pkg/trait/camel_test.go @@ -72,7 +72,7 @@ func TestApplyCamelTraitWithoutEnvironmentCatalogAndUnmatchableVersionFails(t *t func TestCamelTraitGenerateMavenProjectSucceeds(t *testing.T) { trait, _ := createNominalCamelTest() - mvnProject, err := trait.GenerateMavenProject("1.23.0") + mvnProject, err := trait.GenerateMavenProject("1.23.0", "1.0.0") assert.Nil(t, err) assert.NotNil(t, mvnProject) assert.Equal(t, "org.apache.camel.k.integration", mvnProject.GroupID) diff --git a/pkg/util/camel/camel.go b/pkg/util/camel/camel.go deleted file mode 100644 index 65ad6f5..0000000 --- a/pkg/util/camel/camel.go +++ /dev/null @@ -1,42 +0,0 @@ -/* -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 ( - "context" - "fmt" - - "github.com/apache/camel-k/pkg/client" -) - -// R -- -var R Runtime - -func init() { - R = NewRuntime() -} - -// Catalog -- -func Catalog(ctx context.Context, client client.Client, namespace string, version string) (*RuntimeCatalog, error) { - c, err := R.LoadCatalog(ctx, client, namespace, version) - if c == nil && err != nil { - return nil, fmt.Errorf("unable to find catalog matching version requirement: %s", version) - } - - return c, err -} diff --git a/pkg/util/camel/camel_runtime.go b/pkg/util/camel/camel_runtime.go index fed3a0a..178b432 100644 --- a/pkg/util/camel/camel_runtime.go +++ b/pkg/util/camel/camel_runtime.go @@ -19,7 +19,7 @@ package camel import ( "context" - "sync" + "fmt" "github.com/apache/camel-k/pkg/apis/camel/v1alpha1" "github.com/apache/camel-k/pkg/client" @@ -27,28 +27,8 @@ import ( k8sclient "sigs.k8s.io/controller-runtime/pkg/client" ) -// NewRuntime -- -func NewRuntime() Runtime { - return Runtime{ - catalogs: make(map[string]RuntimeCatalog), - } -} - -// Runtime -- -type Runtime struct { - catalogs map[string]RuntimeCatalog - lock sync.Mutex -} - // LoadCatalog -- -func (r *Runtime) LoadCatalog(ctx context.Context, client client.Client, namespace string, version string) (*RuntimeCatalog, error) { - r.lock.Lock() - defer r.lock.Unlock() - - if c, ok := r.catalogs[version]; ok { - return &c, nil - } - +func LoadCatalog(ctx context.Context, client client.Client, namespace string, camelVersion string, runtimeVersion string) (*RuntimeCatalog, error) { var catalog *RuntimeCatalog var err error @@ -58,10 +38,14 @@ func (r *Runtime) LoadCatalog(ctx context.Context, client client.Client, namespa return nil, err } - catalog, err = FindBestMatch(version, list.Items) + catalog, err = FindBestMatch(list.Items, camelVersion, runtimeVersion) if err != nil { return nil, err } + if catalog == nil && err != nil { + return nil, fmt.Errorf("unable to find catalog matching version requirement: camel=%s, runtime=%s", camelVersion, runtimeVersion) + } + return catalog, nil } diff --git a/pkg/util/camel/camel_types.go b/pkg/util/camel/camel_types.go new file mode 100644 index 0000000..364d162 --- /dev/null +++ b/pkg/util/camel/camel_types.go @@ -0,0 +1,58 @@ +/* +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 ( + "github.com/Masterminds/semver" + "github.com/apache/camel-k/pkg/apis/camel/v1alpha1" +) + +// CatalogVersion -- +type CatalogVersion struct { + Version *semver.Version + RuntimeVersion *semver.Version + Catalog *v1alpha1.CamelCatalog +} + +// CatalogVersionCollection -- +type CatalogVersionCollection []CatalogVersion + +// Len returns the length of a collection. The number of CatalogVersion instances +// on the slice. +func (c CatalogVersionCollection) Len() int { + return len(c) +} + +// Less is needed for the sort interface to compare two CatalogVersion objects on the +// slice. If checks if one is less than the other. +func (c CatalogVersionCollection) Less(i, j int) bool { + if c[i].Version.LessThan(c[j].Version) { + return true + } + if c[i].Version.GreaterThan(c[j].Version) { + return false + } + + return c[i].RuntimeVersion.LessThan(c[j].RuntimeVersion) +} + +// Swap is needed for the sort interface to replace the CatalogVersion objects +// at two different positions in the slice. +func (c CatalogVersionCollection) Swap(i, j int) { + c[i], c[j] = c[j], c[i] +} diff --git a/pkg/util/camel/camel_util.go b/pkg/util/camel/camel_util.go index a06a5cf..4741222 100644 --- a/pkg/util/camel/camel_util.go +++ b/pkg/util/camel/camel_util.go @@ -26,67 +26,73 @@ import ( ) // FindBestMatch -- -func FindBestMatch(version string, catalogs []v1alpha1.CamelCatalog) (*RuntimeCatalog, error) { - constraint, err := semver.NewConstraint(version) - - // - // if the version is not a constraint, use exact match - // - if err != nil || constraint == nil { - if err != nil { - log.Debug("Unable to parse constraint: %s, error:\n", version, err.Error()) - } - if constraint == nil { - log.Debug("Unable to parse constraint: %s\n", version) +func FindBestMatch(catalogs []v1alpha1.CamelCatalog, camelVersion string, runtimeVersion string) (*RuntimeCatalog, error) { + for _, catalog := range catalogs { + if catalog.Spec.Version == camelVersion && catalog.Spec.RuntimeVersion == runtimeVersion { + return NewRuntimeCatalog(catalog.Spec), nil } + } - return FindExactMatch(version, catalogs) + vc := NewSemVerConstraint(camelVersion) + rc := NewSemVerConstraint(runtimeVersion) + if vc == nil || rc == nil { + return nil, nil } - return FindBestSemVerMatch(constraint, catalogs) + cc := NewCatalogVersionCollection(catalogs) + for _, x := range cc { + if vc.Check(x.Version) && rc.Check(x.RuntimeVersion) { + return NewRuntimeCatalog(x.Catalog.Spec), nil + } + } + + return nil, nil } -// FindExactMatch -- -func FindExactMatch(version string, catalogs []v1alpha1.CamelCatalog) (*RuntimeCatalog, error) { - for _, catalog := range catalogs { - if catalog.Spec.Version == version { - return NewRuntimeCatalog(catalog.Spec), nil +// NewSemVerConstraint -- +func NewSemVerConstraint(versionConstraint string) *semver.Constraints { + constraint, err := semver.NewConstraint(versionConstraint) + if err != nil || constraint == nil { + if err != nil { + log.Debug("Unable to parse version constraint: %s, error:\n", versionConstraint, err.Error()) + } + if constraint == nil { + log.Debug("Unable to parse version constraint: %s\n", versionConstraint) } } - return nil, nil + return constraint } -// FindBestSemVerMatch -- -func FindBestSemVerMatch(constraint *semver.Constraints, catalogs []v1alpha1.CamelCatalog) (*RuntimeCatalog, error) { - versions := make([]*semver.Version, 0) +// NewCatalogVersionCollection -- +func NewCatalogVersionCollection(catalogs []v1alpha1.CamelCatalog) CatalogVersionCollection { + versions := make([]CatalogVersion, 0, len(catalogs)) - for _, catalog := range catalogs { - v, err := semver.NewVersion(catalog.Spec.Version) + for i := range catalogs { + cv, err := semver.NewVersion(catalogs[i].Spec.Version) + if err != nil { + log.Debugf("Invalid semver version (camel) %s", cv) + continue + } + + rv, err := semver.NewVersion(catalogs[i].Spec.RuntimeVersion) if err != nil { - log.Debugf("Invalid semver version %s, skip it", catalog.Spec.Version) + log.Debugf("Invalid semver version (runtime) %s", rv) continue } - versions = append(versions, v) + versions = append(versions, CatalogVersion{ + Version: cv, + RuntimeVersion: rv, + Catalog: &catalogs[i], + }) } + answer := CatalogVersionCollection(versions) + sort.Sort( - sort.Reverse(semver.Collection(versions)), + sort.Reverse(answer), ) - for _, v := range versions { - ver := v - - if constraint.Check(ver) { - for _, catalog := range catalogs { - if catalog.Spec.Version == ver.Original() { - return NewRuntimeCatalog(catalog.Spec), nil - } - } - - } - } - - return nil, nil + return answer } diff --git a/pkg/util/camel/camel_util_test.go b/pkg/util/camel/camel_util_test.go index dc82f23..5dff8ab 100644 --- a/pkg/util/camel/camel_util_test.go +++ b/pkg/util/camel/camel_util_test.go @@ -25,72 +25,147 @@ import ( "github.com/apache/camel-k/pkg/apis/camel/v1alpha1" ) +func TestFindBestMatch_Camel(t *testing.T) { + catalogs := []v1alpha1.CamelCatalog{ + {Spec: v1alpha1.CamelCatalogSpec{Version: "2.23.0", RuntimeVersion: "1.0.0"}}, + {Spec: v1alpha1.CamelCatalogSpec{Version: "2.23.1", RuntimeVersion: "1.0.0"}}, + {Spec: v1alpha1.CamelCatalogSpec{Version: "2.22.1", RuntimeVersion: "1.0.0"}}, + } + + c, err := FindBestMatch(catalogs, "~2.23.x", "1.0.0") + assert.Nil(t, err) + assert.NotNil(t, c) + assert.Equal(t, "2.23.1", c.Version) +} + +func TestFindBestMatch_Runtime(t *testing.T) { + catalogs := []v1alpha1.CamelCatalog{ + {Spec: v1alpha1.CamelCatalogSpec{Version: "2.23.0", RuntimeVersion: "1.0.0"}}, + {Spec: v1alpha1.CamelCatalogSpec{Version: "2.23.0", RuntimeVersion: "1.0.1"}}, + {Spec: v1alpha1.CamelCatalogSpec{Version: "2.22.1", RuntimeVersion: "1.0.0"}}, + } + + c, err := FindBestMatch(catalogs, "2.23.0", "~1.0.x") + assert.Nil(t, err) + assert.NotNil(t, c) + assert.Equal(t, "2.23.0", c.Version) + assert.Equal(t, "1.0.1", c.RuntimeVersion) +} + func TestFindBestMatch(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.0", RuntimeVersion: "1.0.0"}}, + {Spec: v1alpha1.CamelCatalogSpec{Version: "2.23.1", RuntimeVersion: "1.0.0"}}, + {Spec: v1alpha1.CamelCatalogSpec{Version: "2.23.1", RuntimeVersion: "1.0.1"}}, + {Spec: v1alpha1.CamelCatalogSpec{Version: "2.22.1", RuntimeVersion: "1.0.0"}}, } - c, err := FindBestMatch("~2.23.x", catalogs) + c, err := FindBestMatch(catalogs, "~2.23.x", "~1.0.x") assert.Nil(t, err) assert.NotNil(t, c) assert.Equal(t, "2.23.1", c.Version) + assert.Equal(t, "1.0.1", c.RuntimeVersion) +} + +func TestFindExactSemVerMatch_Camel(t *testing.T) { + catalogs := []v1alpha1.CamelCatalog{ + {Spec: v1alpha1.CamelCatalogSpec{Version: "2.23.0", RuntimeVersion: "1.0.0"}}, + {Spec: v1alpha1.CamelCatalogSpec{Version: "2.23.1", RuntimeVersion: "1.0.0"}}, + {Spec: v1alpha1.CamelCatalogSpec{Version: "2.22.1", RuntimeVersion: "1.0.0"}}, + } + + c, err := FindBestMatch(catalogs, "2.23.0", "1.0.0") + assert.Nil(t, err) + assert.NotNil(t, c) + assert.Equal(t, "2.23.0", c.Version) } -func TestFindExactSemVerMatch(t *testing.T) { +func TestFindExactSemVerMatch_Runtime(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.0", RuntimeVersion: "1.0.0"}}, + {Spec: v1alpha1.CamelCatalogSpec{Version: "2.23.1", RuntimeVersion: "1.0.1"}}, + {Spec: v1alpha1.CamelCatalogSpec{Version: "2.22.1", RuntimeVersion: "1.0.0"}}, } - c, err := FindBestMatch("2.23.0", catalogs) + c, err := FindBestMatch(catalogs, "2.23.0", "1.0.0") assert.Nil(t, err) assert.NotNil(t, c) assert.Equal(t, "2.23.0", c.Version) + assert.Equal(t, "1.0.0", c.RuntimeVersion) } -func TestFindExactMatch(t *testing.T) { +func TestFindExactMatch_Camel(t *testing.T) { catalogs := []v1alpha1.CamelCatalog{ - { - Spec: v1alpha1.CamelCatalogSpec{Version: "2.23.1"}, - }, - { - Spec: v1alpha1.CamelCatalogSpec{Version: "2.23.1-tag-00001"}, - }, - { - Spec: v1alpha1.CamelCatalogSpec{Version: "2.23.1-tag-00002"}, - }, + {Spec: v1alpha1.CamelCatalogSpec{Version: "2.23.1", RuntimeVersion: "1.0.0"}}, + {Spec: v1alpha1.CamelCatalogSpec{Version: "2.23.1-tag-00001", RuntimeVersion: "1.0.0"}}, + {Spec: v1alpha1.CamelCatalogSpec{Version: "2.23.1-tag-00002", RuntimeVersion: "1.0.0"}}, + {Spec: v1alpha1.CamelCatalogSpec{Version: "2.22.1", RuntimeVersion: "1.0.0"}}, } - c, err := FindBestMatch("2.23.1-tag-00001", catalogs) + c, err := FindBestMatch(catalogs, "2.23.1-tag-00001", "1.0.0") assert.Nil(t, err) assert.NotNil(t, c) assert.Equal(t, "2.23.1-tag-00001", c.Version) } +func TestFindExactMatch_Runtime(t *testing.T) { + catalogs := []v1alpha1.CamelCatalog{ + {Spec: v1alpha1.CamelCatalogSpec{Version: "2.23.1", RuntimeVersion: "1.0.0"}}, + {Spec: v1alpha1.CamelCatalogSpec{Version: "2.23.1-tag-00001", RuntimeVersion: "1.0.1"}}, + {Spec: v1alpha1.CamelCatalogSpec{Version: "2.23.1-tag-00002", RuntimeVersion: "1.0.0"}}, + {Spec: v1alpha1.CamelCatalogSpec{Version: "2.22.1", RuntimeVersion: "1.0.0"}}, + } + + c, err := FindBestMatch(catalogs, "2.23.1-tag-00001", "1.0.1") + assert.Nil(t, err) + assert.NotNil(t, c) + assert.Equal(t, "2.23.1-tag-00001", c.Version) + assert.Equal(t, "1.0.1", c.RuntimeVersion) +} + +func TestFindRangeMatch_Camel(t *testing.T) { + catalogs := []v1alpha1.CamelCatalog{ + {Spec: v1alpha1.CamelCatalogSpec{Version: "2.23.0", RuntimeVersion: "1.0.0"}}, + {Spec: v1alpha1.CamelCatalogSpec{Version: "2.23.1", RuntimeVersion: "1.0.0"}}, + {Spec: v1alpha1.CamelCatalogSpec{Version: "2.23.2", RuntimeVersion: "1.0.0"}}, + {Spec: v1alpha1.CamelCatalogSpec{Version: "2.22.1", RuntimeVersion: "1.0.0"}}, + } + + c, err := FindBestMatch(catalogs, ">= 2.23.0, < 2.23.2", "1.0.0") + assert.Nil(t, err) + assert.NotNil(t, c) + assert.Equal(t, "2.23.1", c.Version) +} + +func TestFindRangeMatch_Runtime(t *testing.T) { + catalogs := []v1alpha1.CamelCatalog{ + {Spec: v1alpha1.CamelCatalogSpec{Version: "2.23.0", RuntimeVersion: "1.0.0"}}, + {Spec: v1alpha1.CamelCatalogSpec{Version: "2.23.1", RuntimeVersion: "1.0.0"}}, + {Spec: v1alpha1.CamelCatalogSpec{Version: "2.23.2", RuntimeVersion: "1.0.0"}}, + {Spec: v1alpha1.CamelCatalogSpec{Version: "2.23.0", RuntimeVersion: "1.0.2"}}, + {Spec: v1alpha1.CamelCatalogSpec{Version: "2.22.1", RuntimeVersion: "1.0.0"}}, + } + + c, err := FindBestMatch(catalogs, "2.23.0", "> 1.0.1, < 1.0.3") + assert.Nil(t, err) + assert.NotNil(t, c) + assert.Equal(t, "2.23.0", c.Version) + assert.Equal(t, "1.0.2", c.RuntimeVersion) +} + 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"}, - }, + {Spec: v1alpha1.CamelCatalogSpec{Version: "2.23.0", RuntimeVersion: "1.0.0"}}, + {Spec: v1alpha1.CamelCatalogSpec{Version: "2.23.1", RuntimeVersion: "1.0.0"}}, + {Spec: v1alpha1.CamelCatalogSpec{Version: "2.23.2", RuntimeVersion: "1.0.0"}}, + {Spec: v1alpha1.CamelCatalogSpec{Version: "2.23.0", RuntimeVersion: "1.0.2"}}, + {Spec: v1alpha1.CamelCatalogSpec{Version: "2.23.1", RuntimeVersion: "1.0.2"}}, + {Spec: v1alpha1.CamelCatalogSpec{Version: "2.22.1", RuntimeVersion: "1.0.0"}}, } - c, err := FindBestMatch(">= 2.23.0, < 2.23.2", catalogs) + c, err := FindBestMatch(catalogs, ">= 2.23.0, < 2.23.2", "> 1.0.1, < 1.0.3") assert.Nil(t, err) assert.NotNil(t, c) assert.Equal(t, "2.23.1", c.Version) + assert.Equal(t, "1.0.2", c.RuntimeVersion) } diff --git a/pkg/util/defaults/defaults.go b/pkg/util/defaults/defaults.go index cc780cf..a728332 100644 --- a/pkg/util/defaults/defaults.go +++ b/pkg/util/defaults/defaults.go @@ -24,11 +24,17 @@ const ( // Version -- Version = "1.0.0-M3-SNAPSHOT" + // DefaultCamelVersion -- + DefaultCamelVersion = "3.0.0-RC1" + // CamelVersionConstraint -- CamelVersionConstraint = ">=3.0.0-RC1" - // RuntimeVersion -- - RuntimeVersion = "1.0.4" + // DefaultRuntimeVersion -- + DefaultRuntimeVersion = "1.0.4" + + // RuntimeVersionConstraint -- + RuntimeVersionConstraint = ">=1.0.4" // KanikoVersion -- KanikoVersion = "0.9.0" diff --git a/pkg/util/test/catalog.go b/pkg/util/test/catalog.go index 110fe31..b20b9c3 100644 --- a/pkg/util/test/catalog.go +++ b/pkg/util/test/catalog.go @@ -43,5 +43,5 @@ func DefaultCatalog() (*camel.RuntimeCatalog, error) { } } - return camel.FindBestMatch(defaults.CamelVersionConstraint, catalogs) + return camel.FindBestMatch(catalogs, defaults.DefaultCamelVersion, defaults.DefaultRuntimeVersion) } diff --git a/pkg/util/test/catalog_test.go b/pkg/util/test/catalog_test.go index 6cdabc2..a93e1cf 100644 --- a/pkg/util/test/catalog_test.go +++ b/pkg/util/test/catalog_test.go @@ -20,6 +20,8 @@ package test import ( "testing" + "github.com/apache/camel-k/pkg/util/defaults" + "github.com/stretchr/testify/assert" ) @@ -27,7 +29,7 @@ func TestRuntimeContainsEmbeddedArtifacts(t *testing.T) { catalog, err := DefaultCatalog() assert.Nil(t, err) - assert.Equal(t, "3.0.0-RC1", catalog.Version) + assert.Equal(t, defaults.DefaultCamelVersion, catalog.Version) artifact := catalog.GetArtifactByScheme("knative") assert.Equal(t, 1, len(artifact.Schemes)) diff --git a/script/Makefile b/script/Makefile index 14cd358..55c53d9 100644 --- a/script/Makefile +++ b/script/Makefile @@ -17,6 +17,7 @@ VERSIONFILE := pkg/util/defaults/defaults.go VERSION := 1.0.0-M3-SNAPSHOT LAST_RELEASED_VERSION := 1.0.0-M1 RUNTIME_VERSION := 1.0.4 +RUNTIME_VERSION_CONSTRAINT := >=1.0.4 CAMEL_VERSION := 3.0.0-RC1 CAMEL_VERSION_CONSTRAINT := >=3.0.0-RC1 KANIKO_VERSION := 0.9.0 @@ -76,11 +77,17 @@ codegen: @echo " // Version -- " >> $(VERSIONFILE) @echo " Version = \"$(VERSION)\"" >> $(VERSIONFILE) @echo "" >> $(VERSIONFILE) + @echo " // DefaultCamelVersion -- " >> $(VERSIONFILE) + @echo " DefaultCamelVersion = \"$(CAMEL_VERSION)\"" >> $(VERSIONFILE) + @echo "" >> $(VERSIONFILE) @echo " // CamelVersionConstraint -- " >> $(VERSIONFILE) @echo " CamelVersionConstraint = \"$(CAMEL_VERSION_CONSTRAINT)\"" >> $(VERSIONFILE) @echo "" >> $(VERSIONFILE) - @echo " // RuntimeVersion -- " >> $(VERSIONFILE) - @echo " RuntimeVersion = \"$(RUNTIME_VERSION)\"" >> $(VERSIONFILE) + @echo " // DefaultRuntimeVersion -- " >> $(VERSIONFILE) + @echo " DefaultRuntimeVersion = \"$(RUNTIME_VERSION)\"" >> $(VERSIONFILE) + @echo "" >> $(VERSIONFILE) + @echo " // RuntimeVersionConstraint -- " >> $(VERSIONFILE) + @echo " RuntimeVersionConstraint = \"$(RUNTIME_VERSION_CONSTRAINT)\"" >> $(VERSIONFILE) @echo "" >> $(VERSIONFILE) @echo " // KanikoVersion -- " >> $(VERSIONFILE) @echo " KanikoVersion = \"$(KANIKO_VERSION)\"" >> $(VERSIONFILE) diff --git a/script/build_catalog.sh b/script/build_catalog.sh index 2227888..50d580b 100755 --- a/script/build_catalog.sh +++ b/script/build_catalog.sh @@ -26,6 +26,5 @@ fi $rootdir/mvnw -q \ -f ${rootdir}/build/maven/pom-catalog.xml \ -Dcatalog.path=${rootdir}/deploy \ - -Dcatalog.version=$1 \ -Dcamel.version=$1 \ -Druntime.version=$2 \ No newline at end of file