Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix semantic version check of camel-k-runtime #4731

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/apache/camel-k/v2
go 1.20

require (
github.com/Masterminds/semver v1.5.0
github.com/Masterminds/semver/v3 v3.2.1
github.com/container-tools/spectrum v0.6.35
github.com/evanphx/json-patch v5.6.0+incompatible
github.com/fsnotify/fsnotify v1.6.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/toml v1.2.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/Masterminds/semver v1.5.0 h1:H65muMkzWKEuNDnfl9d70GUjFniHKHRbFPGBuZ3QEww=
github.com/Masterminds/semver v1.5.0/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y=
github.com/Masterminds/semver/v3 v3.2.1 h1:RN9w6+7QoMeJVGyfmbcgs28Br8cvmnucEXnY0rYXWg0=
github.com/Masterminds/semver/v3 v3.2.1/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYrf8m9wsX0PNOMQ=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/ProtonMail/go-crypto v0.0.0-20230217124315-7d5c6f04bbb8 h1:wPbRQzjjwFc0ih8puEVAOFGELsn1zoIIYdxvML7mDxA=
github.com/ProtonMail/go-crypto v0.0.0-20230217124315-7d5c6f04bbb8/go.mod h1:I0gYDMZ6Z5GRU7l58bNFSkPTFN6Yl12dsUlAZ8xy98g=
Expand Down
18 changes: 5 additions & 13 deletions pkg/cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@ package cmd

import (
"context"
"errors"
"fmt"

v1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1"
"github.com/apache/camel-k/v2/pkg/util/camel"

k8serrors "k8s.io/apimachinery/pkg/api/errors"

"github.com/Masterminds/semver"
semver "github.com/Masterminds/semver/v3"
"github.com/spf13/cobra"

"github.com/apache/camel-k/v2/pkg/client"
Expand Down Expand Up @@ -153,19 +152,12 @@ func operatorInfo(ctx context.Context, c client.Client, namespace string) (map[s
if err != nil {
return nil, err
}
if catalog == nil {
msg := fmt.Sprintf("CamelCatalog version: %s", platform.Status.Build.RuntimeVersion)
if platform.Status.Build.RuntimeProvider != "" {
msg += fmt.Sprintf(", provider: %s", platform.Status.Build.RuntimeProvider)
}
msg += fmt.Sprintf(" can't be found in %s namespace", platform.Namespace)
return nil, errors.New(msg)
if catalog != nil {
infos["Camel Quarkus version"] = catalog.CamelCatalogSpec.Runtime.Metadata["camel-quarkus.version"]
infos["Camel version"] = catalog.CamelCatalogSpec.Runtime.Metadata["camel.version"]
infos["Quarkus version"] = catalog.CamelCatalogSpec.Runtime.Metadata["quarkus.version"]
}

infos["Camel Quarkus version"] = catalog.CamelCatalogSpec.Runtime.Metadata["camel-quarkus.version"]
infos["Camel version"] = catalog.CamelCatalogSpec.Runtime.Metadata["camel.version"]
infos["Quarkus version"] = catalog.CamelCatalogSpec.Runtime.Metadata["quarkus.version"]

return infos, nil
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/controller/catalog/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ func buildRuntimeBuilderImageSpectrum(options spectrum.Options) error {
if options.Base == "" {
return fmt.Errorf("missing base image, likely catalog is not compatible with this Camel K version")
}
Log.Infof("Making up Camel K builder container %s", options.Target)
Log.Infof("Making up Camel K builder container %s - base image: %s", options.Target, options.Base)

if jobs := runtime.GOMAXPROCS(0); jobs > 1 {
options.Jobs = jobs
Expand All @@ -483,6 +483,7 @@ func buildRuntimeBuilderImageSpectrum(options spectrum.Options) error {

_, err := spectrum.Build(options, directories...)
if err != nil {
Log.Error(err, "Error trying to build Camel K builder container")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are returning the error. The caller should know what to do with it, if printing a warning or what else.

return err
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/install/openshift.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"fmt"
"reflect"

"github.com/Masterminds/semver"
semver "github.com/Masterminds/semver/v3"

k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down
2 changes: 1 addition & 1 deletion pkg/util/camel/camel_runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func LoadCatalog(ctx context.Context, client client.Client, namespace string, ru
return nil, err
}

catalog, err := findBestMatch(list.Items, runtime)
catalog, err := findCatalog(list.Items, runtime)
if err != nil {
return nil, err
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/util/camel/camel_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@ limitations under the License.
package camel

import (
"github.com/Masterminds/semver"
v1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1"
)

// CatalogVersion --.
type CatalogVersion struct {
RuntimeVersion *semver.Version
RuntimeVersion string
Catalog *v1.CamelCatalog
}

Expand All @@ -40,7 +39,7 @@ func (c CatalogVersionCollection) Len() int {
// 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 {
return c[i].RuntimeVersion.LessThan(c[j].RuntimeVersion)
return c[i].RuntimeVersion < c[j].RuntimeVersion
}

// Swap is needed for the sort interface to replace the CatalogVersion objects
Expand Down
59 changes: 1 addition & 58 deletions pkg/util/camel/camel_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,9 @@ package camel

import (
"path/filepath"
"sort"
"strings"

"github.com/Masterminds/semver"

v1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1"
"github.com/apache/camel-k/v2/pkg/util/log"
)

var (
Expand All @@ -39,68 +35,15 @@ var (
ServiceBindingsMountPath = filepath.Join(ConfDPath, "_servicebindings")
)

func findBestMatch(catalogs []v1.CamelCatalog, runtime v1.RuntimeSpec) (*RuntimeCatalog, error) {
func findCatalog(catalogs []v1.CamelCatalog, runtime v1.RuntimeSpec) (*RuntimeCatalog, error) {
for _, catalog := range catalogs {
if catalog.Spec.Runtime.Version == runtime.Version && catalog.Spec.Runtime.Provider == runtime.Provider {
return NewRuntimeCatalog(catalog), nil
}
}

rc := newSemVerConstraint(runtime.Version)
if rc == nil {
return nil, nil
}

cc := newCatalogVersionCollection(catalogs)
for _, c := range cc {
if rc.Check(c.RuntimeVersion) {
return NewRuntimeCatalog(*c.Catalog), nil
}
}

return nil, nil
}

func newSemVerConstraint(versionConstraint string) *semver.Constraints {
constraint, err := semver.NewConstraint(versionConstraint)
if err != nil || constraint == nil {
if err != nil {
log.Debugf("Unable to parse version constraint: %s, error: %s", versionConstraint, err.Error())
}
if constraint == nil {
log.Debugf("Unable to parse version constraint: %s", versionConstraint)
}
}

return constraint
}

func newCatalogVersionCollection(catalogs []v1.CamelCatalog) CatalogVersionCollection {
versions := make([]CatalogVersion, 0, len(catalogs))

for i := range catalogs {
rv, err := semver.NewVersion(catalogs[i].Spec.Runtime.Version)
if err != nil {
log.Debugf("Invalid semver version (runtime) %s", rv)

continue
}

versions = append(versions, CatalogVersion{
RuntimeVersion: rv,
Catalog: &catalogs[i],
})
}

answer := CatalogVersionCollection(versions)

sort.Sort(
sort.Reverse(answer),
)

return answer
}

func getDependency(artifact v1.CamelArtifact, runtimeProvider v1.RuntimeProvider) string {
if runtimeProvider == v1.RuntimeProviderQuarkus {
return strings.Replace(artifact.ArtifactID, "camel-quarkus-", "camel:", 1)
Expand Down
61 changes: 7 additions & 54 deletions pkg/util/camel/camel_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,52 +18,36 @@ limitations under the License.
package camel

import (
"sort"
"testing"

"github.com/Masterminds/semver"
"github.com/stretchr/testify/assert"

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

func TestFindBestMatch(t *testing.T) {
func TestFindExactMatch(t *testing.T) {
catalogs := []v1.CamelCatalog{
{Spec: v1.CamelCatalogSpec{Runtime: v1.RuntimeSpec{Version: "1.0.0", Provider: v1.RuntimeProviderQuarkus}}},
{Spec: v1.CamelCatalogSpec{Runtime: v1.RuntimeSpec{Version: "1.0.1", Provider: v1.RuntimeProviderQuarkus}}},
}

c, err := findBestMatch(catalogs, v1.RuntimeSpec{Version: "~1.0.x", Provider: v1.RuntimeProviderQuarkus})
assert.Nil(t, err)
assert.NotNil(t, c)
assert.Equal(t, "1.0.1", c.Runtime.Version)
assert.Equal(t, v1.RuntimeProviderQuarkus, c.Runtime.Provider)
}

func TestFindExactSemVerMatch(t *testing.T) {
catalogs := []v1.CamelCatalog{
{Spec: v1.CamelCatalogSpec{Runtime: v1.RuntimeSpec{Version: "1.0.0", Provider: v1.RuntimeProviderQuarkus}}},
{Spec: v1.CamelCatalogSpec{Runtime: v1.RuntimeSpec{Version: "1.0.1", Provider: v1.RuntimeProviderQuarkus}}},
}

c, err := findBestMatch(catalogs, v1.RuntimeSpec{Version: "1.0.0", Provider: v1.RuntimeProviderQuarkus})
c, err := findCatalog(catalogs, v1.RuntimeSpec{Version: "1.0.0", Provider: v1.RuntimeProviderQuarkus})
assert.Nil(t, err)
assert.NotNil(t, c)
assert.Equal(t, "1.0.0", c.Runtime.Version)
assert.Equal(t, v1.RuntimeProviderQuarkus, c.Runtime.Provider)
}

func TestFindRangeMatch(t *testing.T) {
func TestFindExactMatchWithSuffix(t *testing.T) {
catalogs := []v1.CamelCatalog{
{Spec: v1.CamelCatalogSpec{Runtime: v1.RuntimeSpec{Version: "1.0.0", Provider: v1.RuntimeProviderQuarkus}}},
{Spec: v1.CamelCatalogSpec{Runtime: v1.RuntimeSpec{Version: "1.0.1", Provider: v1.RuntimeProviderQuarkus}}},
{Spec: v1.CamelCatalogSpec{Runtime: v1.RuntimeSpec{Version: "1.0.2", Provider: v1.RuntimeProviderQuarkus}}},
{Spec: v1.CamelCatalogSpec{Runtime: v1.RuntimeSpec{Version: "1.0.1.beta-0001", Provider: v1.RuntimeProviderQuarkus}}},
}

c, err := findBestMatch(catalogs, v1.RuntimeSpec{Version: "> 1.0.1, < 1.0.3", Provider: v1.RuntimeProviderQuarkus})
c, err := findCatalog(catalogs, v1.RuntimeSpec{Version: "1.0.1.beta-0001", Provider: v1.RuntimeProviderQuarkus})
assert.Nil(t, err)
assert.NotNil(t, c)
assert.Equal(t, "1.0.2", c.Runtime.Version)
assert.Equal(t, "1.0.1.beta-0001", c.Runtime.Version)
assert.Equal(t, v1.RuntimeProviderQuarkus, c.Runtime.Provider)
}

Expand All @@ -72,38 +56,7 @@ func TestMissingMatch(t *testing.T) {
{Spec: v1.CamelCatalogSpec{Runtime: v1.RuntimeSpec{Version: "1.0.0", Provider: v1.RuntimeProviderQuarkus}}},
}

c, err := findBestMatch(catalogs, v1.RuntimeSpec{Version: "1.0.1", Provider: v1.RuntimeProviderQuarkus})
assert.Nil(t, err)
assert.Nil(t, c)
}

func TestNewCatalogVersionCollection(t *testing.T) {
catalogs := []v1.CamelCatalog{
{Spec: v1.CamelCatalogSpec{Runtime: v1.RuntimeSpec{Version: "1.0.0", Provider: v1.RuntimeProviderQuarkus}}},
}

versions := make([]CatalogVersion, 0, len(catalogs))
rv, _ := semver.NewVersion(catalogs[0].Spec.Runtime.Version)
versions = append(versions, CatalogVersion{
RuntimeVersion: rv,
Catalog: &catalogs[0],
})
expected := CatalogVersionCollection(versions)
sort.Sort(sort.Reverse(expected))

c := newCatalogVersionCollection(catalogs)

assert.Equal(t, expected, c)

}
func TestIncorrectConstraint(t *testing.T) {
rc := newSemVerConstraint("1.A.0")
assert.Nil(t, rc)

catalogs := []v1.CamelCatalog{
{Spec: v1.CamelCatalogSpec{Runtime: v1.RuntimeSpec{Version: "1.A.0", Provider: v1.RuntimeProviderQuarkus}}},
}
c, err := findBestMatch(catalogs, v1.RuntimeSpec{Version: "1.0.0", Provider: v1.RuntimeProviderQuarkus})
c, err := findCatalog(catalogs, v1.RuntimeSpec{Version: "1.0.1", Provider: v1.RuntimeProviderQuarkus})
assert.Nil(t, err)
assert.Nil(t, c)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/util/camel/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func catalogForRuntimeProvider(provider v1.RuntimeProvider) (*RuntimeCatalog, er
catalogs = append(catalogs, c)
}

return findBestMatch(catalogs, v1.RuntimeSpec{
return findCatalog(catalogs, v1.RuntimeSpec{
Version: defaults.DefaultRuntimeVersion,
Provider: provider,
Metadata: make(map[string]string),
Expand Down
Loading