You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by pc...@apache.org on 2025/11/22 11:13:10 UTC

(camel-k) branch main updated: chore(trait): gc can be deleted test

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

pcongiusti 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 7576f54fd chore(trait): gc can be deleted test
7576f54fd is described below

commit 7576f54fdcd4f813d6db658c77fa482da9bc07d7
Author: Pasquale Congiusti <pa...@gmail.com>
AuthorDate: Sat Nov 22 09:50:17 2025 +0100

    chore(trait): gc can be deleted test
---
 pkg/trait/gc.go      | 12 ++++++++----
 pkg/trait/gc_test.go | 39 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 47 insertions(+), 4 deletions(-)

diff --git a/pkg/trait/gc.go b/pkg/trait/gc.go
index c25f19f9d..7520cca9e 100644
--- a/pkg/trait/gc.go
+++ b/pkg/trait/gc.go
@@ -198,6 +198,8 @@ func (t *gcTrait) garbageCollectResources(e *Environment) error {
 	return t.deleteEachOf(e.Ctx, deletableGVKs, e, selector)
 }
 
+// deleteEachOf takes care of the effective deletion of each deletableGVKs passed for the given selector. It should be any older generation resource
+// of the Integration.
 func (t *gcTrait) deleteEachOf(ctx context.Context, deletableGVKs map[schema.GroupVersionKind]struct{}, e *Environment, selector labels.Selector) error {
 	for GVK := range deletableGVKs {
 		resources := unstructured.UnstructuredList{
@@ -220,7 +222,7 @@ func (t *gcTrait) deleteEachOf(ctx context.Context, deletableGVKs map[schema.Gro
 
 		for _, resource := range resources.Items {
 			r := resource
-			if !t.canBeDeleted(e, r) {
+			if !canBeDeleted(e.Integration, r) {
 				continue
 			}
 			err := t.Client.Delete(ctx, &r, ctrl.PropagationPolicy(metav1.DeletePropagationBackground))
@@ -238,10 +240,10 @@ func (t *gcTrait) deleteEachOf(ctx context.Context, deletableGVKs map[schema.Gro
 	return nil
 }
 
-func (t *gcTrait) canBeDeleted(e *Environment, u unstructured.Unstructured) bool {
-	// Only delete direct children of the integration, otherwise we can affect the behavior of external controllers (i.e. Knative)
+// canBeDeleted is an important security check. We make sure that we are only deleting those resources belonging to the given Integration.
+func canBeDeleted(it *v1.Integration, u unstructured.Unstructured) bool {
 	for _, o := range u.GetOwnerReferences() {
-		if o.Kind == v1.IntegrationKind && strings.HasPrefix(o.APIVersion, v1.SchemeGroupVersion.Group) && o.Name == e.Integration.Name {
+		if o.Kind == v1.IntegrationKind && strings.HasPrefix(o.APIVersion, v1.SchemeGroupVersion.Group) && o.Name == it.Name {
 			return true
 		}
 	}
@@ -249,6 +251,8 @@ func (t *gcTrait) canBeDeleted(e *Environment, u unstructured.Unstructured) bool
 	return false
 }
 
+// getDeletableTypes returns the list of deletable types resources, inspecting the rules for which the operator SA is allowed in the
+// Integration namespace.
 func (t *gcTrait) getDeletableTypes(e *Environment) (map[schema.GroupVersionKind]struct{}, error) {
 	lock.Lock()
 	defer lock.Unlock()
diff --git a/pkg/trait/gc_test.go b/pkg/trait/gc_test.go
index cb8bcab18..40d77f1b4 100644
--- a/pkg/trait/gc_test.go
+++ b/pkg/trait/gc_test.go
@@ -30,6 +30,7 @@ import (
 	appsv1 "k8s.io/api/apps/v1"
 	corev1 "k8s.io/api/core/v1"
 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
+	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
 	"k8s.io/utils/ptr"
 	eventingv1 "knative.dev/eventing/pkg/apis/eventing/v1"
 	servingv1 "knative.dev/serving/pkg/apis/serving/v1"
@@ -349,3 +350,41 @@ func createNominalGCTest() (*gcTrait, *Environment) {
 
 	return trait, environment
 }
+
+func TestCanResourceBeDeleted(t *testing.T) {
+	it := &v1.Integration{
+		ObjectMeta: metav1.ObjectMeta{
+			Name: "my-it",
+		},
+	}
+
+	resNoOwner := unstructured.Unstructured{}
+	resNoOwner.SetOwnerReferences(
+		[]metav1.OwnerReference{},
+	)
+	assert.False(t, canBeDeleted(it, resNoOwner))
+
+	resNotThisItOwner := unstructured.Unstructured{}
+	resNotThisItOwner.SetOwnerReferences(
+		[]metav1.OwnerReference{
+			metav1.OwnerReference{
+				APIVersion: v1.SchemeGroupVersion.String(),
+				Kind:       "Integration",
+				Name:       "another-it",
+			},
+		},
+	)
+	assert.False(t, canBeDeleted(it, resNotThisItOwner))
+
+	resThisItOwner := unstructured.Unstructured{}
+	resThisItOwner.SetOwnerReferences(
+		[]metav1.OwnerReference{
+			metav1.OwnerReference{
+				APIVersion: v1.SchemeGroupVersion.String(),
+				Kind:       "Integration",
+				Name:       "my-it",
+			},
+		},
+	)
+	assert.True(t, canBeDeleted(it, resThisItOwner))
+}