astefanutti commented on a change in pull request #496: Perform garbage 
collection asynchronously
URL: https://github.com/apache/camel-k/pull/496#discussion_r259840131
 
 

 ##########
 File path: pkg/trait/gc.go
 ##########
 @@ -72,36 +74,47 @@ func (t *garbageCollectorTrait) Apply(e *Environment) 
error {
        }
        // Register a post action that deletes the existing resources that are 
labelled
        // with the previous integration generations.
+       // The collection and deletion are performed asynchronously to avoid 
blocking
+       // the reconcile loop.
        e.PostActions = append(e.PostActions, func(environment *Environment) 
error {
-               selectors := []string{
-                       fmt.Sprintf("camel.apache.org/integration=%s", 
e.Integration.Name),
-                       "camel.apache.org/generation",
-                       fmt.Sprintf("camel.apache.org/generation notin (%d)", 
e.Integration.GetGeneration()),
-               }
-
-               // Retrieve older generation resources that may can enlisted 
for garbage collection
-               resources, err := kubernetes.LookUpResources(context.TODO(), 
e.Client, e.Integration.Namespace, selectors)
-               if err != nil {
-                       return err
-               }
-               // And delete them
-               for _, resource := range resources {
-                       // pind the resource
-                       resource := resource
-
-                       err = e.Client.Delete(context.TODO(), &resource)
-                       if err != nil {
-                               // The resource may have already been deleted
-                               if !k8serrors.IsNotFound(err) {
-                                       
t.L.ForIntegration(e.Integration).Errorf(err, "cannot delete child resource: 
%s/%s", resource.GetKind(), resource.GetName())
-                               }
-                       } else {
-                               t.L.ForIntegration(e.Integration).Debugf("child 
resource deleted: %s/%s", resource.GetKind(), resource.GetName())
-                       }
-               }
-
+               go t.garbageCollectResources(e)
                return nil
        })
 
        return nil
 }
+
+func (t *garbageCollectorTrait) garbageCollectResources(e *Environment) {
+       // Retrieve older generation resources that may can enlisted for 
garbage collection
+       // We rely on the discovery API to retrieve all the resources group and 
kind.
+       // That results in an unbounded collection that can be a bit slow.
+       // We may want to refine that step by white-listing or enlisting types 
to speed-up
+       // the collection duration.
+
+       selectors := []string{
+               fmt.Sprintf("camel.apache.org/integration=%s", 
e.Integration.Name),
+               "camel.apache.org/generation",
+               fmt.Sprintf("camel.apache.org/generation notin (%d)", 
e.Integration.GetGeneration()),
 
 Review comment:
   Very possible 🤕!  I've just added a check that makes sure only older 
generation resources are deleted. Thanks!

----------------------------------------------------------------
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:
[email protected]


With regards,
Apache Git Services

Reply via email to