Skip to content

Commit

Permalink
Allow disabling of prune on certain resources
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Prodan <stefan.prodan@gmail.com>
  • Loading branch information
stefanprodan committed Feb 7, 2021
1 parent d0f3aa1 commit c887f55
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 15 deletions.
41 changes: 26 additions & 15 deletions controllers/kustomization_gc.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package controllers
import (
"context"
"fmt"
"strings"
"time"

"github.com/go-logr/logr"
Expand Down Expand Up @@ -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))
}
}
}
Expand All @@ -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())
Expand All @@ -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))
}
}

Expand All @@ -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)
}
Expand Down
7 changes: 7 additions & 0 deletions docs/spec/v1beta1/kustomization.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c887f55

Please sign in to comment.