Skip to content

Commit 8cc8a9f

Browse files
committed
add option to orphan objects on deletion
an annotation <operator.name>/delete-policy: ""|default|oprhan can now be set on objects; if the value is empty or 'default', then the normal behavior applies; in case it is 'orphan', the object will just stay instead of being deleted (and it will be no longer tracked or considered as dependent of the component)
1 parent b507b92 commit 8cc8a9f

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

pkg/component/reconcile.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,7 @@ type reconcileTarget[T Component] struct {
475475
annotationKeyDigest string
476476
annotationKeyReconcilePolicy string
477477
annotationKeyUpdatePolicy string
478+
annotationKeyDeletePolicy string
478479
annotationKeyOrder string
479480
annotationKeyPurgeOrder string
480481
annotationKeyOwnerId string
@@ -490,6 +491,7 @@ func newReconcileTarget[T Component](reconcilerName string, reconcilerId string,
490491
annotationKeyDigest: reconcilerName + "/" + types.AnnotationKeySuffixDigest,
491492
annotationKeyReconcilePolicy: reconcilerName + "/" + types.AnnotationKeySuffixReconcilePolicy,
492493
annotationKeyUpdatePolicy: reconcilerName + "/" + types.AnnotationKeySuffixUpdatePolicy,
494+
annotationKeyDeletePolicy: reconcilerName + "/" + types.AnnotationKeySuffixDeletePolicy,
493495
annotationKeyOrder: reconcilerName + "/" + types.AnnotationKeySuffixOrder,
494496
annotationKeyPurgeOrder: reconcilerName + "/" + types.AnnotationKeySuffixPurgeOrder,
495497
annotationKeyOwnerId: reconcilerName + "/" + types.AnnotationKeySuffixOwnerId,
@@ -781,9 +783,17 @@ func (t *reconcileTarget[T]) Reconcile(ctx context.Context, component T) (bool,
781783
return false, errors.Wrapf(err, "error reading object %s", item)
782784
}
783785

786+
orphan := false
787+
if existingObject != nil {
788+
orphan = existingObject.GetAnnotations()[t.annotationKeyDeletePolicy] == types.DeletePolicyOrphan
789+
}
790+
784791
switch item.Phase {
785792
case PhaseScheduledForDeletion:
786793
if numManagedToBeDeleted == 0 || t.isManaged(item, component) {
794+
if orphan {
795+
continue
796+
}
787797
// note: here is a theoretical risk that we delete an existing foreign object, because informers are not yet synced
788798
// however not sending the delete request is also not an option, because this might lead to orphaned own dependents
789799
if err := t.deleteObject(ctx, item, existingObject); err != nil {
@@ -795,6 +805,9 @@ func (t *reconcileTarget[T]) Reconcile(ctx context.Context, component T) (bool,
795805
numToBeDeleted++
796806
case PhaseScheduledForCompletion:
797807
if numManagedToBeDeleted == 0 || t.isManaged(item, component) {
808+
if orphan {
809+
return false, fmt.Errorf("invalid usage of deletion policy: object %s is scheduled for completion (due to purge order) and therefore cannot be orphaned", item)
810+
}
798811
// note: here is a theoretical risk that we delete an existing foreign object, because informers are not yet synced
799812
// however not sending the delete request is also not an option, because this might lead to orphaned own dependents
800813
if err := t.deleteObject(ctx, item, existingObject); err != nil {
@@ -994,6 +1007,10 @@ func (t *reconcileTarget[T]) Delete(ctx context.Context, component T) (bool, err
9941007
}
9951008

9961009
if numManaged == 0 || t.isManaged(item, component) {
1010+
// orphan the object, if according deletion policy is set
1011+
if existingObject != nil && existingObject.GetAnnotations()[t.annotationKeyDeletePolicy] == types.DeletePolicyOrphan {
1012+
continue
1013+
}
9971014
// delete the object
9981015
// note: here is a theoretical risk that we delete an existing (foreign) object, because informers are not yet synced
9991016
// however not sending the delete request is also not an option, because this might lead to orphaned own dependents

pkg/types/constants.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const (
1010
AnnotationKeySuffixDigest = "digest"
1111
AnnotationKeySuffixReconcilePolicy = "reconcile-policy"
1212
AnnotationKeySuffixUpdatePolicy = "update-policy"
13+
AnnotationKeySuffixDeletePolicy = "delete-policy"
1314
AnnotationKeySuffixOrder = "order"
1415
AnnotationKeySuffixPurgeOrder = "purge-order"
1516
AnnotationKeySuffixOwnerId = "owner-id"
@@ -25,3 +26,8 @@ const (
2526
UpdatePolicyDefault = "default"
2627
UpdatePolicyRecreate = "recreate"
2728
)
29+
30+
const (
31+
DeletePolicyDefault = "default"
32+
DeletePolicyOrphan = "orphan"
33+
)

website/content/en/docs/concepts/dependents.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ To support such cases, the `Generator` implementation can set the following anno
2626
- `mycomponent-operator.mydomain.io/update-policy`: defines how the object (if existing) is updated; can be one of:
2727
- `default` (which is the default): a regular update (i.e. PUT) call will be made to the Kubernetes API server
2828
- `recreate`: if the object would be updated, it will be deleted and recreated instead
29+
- `mycomponent-operator.mydomain.io/delete-policy`: defines what happens if the object is deleted; can be one of:
30+
- `default` (which is the default): a delete call will be sent to the Kubernetes API server
31+
- `orphan`: the object will not be deleted, and it will be no longer tracked
2932
- `mycomponent-operator.mydomain.io/order`: the order at which this object will be reconciled; dependents will be reconciled order by order; that is, objects of the same order will be deployed in the canonical order, and the controller will only proceed to the next order if all objects of previous orders are ready; specified orders can be negative or positive numbers between -32768 and 32767, objects with no explicit order set are treated as if they would specify order 0.
3033
- `mycomponent-operator.mydomain.io/purge-order`: (optional) the order by which this object will be purged
3134

0 commit comments

Comments
 (0)