reflector.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /*
  2. Copyright 2014 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 cache
  14. import (
  15. "errors"
  16. "fmt"
  17. "io"
  18. "math/rand"
  19. "net"
  20. "net/url"
  21. "reflect"
  22. "regexp"
  23. goruntime "runtime"
  24. "runtime/debug"
  25. "strconv"
  26. "strings"
  27. "sync"
  28. "syscall"
  29. "time"
  30. "github.com/golang/glog"
  31. apierrs "k8s.io/apimachinery/pkg/api/errors"
  32. "k8s.io/apimachinery/pkg/api/meta"
  33. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  34. "k8s.io/apimachinery/pkg/runtime"
  35. utilruntime "k8s.io/apimachinery/pkg/util/runtime"
  36. "k8s.io/apimachinery/pkg/util/wait"
  37. "k8s.io/apimachinery/pkg/watch"
  38. "k8s.io/client-go/util/clock"
  39. )
  40. // Reflector watches a specified resource and causes all changes to be reflected in the given store.
  41. type Reflector struct {
  42. // name identifies this reflector. By default it will be a file:line if possible.
  43. name string
  44. // The type of object we expect to place in the store.
  45. expectedType reflect.Type
  46. // The destination to sync up with the watch source
  47. store Store
  48. // listerWatcher is used to perform lists and watches.
  49. listerWatcher ListerWatcher
  50. // period controls timing between one watch ending and
  51. // the beginning of the next one.
  52. period time.Duration
  53. resyncPeriod time.Duration
  54. ShouldResync func() bool
  55. // clock allows tests to manipulate time
  56. clock clock.Clock
  57. // lastSyncResourceVersion is the resource version token last
  58. // observed when doing a sync with the underlying store
  59. // it is thread safe, but not synchronized with the underlying store
  60. lastSyncResourceVersion string
  61. // lastSyncResourceVersionMutex guards read/write access to lastSyncResourceVersion
  62. lastSyncResourceVersionMutex sync.RWMutex
  63. }
  64. var (
  65. // We try to spread the load on apiserver by setting timeouts for
  66. // watch requests - it is random in [minWatchTimeout, 2*minWatchTimeout].
  67. // However, it can be modified to avoid periodic resync to break the
  68. // TCP connection.
  69. minWatchTimeout = 5 * time.Minute
  70. )
  71. // NewNamespaceKeyedIndexerAndReflector creates an Indexer and a Reflector
  72. // The indexer is configured to key on namespace
  73. func NewNamespaceKeyedIndexerAndReflector(lw ListerWatcher, expectedType interface{}, resyncPeriod time.Duration) (indexer Indexer, reflector *Reflector) {
  74. indexer = NewIndexer(MetaNamespaceKeyFunc, Indexers{"namespace": MetaNamespaceIndexFunc})
  75. reflector = NewReflector(lw, expectedType, indexer, resyncPeriod)
  76. return indexer, reflector
  77. }
  78. // NewReflector creates a new Reflector object which will keep the given store up to
  79. // date with the server's contents for the given resource. Reflector promises to
  80. // only put things in the store that have the type of expectedType, unless expectedType
  81. // is nil. If resyncPeriod is non-zero, then lists will be executed after every
  82. // resyncPeriod, so that you can use reflectors to periodically process everything as
  83. // well as incrementally processing the things that change.
  84. func NewReflector(lw ListerWatcher, expectedType interface{}, store Store, resyncPeriod time.Duration) *Reflector {
  85. return NewNamedReflector(getDefaultReflectorName(internalPackages...), lw, expectedType, store, resyncPeriod)
  86. }
  87. // NewNamedReflector same as NewReflector, but with a specified name for logging
  88. func NewNamedReflector(name string, lw ListerWatcher, expectedType interface{}, store Store, resyncPeriod time.Duration) *Reflector {
  89. r := &Reflector{
  90. name: name,
  91. listerWatcher: lw,
  92. store: store,
  93. expectedType: reflect.TypeOf(expectedType),
  94. period: time.Second,
  95. resyncPeriod: resyncPeriod,
  96. clock: &clock.RealClock{},
  97. }
  98. return r
  99. }
  100. // internalPackages are packages that ignored when creating a default reflector name. These packages are in the common
  101. // call chains to NewReflector, so they'd be low entropy names for reflectors
  102. var internalPackages = []string{"client-go/tools/cache/", "/runtime/asm_"}
  103. // getDefaultReflectorName walks back through the call stack until we find a caller from outside of the ignoredPackages
  104. // it returns back a shortpath/filename:line to aid in identification of this reflector when it starts logging
  105. func getDefaultReflectorName(ignoredPackages ...string) string {
  106. name := "????"
  107. const maxStack = 10
  108. for i := 1; i < maxStack; i++ {
  109. _, file, line, ok := goruntime.Caller(i)
  110. if !ok {
  111. file, line, ok = extractStackCreator()
  112. if !ok {
  113. break
  114. }
  115. i += maxStack
  116. }
  117. if hasPackage(file, ignoredPackages) {
  118. continue
  119. }
  120. file = trimPackagePrefix(file)
  121. name = fmt.Sprintf("%s:%d", file, line)
  122. break
  123. }
  124. return name
  125. }
  126. // hasPackage returns true if the file is in one of the ignored packages.
  127. func hasPackage(file string, ignoredPackages []string) bool {
  128. for _, ignoredPackage := range ignoredPackages {
  129. if strings.Contains(file, ignoredPackage) {
  130. return true
  131. }
  132. }
  133. return false
  134. }
  135. // trimPackagePrefix reduces duplicate values off the front of a package name.
  136. func trimPackagePrefix(file string) string {
  137. if l := strings.LastIndex(file, "k8s.io/client-go/pkg/"); l >= 0 {
  138. return file[l+len("k8s.io/client-go/"):]
  139. }
  140. if l := strings.LastIndex(file, "/src/"); l >= 0 {
  141. return file[l+5:]
  142. }
  143. if l := strings.LastIndex(file, "/pkg/"); l >= 0 {
  144. return file[l+1:]
  145. }
  146. return file
  147. }
  148. var stackCreator = regexp.MustCompile(`(?m)^created by (.*)\n\s+(.*):(\d+) \+0x[[:xdigit:]]+$`)
  149. // extractStackCreator retrieves the goroutine file and line that launched this stack. Returns false
  150. // if the creator cannot be located.
  151. // TODO: Go does not expose this via runtime https://github.com/golang/go/issues/11440
  152. func extractStackCreator() (string, int, bool) {
  153. stack := debug.Stack()
  154. matches := stackCreator.FindStringSubmatch(string(stack))
  155. if matches == nil || len(matches) != 4 {
  156. return "", 0, false
  157. }
  158. line, err := strconv.Atoi(matches[3])
  159. if err != nil {
  160. return "", 0, false
  161. }
  162. return matches[2], line, true
  163. }
  164. // Run starts a watch and handles watch events. Will restart the watch if it is closed.
  165. // Run starts a goroutine and returns immediately.
  166. func (r *Reflector) Run() {
  167. glog.V(3).Infof("Starting reflector %v (%s) from %s", r.expectedType, r.resyncPeriod, r.name)
  168. go wait.Until(func() {
  169. if err := r.ListAndWatch(wait.NeverStop); err != nil {
  170. utilruntime.HandleError(err)
  171. }
  172. }, r.period, wait.NeverStop)
  173. }
  174. // RunUntil starts a watch and handles watch events. Will restart the watch if it is closed.
  175. // RunUntil starts a goroutine and returns immediately. It will exit when stopCh is closed.
  176. func (r *Reflector) RunUntil(stopCh <-chan struct{}) {
  177. glog.V(3).Infof("Starting reflector %v (%s) from %s", r.expectedType, r.resyncPeriod, r.name)
  178. go wait.Until(func() {
  179. if err := r.ListAndWatch(stopCh); err != nil {
  180. utilruntime.HandleError(err)
  181. }
  182. }, r.period, stopCh)
  183. }
  184. var (
  185. // nothing will ever be sent down this channel
  186. neverExitWatch <-chan time.Time = make(chan time.Time)
  187. // Used to indicate that watching stopped so that a resync could happen.
  188. errorResyncRequested = errors.New("resync channel fired")
  189. // Used to indicate that watching stopped because of a signal from the stop
  190. // channel passed in from a client of the reflector.
  191. errorStopRequested = errors.New("Stop requested")
  192. )
  193. // resyncChan returns a channel which will receive something when a resync is
  194. // required, and a cleanup function.
  195. func (r *Reflector) resyncChan() (<-chan time.Time, func() bool) {
  196. if r.resyncPeriod == 0 {
  197. return neverExitWatch, func() bool { return false }
  198. }
  199. // The cleanup function is required: imagine the scenario where watches
  200. // always fail so we end up listing frequently. Then, if we don't
  201. // manually stop the timer, we could end up with many timers active
  202. // concurrently.
  203. t := r.clock.NewTimer(r.resyncPeriod)
  204. return t.C(), t.Stop
  205. }
  206. // ListAndWatch first lists all items and get the resource version at the moment of call,
  207. // and then use the resource version to watch.
  208. // It returns error if ListAndWatch didn't even try to initialize watch.
  209. func (r *Reflector) ListAndWatch(stopCh <-chan struct{}) error {
  210. glog.V(3).Infof("Listing and watching %v from %s", r.expectedType, r.name)
  211. var resourceVersion string
  212. resyncCh, cleanup := r.resyncChan()
  213. defer cleanup()
  214. // Explicitly set "0" as resource version - it's fine for the List()
  215. // to be served from cache and potentially be delayed relative to
  216. // etcd contents. Reflector framework will catch up via Watch() eventually.
  217. options := metav1.ListOptions{ResourceVersion: "0"}
  218. list, err := r.listerWatcher.List(options)
  219. if err != nil {
  220. return fmt.Errorf("%s: Failed to list %v: %v", r.name, r.expectedType, err)
  221. }
  222. listMetaInterface, err := meta.ListAccessor(list)
  223. if err != nil {
  224. return fmt.Errorf("%s: Unable to understand list result %#v: %v", r.name, list, err)
  225. }
  226. resourceVersion = listMetaInterface.GetResourceVersion()
  227. items, err := meta.ExtractList(list)
  228. if err != nil {
  229. return fmt.Errorf("%s: Unable to understand list result %#v (%v)", r.name, list, err)
  230. }
  231. if err := r.syncWith(items, resourceVersion); err != nil {
  232. return fmt.Errorf("%s: Unable to sync list result: %v", r.name, err)
  233. }
  234. r.setLastSyncResourceVersion(resourceVersion)
  235. resyncerrc := make(chan error, 1)
  236. cancelCh := make(chan struct{})
  237. defer close(cancelCh)
  238. go func() {
  239. for {
  240. select {
  241. case <-resyncCh:
  242. case <-stopCh:
  243. return
  244. case <-cancelCh:
  245. return
  246. }
  247. if r.ShouldResync == nil || r.ShouldResync() {
  248. glog.V(4).Infof("%s: forcing resync", r.name)
  249. if err := r.store.Resync(); err != nil {
  250. resyncerrc <- err
  251. return
  252. }
  253. }
  254. cleanup()
  255. resyncCh, cleanup = r.resyncChan()
  256. }
  257. }()
  258. for {
  259. timemoutseconds := int64(minWatchTimeout.Seconds() * (rand.Float64() + 1.0))
  260. options = metav1.ListOptions{
  261. ResourceVersion: resourceVersion,
  262. // We want to avoid situations of hanging watchers. Stop any wachers that do not
  263. // receive any events within the timeout window.
  264. TimeoutSeconds: &timemoutseconds,
  265. }
  266. w, err := r.listerWatcher.Watch(options)
  267. if err != nil {
  268. switch err {
  269. case io.EOF:
  270. // watch closed normally
  271. case io.ErrUnexpectedEOF:
  272. glog.V(1).Infof("%s: Watch for %v closed with unexpected EOF: %v", r.name, r.expectedType, err)
  273. default:
  274. utilruntime.HandleError(fmt.Errorf("%s: Failed to watch %v: %v", r.name, r.expectedType, err))
  275. }
  276. // If this is "connection refused" error, it means that most likely apiserver is not responsive.
  277. // It doesn't make sense to re-list all objects because most likely we will be able to restart
  278. // watch where we ended.
  279. // If that's the case wait and resend watch request.
  280. if urlError, ok := err.(*url.Error); ok {
  281. if opError, ok := urlError.Err.(*net.OpError); ok {
  282. if errno, ok := opError.Err.(syscall.Errno); ok && errno == syscall.ECONNREFUSED {
  283. time.Sleep(time.Second)
  284. continue
  285. }
  286. }
  287. }
  288. return nil
  289. }
  290. if err := r.watchHandler(w, &resourceVersion, resyncerrc, stopCh); err != nil {
  291. if err != errorStopRequested {
  292. glog.Warningf("%s: watch of %v ended with: %v", r.name, r.expectedType, err)
  293. }
  294. return nil
  295. }
  296. }
  297. }
  298. // syncWith replaces the store's items with the given list.
  299. func (r *Reflector) syncWith(items []runtime.Object, resourceVersion string) error {
  300. found := make([]interface{}, 0, len(items))
  301. for _, item := range items {
  302. found = append(found, item)
  303. }
  304. return r.store.Replace(found, resourceVersion)
  305. }
  306. // watchHandler watches w and keeps *resourceVersion up to date.
  307. func (r *Reflector) watchHandler(w watch.Interface, resourceVersion *string, errc chan error, stopCh <-chan struct{}) error {
  308. start := r.clock.Now()
  309. eventCount := 0
  310. // Stopping the watcher should be idempotent and if we return from this function there's no way
  311. // we're coming back in with the same watch interface.
  312. defer w.Stop()
  313. loop:
  314. for {
  315. select {
  316. case <-stopCh:
  317. return errorStopRequested
  318. case err := <-errc:
  319. return err
  320. case event, ok := <-w.ResultChan():
  321. if !ok {
  322. break loop
  323. }
  324. if event.Type == watch.Error {
  325. return apierrs.FromObject(event.Object)
  326. }
  327. if e, a := r.expectedType, reflect.TypeOf(event.Object); e != nil && e != a {
  328. utilruntime.HandleError(fmt.Errorf("%s: expected type %v, but watch event object had type %v", r.name, e, a))
  329. continue
  330. }
  331. meta, err := meta.Accessor(event.Object)
  332. if err != nil {
  333. utilruntime.HandleError(fmt.Errorf("%s: unable to understand watch event %#v", r.name, event))
  334. continue
  335. }
  336. newResourceVersion := meta.GetResourceVersion()
  337. switch event.Type {
  338. case watch.Added:
  339. err := r.store.Add(event.Object)
  340. if err != nil {
  341. utilruntime.HandleError(fmt.Errorf("%s: unable to add watch event object (%#v) to store: %v", r.name, event.Object, err))
  342. }
  343. case watch.Modified:
  344. err := r.store.Update(event.Object)
  345. if err != nil {
  346. utilruntime.HandleError(fmt.Errorf("%s: unable to update watch event object (%#v) to store: %v", r.name, event.Object, err))
  347. }
  348. case watch.Deleted:
  349. // TODO: Will any consumers need access to the "last known
  350. // state", which is passed in event.Object? If so, may need
  351. // to change this.
  352. err := r.store.Delete(event.Object)
  353. if err != nil {
  354. utilruntime.HandleError(fmt.Errorf("%s: unable to delete watch event object (%#v) from store: %v", r.name, event.Object, err))
  355. }
  356. default:
  357. utilruntime.HandleError(fmt.Errorf("%s: unable to understand watch event %#v", r.name, event))
  358. }
  359. *resourceVersion = newResourceVersion
  360. r.setLastSyncResourceVersion(newResourceVersion)
  361. eventCount++
  362. }
  363. }
  364. watchDuration := r.clock.Now().Sub(start)
  365. if watchDuration < 1*time.Second && eventCount == 0 {
  366. glog.V(4).Infof("%s: Unexpected watch close - watch lasted less than a second and no items received", r.name)
  367. return errors.New("very short watch")
  368. }
  369. glog.V(4).Infof("%s: Watch close - %v total %v items received", r.name, r.expectedType, eventCount)
  370. return nil
  371. }
  372. // LastSyncResourceVersion is the resource version observed when last sync with the underlying store
  373. // The value returned is not synchronized with access to the underlying store and is not thread-safe
  374. func (r *Reflector) LastSyncResourceVersion() string {
  375. r.lastSyncResourceVersionMutex.RLock()
  376. defer r.lastSyncResourceVersionMutex.RUnlock()
  377. return r.lastSyncResourceVersion
  378. }
  379. func (r *Reflector) setLastSyncResourceVersion(v string) {
  380. r.lastSyncResourceVersionMutex.Lock()
  381. defer r.lastSyncResourceVersionMutex.Unlock()
  382. r.lastSyncResourceVersion = v
  383. }