forked from apache/camel-k
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add finalizer to ensure integration children are cleaned up apache#477
- Loading branch information
1 parent
5e3d879
commit 224c213
Showing
10 changed files
with
345 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return action.client.Update(ctx, target) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
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 trait | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/apache/camel-k/pkg/util/finalizer" | ||
|
||
"github.com/apache/camel-k/pkg/apis/camel/v1alpha1" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestOwnerWithFinalizer(t *testing.T) { | ||
env := createTestEnv(t, v1alpha1.IntegrationPlatformClusterOpenShift, "camel:core") | ||
env.Integration.Finalizers = []string{finalizer.CamelIntegrationFinalizer} | ||
|
||
processTestEnv(t, env) | ||
|
||
assert.NotEmpty(t, env.ExecutedTraits) | ||
assert.Nil(t, env.GetTrait(ID("owner"))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.