Skip to content

Commit

Permalink
chore(cli): add a promote message
Browse files Browse the repository at this point in the history
  • Loading branch information
squakez committed Sep 30, 2022
1 parent 6911123 commit 276e4a1
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 20 deletions.
3 changes: 2 additions & 1 deletion e2e/support/test_support.go
Original file line number Diff line number Diff line change
Expand Up @@ -1877,7 +1877,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
}
}

Expand Down
9 changes: 2 additions & 7 deletions pkg/cmd/bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand Down
18 changes: 15 additions & 3 deletions pkg/cmd/promote.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/kameletbinding/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/install/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/install/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/trait/pull_secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 7 additions & 5 deletions pkg/util/kubernetes/replace.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) {
Expand Down

0 comments on commit 276e4a1

Please sign in to comment.