UntilWithoutRetry reads items from the watch until each provided condition succeeds, and then returns the last watch encountered. The first condition that returns an error terminates the watch (and the event is also returned). If no event has been received, the returned event will be nil. Conditions
(ctx context.Context, watcher watch.Interface, conditions ...ConditionFunc)
| 56 | // Warning: solving such issues. |
| 57 | // TODO: Consider making this function private to prevent misuse when the other occurrences in our codebase are gone. |
| 58 | func UntilWithoutRetry(ctx context.Context, watcher watch.Interface, conditions ...ConditionFunc) (*watch.Event, error) { |
| 59 | ch := watcher.ResultChan() |
| 60 | defer watcher.Stop() |
| 61 | var lastEvent *watch.Event |
| 62 | for _, condition := range conditions { |
| 63 | // check the next condition against the previous event and short circuit waiting for the next watch |
| 64 | if lastEvent != nil { |
| 65 | done, err := condition(*lastEvent) |
| 66 | if err != nil { |
| 67 | return lastEvent, err |
| 68 | } |
| 69 | if done { |
| 70 | continue |
| 71 | } |
| 72 | } |
| 73 | ConditionSucceeded: |
| 74 | for { |
| 75 | select { |
| 76 | case event, ok := <-ch: |
| 77 | if !ok { |
| 78 | return lastEvent, ErrWatchClosed |
| 79 | } |
| 80 | lastEvent = &event |
| 81 | |
| 82 | done, err := condition(event) |
| 83 | if err != nil { |
| 84 | return lastEvent, err |
| 85 | } |
| 86 | if done { |
| 87 | break ConditionSucceeded |
| 88 | } |
| 89 | |
| 90 | case <-ctx.Done(): |
| 91 | return lastEvent, wait.ErrWaitTimeout |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | return lastEvent, nil |
| 96 | } |
| 97 | |
| 98 | // Until wraps the watcherClient's watch function with RetryWatcher making sure that watcher gets restarted in case of errors. |
| 99 | // The initialResourceVersion will be given to watch method when first called. It shall not be "" or "0" |