Skip to content

Commit 845246c

Browse files
committed
Ensure only annotations are updated
As the UpdateCriticialAnnotations function is now not exclusively called in the creation process, we have to ensure no other fields like the spec are updated, so we don't interfer with the normal LateInitialize logic Signed-off-by: twobiers <[email protected]>
1 parent 4fa7610 commit 845246c

File tree

3 files changed

+69
-19
lines changed

3 files changed

+69
-19
lines changed

pkg/reconciler/managed/api.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -277,15 +277,25 @@ func NewRetryingCriticalAnnotationUpdater(c client.Client) *RetryingCriticalAnno
277277
// UpdateCriticalAnnotations updates (i.e. persists) the annotations of the
278278
// supplied Object. It retries in the face of any API server error several times
279279
// in order to ensure annotations that contain critical state are persisted.
280-
// Pending changes to the supplied Object's spec, status, or other metadata
281-
// might get reset to their current state according to the API server, e.g. in
282-
// case of a conflict error.
280+
// Only annotations will be updated as part of this operation, other fields of the
281+
// supplied Object will not be modified.
283282
func (u *RetryingCriticalAnnotationUpdater) UpdateCriticalAnnotations(ctx context.Context, o client.Object) error {
284283
a := o.GetAnnotations()
285284
err := retry.OnError(retry.DefaultRetry, func(err error) bool {
286285
return !errors.Is(err, context.Canceled)
287286
}, func() error {
288-
err := u.client.Update(ctx, o)
287+
patchMap := map[string]interface{}{
288+
"metadata": map[string]any{
289+
"annotations": a,
290+
},
291+
}
292+
293+
patchData, err := json.Marshal(patchMap)
294+
if err != nil {
295+
return err
296+
}
297+
298+
err = u.client.Patch(ctx, o, client.RawPatch(types.MergePatchType, patchData), client.FieldOwner(fieldOwnerAPISimpleRefResolver), client.ForceOwnership)
289299
if kerrors.IsConflict(err) {
290300
if getErr := u.client.Get(ctx, client.ObjectKeyFromObject(o), o); getErr != nil {
291301
return getErr

pkg/reconciler/managed/api_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ func TestRetryingCriticalAnnotationUpdater(t *testing.T) {
560560
reason: "We should return any error we encounter getting the supplied object",
561561
c: &test.MockClient{
562562
MockGet: test.NewMockGetFn(errBoom, setLabels),
563-
MockUpdate: test.NewMockUpdateFn(kerrors.NewConflict(schema.GroupResource{
563+
MockPatch: test.NewMockPatchFn(kerrors.NewConflict(schema.GroupResource{
564564
Group: "foo.com",
565565
Resource: "bars",
566566
}, "abc", errBoom)),
@@ -576,8 +576,8 @@ func TestRetryingCriticalAnnotationUpdater(t *testing.T) {
576576
"UpdateError": {
577577
reason: "We should return any error we encounter updating the supplied object",
578578
c: &test.MockClient{
579-
MockGet: test.NewMockGetFn(nil, setLabels),
580-
MockUpdate: test.NewMockUpdateFn(errBoom),
579+
MockGet: test.NewMockGetFn(nil, setLabels),
580+
MockPatch: test.NewMockPatchFn(errBoom),
581581
},
582582
args: args{
583583
o: &fake.LegacyManaged{},
@@ -591,7 +591,7 @@ func TestRetryingCriticalAnnotationUpdater(t *testing.T) {
591591
reason: "A successful get after a conflict should not hide the conflict error and prevent retries",
592592
c: &test.MockClient{
593593
MockGet: test.NewMockGetFn(nil, setLabels),
594-
MockUpdate: test.NewMockUpdateFn(kerrors.NewConflict(schema.GroupResource{
594+
MockPatch: test.NewMockPatchFn(kerrors.NewConflict(schema.GroupResource{
595595
Group: "foo.com",
596596
Resource: "bars",
597597
}, "abc", errBoom)),
@@ -610,8 +610,8 @@ func TestRetryingCriticalAnnotationUpdater(t *testing.T) {
610610
"Success": {
611611
reason: "We should return without error if we successfully update our annotations",
612612
c: &test.MockClient{
613-
MockGet: test.NewMockGetFn(nil, setLabels),
614-
MockUpdate: test.NewMockUpdateFn(errBoom),
613+
MockGet: test.NewMockGetFn(nil, setLabels),
614+
MockPatch: test.NewMockPatchFn(errBoom),
615615
},
616616
args: args{
617617
o: &fake.LegacyManaged{},

0 commit comments

Comments
 (0)