resource_quota_monitor.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*
  2. Copyright 2017 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 resourcequota
  14. import (
  15. "fmt"
  16. "sync"
  17. "time"
  18. "k8s.io/klog"
  19. "k8s.io/api/core/v1"
  20. "k8s.io/apimachinery/pkg/api/meta"
  21. "k8s.io/apimachinery/pkg/runtime/schema"
  22. "k8s.io/apimachinery/pkg/util/clock"
  23. utilerrors "k8s.io/apimachinery/pkg/util/errors"
  24. utilruntime "k8s.io/apimachinery/pkg/util/runtime"
  25. "k8s.io/apimachinery/pkg/util/wait"
  26. "k8s.io/client-go/tools/cache"
  27. "k8s.io/client-go/util/workqueue"
  28. "k8s.io/kubernetes/pkg/controller"
  29. quota "k8s.io/kubernetes/pkg/quota/v1"
  30. "k8s.io/kubernetes/pkg/quota/v1/evaluator/core"
  31. "k8s.io/kubernetes/pkg/quota/v1/generic"
  32. )
  33. type eventType int
  34. func (e eventType) String() string {
  35. switch e {
  36. case addEvent:
  37. return "add"
  38. case updateEvent:
  39. return "update"
  40. case deleteEvent:
  41. return "delete"
  42. default:
  43. return fmt.Sprintf("unknown(%d)", int(e))
  44. }
  45. }
  46. const (
  47. addEvent eventType = iota
  48. updateEvent
  49. deleteEvent
  50. )
  51. type event struct {
  52. eventType eventType
  53. obj interface{}
  54. oldObj interface{}
  55. gvr schema.GroupVersionResource
  56. }
  57. type QuotaMonitor struct {
  58. // each monitor list/watches a resource and determines if we should replenish quota
  59. monitors monitors
  60. monitorLock sync.RWMutex
  61. // informersStarted is closed after after all of the controllers have been initialized and are running.
  62. // After that it is safe to start them here, before that it is not.
  63. informersStarted <-chan struct{}
  64. // stopCh drives shutdown. When a receive from it unblocks, monitors will shut down.
  65. // This channel is also protected by monitorLock.
  66. stopCh <-chan struct{}
  67. // running tracks whether Run() has been called.
  68. // it is protected by monitorLock.
  69. running bool
  70. // monitors are the producer of the resourceChanges queue
  71. resourceChanges workqueue.RateLimitingInterface
  72. // interfaces with informers
  73. informerFactory controller.InformerFactory
  74. // list of resources to ignore
  75. ignoredResources map[schema.GroupResource]struct{}
  76. // The period that should be used to re-sync the monitored resource
  77. resyncPeriod controller.ResyncPeriodFunc
  78. // callback to alert that a change may require quota recalculation
  79. replenishmentFunc ReplenishmentFunc
  80. // maintains list of evaluators
  81. registry quota.Registry
  82. }
  83. func NewQuotaMonitor(informersStarted <-chan struct{}, informerFactory controller.InformerFactory, ignoredResources map[schema.GroupResource]struct{}, resyncPeriod controller.ResyncPeriodFunc, replenishmentFunc ReplenishmentFunc, registry quota.Registry) *QuotaMonitor {
  84. return &QuotaMonitor{
  85. informersStarted: informersStarted,
  86. informerFactory: informerFactory,
  87. ignoredResources: ignoredResources,
  88. resourceChanges: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "resource_quota_controller_resource_changes"),
  89. resyncPeriod: resyncPeriod,
  90. replenishmentFunc: replenishmentFunc,
  91. registry: registry,
  92. }
  93. }
  94. // monitor runs a Controller with a local stop channel.
  95. type monitor struct {
  96. controller cache.Controller
  97. // stopCh stops Controller. If stopCh is nil, the monitor is considered to be
  98. // not yet started.
  99. stopCh chan struct{}
  100. }
  101. // Run is intended to be called in a goroutine. Multiple calls of this is an
  102. // error.
  103. func (m *monitor) Run() {
  104. m.controller.Run(m.stopCh)
  105. }
  106. type monitors map[schema.GroupVersionResource]*monitor
  107. func (qm *QuotaMonitor) controllerFor(resource schema.GroupVersionResource) (cache.Controller, error) {
  108. // TODO: pass this down
  109. clock := clock.RealClock{}
  110. handlers := cache.ResourceEventHandlerFuncs{
  111. UpdateFunc: func(oldObj, newObj interface{}) {
  112. // TODO: leaky abstraction! live w/ it for now, but should pass down an update filter func.
  113. // we only want to queue the updates we care about though as too much noise will overwhelm queue.
  114. notifyUpdate := false
  115. switch resource.GroupResource() {
  116. case schema.GroupResource{Resource: "pods"}:
  117. oldPod := oldObj.(*v1.Pod)
  118. newPod := newObj.(*v1.Pod)
  119. notifyUpdate = core.QuotaV1Pod(oldPod, clock) && !core.QuotaV1Pod(newPod, clock)
  120. case schema.GroupResource{Resource: "services"}:
  121. oldService := oldObj.(*v1.Service)
  122. newService := newObj.(*v1.Service)
  123. notifyUpdate = core.GetQuotaServiceType(oldService) != core.GetQuotaServiceType(newService)
  124. }
  125. if notifyUpdate {
  126. event := &event{
  127. eventType: updateEvent,
  128. obj: newObj,
  129. oldObj: oldObj,
  130. gvr: resource,
  131. }
  132. qm.resourceChanges.Add(event)
  133. }
  134. },
  135. DeleteFunc: func(obj interface{}) {
  136. // delta fifo may wrap the object in a cache.DeletedFinalStateUnknown, unwrap it
  137. if deletedFinalStateUnknown, ok := obj.(cache.DeletedFinalStateUnknown); ok {
  138. obj = deletedFinalStateUnknown.Obj
  139. }
  140. event := &event{
  141. eventType: deleteEvent,
  142. obj: obj,
  143. gvr: resource,
  144. }
  145. qm.resourceChanges.Add(event)
  146. },
  147. }
  148. shared, err := qm.informerFactory.ForResource(resource)
  149. if err == nil {
  150. klog.V(4).Infof("QuotaMonitor using a shared informer for resource %q", resource.String())
  151. shared.Informer().AddEventHandlerWithResyncPeriod(handlers, qm.resyncPeriod())
  152. return shared.Informer().GetController(), nil
  153. }
  154. klog.V(4).Infof("QuotaMonitor unable to use a shared informer for resource %q: %v", resource.String(), err)
  155. // TODO: if we can share storage with garbage collector, it may make sense to support other resources
  156. // until that time, aggregated api servers will have to run their own controller to reconcile their own quota.
  157. return nil, fmt.Errorf("unable to monitor quota for resource %q", resource.String())
  158. }
  159. // SyncMonitors rebuilds the monitor set according to the supplied resources,
  160. // creating or deleting monitors as necessary. It will return any error
  161. // encountered, but will make an attempt to create a monitor for each resource
  162. // instead of immediately exiting on an error. It may be called before or after
  163. // Run. Monitors are NOT started as part of the sync. To ensure all existing
  164. // monitors are started, call StartMonitors.
  165. func (qm *QuotaMonitor) SyncMonitors(resources map[schema.GroupVersionResource]struct{}) error {
  166. qm.monitorLock.Lock()
  167. defer qm.monitorLock.Unlock()
  168. toRemove := qm.monitors
  169. if toRemove == nil {
  170. toRemove = monitors{}
  171. }
  172. current := monitors{}
  173. errs := []error{}
  174. kept := 0
  175. added := 0
  176. for resource := range resources {
  177. if _, ok := qm.ignoredResources[resource.GroupResource()]; ok {
  178. continue
  179. }
  180. if m, ok := toRemove[resource]; ok {
  181. current[resource] = m
  182. delete(toRemove, resource)
  183. kept++
  184. continue
  185. }
  186. c, err := qm.controllerFor(resource)
  187. if err != nil {
  188. errs = append(errs, fmt.Errorf("couldn't start monitor for resource %q: %v", resource, err))
  189. continue
  190. }
  191. // check if we need to create an evaluator for this resource (if none previously registered)
  192. evaluator := qm.registry.Get(resource.GroupResource())
  193. if evaluator == nil {
  194. listerFunc := generic.ListerFuncForResourceFunc(qm.informerFactory.ForResource)
  195. listResourceFunc := generic.ListResourceUsingListerFunc(listerFunc, resource)
  196. evaluator = generic.NewObjectCountEvaluator(resource.GroupResource(), listResourceFunc, "")
  197. qm.registry.Add(evaluator)
  198. klog.Infof("QuotaMonitor created object count evaluator for %s", resource.GroupResource())
  199. }
  200. // track the monitor
  201. current[resource] = &monitor{controller: c}
  202. added++
  203. }
  204. qm.monitors = current
  205. for _, monitor := range toRemove {
  206. if monitor.stopCh != nil {
  207. close(monitor.stopCh)
  208. }
  209. }
  210. klog.V(4).Infof("quota synced monitors; added %d, kept %d, removed %d", added, kept, len(toRemove))
  211. // NewAggregate returns nil if errs is 0-length
  212. return utilerrors.NewAggregate(errs)
  213. }
  214. // StartMonitors ensures the current set of monitors are running. Any newly
  215. // started monitors will also cause shared informers to be started.
  216. //
  217. // If called before Run, StartMonitors does nothing (as there is no stop channel
  218. // to support monitor/informer execution).
  219. func (qm *QuotaMonitor) StartMonitors() {
  220. qm.monitorLock.Lock()
  221. defer qm.monitorLock.Unlock()
  222. if !qm.running {
  223. return
  224. }
  225. // we're waiting until after the informer start that happens once all the controllers are initialized. This ensures
  226. // that they don't get unexpected events on their work queues.
  227. <-qm.informersStarted
  228. monitors := qm.monitors
  229. started := 0
  230. for _, monitor := range monitors {
  231. if monitor.stopCh == nil {
  232. monitor.stopCh = make(chan struct{})
  233. qm.informerFactory.Start(qm.stopCh)
  234. go monitor.Run()
  235. started++
  236. }
  237. }
  238. klog.V(4).Infof("QuotaMonitor started %d new monitors, %d currently running", started, len(monitors))
  239. }
  240. // IsSynced returns true if any monitors exist AND all those monitors'
  241. // controllers HasSynced functions return true. This means IsSynced could return
  242. // true at one time, and then later return false if all monitors were
  243. // reconstructed.
  244. func (qm *QuotaMonitor) IsSynced() bool {
  245. qm.monitorLock.RLock()
  246. defer qm.monitorLock.RUnlock()
  247. if len(qm.monitors) == 0 {
  248. klog.V(4).Info("quota monitor not synced: no monitors")
  249. return false
  250. }
  251. for resource, monitor := range qm.monitors {
  252. if !monitor.controller.HasSynced() {
  253. klog.V(4).Infof("quota monitor not synced: %v", resource)
  254. return false
  255. }
  256. }
  257. return true
  258. }
  259. // Run sets the stop channel and starts monitor execution until stopCh is
  260. // closed. Any running monitors will be stopped before Run returns.
  261. func (qm *QuotaMonitor) Run(stopCh <-chan struct{}) {
  262. klog.Infof("QuotaMonitor running")
  263. defer klog.Infof("QuotaMonitor stopping")
  264. // Set up the stop channel.
  265. qm.monitorLock.Lock()
  266. qm.stopCh = stopCh
  267. qm.running = true
  268. qm.monitorLock.Unlock()
  269. // Start monitors and begin change processing until the stop channel is
  270. // closed.
  271. qm.StartMonitors()
  272. wait.Until(qm.runProcessResourceChanges, 1*time.Second, stopCh)
  273. // Stop any running monitors.
  274. qm.monitorLock.Lock()
  275. defer qm.monitorLock.Unlock()
  276. monitors := qm.monitors
  277. stopped := 0
  278. for _, monitor := range monitors {
  279. if monitor.stopCh != nil {
  280. stopped++
  281. close(monitor.stopCh)
  282. }
  283. }
  284. klog.Infof("QuotaMonitor stopped %d of %d monitors", stopped, len(monitors))
  285. }
  286. func (qm *QuotaMonitor) runProcessResourceChanges() {
  287. for qm.processResourceChanges() {
  288. }
  289. }
  290. // Dequeueing an event from resourceChanges to process
  291. func (qm *QuotaMonitor) processResourceChanges() bool {
  292. item, quit := qm.resourceChanges.Get()
  293. if quit {
  294. return false
  295. }
  296. defer qm.resourceChanges.Done(item)
  297. event, ok := item.(*event)
  298. if !ok {
  299. utilruntime.HandleError(fmt.Errorf("expect a *event, got %v", item))
  300. return true
  301. }
  302. obj := event.obj
  303. accessor, err := meta.Accessor(obj)
  304. if err != nil {
  305. utilruntime.HandleError(fmt.Errorf("cannot access obj: %v", err))
  306. return true
  307. }
  308. klog.V(4).Infof("QuotaMonitor process object: %s, namespace %s, name %s, uid %s, event type %v", event.gvr.String(), accessor.GetNamespace(), accessor.GetName(), string(accessor.GetUID()), event.eventType)
  309. qm.replenishmentFunc(event.gvr.GroupResource(), accessor.GetNamespace())
  310. return true
  311. }