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

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

commit 926e5788fcaa1e03467a5457daff3f93b1efd5ea
Author: Pasquale Congiusti <[email protected]>
AuthorDate: Fri Sep 30 10:05:31 2022 +0200

    chore(cli): add a promote message
    
    Closes #3676
---
 e2e/support/test_support.go                 |  3 ++-
 pkg/cmd/bind.go                             |  9 ++-------
 pkg/cmd/promote.go                          | 18 +++++++++++++++---
 pkg/controller/kameletbinding/initialize.go |  2 +-
 pkg/install/cluster.go                      |  3 ++-
 pkg/install/common.go                       |  2 +-
 pkg/trait/pull_secret.go                    |  2 +-
 pkg/util/kubernetes/replace.go              | 12 +++++++-----
 8 files changed, 31 insertions(+), 20 deletions(-)

diff --git a/e2e/support/test_support.go b/e2e/support/test_support.go
index c6074a096..7860c8bc6 100644
--- a/e2e/support/test_support.go
+++ b/e2e/support/test_support.go
@@ -1864,7 +1864,8 @@ func BindKameletToWithErrorHandler(ns string, name 
string, annotations map[strin
                if errorHandler != nil {
                        kb.Spec.ErrorHandler = asErrorHandlerSpec(errorHandler)
                }
-               return kubernetes.ReplaceResource(TestContext, TestClient(), 
&kb)
+               _, err := kubernetes.ReplaceResource(TestContext, TestClient(), 
&kb)
+               return err
        }
 }
 
diff --git a/pkg/cmd/bind.go b/pkg/cmd/bind.go
index d92128cb6..d956bf271 100644
--- a/pkg/cmd/bind.go
+++ b/pkg/cmd/bind.go
@@ -260,17 +260,12 @@ func (o *bindCmdOptions) run(cmd *cobra.Command, args 
[]string) error {
                return showOutput(cmd, &binding, o.OutputFormat, 
client.GetScheme())
        }
 
-       existed := false
-       err = client.Create(o.Context, &binding)
-       if err != nil && k8serrors.IsAlreadyExists(err) {
-               existed = true
-               err = kubernetes.ReplaceResource(o.Context, client, &binding)
-       }
+       replaced, err := kubernetes.ReplaceResource(o.Context, client, &binding)
        if err != nil {
                return err
        }
 
