diff --git a/pkg/apis/camel/v1alpha1/integration_types.go b/pkg/apis/camel/v1alpha1/integration_types.go index e4082e95f8..2b0ba8e9e6 100644 --- a/pkg/apis/camel/v1alpha1/integration_types.go +++ b/pkg/apis/camel/v1alpha1/integration_types.go @@ -166,8 +166,6 @@ const ( IntegrationPhaseRunning IntegrationPhase = "Running" // IntegrationPhaseError -- IntegrationPhaseError IntegrationPhase = "Error" - // IntegrationPhaseDeleting -- - IntegrationPhaseDeleting IntegrationPhase = "Deleting" // IntegrationConditionKitAvailable -- IntegrationConditionKitAvailable IntegrationConditionType = "IntegrationKitAvailable" diff --git a/pkg/cmd/run.go b/pkg/cmd/run.go index 97e4ff6be8..672c3bcd09 100644 --- a/pkg/cmd/run.go +++ b/pkg/cmd/run.go @@ -31,8 +31,6 @@ import ( "strings" "syscall" - "github.com/apache/camel-k/pkg/util/finalizer" - "github.com/apache/camel-k/pkg/apis/camel/v1alpha1" "github.com/apache/camel-k/pkg/client" "github.com/apache/camel-k/pkg/gzip" @@ -85,7 +83,6 @@ func newCmdRun(rootCmdOptions *RootCmdOptions) *cobra.Command { cmd.Flags().BoolVar(&options.Compression, "compression", false, "Enable store source as a compressed binary blob") cmd.Flags().StringSliceVar(&options.Resources, "resource", nil, "Add a resource") cmd.Flags().StringSliceVar(&options.OpenAPIs, "open-api", nil, "Add an OpenAPI v2 spec") - cmd.Flags().StringVar(&options.DeletionPolicy, "deletion-policy", "owner", "Policy used to cleanup child resources, default owner") cmd.Flags().StringSliceVarP(&options.Volumes, "volume", "v", nil, "Mount a volume into the integration container. E.g \"-v pvcname:/container/path\"") cmd.Flags().StringSliceVarP(&options.EnvVars, "env", "e", nil, "Set an environment variable in the integration container. E.g \"-e MY_VAR=my-value\"") @@ -102,7 +99,6 @@ type runCmdOptions struct { Logs bool Sync bool Dev bool - DeletionPolicy string IntegrationKit string IntegrationName string Profile string @@ -376,12 +372,6 @@ func (o *runCmdOptions) updateIntegrationCode(c client.Client, sources []string) }) } - if o.DeletionPolicy == "label" { - integration.Finalizers = []string{ - finalizer.CamelIntegrationFinalizer, - } - } - for _, item := range o.Dependencies { integration.Spec.AddDependency(item) } diff --git a/pkg/controller/integration/delete.go b/pkg/controller/integration/delete.go deleted file mode 100644 index c1d4ec7fd7..0000000000 --- a/pkg/controller/integration/delete.go +++ /dev/null @@ -1,203 +0,0 @@ -/* -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" - "strings" - - "github.com/apache/camel-k/pkg/apis/camel/v1alpha1" - "github.com/apache/camel-k/pkg/util/finalizer" - "github.com/apache/camel-k/pkg/util/kubernetes" - "github.com/apache/camel-k/pkg/util/log" - - "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" - - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - - k8serrors "k8s.io/apimachinery/pkg/api/errors" - k8sclient "sigs.k8s.io/controller-runtime/pkg/client" -) - -// 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) (*v1alpha1.Integration, error) { - l := log.Log.ForIntegration(integration) - - ok, err := finalizer.Exists(integration, finalizer.CamelIntegrationFinalizer) - if err != nil { - return nil, err - } - if !ok { - return nil, nil - } - - // Select all resources created by this integration - selectors := []string{ - fmt.Sprintf("camel.apache.org/integration=%s", integration.Name), - } - - l.Info("Collecting resources to delete") - resources, err := kubernetes.LookUpResources(ctx, action.client, integration.Namespace, selectors) - if err != nil { - return nil, err - } - - // If the ForegroundDeletion deletion is not set remove the finalizer and - // delete child resources from a dedicated goroutine - foreground, err := finalizer.Exists(integration, finalizer.ForegroundDeletion) - if err != nil { - return nil, err - } - - if !foreground { - // - // Async - // - if err := action.removeFinalizer(ctx, integration); err != nil { - return nil, err - } - - go func() { - if err := action.deleteChildResources(context.TODO(), &l, resources); err != nil { - l.Error(err, "error deleting child resources") - } - }() - } else { - // - // Sync - // - if err := action.deleteChildResources(ctx, &l, resources); err != nil { - return nil, err - } - if err = action.removeFinalizer(ctx, integration); err != nil { - return nil, err - } - } - - return nil, nil -} - -func (action *deleteAction) removeFinalizer(ctx context.Context, integration *v1alpha1.Integration) error { - _, err := finalizer.Remove(integration, finalizer.CamelIntegrationFinalizer) - if err != nil { - return err - } - - return action.client.Update(ctx, integration) -} - -func (action *deleteAction) deleteChildResources(ctx context.Context, l *log.Logger, resources []unstructured.Unstructured) error { - l.Infof("Resources to delete: %d", len(resources)) - - var err error - - resources, err = action.deleteChildResourceWithCondition(ctx, l, resources, func(u unstructured.Unstructured) bool { - return u.GetKind() == "Service" && strings.HasPrefix(u.GetAPIVersion(), "serving.knative.dev/") - }) - if err != nil { - return err - } - - resources, err = action.deleteChildResourceWithCondition(ctx, l, resources, func(u unstructured.Unstructured) bool { - return u.GetKind() == "Deployment" - }) - if err != nil { - return err - } - - resources, err = action.deleteChildResourceWithCondition(ctx, l, resources, func(u unstructured.Unstructured) bool { - return u.GetKind() == "ReplicaSet" - }) - if err != nil { - return err - } - - resources, err = action.deleteChildResourceWithCondition(ctx, l, resources, func(u unstructured.Unstructured) bool { - return u.GetKind() == "Pod" - }) - if err != nil { - return err - } - - // Delete remaining resources - for _, resource := range resources { - // pin the resource - resource := resource - - if err := action.deleteChildResource(ctx, l, resource); err != nil { - return err - } - } - - return nil -} - -func (action *deleteAction) deleteChildResourceWithCondition( - ctx context.Context, l *log.Logger, resources []unstructured.Unstructured, condition func(unstructured.Unstructured) bool) ([]unstructured.Unstructured, error) { - - remaining := resources[:0] - for _, resource := range resources { - // pin the resource - resource := resource - - if condition(resource) { - if err := action.deleteChildResource(ctx, l, resource); err != nil { - return resources, err - } - - continue - } - - remaining = append(remaining, resource) - } - - return remaining, nil -} - -func (action *deleteAction) deleteChildResource(ctx context.Context, l *log.Logger, resource unstructured.Unstructured) error { - l.Infof("Deleting child resource: %s:%s/%s", resource.GetAPIVersion(), resource.GetKind(), resource.GetName()) - - err := action.client.Delete(ctx, &resource, k8sclient.PropagationPolicy(metav1.DeletePropagationOrphan)) - if err != nil { - // The resource may have already been deleted - if !k8serrors.IsNotFound(err) { - l.Errorf(err, "cannot delete child resource: %s:%s/%s", resource.GetAPIVersion(), resource.GetKind(), resource.GetName()) - } - } else { - l.Infof("Child resource deleted: %s:%s/%s", resource.GetAPIVersion(), resource.GetKind(), resource.GetName()) - } - - return nil -} diff --git a/pkg/controller/integration/integration_controller.go b/pkg/controller/integration/integration_controller.go index 0923515ae2..a8c7935198 100644 --- a/pkg/controller/integration/integration_controller.go +++ b/pkg/controller/integration/integration_controller.go @@ -228,11 +228,6 @@ func (r *ReconcileIntegration) Reconcile(request reconcile.Request) (reconcile.R target := instance.DeepCopy() targetLog := rlog.ForIntegration(target) - // Delete phase - if target.GetDeletionTimestamp() != nil { - target.Status.Phase = v1alpha1.IntegrationPhaseDeleting - } - if target.Status.Phase == v1alpha1.IntegrationPhaseNone || target.Status.Phase == v1alpha1.IntegrationPhaseWaitingForPlatform { pl, err := platform.GetOrLookup(ctx, r.client, target.Namespace, target.Status.Platform) if err != nil || pl.Status.Phase != v1alpha1.IntegrationPlatformPhaseReady { @@ -262,7 +257,6 @@ func (r *ReconcileIntegration) Reconcile(request reconcile.Request) (reconcile.R NewDeployAction(), NewMonitorAction(), NewErrorAction(), - NewDeleteAction(), } for _, a := range actions { diff --git a/pkg/trait/owner.go b/pkg/trait/owner.go index a056fcb0bc..bef7ab4f6b 100644 --- a/pkg/trait/owner.go +++ b/pkg/trait/owner.go @@ -21,9 +21,6 @@ import ( "strings" "github.com/apache/camel-k/pkg/apis/camel/v1alpha1" - "github.com/apache/camel-k/pkg/util/finalizer" - - "github.com/pkg/errors" appsv1 "k8s.io/api/apps/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -79,30 +76,18 @@ func (t *ownerTrait) Apply(e *Environment) error { } } - ok, err := finalizer.Exists(e.Integration, finalizer.CamelIntegrationFinalizer) - if err != nil { - return errors.Wrap(err, "failed to read finalizer"+finalizer.CamelIntegrationFinalizer) - } - e.Resources.VisitMetaObject(func(res metav1.Object) { - // - // do not add owner reference if the finalizer is set - // so resources are not automatically deleted by k8s - // when owner is deleted - // - if !ok { - references := []metav1.OwnerReference{ - { - APIVersion: e.Integration.APIVersion, - Kind: e.Integration.Kind, - Name: e.Integration.Name, - UID: e.Integration.UID, - Controller: &controller, - BlockOwnerDeletion: &blockOwnerDeletion, - }, - } - res.SetOwnerReferences(references) + references := []metav1.OwnerReference{ + { + APIVersion: e.Integration.APIVersion, + Kind: e.Integration.Kind, + Name: e.Integration.Name, + UID: e.Integration.UID, + Controller: &controller, + BlockOwnerDeletion: &blockOwnerDeletion, + }, } + res.SetOwnerReferences(references) // Transfer annotations t.propagateLabelAndAnnotations(res, targetLabels, targetAnnotations) diff --git a/pkg/trait/owner_test.go b/pkg/trait/owner_test.go index 16410b44af..8b57c0e06d 100644 --- a/pkg/trait/owner_test.go +++ b/pkg/trait/owner_test.go @@ -21,7 +21,6 @@ import ( "testing" "github.com/apache/camel-k/pkg/apis/camel/v1alpha1" - "github.com/apache/camel-k/pkg/util/finalizer" "github.com/stretchr/testify/assert" @@ -29,19 +28,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) -func TestOwnerWithFinalizer(t *testing.T) { - env := SetUpOwnerEnvironment(t) - env.Integration.Finalizers = []string{finalizer.CamelIntegrationFinalizer} - - processTestEnv(t, env) - - assert.NotEmpty(t, env.ExecutedTraits) - assert.NotNil(t, env.GetTrait(ID("owner"))) - - ValidateOwnerResources(t, env, false) -} - -func TestOwnerWithoutFinalizer(t *testing.T) { +func TestOwner(t *testing.T) { env := SetUpOwnerEnvironment(t) processTestEnv(t, env) diff --git a/pkg/util/finalizer/finalizer.go b/pkg/util/finalizer/finalizer.go deleted file mode 100644 index cecc4b73bf..0000000000 --- a/pkg/util/finalizer/finalizer.go +++ /dev/null @@ -1,81 +0,0 @@ -/* -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 finalizer - -import ( - "k8s.io/apimachinery/pkg/api/meta" - "k8s.io/apimachinery/pkg/runtime" - "k8s.io/apimachinery/pkg/util/sets" -) - -const ( - // CamelIntegrationFinalizer -- - CamelIntegrationFinalizer = "finalizer.integration.camel.apache.org" - - // ForegroundDeletion -- - ForegroundDeletion = "foregroundDeletion" -) - -// Add -- -func Add(obj runtime.Object, value string) error { - accessor, err := meta.Accessor(obj) - if err != nil { - return err - } - - finalizers := sets.NewString(accessor.GetFinalizers()...) - finalizers.Insert(value) - accessor.SetFinalizers(finalizers.List()) - - return nil -} - -// Exists -- -func Exists(obj runtime.Object, finalizer string) (bool, error) { - fzs, err := GetAll(obj) - if err != nil { - return false, err - } - for _, fin := range fzs { - if fin == finalizer { - return true, nil - } - } - return false, nil -} - -// GetAll -- -func GetAll(obj runtime.Object) ([]string, error) { - accessor, err := meta.Accessor(obj) - if err != nil { - return nil, err - } - return accessor.GetFinalizers(), nil -} - -// Remove -- -func Remove(obj runtime.Object, value string) ([]string, error) { - accessor, err := meta.Accessor(obj) - if err != nil { - return nil, err - } - finalizers := sets.NewString(accessor.GetFinalizers()...) - finalizers.Delete(value) - newFinalizers := finalizers.List() - accessor.SetFinalizers(newFinalizers) - return newFinalizers, nil -}