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

commit 629e5ca6774189cd02b1fa4dff917494554d6d57
Author: Pasquale Congiusti <[email protected]>
AuthorDate: Fri Jul 28 15:21:24 2023 +0200

    feat(cmd): promote image only
    
    Ability to show the container image used in some integration
---
 pkg/cmd/promote.go      | 20 ++++++++++++++++++--
 pkg/cmd/promote_test.go | 39 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 57 insertions(+), 2 deletions(-)

diff --git a/pkg/cmd/promote.go b/pkg/cmd/promote.go
index e332f4da2..b12ebc948 100644
--- a/pkg/cmd/promote.go
+++ b/pkg/cmd/promote.go
@@ -58,6 +58,7 @@ func newCmdPromote(rootCmdOptions *RootCmdOptions) 
(*cobra.Command, *promoteCmdO
 
        cmd.Flags().String("to", "", "The namespace where to promote the 
Integration")
        cmd.Flags().StringP("output", "o", "", "Output format. One of: 
json|yaml")
+       cmd.Flags().BoolP("image", "i", false, "Output the container image 
only")
 
        return &cmd, &options
 }
@@ -66,6 +67,7 @@ type promoteCmdOptions struct {
        *RootCmdOptions
        To           string `mapstructure:"to" yaml:",omitempty"`
        OutputFormat string `mapstructure:"output" yaml:",omitempty"`
+       Image        bool   `mapstructure:"image" yaml:",omitempty"`
 }
 
 func (o *promoteCmdOptions) validate(_ *cobra.Command, args []string) error {
@@ -88,7 +90,7 @@ func (o *promoteCmdOptions) run(cmd *cobra.Command, args 
[]string) error {
        if err != nil {
                return fmt.Errorf("could not retrieve cluster client: %w", err)
        }
-       if o.OutputFormat == "" {
+       if !o.isDryRun() {
                // Skip these checks if in dry mode
                opSource, err := operatorInfo(o.Context, c, o.Namespace)
                if err != nil {
@@ -123,7 +125,13 @@ func (o *promoteCmdOptions) run(cmd *cobra.Command, args 
[]string) error {
                return fmt.Errorf("could not promote an Integration in %s 
status", sourceIntegration.Status.Phase)
        }
 
-       if o.OutputFormat == "" {
+       // Image only mode
+       if o.Image {
+               showImageOnly(cmd, sourceIntegration)
+               return nil
+       }
+
+       if !o.isDryRun() {
                // Skip these checks if in dry mode
                err = o.validateDestResources(c, sourceIntegration)
                if err != nil {
@@ -511,6 +519,10 @@ func (o *promoteCmdOptions) replaceResource(res 
k8sclient.Object) (bool, error)
        return kubernetes.ReplaceResource(o.Context, o._client, res)
 }
 
+func (o *promoteCmdOptions) isDryRun() bool {
+       return o.OutputFormat != "" || o.Image
+}
+
 // RoleBinding is required to allow access to images in one namespace
 // by another namespace. Without this on rbac-enabled clusters, the
 // image cannot be pulled.
@@ -541,3 +553,7 @@ func addSystemPullerRoleBinding(ctx context.Context, c 
client.Client, sourceNS s
 
        return err
 }
+
+func showImageOnly(cmd *cobra.Command, integration *v1.Integration) {
+       fmt.Fprintln(cmd.OutOrStdout(), integration.Status.Image)
+}
diff --git a/pkg/cmd/promote_test.go b/pkg/cmd/promote_test.go
index 22529788a..f6817ecbe 100644
--- a/pkg/cmd/promote_test.go
+++ b/pkg/cmd/promote_test.go
@@ -246,3 +246,42 @@ spec:
 status: {}
 `, output)
 }
+
+func TestItImageOnly(t *testing.T) {
+       srcPlatform := v1.NewIntegrationPlatform("default", 
platform.DefaultPlatformName)
+       srcPlatform.Status.Version = defaults.Version
+       srcPlatform.Status.Build.RuntimeVersion = defaults.DefaultRuntimeVersion
+       srcPlatform.Status.Phase = v1.IntegrationPlatformPhaseReady
+       dstPlatform := v1.NewIntegrationPlatform("prod-namespace", 
platform.DefaultPlatformName)
+       dstPlatform.Status.Version = defaults.Version
+       dstPlatform.Status.Build.RuntimeVersion = defaults.DefaultRuntimeVersion
+       dstPlatform.Status.Phase = v1.IntegrationPlatformPhaseReady
+       defaultIntegration := nominalIntegration("my-it-test")
+       srcCatalog := createTestCamelCatalog(srcPlatform)
+       dstCatalog := createTestCamelCatalog(dstPlatform)
+
+       _, promoteCmd, _ := initializePromoteCmdOptions(t, &srcPlatform, 
&dstPlatform, &defaultIntegration, &srcCatalog, &dstCatalog)
+       output, err := test.ExecuteCommand(promoteCmd, cmdPromote, 
"my-it-test", "--to", "prod-namespace", "-i", "-n", "default")
+       assert.Nil(t, err)
+       assert.Equal(t, fmt.Sprintf("my-special-image\n"), output)
+}
+
+func TestPipeImageOnly(t *testing.T) {
+       srcPlatform := v1.NewIntegrationPlatform("default", 
platform.DefaultPlatformName)
+       srcPlatform.Status.Version = defaults.Version
+       srcPlatform.Status.Build.RuntimeVersion = defaults.DefaultRuntimeVersion
+       srcPlatform.Status.Phase = v1.IntegrationPlatformPhaseReady
+       dstPlatform := v1.NewIntegrationPlatform("prod-namespace", 
platform.DefaultPlatformName)
+       dstPlatform.Status.Version = defaults.Version
+       dstPlatform.Status.Build.RuntimeVersion = defaults.DefaultRuntimeVersion
+       dstPlatform.Status.Phase = v1.IntegrationPlatformPhaseReady
+       defaultKB := nominalPipe("my-kb-test")
+       defaultIntegration := nominalIntegration("my-kb-test")
+       srcCatalog := createTestCamelCatalog(srcPlatform)
+       dstCatalog := createTestCamelCatalog(dstPlatform)
+
+       _, promoteCmd, _ := initializePromoteCmdOptions(t, &srcPlatform, 
&dstPlatform, &defaultKB, &defaultIntegration, &srcCatalog, &dstCatalog)
+       output, err := test.ExecuteCommand(promoteCmd, cmdPromote, 
"my-kb-test", "--to", "prod-namespace", "-i", "-n", "default")
+       assert.Nil(t, err)
+       assert.Equal(t, fmt.Sprintf("my-special-image\n"), output)
+}

Reply via email to