reflector.go 16 KB

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