Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow disabling of prune on certain resources #267

Merged
merged 1 commit into from
Feb 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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