Skip to content

Commit 1e2538a

Browse files
authored
chore: improved orphan deletion description (#1024)
1 parent 18989fc commit 1e2538a

File tree

3 files changed

+49
-8
lines changed

3 files changed

+49
-8
lines changed

controllers/basic_controller.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -379,23 +379,24 @@ func (i *instanceReconcilerHelper) getObjectRefs(ctx context.Context, o client.O
379379
// that we can retry during the next reconciliation. When applicable, it retrieves an associated object that
380380
// has to be deleted from Kubernetes, and it could be a secret associated with an instance.
381381
func (i *instanceReconcilerHelper) finalize(ctx context.Context, o v1alpha1.AivenManagedObject) (bool, error) {
382-
i.rec.Event(o, corev1.EventTypeNormal, eventTryingToDeleteAtAiven, "trying to delete instance at aiven")
383-
384382
var err error
385383
finalised := true
386384
deletionPolicy := deletionPolicyDelete
387385

388386
// Parse the annotations for the deletion policy. For simplicity, we only allow 'Orphan'.
389387
// If set will skip the deletion of the remote object. Disable by removing the annotation.
390388
if p, ok := o.GetAnnotations()[deletionPolicyAnnotation]; ok {
391-
deletionPolicy = deletionPolicyOrphan
392-
if p != deletionPolicyOrphan {
389+
if p == deletionPolicyOrphan {
390+
deletionPolicy = deletionPolicyOrphan
391+
i.log.Info("'Orphan' deletion policy detected - Aiven resource will be preserved on Kubernetes resource deletion")
392+
} else {
393393
i.log.Info(fmt.Sprintf("Invalid deletion policy! Only '%s' is allowed.", deletionPolicyOrphan))
394394
finalised = false
395395
}
396396
}
397397

398398
if deletionPolicy == deletionPolicyDelete {
399+
i.rec.Event(o, corev1.EventTypeNormal, eventTryingToDeleteAtAiven, "trying to delete instance at aiven")
399400
finalised, err = i.h.delete(ctx, i.avnGen, o)
400401
if err != nil {
401402
meta.SetStatusCondition(o.Conditions(), getErrorCondition(errConditionDelete, err))
@@ -432,20 +433,24 @@ func (i *instanceReconcilerHelper) finalize(ctx context.Context, o v1alpha1.Aive
432433

433434
// checking if instance was finalized, if not triggering a requeue
434435
if !finalised {
435-
i.log.Info("instance is not yet deleted at aiven, triggering requeue")
436+
i.log.Info("instance is not yet deleted at Aiven, triggering requeue")
436437
return true, nil
437438
}
438439

439-
i.log.Info("instance was successfully deleted at aiven, removing finalizer")
440-
i.rec.Event(o, corev1.EventTypeNormal, eventSuccessfullyDeletedAtAiven, "instance is gone at aiven now")
440+
if deletionPolicy == deletionPolicyOrphan {
441+
i.log.Info("Kubernetes resource finalized with orphan policy - Aiven resource preserved")
442+
} else {
443+
i.log.Info("instance was successfully deleted at Aiven, removing finalizer")
444+
i.rec.Event(o, corev1.EventTypeNormal, eventSuccessfullyDeletedAtAiven, "instance is gone at aiven now")
445+
}
441446

442447
// remove finalizer, once all finalizers have been removed, the object will be deleted.
443448
if err := removeFinalizer(ctx, i.k8s, o, instanceDeletionFinalizer); err != nil {
444449
i.rec.Event(o, corev1.EventTypeWarning, eventUnableToDeleteFinalizer, err.Error())
445450
return false, fmt.Errorf("unable to remove finalizer: %w", err)
446451
}
447452

448-
i.log.Info("finalizer was removed, instance is deleted")
453+
i.log.Info("finalizer was removed, resource is deleted")
449454
return false, nil
450455
}
451456

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Deletion Policy
2+
3+
The Aiven Operator provides a deletion policy annotation that prevents deletion of Aiven resources when Kubernetes resources are removed. This feature works with all Aiven operator resources.
4+
5+
## Overview
6+
7+
By default, when you delete an Aiven operator resource (like `PostgreSQL`, `KafkaTopic`, `ServiceUser`, etc.), the operator deletes both the Kubernetes resource and the corresponding Aiven service. The `controllers.aiven.io/deletion-policy: Orphan` annotation allows you to override this behavior and preserve the Aiven resource while removing only the Kubernetes resource.
8+
9+
## Using the Deletion Policy
10+
11+
### Step 1: Add the Annotation
12+
13+
Add the deletion policy annotation to the resource you want to protect:
14+
15+
```yaml
16+
apiVersion: aiven.io/v1alpha1
17+
kind: PostgreSQL # or any other Aiven resource
18+
metadata:
19+
name: my-database
20+
namespace: my-namespace
21+
annotations:
22+
controllers.aiven.io/deletion-policy: Orphan
23+
spec:
24+
# ... existing configuration
25+
```
26+
27+
### Step 2: Delete the Kubernetes Resource
28+
29+
Now you can safely delete the Kubernetes resource:
30+
31+
```bash
32+
kubectl delete postgresql my-database -n my-namespace
33+
```
34+
35+
The Kubernetes resource is deleted, but the Aiven service remains intact and continues running.

docs/mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ nav:
7373
- troubleshooting.md
7474
- installation/uninstalling.md
7575
- guides/token-management.md
76+
- guides/deletion-policy.md
7677
- Resources: &crds
7778
- resources/alloydbomni.md
7879
- resources/cassandra.md

0 commit comments

Comments
 (0)