Skip to content

Commit 8be1e16

Browse files
authored
Merge pull request #210 from fluxcd/gc-prune
Refactor garbage collection
2 parents 6a3c585 + a713807 commit 8be1e16

File tree

5 files changed

+35
-63
lines changed

5 files changed

+35
-63
lines changed

config/manager/deployment.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ spec:
1717
prometheus.io/scrape: "true"
1818
prometheus.io/port: "8080"
1919
spec:
20-
terminationGracePeriodSeconds: 10
20+
terminationGracePeriodSeconds: 60
2121
# Required for AWS IAM Role bindings
2222
# https://docs.aws.amazon.com/eks/latest/userguide/iam-roles-for-service-accounts-technical-overview.html
2323
securityContext:

controllers/kustomization_controller.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ func (r *KustomizationReconciler) reconcile(
382382
}
383383

384384
// prune
385-
err = r.prune(client, kustomization, snapshot)
385+
err = r.prune(client, kustomization, checksum)
386386
if err != nil {
387387
return kustomizev1.KustomizationNotReady(
388388
kustomization,
@@ -552,7 +552,7 @@ func (r *KustomizationReconciler) reconcileDelete(ctx context.Context, log logr.
552552
log.Error(err, "Unable to prune for finalizer")
553553
return ctrl.Result{}, err
554554
}
555-
if err := r.prune(client, kustomization, kustomization.Status.Snapshot); err != nil {
555+
if err := r.prune(client, kustomization, ""); err != nil {
556556
r.event(kustomization, kustomization.Status.LastAppliedRevision, events.EventSeverityError, "pruning for deleted resource failed", nil)
557557
// Return the error so we retry the failed garbage collection
558558
return ctrl.Result{}, err
@@ -777,15 +777,15 @@ func (r *KustomizationReconciler) applyWithRetry(kustomization kustomizev1.Kusto
777777
return changeSet, nil
778778
}
779779

780-
func (r *KustomizationReconciler) prune(client client.Client, kustomization kustomizev1.Kustomization, snapshot *kustomizev1.Snapshot) error {
781-
if !kustomization.Spec.Prune || kustomization.Status.Snapshot == nil || snapshot == nil {
780+
func (r *KustomizationReconciler) prune(client client.Client, kustomization kustomizev1.Kustomization, newChecksum string) error {
781+
if !kustomization.Spec.Prune || kustomization.Status.Snapshot == nil {
782782
return nil
783783
}
784-
if kustomization.DeletionTimestamp.IsZero() && kustomization.Status.Snapshot.Checksum == snapshot.Checksum {
784+
if kustomization.DeletionTimestamp.IsZero() && kustomization.Status.Snapshot.Checksum == newChecksum {
785785
return nil
786786
}
787787

788-
gc := NewGarbageCollector(client, *kustomization.Status.Snapshot, r.Log)
788+
gc := NewGarbageCollector(client, *kustomization.Status.Snapshot, newChecksum, r.Log)
789789

790790
if output, ok := gc.Prune(kustomization.GetTimeout(),
791791
kustomization.GetName(),
@@ -798,7 +798,7 @@ func (r *KustomizationReconciler) prune(client client.Client, kustomization kust
798798
strings.ToLower(kustomization.Kind),
799799
fmt.Sprintf("%s/%s", kustomization.GetNamespace(), kustomization.GetName()),
800800
).Info(fmt.Sprintf("garbage collection completed: %s", output))
801-
r.event(kustomization, snapshot.Checksum, events.EventSeverityInfo, output, nil)
801+
r.event(kustomization, newChecksum, events.EventSeverityInfo, output, nil)
802802
}
803803
}
804804
return nil

controllers/kustomization_gc.go

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,18 @@ import (
3030
)
3131

3232
type KustomizeGarbageCollector struct {
33-
snapshot kustomizev1.Snapshot
34-
log logr.Logger
33+
snapshot kustomizev1.Snapshot
34+
newChecksum string
35+
log logr.Logger
3536
client.Client
3637
}
3738

38-
func NewGarbageCollector(kubeClient client.Client, snapshot kustomizev1.Snapshot, log logr.Logger) *KustomizeGarbageCollector {
39+
func NewGarbageCollector(kubeClient client.Client, snapshot kustomizev1.Snapshot, newChecksum string, log logr.Logger) *KustomizeGarbageCollector {
3940
return &KustomizeGarbageCollector{
40-
Client: kubeClient,
41-
snapshot: snapshot,
42-
log: log,
41+
Client: kubeClient,
42+
snapshot: snapshot,
43+
newChecksum: newChecksum,
44+
log: log,
4345
}
4446
}
4547

@@ -65,10 +67,10 @@ func (kgc *KustomizeGarbageCollector) Prune(timeout time.Duration, name string,
6567
Version: gvk.Version,
6668
})
6769

68-
err := kgc.List(ctx, ulist, client.InNamespace(ns), kgc.matchingLabels(name, namespace, kgc.snapshot.Checksum))
70+
err := kgc.List(ctx, ulist, client.InNamespace(ns), kgc.matchingLabels(name, namespace))
6971
if err == nil {
7072
for _, item := range ulist.Items {
71-
if item.GetDeletionTimestamp().IsZero() {
73+
if kgc.isStale(item) && item.GetDeletionTimestamp().IsZero() {
7274
name := fmt.Sprintf("%s/%s/%s", item.GetKind(), item.GetNamespace(), item.GetName())
7375
err = kgc.Delete(ctx, &item)
7476
if err != nil {
@@ -94,10 +96,10 @@ func (kgc *KustomizeGarbageCollector) Prune(timeout time.Duration, name string,
9496
Version: gvk.Version,
9597
})
9698

97-
err := kgc.List(ctx, ulist, kgc.matchingLabels(name, namespace, kgc.snapshot.Checksum))
99+
err := kgc.List(ctx, ulist, kgc.matchingLabels(name, namespace))
98100
if err == nil {
99101
for _, item := range ulist.Items {
100-
if item.GetDeletionTimestamp().IsZero() {
102+
if kgc.isStale(item) && item.GetDeletionTimestamp().IsZero() {
101103
name := fmt.Sprintf("%s/%s", item.GetKind(), item.GetName())
102104
err = kgc.Delete(ctx, &item)
103105
if err != nil {
@@ -120,8 +122,13 @@ func (kgc *KustomizeGarbageCollector) Prune(timeout time.Duration, name string,
120122
return changeSet, true
121123
}
122124

123-
func (kgc *KustomizeGarbageCollector) matchingLabels(name, namespace, checksum string) client.MatchingLabels {
124-
return gcLabels(name, namespace, checksum)
125+
func (kgc *KustomizeGarbageCollector) isStale(obj unstructured.Unstructured) bool {
126+
itemChecksum := obj.GetLabels()[fmt.Sprintf("%s/checksum", kustomizev1.GroupVersion.Group)]
127+
return kgc.newChecksum == "" || itemChecksum != kgc.newChecksum
128+
}
129+
130+
func (kgc *KustomizeGarbageCollector) matchingLabels(name, namespace string) client.MatchingLabels {
131+
return selectorLabels(name, namespace)
125132
}
126133

127134
func gcLabels(name, namespace, checksum string) map[string]string {

go.mod

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,7 @@ require (
3030
sigs.k8s.io/kustomize/api v0.7.0
3131
sigs.k8s.io/yaml v1.2.0
3232
)
33+
34+
// controller-runtime v0.6.4 bug:
35+
// v1.ListOptions is not suitable for converting to \"helm.toolkit.fluxcd.io/v2beta1\" in scheme \"pkg/runtime/scheme.go:101
36+
replace sigs.k8s.io/controller-runtime => sigs.k8s.io/controller-runtime v0.6.3

0 commit comments

Comments
 (0)