Skip to content

Commit 90c3721

Browse files
erikgbstefanprodantenstad
committed
Allow control of finalization garbage collection
Signed-off-by: Erik Godding Boye <[email protected]> Co-authored-by: Stefan Prodan <[email protected]> Co-authored-by: Amund Tenstad <[email protected]>
1 parent a87337c commit 90c3721

File tree

6 files changed

+357
-1
lines changed

6 files changed

+357
-1
lines changed

api/v1/kustomization_types.go

+20
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ const (
3333
MergeValue = "Merge"
3434
IfNotPresentValue = "IfNotPresent"
3535
IgnoreValue = "Ignore"
36+
37+
DeletionPolicyMirrorPrune = "MirrorPrune"
38+
DeletionPolicyDelete = "Delete"
39+
DeletionPolicyOrphan = "Orphan"
3640
)
3741

3842
// KustomizationSpec defines the configuration to calculate the desired state
@@ -95,6 +99,14 @@ type KustomizationSpec struct {
9599
// +required
96100
Prune bool `json:"prune"`
97101

102+
// DeletionPolicy can be used to control garbage collection when this
103+
// Kustomization is deleted. Valid values are ('MirrorPrune', 'Delete',
104+
// 'Orphan'). 'MirrorPrune' mirrors the Prune field (false = 'Ophan',
105+
// true = 'Delete'). Defaults to 'MirrorPrune'.
106+
// +kubebuilder:validation:Enum=MirrorPrune;Delete;Orphan
107+
// +optional
108+
DeletionPolicy string `json:"deletionPolicy,omitempty"`
109+
98110
// A list of resources to be included in the health assessment.
99111
// +optional
100112
HealthChecks []meta.NamespacedObjectKindReference `json:"healthChecks,omitempty"`
@@ -287,6 +299,14 @@ func (in Kustomization) GetRequeueAfter() time.Duration {
287299
return in.Spec.Interval.Duration
288300
}
289301

302+
// GetDeletionPolicy returns the deletion policy and default value if not specified.
303+
func (in Kustomization) GetDeletionPolicy() string {
304+
if in.Spec.DeletionPolicy == "" {
305+
return DeletionPolicyMirrorPrune
306+
}
307+
return in.Spec.DeletionPolicy
308+
}
309+
290310
// GetDependsOn returns the list of dependencies across-namespaces.
291311
func (in Kustomization) GetDependsOn() []meta.NamespacedObjectReference {
292312
return in.Spec.DependsOn

config/crd/bases/kustomize.toolkit.fluxcd.io_kustomizations.yaml

+11
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,17 @@ spec:
9898
required:
9999
- provider
100100
type: object
101+
deletionPolicy:
102+
description: |-
103+
DeletionPolicy can be used to control garbage collection when this
104+
Kustomization is deleted. Valid values are ('MirrorPrune', 'Delete',
105+
'Orphan'). 'MirrorPrune' mirrors the Prune field (false = 'Ophan',
106+
true = 'Delete'). Defaults to 'MirrorPrune'.
107+
enum:
108+
- MirrorPrune
109+
- Delete
110+
- Orphan
111+
type: string
101112
dependsOn:
102113
description: |-
103114
DependsOn may contain a meta.NamespacedObjectReference slice

docs/api/v1/kustomize.md

+30
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,21 @@ bool
208208
</tr>
209209
<tr>
210210
<td>
211+
<code>deletionPolicy</code><br>
212+
<em>
213+
string
214+
</em>
215+
</td>
216+
<td>
217+
<em>(Optional)</em>
218+
<p>DeletionPolicy can be used to control garbage collection when this
219+
Kustomization is deleted. Valid values are (&lsquo;MirrorPrune&rsquo;, &lsquo;Delete&rsquo;,
220+
&lsquo;Orphan&rsquo;). &lsquo;MirrorPrune&rsquo; mirrors the Prune field (false = &lsquo;Ophan&rsquo;,
221+
true = &lsquo;Delete&rsquo;). Defaults to &lsquo;MirrorPrune&rsquo;.</p>
222+
</td>
223+
</tr>
224+
<tr>
225+
<td>
211226
<code>healthChecks</code><br>
212227
<em>
213228
<a href="https://godoc.org/github.com/fluxcd/pkg/apis/meta#NamespacedObjectKindReference">
@@ -716,6 +731,21 @@ bool
716731
</tr>
717732
<tr>
718733
<td>
734+
<code>deletionPolicy</code><br>
735+
<em>
736+
string
737+
</em>
738+
</td>
739+
<td>
740+
<em>(Optional)</em>
741+
<p>DeletionPolicy can be used to control garbage collection when this
742+
Kustomization is deleted. Valid values are (&lsquo;MirrorPrune&rsquo;, &lsquo;Delete&rsquo;,
743+
&lsquo;Orphan&rsquo;). &lsquo;MirrorPrune&rsquo; mirrors the Prune field (false = &lsquo;Ophan&rsquo;,
744+
true = &lsquo;Delete&rsquo;). Defaults to &lsquo;MirrorPrune&rsquo;.</p>
745+
</td>
746+
</tr>
747+
<tr>
748+
<td>
719749
<code>healthChecks</code><br>
720750
<em>
721751
<a href="https://godoc.org/github.com/fluxcd/pkg/apis/meta#NamespacedObjectKindReference">

docs/spec/v1/kustomizations.md

+33
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,39 @@ kustomize.toolkit.fluxcd.io/prune: disabled
169169
For details on how the controller tracks Kubernetes objects and determines what
170170
to garbage collect, see [`.status.inventory`](#inventory).
171171

172+
### Deletion policy
173+
174+
`.spec.deletionPolicy` is an optional field that is allowing control over
175+
garbage collection when a Kustomization object is deleted. The default behavior
176+
is to mirror the value of [`.spec.prune`](#prune).
177+
178+
Valid values:
179+
180+
- `MirrorPrune` (default) - The managed resources will be deleted if `prune` is
181+
`true` and orphaned if `false`.
182+
- `Delete` - Ensure the managed resources are deleted before the Kustomization
183+
is finalized.
184+
- `Orphan` - Leave the managed resources when the Kustomization is deleted.
185+
186+
For special cases when the managed resources are removed by other means (e.g.
187+
the deletion of the namespace specified with
188+
[`.spec.targetNamespace`](#target-namespace)), you can set the deletion policy
189+
to `Orphan`:
190+
191+
```yaml
192+
---
193+
apiVersion: kustomize.toolkit.fluxcd.io/v1
194+
kind: Kustomization
195+
metadata:
196+
name: app
197+
namespace: tenant
198+
spec:
199+
# ...omitted for brevity
200+
targetNamespace: app-namespace
201+
prune: true
202+
deletionPolicy: Orphan
203+
```
204+
172205
### Interval
173206

174207
`.spec.interval` is a required field that specifies the interval at which the

internal/controller/kustomization_controller.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -956,10 +956,17 @@ func (r *KustomizationReconciler) prune(ctx context.Context,
956956
return false, nil
957957
}
958958

959+
func finalizerShouldDeleteResources(obj *kustomizev1.Kustomization) bool {
960+
if obj.GetDeletionPolicy() == kustomizev1.DeletionPolicyMirrorPrune {
961+
return obj.Spec.Prune
962+
}
963+
return obj.Spec.DeletionPolicy == kustomizev1.DeletionPolicyDelete
964+
}
965+
959966
func (r *KustomizationReconciler) finalize(ctx context.Context,
960967
obj *kustomizev1.Kustomization) (ctrl.Result, error) {
961968
log := ctrl.LoggerFrom(ctx)
962-
if obj.Spec.Prune &&
969+
if finalizerShouldDeleteResources(obj) &&
963970
!obj.Spec.Suspend &&
964971
obj.Status.Inventory != nil &&
965972
obj.Status.Inventory.Entries != nil {

0 commit comments

Comments
 (0)