| 35 | ) |
| 36 | |
| 37 | func Example() { |
| 38 | // source simulates an apiserver object endpoint. |
| 39 | source := fcache.NewFakeControllerSource() |
| 40 | |
| 41 | // This will hold the downstream state, as we know it. |
| 42 | downstream := NewStore(DeletionHandlingMetaNamespaceKeyFunc) |
| 43 | |
| 44 | // This will hold incoming changes. Note how we pass downstream in as a |
| 45 | // KeyLister, that way resync operations will result in the correct set |
| 46 | // of update/delete deltas. |
| 47 | fifo := NewDeltaFIFO(MetaNamespaceKeyFunc, downstream) |
| 48 | |
| 49 | // Let's do threadsafe output to get predictable test results. |
| 50 | deletionCounter := make(chan string, 1000) |
| 51 | |
| 52 | cfg := &Config{ |
| 53 | Queue: fifo, |
| 54 | ListerWatcher: source, |
| 55 | ObjectType: &v1.Pod{}, |
| 56 | FullResyncPeriod: time.Millisecond * 100, |
| 57 | RetryOnError: false, |
| 58 | |
| 59 | // Let's implement a simple controller that just deletes |
| 60 | // everything that comes in. |
| 61 | Process: func(obj interface{}) error { |
| 62 | // Obj is from the Pop method of the Queue we make above. |
| 63 | newest := obj.(Deltas).Newest() |
| 64 | |
| 65 | if newest.Type != Deleted { |
| 66 | // Update our downstream store. |
| 67 | err := downstream.Add(newest.Object) |
| 68 | if err != nil { |
| 69 | return err |
| 70 | } |
| 71 | |
| 72 | // Delete this object. |
| 73 | source.Delete(newest.Object.(runtime.Object)) |
| 74 | } else { |
| 75 | // Update our downstream store. |
| 76 | err := downstream.Delete(newest.Object) |
| 77 | if err != nil { |
| 78 | return err |
| 79 | } |
| 80 | |
| 81 | // fifo's KeyOf is easiest, because it handles |
| 82 | // DeletedFinalStateUnknown markers. |
| 83 | key, err := fifo.KeyOf(newest.Object) |
| 84 | if err != nil { |
| 85 | return err |
| 86 | } |
| 87 | |
| 88 | // Report this deletion. |
| 89 | deletionCounter <- key |
| 90 | } |
| 91 | return nil |
| 92 | }, |
| 93 | } |
| 94 | |