until.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package watch
  14. import (
  15. "errors"
  16. "time"
  17. "k8s.io/apimachinery/pkg/util/wait"
  18. )
  19. // ConditionFunc returns true if the condition has been reached, false if it has not been reached yet,
  20. // or an error if the condition cannot be checked and should terminate. In general, it is better to define
  21. // level driven conditions over edge driven conditions (pod has ready=true, vs pod modified and ready changed
  22. // from false to true).
  23. type ConditionFunc func(event Event) (bool, error)
  24. // ErrWatchClosed is returned when the watch channel is closed before timeout in Until.
  25. var ErrWatchClosed = errors.New("watch closed before Until timeout")
  26. // Until reads items from the watch until each provided condition succeeds, and then returns the last watch
  27. // encountered. The first condition that returns an error terminates the watch (and the event is also returned).
  28. // If no event has been received, the returned event will be nil.
  29. // Conditions are satisfied sequentially so as to provide a useful primitive for higher level composition.
  30. // A zero timeout means to wait forever.
  31. func Until(timeout time.Duration, watcher Interface, conditions ...ConditionFunc) (*Event, error) {
  32. ch := watcher.ResultChan()
  33. defer watcher.Stop()
  34. var after <-chan time.Time
  35. if timeout > 0 {
  36. after = time.After(timeout)
  37. } else {
  38. ch := make(chan time.Time)
  39. defer close(ch)
  40. after = ch
  41. }
  42. var lastEvent *Event
  43. for _, condition := range conditions {
  44. // check the next condition against the previous event and short circuit waiting for the next watch
  45. if lastEvent != nil {
  46. done, err := condition(*lastEvent)
  47. if err != nil {
  48. return lastEvent, err
  49. }
  50. if done {
  51. continue
  52. }
  53. }
  54. ConditionSucceeded:
  55. for {
  56. select {
  57. case event, ok := <-ch:
  58. if !ok {
  59. return lastEvent, ErrWatchClosed
  60. }
  61. lastEvent = &event
  62. // TODO: check for watch expired error and retry watch from latest point?
  63. done, err := condition(event)
  64. if err != nil {
  65. return lastEvent, err
  66. }
  67. if done {
  68. break ConditionSucceeded
  69. }
  70. case <-after:
  71. return lastEvent, wait.ErrWaitTimeout
  72. }
  73. }
  74. }
  75. return lastEvent, nil
  76. }