diff --git a/internal/util/ssa/patch.go b/internal/util/ssa/patch.go index 4f90d5e043b2..ad34c58964fc 100644 --- a/internal/util/ssa/patch.go +++ b/internal/util/ssa/patch.go @@ -76,6 +76,11 @@ func Patch(ctx context.Context, c client.Client, fieldManager string, modified c return err } + gvk, err := apiutil.GVKForObject(modifiedUnstructured, c.Scheme()) + if err != nil { + return errors.Wrapf(err, "failed to apply object: failed to get GroupVersionKind of modified object %s", klog.KObj(modifiedUnstructured)) + } + var requestIdentifier string if options.WithCachingProxy { // Check if the request is cached. @@ -88,15 +93,12 @@ func Patch(ctx context.Context, c client.Client, fieldManager string, modified c if err := c.Scheme().Convert(options.Original, modified, ctx); err != nil { return errors.Wrapf(err, "failed to write original into modified object") } + // Recover gvk e.g. for logging. + modified.GetObjectKind().SetGroupVersionKind(gvk) return nil } } - gvk, err := apiutil.GVKForObject(modifiedUnstructured, c.Scheme()) - if err != nil { - return errors.Wrapf(err, "failed to apply object: failed to get GroupVersionKind of modified object %s", klog.KObj(modifiedUnstructured)) - } - patchOptions := []client.PatchOption{ client.ForceOwnership, client.FieldOwner(fieldManager), @@ -110,6 +112,9 @@ func Patch(ctx context.Context, c client.Client, fieldManager string, modified c return errors.Wrapf(err, "failed to write modified object") } + // Recover gvk e.g. for logging. + modified.GetObjectKind().SetGroupVersionKind(gvk) + if options.WithCachingProxy { // If the SSA call did not update the object, add the request to the cache. if options.Original.GetResourceVersion() == modifiedUnstructured.GetResourceVersion() { diff --git a/internal/util/ssa/patch_test.go b/internal/util/ssa/patch_test.go index e7b89e1cef67..dbb2d4ea0b86 100644 --- a/internal/util/ssa/patch_test.go +++ b/internal/util/ssa/patch_test.go @@ -125,6 +125,8 @@ func TestPatch(t *testing.T) { // 1. Create the object createObject := initialObject.DeepCopy() g.Expect(Patch(ctx, env.GetClient(), fieldManager, createObject)).To(Succeed()) + // Verify that gvk is still set + g.Expect(createObject.GroupVersionKind()).To(Equal(initialObject.GroupVersionKind())) // Note: We have to patch the status here to explicitly set these two status fields. // If we don't do it the Machine defaulting webhook will try to set the two fields to false. // For an unknown reason this will happen with the 2nd update call (3.) below and not before. @@ -154,6 +156,8 @@ func TestPatch(t *testing.T) { g.Expect(err).ToNot(HaveOccurred()) // Update the object g.Expect(Patch(ctx, env.GetClient(), fieldManager, modifiedObject, WithCachingProxy{Cache: ssaCache, Original: originalObject})).To(Succeed()) + // Verify that gvk is still set + g.Expect(modifiedObject.GroupVersionKind()).To(Equal(initialObject.GroupVersionKind())) // Verify that request was not cached (as it changed the object) g.Expect(ssaCache.Has(requestIdentifier)).To(BeFalse()) @@ -173,5 +177,7 @@ func TestPatch(t *testing.T) { g.Expect(Patch(ctx, env.GetClient(), fieldManager, modifiedObject, WithCachingProxy{Cache: ssaCache, Original: originalObject})).To(Succeed()) // Verify that request was cached (as it did not change the object) g.Expect(ssaCache.Has(requestIdentifier)).To(BeTrue()) + // Verify that gvk is still set + g.Expect(modifiedObject.GroupVersionKind()).To(Equal(initialObject.GroupVersionKind())) }) }