-
Hi, I am new to Kubebuilder, so the question is "Should I use DeepCopy() to update the object's Spec?". if so, why? sample code:
|
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 1 reply
-
This is not necessary. Only note that both Update and Patch methods will overwrite the new object returned from APIServer into the given object, which means the So, you should DeepCopy it only if you don't want the original object to be modified. |
Beta Was this translation helpful? Give feedback.
-
So everything is clear. I have tested it and watched So in what cases I don't need the original to get updated in my code? Do you have any example scenarios? I appreciate your help |
Beta Was this translation helpful? Give feedback.
-
If you ever read the KCM code, you may find those built-in controllers always deep copy objects before modify them. That's because those objects are references that listed from Informer directly, which means modify them will lead to the objects in Informer cache been changed unexpectedly. But controller-runtime, on the other hand, will deep copy objects before returning them to List method by default, which means the objects you listed are already deep copied. So you can directly pass them into Update or Patch, unless you have used the |
Beta Was this translation helpful? Give feedback.
-
Closing since it is answered. |
Beta Was this translation helpful? Give feedback.
This is not necessary. Only note that both Update and Patch methods will overwrite the new object returned from APIServer into the given object, which means the
objToUpdate
will be changed to the latest object after patched successfully.So, you should DeepCopy it only if you don't want the original object to be modified.