Skip to content

Commit b6da9c1

Browse files
authored
đź“– Explain what may happen when using OnlyMetadata and Get on the concrete type instead of PartialMetadataObject (#1747)
* Document in API the consequence of mixing concrete and metadata * Consequence of mixing concrete and metadata: add pod.SetGroupVersionKind
1 parent cfd9276 commit b6da9c1

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

‎pkg/builder/options.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,29 @@ var (
107107
// metav1.PartialObjectMetadata to the client when fetching objects in your
108108
// reconciler, otherwise you'll end up with a duplicate structured or
109109
// unstructured cache.
110+
//
111+
// When watching a resource with OnlyMetadata, for example the v1.Pod, you
112+
// should not Get and List using the v1.Pod type. Instead, you should use
113+
// the special metav1.PartialObjectMetadata type.
114+
//
115+
// ❌ Incorrect:
116+
//
117+
// pod := &v1.Pod{}
118+
// mgr.GetClient().Get(ctx, nsAndName, pod)
119+
//
120+
// âś… Correct:
121+
//
122+
// pod := &metav1.PartialObjectMetadata{}
123+
// pod.SetGroupVersionKind(schema.GroupVersionKind{
124+
// Group: "",
125+
// Version: "v1",
126+
// Kind: "Pod",
127+
// })
128+
// mgr.GetClient().Get(ctx, nsAndName, pod)
129+
//
130+
// In the first case, controller-runtime will create another cache for the
131+
// concrete type on top of the metadata cache; this increases memory
132+
// consumption and leads to race conditions as caches are not in sync.
110133
OnlyMetadata = projectAs(projectAsMetadata)
111134

112135
_ ForOption = OnlyMetadata

0 commit comments

Comments
 (0)