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

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


The following commit(s) were added to refs/heads/main by this push:
     new 8f24bac3e fix(#3836) Expose runtime details in kamel version -a -v
8f24bac3e is described below

commit 8f24bac3e81cdb052b852ec2ece48a2ce9df03f3
Author: Jan Bouska <[email protected]>
AuthorDate: Thu Apr 6 10:28:26 2023 +0200

    fix(#3836) Expose runtime details in kamel version -a -v
---
 pkg/cmd/promote_test.go | 18 +++++++++++++++---
 pkg/cmd/version.go      | 21 +++++++++++++++++++--
 pkg/cmd/version_test.go | 34 ++++++++++++++++++++++++++++++++--
 3 files changed, 66 insertions(+), 7 deletions(-)

diff --git a/pkg/cmd/promote_test.go b/pkg/cmd/promote_test.go
index 40741eade..8fd74064e 100644
--- a/pkg/cmd/promote_test.go
+++ b/pkg/cmd/promote_test.go
@@ -63,8 +63,10 @@ func TestIntegrationNotCompatible(t *testing.T) {
        dstPlatform.Status.Build.RuntimeVersion = "0.0.1"
        dstPlatform.Status.Phase = v1.IntegrationPlatformPhaseReady
        defaultIntegration := nominalIntegration("my-it-test")
+       srcCatalog := createCamelCatalog(srcPlatform)
+       dstCatalog := createCamelCatalog(dstPlatform)
 
-       promoteCmdOptions, promoteCmd, _ := initializePromoteCmdOptions(t, 
&srcPlatform, &dstPlatform, &defaultIntegration)
+       promoteCmdOptions, promoteCmd, _ := initializePromoteCmdOptions(t, 
&srcPlatform, &dstPlatform, &defaultIntegration, &srcCatalog, &dstCatalog)
        _, err := test.ExecuteCommand(promoteCmd, cmdPromote, "my-it-test", 
"--to", "prod-namespace", "-o", "yaml", "-n", "default")
        assert.Equal(t, "yaml", promoteCmdOptions.OutputFormat)
        assert.NotNil(t, err)
@@ -84,8 +86,10 @@ func TestIntegrationDryRun(t *testing.T) {
        dstPlatform.Status.Build.RuntimeVersion = defaults.DefaultRuntimeVersion
        dstPlatform.Status.Phase = v1.IntegrationPlatformPhaseReady
        defaultIntegration := nominalIntegration("my-it-test")
+       srcCatalog := createCamelCatalog(srcPlatform)
+       dstCatalog := createCamelCatalog(dstPlatform)
 
-       promoteCmdOptions, promoteCmd, _ := initializePromoteCmdOptions(t, 
&srcPlatform, &dstPlatform, &defaultIntegration)
+       promoteCmdOptions, promoteCmd, _ := initializePromoteCmdOptions(t, 
&srcPlatform, &dstPlatform, &defaultIntegration, &srcCatalog, &dstCatalog)
        output, err := test.ExecuteCommand(promoteCmd, cmdPromote, 
"my-it-test", "--to", "prod-namespace", "-o", "yaml", "-n", "default")
        assert.Equal(t, "yaml", promoteCmdOptions.OutputFormat)
        assert.Nil(t, err)
@@ -121,8 +125,10 @@ func TestKameletBindingDryRun(t *testing.T) {
        dstPlatform.Status.Phase = v1.IntegrationPlatformPhaseReady
        defaultKB := nominalKameletBinding("my-kb-test")
        defaultIntegration := nominalIntegration("my-kb-test")
+       srcCatalog := createCamelCatalog(srcPlatform)
+       dstCatalog := createCamelCatalog(dstPlatform)
 
-       promoteCmdOptions, promoteCmd, _ := initializePromoteCmdOptions(t, 
&srcPlatform, &dstPlatform, &defaultKB, &defaultIntegration)
+       promoteCmdOptions, promoteCmd, _ := initializePromoteCmdOptions(t, 
&srcPlatform, &dstPlatform, &defaultKB, &defaultIntegration, &srcCatalog, 
&dstCatalog)
        output, err := test.ExecuteCommand(promoteCmd, cmdPromote, 
"my-kb-test", "--to", "prod-namespace", "-o", "yaml", "-n", "default")
        assert.Equal(t, "yaml", promoteCmdOptions.OutputFormat)
        assert.Nil(t, err)
@@ -149,3 +155,9 @@ func nominalKameletBinding(name string) 
v1alpha1.KameletBinding {
        kb.Status.Phase = v1alpha1.KameletBindingPhaseReady
        return kb
 }
+
+func createCamelCatalog(platform v1.IntegrationPlatform) v1.CamelCatalog {
+       c := v1.NewCamelCatalog(platform.Namespace, 
defaults.DefaultRuntimeVersion)
+       c.Spec = v1.CamelCatalogSpec{Runtime: v1.RuntimeSpec{Provider: 
platform.Status.Build.RuntimeProvider, Version: 
platform.Status.Build.RuntimeVersion}}
+       return c
+}
diff --git a/pkg/cmd/version.go b/pkg/cmd/version.go
index 0dfabbc08..3314e1274 100644
--- a/pkg/cmd/version.go
+++ b/pkg/cmd/version.go
@@ -20,8 +20,12 @@ package cmd
 import (
        "context"
        "fmt"
+       "regexp"
        "strings"
 
+       v1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1"
+       "github.com/apache/camel-k/v2/pkg/util/camel"
+
        "golang.org/x/text/cases"
        "golang.org/x/text/language"
 
@@ -44,6 +48,8 @@ const (
        infoVersion = "Version"
 )
 
+var replaceRegexp = regexp.MustCompile(`(\s+\.\s*)|(\s+-\s*)`)
+
 func newCmdVersion(rootCmdOptions *RootCmdOptions) (*cobra.Command, 
*versionCmdOptions) {
        options := versionCmdOptions{
                RootCmdOptions: rootCmdOptions,
@@ -119,7 +125,6 @@ func (o *versionCmdOptions) displayOperatorVersion(cmd 
*cobra.Command, c client.
                        fmt.Fprintf(cmd.OutOrStdout(), "Unable to retrieve 
operator version: The IntegrationPlatform resource hasn't been reconciled yet!")
                } else {
                        fmt.Fprintf(cmd.OutOrStdout(), "Camel K Operator %s\n", 
operatorInfo[infoVersion])
-
                        if o.Verbose {
                                for k, v := range operatorInfo {
                                        if k != infoVersion {
@@ -133,7 +138,6 @@ func (o *versionCmdOptions) displayOperatorVersion(cmd 
*cobra.Command, c client.
 
 func operatorInfo(ctx context.Context, c client.Client, namespace string) 
(map[string]string, error) {
        infos := make(map[string]string)
-
        platform, err := platformutil.GetOrFindLocal(ctx, c, namespace)
        if err != nil && k8serrors.IsNotFound(err) {
                // find default operator platform in any namespace
@@ -159,6 +163,17 @@ func operatorInfo(ctx context.Context, c client.Client, 
namespace string) (map[s
                }
        }
 
+       catalog, err := camel.LoadCatalog(ctx, c, namespace, 
v1.RuntimeSpec{Version: platform.Status.Build.RuntimeVersion, Provider: 
platform.Status.Build.RuntimeProvider})
+       if err != nil {
+               return nil, err
+       }
+       if catalog == nil {
+               return nil, fmt.Errorf("CamelCatalog can't be found in %s 
namespace", platform.Namespace)
+       }
+       for k, v := range catalog.CamelCatalogSpec.Runtime.Metadata {
+               infos[k] = v
+       }
+
        ccInfo := fromCamelCase(infos)
        log.Debugf("Operator Info for namespace %s: %v", namespace, ccInfo)
        return ccInfo, nil
@@ -166,8 +181,10 @@ func operatorInfo(ctx context.Context, c client.Client, 
namespace string) (map[s
 
 func fromCamelCase(infos map[string]string) map[string]string {
        textKeys := make(map[string]string)
+
        for k, v := range infos {
                key := 
cases.Title(language.English).String(strings.Join(camelcase.Split(k), " "))
+               key = replaceRegexp.ReplaceAllString(key, " ")
                textKeys[key] = v
        }
 
diff --git a/pkg/cmd/version_test.go b/pkg/cmd/version_test.go
index 383fe9cf9..6dda7f1b6 100644
--- a/pkg/cmd/version_test.go
+++ b/pkg/cmd/version_test.go
@@ -21,6 +21,10 @@ import (
        "fmt"
        "testing"
 
+       v1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1"
+       "github.com/apache/camel-k/v2/pkg/platform"
+       "k8s.io/apimachinery/pkg/runtime"
+
        "github.com/apache/camel-k/v2/pkg/util/defaults"
        "github.com/apache/camel-k/v2/pkg/util/test"
        "github.com/spf13/cobra"
@@ -30,10 +34,12 @@ import (
 const cmdVersion = "version"
 
 // nolint: unparam
-func initializeVersionCmdOptions(t *testing.T) (*versionCmdOptions, 
*cobra.Command, RootCmdOptions) {
+func initializeVersionCmdOptions(t *testing.T, initObjs ...runtime.Object) 
(*versionCmdOptions, *cobra.Command, RootCmdOptions) {
        t.Helper()
 
-       options, rootCmd := kamelTestPreAddCommandInit()
+       fakeClient, err := test.NewFakeClient(initObjs...)
+       assert.Nil(t, err)
+       options, rootCmd := kamelTestPreAddCommandInitWithClient(fakeClient)
        versionCmdOptions := addTestVersionCmd(*options, rootCmd)
        kamelTestPostAddCommandInit(t, rootCmd)
 
@@ -76,6 +82,30 @@ func TestVersionClientVerbose(t *testing.T) {
        assert.Equal(t, fmt.Sprintf("Camel K Client %s\nGit Commit: %s\n", 
defaults.Version, defaults.GitCommit), output)
 }
 
+func TestOperatorVersionVerbose(t *testing.T) {
+       platform := v1.NewIntegrationPlatform("default", 
platform.DefaultPlatformName)
+       platform.Status.Version = defaults.Version
+       platform.Status.Build.RuntimeVersion = defaults.DefaultRuntimeVersion
+       platform.Status.Phase = v1.IntegrationPlatformPhaseReady
+       catalog := v1.NewCamelCatalog(platform.Namespace, 
defaults.DefaultRuntimeVersion)
+       catalog.Spec = v1.CamelCatalogSpec{Runtime: v1.RuntimeSpec{Provider: 
platform.Status.Build.RuntimeProvider, Version: 
platform.Status.Build.RuntimeVersion}}
+       // mocked catalog versions
+       catalog.Spec.Runtime.Metadata = map[string]string{
+               "camel-quarkus.version": "2.16.0",
+               "camel.version":         "3.20.1",
+               "quarkus.version":       "2.16.0.Final",
+       }
+
+       versionCmdOptions, rootCmd, _ := initializeVersionCmdOptions(t, 
&platform, &catalog)
+       output, err := test.ExecuteCommand(rootCmd, cmdVersion, "-v", 
"--operator")
+       assert.Nil(t, err)
+       assert.Equal(t, true, versionCmdOptions.Verbose)
+       assert.Contains(t, output, fmt.Sprintf("Camel K Operator %s\n", 
defaults.Version))
+       assert.Contains(t, output, fmt.Sprintf("Camel Version: %s\n", 
catalog.Spec.Runtime.Metadata["camel.version"]))
+       assert.Contains(t, output, fmt.Sprintf("Camel Quarkus Version: %s\n", 
catalog.Spec.Runtime.Metadata["camel-quarkus.version"]))
+       assert.Contains(t, output, fmt.Sprintf("Quarkus Version: %s\n", 
catalog.Spec.Runtime.Metadata["quarkus.version"]))
+}
+
 func TestCompatibleVersions(t *testing.T) {
        _, rootCmd, _ := initializeVersionCmdOptions(t)
        assert.Equal(t, true, compatibleVersions("1.3.0", "1.3.0", rootCmd))

Reply via email to