Skip to content

Commit 77e70e6

Browse files
authored
Merge pull request kubernetes#89161 from MikeSpreitzer/informer-doc-redux
Documented mutation restriction for informer clients
2 parents 3d46b78 + c4774de commit 77e70e6

File tree

2 files changed

+28
-22
lines changed

2 files changed

+28
-22
lines changed

staging/src/k8s.io/client-go/tools/cache/controller.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,11 @@ func (c *controller) processLoop() {
183183
}
184184
}
185185

186-
// ResourceEventHandler can handle notifications for events that happen to a
187-
// resource. The events are informational only, so you can't return an
188-
// error.
186+
// ResourceEventHandler can handle notifications for events that
187+
// happen to a resource. The events are informational only, so you
188+
// can't return an error. The handlers MUST NOT modify the objects
189+
// received; this concerns not only the top level of structure but all
190+
// the data structures reachable from it.
189191
// * OnAdd is called when an object is added.
190192
// * OnUpdate is called when an object is modified. Note that oldObj is the
191193
// last known state of the object-- it is possible that several changes
@@ -205,7 +207,8 @@ type ResourceEventHandler interface {
205207

206208
// ResourceEventHandlerFuncs is an adaptor to let you easily specify as many or
207209
// as few of the notification functions as you want while still implementing
208-
// ResourceEventHandler.
210+
// ResourceEventHandler. This adapter does not remove the prohibition against
211+
// modifying the objects.
209212
type ResourceEventHandlerFuncs struct {
210213
AddFunc func(obj interface{})
211214
UpdateFunc func(oldObj, newObj interface{})
@@ -237,6 +240,7 @@ func (r ResourceEventHandlerFuncs) OnDelete(obj interface{}) {
237240
// in, ensuring the appropriate nested handler method is invoked. An object
238241
// that starts passing the filter after an update is considered an add, and an
239242
// object that stops passing the filter after an update is considered a delete.
243+
// Like the handlers, the filter MUST NOT modify the objects it is given.
240244
type FilteringResourceEventHandler struct {
241245
FilterFunc func(obj interface{}) bool
242246
Handler ResourceEventHandler

staging/src/k8s.io/client-go/tools/cache/shared_informer.go

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,29 +34,31 @@ import (
3434
// SharedInformer provides eventually consistent linkage of its
3535
// clients to the authoritative state of a given collection of
3636
// objects. An object is identified by its API group, kind/resource,
37-
// namespace, and name; the `ObjectMeta.UID` is not part of an
38-
// object's ID as far as this contract is concerned. One
37+
// namespace (if any), and name; the `ObjectMeta.UID` is not part of
38+
// an object's ID as far as this contract is concerned. One
3939
// SharedInformer provides linkage to objects of a particular API
4040
// group and kind/resource. The linked object collection of a
41-
// SharedInformer may be further restricted to one namespace and/or by
42-
// label selector and/or field selector.
41+
// SharedInformer may be further restricted to one namespace (if
42+
// applicable) and/or by label selector and/or field selector.
4343
//
4444
// The authoritative state of an object is what apiservers provide
4545
// access to, and an object goes through a strict sequence of states.
46-
// An object state is either "absent" or present with a
47-
// ResourceVersion and other appropriate content.
46+
// An object state is either (1) present with a ResourceVersion and
47+
// other appropriate content or (2) "absent".
4848
//
49-
// A SharedInformer maintains a local cache, exposed by GetStore() and
50-
// by GetIndexer() in the case of an indexed informer, of the state of
51-
// each relevant object. This cache is eventually consistent with the
52-
// authoritative state. This means that, unless prevented by
53-
// persistent communication problems, if ever a particular object ID X
54-
// is authoritatively associated with a state S then for every
55-
// SharedInformer I whose collection includes (X, S) eventually either
56-
// (1) I's cache associates X with S or a later state of X, (2) I is
57-
// stopped, or (3) the authoritative state service for X terminates.
58-
// To be formally complete, we say that the absent state meets any
59-
// restriction by label selector or field selector.
49+
// A SharedInformer maintains a local cache --- exposed by GetStore(),
50+
// by GetIndexer() in the case of an indexed informer, and possibly by
51+
// machinery involved in creating and/or accessing the informer --- of
52+
// the state of each relevant object. This cache is eventually
53+
// consistent with the authoritative state. This means that, unless
54+
// prevented by persistent communication problems, if ever a
55+
// particular object ID X is authoritatively associated with a state S
56+
// then for every SharedInformer I whose collection includes (X, S)
57+
// eventually either (1) I's cache associates X with S or a later
58+
// state of X, (2) I is stopped, or (3) the authoritative state
59+
// service for X terminates. To be formally complete, we say that the
60+
// absent state meets any restriction by label selector or field
61+
// selector.
6062
//
6163
// For a given informer and relevant object ID X, the sequence of
6264
// states that appears in the informer's cache is a subsequence of the
@@ -98,7 +100,7 @@ import (
98100
// added before `Run()`, eventually either the SharedInformer is
99101
// stopped or the client is notified of the update. A client added
100102
// after `Run()` starts gets a startup batch of notifications of
101-
// additions of the object existing in the cache at the time that
103+
// additions of the objects existing in the cache at the time that
102104
// client was added; also, for every update to the SharedInformer's
103105
// local cache after that client was added, eventually either the
104106
// SharedInformer is stopped or that client is notified of that

0 commit comments

Comments
 (0)