-       if !existed {
+       if !replaced {
                fmt.Fprintln(cmd.OutOrStdout(), `kamelet binding "`+name+`" 
created`)
        } else {
                fmt.Fprintln(cmd.OutOrStdout(), `kamelet binding "`+name+`" 
updated`)
diff --git a/pkg/cmd/promote.go b/pkg/cmd/promote.go
index 17839810f..3ebd67bbc 100644
--- a/pkg/cmd/promote.go
+++ b/pkg/cmd/promote.go
@@ -127,7 +127,13 @@ func (o *promoteCmdOptions) run(cmd *cobra.Command, args 
[]string) error {
                // KameletBinding promotion
                destKameletBinding := 
o.editKameletBinding(sourceKameletBinding, sourceIntegration)
 
-               return o.replaceResource(destKameletBinding)
+               replaced, err := o.replaceResource(destKameletBinding)
+               if !replaced {
+                       fmt.Fprintln(cmd.OutOrStdout(), `Promoted Integration 
"`+name+`" created`)
+               } else {
+                       fmt.Fprintln(cmd.OutOrStdout(), `Promoted Integration 
"`+name+`" updated`)
+               }
+               return err
        }
        // Plain Integration promotion
        destIntegration := o.editIntegration(sourceIntegration)
@@ -138,7 +144,13 @@ func (o *promoteCmdOptions) run(cmd *cobra.Command, args 
[]string) error {
                return err
        }
 
-       return o.replaceResource(destIntegration)
+       replaced, err := o.replaceResource(destIntegration)
+       if !replaced {
+               fmt.Fprintln(cmd.OutOrStdout(), `Promoted Integration 
"`+name+`" created`)
+       } else {
+               fmt.Fprintln(cmd.OutOrStdout(), `Promoted Integration 
"`+name+`" updated`)
+       }
+       return err
 }
 
 func checkOpsCompatibility(cmd *cobra.Command, source, dest map[string]string) 
error {
@@ -450,7 +462,7 @@ func (o *promoteCmdOptions) editKameletBinding(kb 
*v1alpha1.KameletBinding, it *
        return &dst
 }
 
-func (o *promoteCmdOptions) replaceResource(res k8sclient.Object) error {
+func (o *promoteCmdOptions) replaceResource(res k8sclient.Object) (bool, 
error) {
        return kubernetes.ReplaceResource(o.Context, o._client, res)
 }
 
diff --git a/pkg/controller/kameletbinding/initialize.go 
b/pkg/controller/kameletbinding/initialize.go
index 3f85198b5..0ff43d881 100644
--- a/pkg/controller/kameletbinding/initialize.go
+++ b/pkg/controller/kameletbinding/initialize.go
@@ -55,7 +55,7 @@ func (action *initializeAction) Handle(ctx context.Context, 
kameletbinding *v1al
                return nil, err
        }
 
-       if err := kubernetes.ReplaceResource(ctx, action.client, it); err != 
nil {
+       if _, err := kubernetes.ReplaceResource(ctx, action.client, it); err != 
nil {
                return nil, errors.Wrap(err, "could not create integration for 
kamelet binding")
        }
 
diff --git a/pkg/install/cluster.go b/pkg/install/cluster.go
index b135b2d64..149e7df27 100644
--- a/pkg/install/cluster.go
+++ b/pkg/install/cluster.go
@@ -322,7 +322,8 @@ func installCRD(ctx context.Context, c client.Client, kind 
string, version strin
                return nil
        }
 
-       return kubernetes.ReplaceResource(ctx, c, crd)
+       _, err = kubernetes.ReplaceResource(ctx, c, crd)
+       return err
 }
 
 func isClusterRoleInstalled(ctx context.Context, c client.Client, name string) 
(bool, error) {
diff --git a/pkg/install/common.go b/pkg/install/common.go
index a8606ed68..da4bc3e00 100644
--- a/pkg/install/common.go
+++ b/pkg/install/common.go
@@ -113,7 +113,7 @@ func ObjectOrCollect(ctx context.Context, c client.Client, 
namespace string, col
        }
 
        if force {
-               if err := kubernetes.ReplaceResource(ctx, c, obj); err != nil {
+               if _, err := kubernetes.ReplaceResource(ctx, c, obj); err != 
nil {
                        return err
                }
                // For some resources, also reset the status
diff --git a/pkg/trait/pull_secret.go b/pkg/trait/pull_secret.go
index 55ff73e9d..a53db960a 100644
--- a/pkg/trait/pull_secret.go
+++ b/pkg/trait/pull_secret.go
@@ -109,7 +109,7 @@ func (t *pullSecretTrait) delegateImagePuller(e 
*Environment) error {
        // Applying the RoleBinding directly because it's a resource in the 
operator namespace
        // (different from the integration namespace when delegation is 
enabled).
        rb := t.newImagePullerRoleBinding(e)
-       if err := kubernetes.ReplaceResource(e.Ctx, e.Client, rb); err != nil {
+       if _, err := kubernetes.ReplaceResource(e.Ctx, e.Client, rb); err != 
nil {
                return errors.Wrap(err, "error during the creation of the 
system:image-puller delegating role binding")
        }
        return nil
diff --git a/pkg/util/kubernetes/replace.go b/pkg/util/kubernetes/replace.go
index 9e047b3f9..b71b5a6ac 100644
--- a/pkg/util/kubernetes/replace.go
+++ b/pkg/util/kubernetes/replace.go
@@ -38,16 +38,18 @@ import (
 )
 
 // ReplaceResource allows to completely replace a resource on Kubernetes, 
taking care of immutable fields and resource versions.
-func ReplaceResource(ctx context.Context, c client.Client, res ctrl.Object) 
error {
+func ReplaceResource(ctx context.Context, c client.Client, res ctrl.Object) 
(bool, error) {
+       replaced := false
        err := c.Create(ctx, res)
        if err != nil && k8serrors.IsAlreadyExists(err) {
+               replaced = true
                existing, ok := res.DeepCopyObject().(ctrl.Object)
                if !ok {
-                       return fmt.Errorf("type assertion failed: %v", 
res.DeepCopyObject())
+                       return replaced, fmt.Errorf("type assertion failed: 
%v", res.DeepCopyObject())
                }
                err = c.Get(ctx, ctrl.ObjectKeyFromObject(existing), existing)
                if err != nil {
-                       return err
+                       return replaced, err
                }
                mapRequiredMeta(existing, res)
                mapRequiredServiceData(existing, res)
@@ -56,9 +58,9 @@ func ReplaceResource(ctx context.Context, c client.Client, 
res ctrl.Object) erro
                err = c.Update(ctx, res)
        }
        if err != nil {
-               return errors.Wrap(err, "could not create or replace 
"+findResourceDetails(res))
+               return replaced, errors.Wrap(err, "could not create or replace 
"+findResourceDetails(res))
        }
-       return nil
+       return replaced, nil
 }
 
 func mapRequiredMeta(from ctrl.Object, to ctrl.Object) {

Reply via email to