Skip to content

Commit 6729f1f

Browse files
authored
preserve our finalizer during ssa update (#266)
1 parent db4fdb8 commit 6729f1f

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

pkg/reconciler/reconciler.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,7 +1056,7 @@ func (r *Reconciler) readObject(ctx context.Context, key types.ObjectKey) (*unst
10561056

10571057
// create object; object may be a concrete type or unstructured; in any case, type meta must be populated;
10581058
// createdObject is optional; if non-nil, it will be populated with the created object; the same variable can be supplied as object and createObject;
1059-
// if object is a crd or an api services, the reconciler's name will be added as finalizer
1059+
// if object is a crd or an api services, the reconciler's finalizer will be added
10601060
func (r *Reconciler) createObject(ctx context.Context, object client.Object, createdObject any, updatePolicy UpdatePolicy) (err error) {
10611061
if counter := r.metrics.CreateCounter; counter != nil {
10621062
counter.Inc()
@@ -1098,7 +1098,8 @@ func (r *Reconciler) createObject(ctx context.Context, object client.Object, cre
10981098
// update object; object may be a concrete type or unstructured; in any case, type meta must be populated;
10991099
// existingObject is required, and should represent the last-read state of the object; it must not have a deletionTimestamp set;
11001100
// updatedObject is optional; if non-nil, it will be populated with the updated object; the same variable can be supplied as object and updatedObject;
1101-
// if object is a crd or an api services, the reconciler's name will be added as finalizer;
1101+
// if object is a crd or an api services, the reconciler's finalizer will be added;
1102+
// in addition, if the existing object has our finalizer already, then it will be preserved;
11021103
// object may have a resourceVersion; if it does not, the resourceVersion of existingObject will be used for conflict checks during put/patch;
11031104
// if updatePolicy equals UpdatePolicyReplace, an update (put) will be performed; finalizers of existingObject will be copied;
11041105
// if updatePolicy equals UpdatePolicySsaMerge, a conflict-forcing server-side-apply patch will be performed;
@@ -1133,7 +1134,7 @@ func (r *Reconciler) updateObject(ctx context.Context, object client.Object, exi
11331134
return err
11341135
}
11351136
object = &unstructured.Unstructured{Object: data}
1136-
if isCrd(object) || isApiService(object) {
1137+
if isCrd(object) || isApiService(object) || slices.Contains(existingObject.GetFinalizers(), r.finalizer) {
11371138
controllerutil.AddFinalizer(object, r.finalizer)
11381139
}
11391140
// it is allowed that target object contains a resource version; otherwise, we set the resource version to the one of the existing object,
@@ -1183,6 +1184,10 @@ func (r *Reconciler) updateObject(ctx context.Context, object client.Object, exi
11831184
}
11841185
return r.client.Patch(ctx, object, client.Apply, client.FieldOwner(r.fieldOwner), client.ForceOwnership)
11851186
default:
1187+
// add finalizers of existing object; this is maybe not fully correct, but it should be what is intended in most cases;
1188+
// under the assumption, that the submitted target state of the object does not contain any finalizers,
1189+
// the assumption is correct; and this is reasonable, because in the majority of cases, finalizers are added
1190+
// by the responsible controller only; respectively, they are at least not intended to be removed by the client
11861191
for _, finalizer := range existingObject.GetFinalizers() {
11871192
controllerutil.AddFinalizer(object, finalizer)
11881193
}

0 commit comments

Comments
 (0)