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

nferraro 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 0e2423f  feat(cli): make kamel delete command aware of KameletBindings
0e2423f is described below

commit 0e2423f27dd42a82a8a3aa239705e82be1d1aecd
Author: Tadayoshi Sato <[email protected]>
AuthorDate: Mon May 31 17:34:18 2021 +0900

    feat(cli): make kamel delete command aware of KameletBindings
    
    Kamelet Binding controller now attaches "created.by" labels to
    integrations which a kamelet binding creates, and `kamel delete`
    uses these labels to identify bindings to delete together if there is
    any.
    
    Fix #2305
---
 pkg/cmd/delete.go                       | 74 ++++++++++++++++++++++++++++++++-
 pkg/controller/kameletbinding/common.go |  8 ++++
 2 files changed, 80 insertions(+), 2 deletions(-)

diff --git a/pkg/cmd/delete.go b/pkg/cmd/delete.go
index 716b9a9..e0f17f1 100644
--- a/pkg/cmd/delete.go
+++ b/pkg/cmd/delete.go
@@ -18,11 +18,14 @@ limitations under the License.
 package cmd
 
 import (
+       "context"
        "errors"
        "fmt"
        "strconv"
 
        v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
+       "github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
+       "github.com/apache/camel-k/pkg/client"
        "github.com/apache/camel-k/pkg/util/kubernetes"
        "github.com/spf13/cobra"
        k8errors "k8s.io/apimachinery/pkg/api/errors"
@@ -80,7 +83,7 @@ func (command *deleteCmdOptions) run(args []string) error {
        if len(args) != 0 && !command.DeleteAll {
                for _, arg := range args {
                        name := kubernetes.SanitizeName(arg)
-                       err := DeleteIntegration(command.Context, c, name, 
command.Namespace)
+                       integration, err := getIntegration(command.Context, c, 
name, command.Namespace)
                        if err != nil {
                                if k8errors.IsNotFound(err) {
                                        fmt.Println("Integration " + name + " 
not found. Skipped.")
@@ -88,6 +91,10 @@ func (command *deleteCmdOptions) run(args []string) error {
                                        return err
                                }
                        } else {
+                               err := deleteIntegration(command.Context, c, 
integration)
+                               if err != nil {
+                                       return err
+                               }
                                fmt.Println("Integration " + name + " deleted")
                        }
                }
@@ -106,7 +113,7 @@ func (command *deleteCmdOptions) run(args []string) error {
                }
                for _, integration := range integrationList.Items {
                        integration := integration // pin
-                       err := c.Delete(command.Context, &integration)
+                       err := deleteIntegration(command.Context, c, 
&integration)
                        if err != nil {
                                return err
                        }
@@ -120,3 +127,66 @@ func (command *deleteCmdOptions) run(args []string) error {
 
        return nil
 }
+
+func getIntegration(ctx context.Context, c client.Client, name string, 
namespace string) (*v1.Integration, error) {
+       key := k8sclient.ObjectKey{
+               Name:      name,
+               Namespace: namespace,
+       }
+       answer := v1.NewIntegration(namespace, name)
+       if err := c.Get(ctx, key, &answer); err != nil {
+               return nil, err
+       }
+       return &answer, nil
+}
+
+func deleteIntegration(ctx context.Context, c client.Client, integration 
*v1.Integration) error {
+       deleted, binding, err := deleteKameletBindingIfExists(ctx, c, 
integration)
+       if err != nil {
+               return err
+       }
+       if deleted {
+               // Deleting KameletBinding will automatically clean up the 
integration
+               fmt.Println("KameletBinding " + binding + " deleted")
+               return nil
+       }
+       return c.Delete(ctx, integration)
+}
+
+func deleteKameletBindingIfExists(ctx context.Context, c client.Client, 
integration *v1.Integration) (bool, string, error) {
+       kind, name := findCreator(integration)
+       if kind != v1alpha1.KameletBindingKind || name == "" {
+               return false, "", nil
+       }
+
+       binding := v1alpha1.KameletBinding{
+               TypeMeta: metav1.TypeMeta{
+                       Kind:       kind,
+                       APIVersion: v1alpha1.SchemeGroupVersion.String(),
+               },
+               ObjectMeta: metav1.ObjectMeta{
+                       Namespace: integration.Namespace,
+                       Name:      name,
+               },
+       }
+       err := c.Delete(ctx, &binding)
+       if k8errors.IsNotFound(err) {
+               // Simply skip if binding doesn't exist (could be deleted 
already)
+               return false, name, nil
+       }
+       return err == nil, name, err
+}
+
+func findCreator(integration *v1.Integration) (string, string) {
+       kind := integration.GetLabels()[kubernetes.CamelCreatorLabelKind]
+       name := integration.GetLabels()[kubernetes.CamelCreatorLabelName]
+       if kind == "" && name == "" {
+               // Look up in OwnerReferences in case creator labels are absent
+               for _, owner := range integration.GetOwnerReferences() {
+                       if owner.Kind == v1alpha1.KameletBindingKind {
+                               return owner.Kind, owner.Name
+                       }
+               }
+       }
+       return kind, name
+}
diff --git a/pkg/controller/kameletbinding/common.go 
b/pkg/controller/kameletbinding/common.go
index b27ae61..4ad94ba 100644
--- a/pkg/controller/kameletbinding/common.go
+++ b/pkg/controller/kameletbinding/common.go
@@ -30,6 +30,7 @@ import (
        "github.com/apache/camel-k/pkg/util"
        "github.com/apache/camel-k/pkg/util/bindings"
        "github.com/apache/camel-k/pkg/util/knative"
+       "github.com/apache/camel-k/pkg/util/kubernetes"
        "github.com/pkg/errors"
        k8serrors "k8s.io/apimachinery/pkg/api/errors"
        metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -57,6 +58,13 @@ func createIntegrationFor(ctx context.Context, c 
client.Client, kameletbinding *
                },
        }
 
+       // creator labels
+       if it.GetLabels() == nil {
+               it.SetLabels(make(map[string]string))
+       }
+       it.GetLabels()[kubernetes.CamelCreatorLabelKind] = kameletbinding.Kind
+       it.GetLabels()[kubernetes.CamelCreatorLabelName] = kameletbinding.Name
+
        // start from the integration spec defined in the binding
        if kameletbinding.Spec.Integration != nil {
                it.Spec = *kameletbinding.Spec.Integration.DeepCopy()

Reply via email to