You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@camel.apache.org by GitBox <gi...@apache.org> on 2019/02/21 18:46:49 UTC

[GitHub] astefanutti commented on a change in pull request #480: add finalizer to ensure integration children are cleaned up

astefanutti commented on a change in pull request #480: add finalizer to ensure integration children are cleaned up
URL: https://github.com/apache/camel-k/pull/480#discussion_r259064392
 
 

 ##########
 File path: pkg/controller/integration/delete.go
 ##########
 @@ -0,0 +1,103 @@
+/*
+Licensed to the Apache Software Foundation (ASF) under one or more
+contributor license agreements.  See the NOTICE file distributed with
+this work for additional information regarding copyright ownership.
+The ASF licenses this file to You under the Apache License, Version 2.0
+(the "License"); you may not use this file except in compliance with
+the License.  You may obtain a copy of the License at
+
+   http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package integration
+
+import (
+	"context"
+	"fmt"
+
+	"github.com/apache/camel-k/pkg/util/finalizer"
+
+	"github.com/apache/camel-k/pkg/util/log"
+
+	"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
+	"github.com/apache/camel-k/pkg/util/kubernetes"
+
+	k8serrors "k8s.io/apimachinery/pkg/api/errors"
+)
+
+// NewDeleteAction creates a new monitoring action for an integration
+func NewDeleteAction() Action {
+	return &deleteAction{}
+}
+
+type deleteAction struct {
+	baseAction
+}
+
+func (action *deleteAction) Name() string {
+	return "delete"
+}
+
+func (action *deleteAction) CanHandle(integration *v1alpha1.Integration) bool {
+	return integration.Status.Phase == v1alpha1.IntegrationPhaseDeleting
+}
+
+func (action *deleteAction) Handle(ctx context.Context, integration *v1alpha1.Integration) error {
+	ok, err := finalizer.Exists(integration, finalizer.CamelIntegrationFinalizer)
+	if err != nil {
+		return err
+	}
+
+	// If the integration does not have any finalizer, just skip this step
+	if !ok {
+		return nil
+	}
+
+	l := log.Log.ForIntegration(integration)
+
+	// Select all resources created by this integration
+	selectors := []string{
+		fmt.Sprintf("camel.apache.org/integration=%s", integration.Name),
+	}
+
+	resources, err := kubernetes.LookUpResources(ctx, action.client, integration.Namespace, selectors)
+	if err != nil {
+		return err
+	}
+
+	// And delete them
+	for _, resource := range resources {
+		// pin the resource
+		resource := resource
+
+		l.Infof("Deleting child resource: %s/%s", resource.GetKind(), resource.GetName())
+
+		err = action.client.Delete(ctx, &resource)
+		if err != nil {
+			// The resource may have already been deleted
+			if !k8serrors.IsNotFound(err) {
+				l.Errorf(err, "cannot delete child resource: %s/%s", resource.GetKind(), resource.GetName())
+			}
+		} else {
+			l.Infof("Child resource deleted: %s/%s", resource.GetKind(), resource.GetName())
+		}
+	}
+
+	target := integration.DeepCopy()
+
+	//
+	// Remove the finalizer to unlock resource
+	//
+	_, err = finalizer.Remove(target, finalizer.CamelIntegrationFinalizer)
 
 Review comment:
   I wonder whether we should check the `foregroundDeletion` finalizer and emulate a background cascading deletion when no present by removing our finaliser first and then delete the child resources.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services