From e7fefee4e25aeb1f556e95afac207f59a32222b8 Mon Sep 17 00:00:00 2001 From: Claudio Miranda Date: Mon, 11 Sep 2023 17:52:58 -0400 Subject: [PATCH] Fix semantic version check of camel-k-runtime Fix https://github.com/apache/camel-k/issues/4715 --- go.mod | 2 +- go.sum | 4 +- pkg/cmd/version.go | 18 +++----- pkg/controller/catalog/initialize.go | 3 +- pkg/install/openshift.go | 2 +- pkg/util/camel/camel_runtime.go | 2 +- pkg/util/camel/camel_types.go | 5 +-- pkg/util/camel/camel_util.go | 59 +-------------------------- pkg/util/camel/camel_util_test.go | 61 ++++------------------------ pkg/util/camel/catalog.go | 2 +- 10 files changed, 23 insertions(+), 135 deletions(-) diff --git a/go.mod b/go.mod index 3dd3fc7965..607424ced1 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index de1ed5aefb..be4773694f 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/pkg/cmd/version.go b/pkg/cmd/version.go index ee2ac25230..732ff70334 100644 --- a/pkg/cmd/version.go +++ b/pkg/cmd/version.go @@ -19,7 +19,6 @@ package cmd import ( "context" - "errors" "fmt" v1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1" @@ -27,7 +26,7 @@ import ( 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" @@ -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 } diff --git a/pkg/controller/catalog/initialize.go b/pkg/controller/catalog/initialize.go index 83553cbe3d..47af7a4c33 100644 --- a/pkg/controller/catalog/initialize.go +++ b/pkg/controller/catalog/initialize.go @@ -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 @@ -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") return err } diff --git a/pkg/install/openshift.go b/pkg/install/openshift.go index a3f2ef18d0..0a22bc7ad1 100644 --- a/pkg/install/openshift.go +++ b/pkg/install/openshift.go @@ -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" diff --git a/pkg/util/camel/camel_runtime.go b/pkg/util/camel/camel_runtime.go index ed613690ae..3ca4d69fe4 100644 --- a/pkg/util/camel/camel_runtime.go +++ b/pkg/util/camel/camel_runtime.go @@ -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 } diff --git a/pkg/util/camel/camel_types.go b/pkg/util/camel/camel_types.go index 51c0015bbd..a6704d8b03 100644 --- a/pkg/util/camel/camel_types.go +++ b/pkg/util/camel/camel_types.go @@ -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 } @@ -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 diff --git a/pkg/util/camel/camel_util.go b/pkg/util/camel/camel_util.go index b5ef592e37..4af1630c09 100644 --- a/pkg/util/camel/camel_util.go +++ b/pkg/util/camel/camel_util.go @@ -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 ( @@ -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) diff --git a/pkg/util/camel/camel_util_test.go b/pkg/util/camel/camel_util_test.go index d696dab958..1c3e76beee 100644 --- a/pkg/util/camel/camel_util_test.go +++ b/pkg/util/camel/camel_util_test.go @@ -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) } @@ -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) } diff --git a/pkg/util/camel/catalog.go b/pkg/util/camel/catalog.go index ce29480602..60734ef461 100644 --- a/pkg/util/camel/catalog.go +++ b/pkg/util/camel/catalog.go @@ -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),