You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by as...@apache.org on 2019/10/10 12:43:28 UTC
[camel-k] 02/03: chore(GC): Use requirements API instead of parsing
labels to create selector
This is an automated email from the ASF dual-hosted git repository.
astefanutti pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel-k.git
commit 2ea9cb6d88e44d38e83c4d127fcf4d01c5ead486
Author: Antonin Stefanutti <an...@stefanutti.fr>
AuthorDate: Wed Oct 9 16:51:37 2019 +0200
chore(GC): Use requirements API instead of parsing labels to create selector
---
pkg/trait/gc.go | 32 ++++++++++++++------------------
1 file changed, 14 insertions(+), 18 deletions(-)
diff --git a/pkg/trait/gc.go b/pkg/trait/gc.go
index 8b29e74..3512ae3 100644
--- a/pkg/trait/gc.go
+++ b/pkg/trait/gc.go
@@ -19,14 +19,13 @@ package trait
import (
"context"
- "fmt"
"strconv"
- "strings"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/labels"
+ "k8s.io/apimachinery/pkg/selection"
"k8s.io/client-go/discovery"
controller "sigs.k8s.io/controller-runtime/pkg/client"
@@ -89,21 +88,22 @@ func (t *garbageCollectorTrait) Apply(e *Environment) error {
}
func (t *garbageCollectorTrait) garbageCollectResources(e *Environment) {
+ integration, _ := labels.NewRequirement("camel.apache.org/integration", selection.Equals, []string{e.Integration.Name})
+ generation, err := labels.NewRequirement("camel.apache.org/generation", selection.LessThan, []string{strconv.FormatInt(e.Integration.GetGeneration(), 10)})
+ if err != nil {
+ t.L.ForIntegration(e.Integration).Errorf(err, "cannot determine generation requirement")
+ return
+ }
+ selector := labels.NewSelector().
+ Add(*integration).
+ Add(*generation)
+
// Retrieve older generation resources to be 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{
- // Select resources labelled with the current integration.
- fmt.Sprintf("camel.apache.org/integration=%s", e.Integration.Name),
- // Garbage collect older generation resources only.
- // By the time async garbage collecting is executed, newer generations may exist.
- fmt.Sprintf("camel.apache.org/generation<%d", e.Integration.GetGeneration()),
- }
-
- resources, err := lookUpResources(context.TODO(), e.Client, e.Integration.Namespace, selectors)
+ resources, err := lookUpResources(context.TODO(), e.Client, e.Integration.Namespace, selector)
if err != nil {
t.L.ForIntegration(e.Integration).Errorf(err, "cannot collect older generation resources")
return
@@ -124,7 +124,8 @@ func (t *garbageCollectorTrait) garbageCollectResources(e *Environment) {
}
}
}
-func lookUpResources(ctx context.Context, client client.Client, namespace string, selectors []string) ([]unstructured.Unstructured, error) {
+
+func lookUpResources(ctx context.Context, client client.Client, namespace string, selector labels.Selector) ([]unstructured.Unstructured, error) {
// We only take types that support the "create" and "list" verbs as:
// - they have to be created to be deleted :) so that excludes read-only
// resources, e.g., aggregated APIs
@@ -136,11 +137,6 @@ func lookUpResources(ctx context.Context, client client.Client, namespace string
return nil, err
}
- selector, err := labels.Parse(strings.Join(selectors, ","))
- if err != nil {
- return nil, err
- }
-
res := make([]unstructured.Unstructured, 0)
for _, t := range types {