Skip to content

Commit

Permalink
Make sure only older generation resources get garbage collected
Browse files Browse the repository at this point in the history
  • Loading branch information
astefanutti authored and valdar committed May 21, 2019
1 parent cddff00 commit 27d14e6
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions pkg/trait/gc.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ import (
"fmt"
"strconv"

"github.com/apache/camel-k/pkg/util/kubernetes"

k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
"github.com/apache/camel-k/pkg/util/kubernetes"
)

type garbageCollectorTrait struct {
Expand Down Expand Up @@ -94,7 +94,6 @@ func (t *garbageCollectorTrait) garbageCollectResources(e *Environment) {
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()),
}
resources, err := kubernetes.LookUpResources(context.TODO(), e.Client, e.Integration.Namespace, selectors)
if err != nil {
Expand All @@ -107,6 +106,18 @@ func (t *garbageCollectorTrait) garbageCollectResources(e *Environment) {
// pin the resource
resource := resource

labels := resource.GetLabels()
generation, err := strconv.ParseInt(labels["camel.apache.org/generation"], 10, 64)
if err != nil {
t.L.ForIntegration(e.Integration).Errorf(err, "cannot parse generation label: %s", labels["camel.apache.org/generation"])
}

// Garbage collect older generation resource only.
// By the time async garbage collecting is executed, newer generations may exist.
if generation >= e.Integration.GetGeneration() {
continue
}

err = e.Client.Delete(context.TODO(), &resource, client.PropagationPolicy(metav1.DeletePropagationBackground))
if err != nil {
// The resource may have already been deleted
Expand Down

0 comments on commit 27d14e6

Please sign in to comment.