From b2cfe6d3f87b6cffddfe721dcd73ab7d5a96c3e8 Mon Sep 17 00:00:00 2001 From: Stefan Prodan Date: Sun, 7 Feb 2021 13:55:18 +0200 Subject: [PATCH] Allow disabling of prune on certain resources Signed-off-by: Stefan Prodan --- controllers/kustomization_gc.go | 41 +++++++++++++++++++----------- docs/spec/v1beta1/kustomization.md | 7 +++++ 2 files changed, 33 insertions(+), 15 deletions(-) diff --git a/controllers/kustomization_gc.go b/controllers/kustomization_gc.go index bad5c81b..cdb8f54c 100644 --- a/controllers/kustomization_gc.go +++ b/controllers/kustomization_gc.go @@ -19,7 +19,6 @@ package controllers import ( "context" "fmt" - "strings" "time" "github.com/go-logr/logr" @@ -71,25 +70,27 @@ func (kgc *KustomizeGarbageCollector) Prune(timeout time.Duration, name string, err := kgc.List(ctx, ulist, client.InNamespace(ns), kgc.matchingLabels(name, namespace)) if err == nil { for _, item := range ulist.Items { + id := fmt.Sprintf("%s/%s/%s", item.GetKind(), item.GetNamespace(), item.GetName()) + if kgc.shouldSkip(item) { + kgc.log.V(1).Info(fmt.Sprintf("gc is disabled for '%s'", id)) + continue + } + if kgc.isStale(item) && item.GetDeletionTimestamp().IsZero() { - gvkn := fmt.Sprintf("%s/%s/%s", item.GetKind(), item.GetNamespace(), item.GetName()) err = kgc.Delete(ctx, &item) if err != nil { - outErr += fmt.Sprintf("delete failed for %s: %v\n", gvkn, err) + outErr += fmt.Sprintf("delete failed for %s: %v\n", id, err) } else { if len(item.GetFinalizers()) > 0 { - changeSet += fmt.Sprintf("%s marked for deletion\n", gvkn) + changeSet += fmt.Sprintf("%s marked for deletion\n", id) } else { - changeSet += fmt.Sprintf("%s deleted\n", gvkn) + changeSet += fmt.Sprintf("%s deleted\n", id) } } } } } else { - kgc.log.V(1).WithValues( - strings.ToLower(kustomizev1.KustomizationKind), - fmt.Sprintf("%s/%s", namespace, name), - ).Info(fmt.Sprintf("gc query failed for %s: %v", gvk.Kind, err)) + kgc.log.V(1).Info(fmt.Sprintf("gc query failed for %s: %v", gvk.Kind, err)) } } } @@ -105,11 +106,17 @@ func (kgc *KustomizeGarbageCollector) Prune(timeout time.Duration, name string, err := kgc.List(ctx, ulist, kgc.matchingLabels(name, namespace)) if err == nil { for _, item := range ulist.Items { + id := fmt.Sprintf("%s/%s", item.GetKind(), item.GetName()) + + if kgc.shouldSkip(item) { + kgc.log.V(1).Info(fmt.Sprintf("gc is disabled for '%s'", id)) + continue + } + if kgc.isStale(item) && item.GetDeletionTimestamp().IsZero() { - gvkn := fmt.Sprintf("%s/%s", item.GetKind(), item.GetName()) err = kgc.Delete(ctx, &item) if err != nil { - outErr += fmt.Sprintf("delete failed for %s: %v\n", gvkn, err) + outErr += fmt.Sprintf("delete failed for %s: %v\n", id, err) } else { if len(item.GetFinalizers()) > 0 { changeSet += fmt.Sprintf("%s/%s marked for deletion\n", item.GetKind(), item.GetName()) @@ -120,10 +127,7 @@ func (kgc *KustomizeGarbageCollector) Prune(timeout time.Duration, name string, } } } else { - kgc.log.V(1).WithValues( - strings.ToLower(kustomizev1.KustomizationKind), - fmt.Sprintf("%s/%s", namespace, name), - ).Info(fmt.Sprintf("gc query failed for %s: %v", gvk.Kind, err)) + kgc.log.V(1).Info(fmt.Sprintf("gc query failed for %s: %v", gvk.Kind, err)) } } @@ -138,6 +142,13 @@ func (kgc *KustomizeGarbageCollector) isStale(obj unstructured.Unstructured) boo return kgc.newChecksum == "" || itemChecksum != kgc.newChecksum } +func (kgc *KustomizeGarbageCollector) shouldSkip(obj unstructured.Unstructured) bool { + key := fmt.Sprintf("%s/prune", kustomizev1.GroupVersion.Group) + val := "disabled" + + return obj.GetLabels()[key] == val || obj.GetAnnotations()[key] == val +} + func (kgc *KustomizeGarbageCollector) matchingLabels(name, namespace string) client.MatchingLabels { return selectorLabels(name, namespace) } diff --git a/docs/spec/v1beta1/kustomization.md b/docs/spec/v1beta1/kustomization.md index adfdc389..970f61d2 100644 --- a/docs/spec/v1beta1/kustomization.md +++ b/docs/spec/v1beta1/kustomization.md @@ -341,6 +341,13 @@ labels: The checksum label value is updated if the content of `spec.path` changes. When pruning is disabled, the checksum label is omitted. +You can disable pruning for certain resources by either +labeling or annotating them with: + +```yaml +kustomize.toolkit.fluxcd.io/prune: disabled +``` + ## Health assessment A Kustomization can contain a series of health checks used to determine the