crdregistration_controller.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 crdregistration
  14. import (
  15. "fmt"
  16. "time"
  17. "k8s.io/klog"
  18. "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions"
  19. crdinformers "k8s.io/apiextensions-apiserver/pkg/client/informers/internalversion/apiextensions/internalversion"
  20. crdlisters "k8s.io/apiextensions-apiserver/pkg/client/listers/apiextensions/internalversion"
  21. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  22. "k8s.io/apimachinery/pkg/labels"
  23. "k8s.io/apimachinery/pkg/runtime/schema"
  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/kube-aggregator/pkg/apis/apiregistration"
  29. "k8s.io/kubernetes/pkg/controller"
  30. )
  31. // AutoAPIServiceRegistration is an interface which callers can re-declare locally and properly cast to for
  32. // adding and removing APIServices
  33. type AutoAPIServiceRegistration interface {
  34. // AddAPIServiceToSync adds an API service to auto-register.
  35. AddAPIServiceToSync(in *apiregistration.APIService)
  36. // RemoveAPIServiceToSync removes an API service to auto-register.
  37. RemoveAPIServiceToSync(name string)
  38. }
  39. type crdRegistrationController struct {
  40. crdLister crdlisters.CustomResourceDefinitionLister
  41. crdSynced cache.InformerSynced
  42. apiServiceRegistration AutoAPIServiceRegistration
  43. syncHandler func(groupVersion schema.GroupVersion) error
  44. syncedInitialSet chan struct{}
  45. // queue is where incoming work is placed to de-dup and to allow "easy" rate limited requeues on errors
  46. // this is actually keyed by a groupVersion
  47. queue workqueue.RateLimitingInterface
  48. }
  49. // NewCRDRegistrationController returns a controller which will register CRD GroupVersions with the auto APIService registration
  50. // controller so they automatically stay in sync.
  51. func NewCRDRegistrationController(crdinformer crdinformers.CustomResourceDefinitionInformer, apiServiceRegistration AutoAPIServiceRegistration) *crdRegistrationController {
  52. c := &crdRegistrationController{
  53. crdLister: crdinformer.Lister(),
  54. crdSynced: crdinformer.Informer().HasSynced,
  55. apiServiceRegistration: apiServiceRegistration,
  56. syncedInitialSet: make(chan struct{}),
  57. queue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "crd_autoregistration_controller"),
  58. }
  59. c.syncHandler = c.handleVersionUpdate
  60. crdinformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
  61. AddFunc: func(obj interface{}) {
  62. cast := obj.(*apiextensions.CustomResourceDefinition)
  63. c.enqueueCRD(cast)
  64. },
  65. UpdateFunc: func(oldObj, newObj interface{}) {
  66. // Enqueue both old and new object to make sure we remove and add appropriate API services.
  67. // The working queue will resolve any duplicates and only changes will stay in the queue.
  68. c.enqueueCRD(oldObj.(*apiextensions.CustomResourceDefinition))
  69. c.enqueueCRD(newObj.(*apiextensions.CustomResourceDefinition))
  70. },
  71. DeleteFunc: func(obj interface{}) {
  72. cast, ok := obj.(*apiextensions.CustomResourceDefinition)
  73. if !ok {
  74. tombstone, ok := obj.(cache.DeletedFinalStateUnknown)
  75. if !ok {
  76. klog.V(2).Infof("Couldn't get object from tombstone %#v", obj)
  77. return
  78. }
  79. cast, ok = tombstone.Obj.(*apiextensions.CustomResourceDefinition)
  80. if !ok {
  81. klog.V(2).Infof("Tombstone contained unexpected object: %#v", obj)
  82. return
  83. }
  84. }
  85. c.enqueueCRD(cast)
  86. },
  87. })
  88. return c
  89. }
  90. func (c *crdRegistrationController) Run(threadiness int, stopCh <-chan struct{}) {
  91. defer utilruntime.HandleCrash()
  92. // make sure the work queue is shutdown which will trigger workers to end
  93. defer c.queue.ShutDown()
  94. klog.Infof("Starting crd-autoregister controller")
  95. defer klog.Infof("Shutting down crd-autoregister controller")
  96. // wait for your secondary caches to fill before starting your work
  97. if !controller.WaitForCacheSync("crd-autoregister", stopCh, c.crdSynced) {
  98. return
  99. }
  100. // process each item in the list once
  101. if crds, err := c.crdLister.List(labels.Everything()); err != nil {
  102. utilruntime.HandleError(err)
  103. } else {
  104. for _, crd := range crds {
  105. for _, version := range crd.Spec.Versions {
  106. if err := c.syncHandler(schema.GroupVersion{Group: crd.Spec.Group, Version: version.Name}); err != nil {
  107. utilruntime.HandleError(err)
  108. }
  109. }
  110. }
  111. }
  112. close(c.syncedInitialSet)
  113. // start up your worker threads based on threadiness. Some controllers have multiple kinds of workers
  114. for i := 0; i < threadiness; i++ {
  115. // runWorker will loop until "something bad" happens. The .Until will then rekick the worker
  116. // after one second
  117. go wait.Until(c.runWorker, time.Second, stopCh)
  118. }
  119. // wait until we're told to stop
  120. <-stopCh
  121. }
  122. // WaitForInitialSync blocks until the initial set of CRD resources has been processed
  123. func (c *crdRegistrationController) WaitForInitialSync() {
  124. <-c.syncedInitialSet
  125. }
  126. func (c *crdRegistrationController) runWorker() {
  127. // hot loop until we're told to stop. processNextWorkItem will automatically wait until there's work
  128. // available, so we don't worry about secondary waits
  129. for c.processNextWorkItem() {
  130. }
  131. }
  132. // processNextWorkItem deals with one key off the queue. It returns false when it's time to quit.
  133. func (c *crdRegistrationController) processNextWorkItem() bool {
  134. // pull the next work item from queue. It should be a key we use to lookup something in a cache
  135. key, quit := c.queue.Get()
  136. if quit {
  137. return false
  138. }
  139. // you always have to indicate to the queue that you've completed a piece of work
  140. defer c.queue.Done(key)
  141. // do your work on the key. This method will contains your "do stuff" logic
  142. err := c.syncHandler(key.(schema.GroupVersion))
  143. if err == nil {
  144. // if you had no error, tell the queue to stop tracking history for your key. This will
  145. // reset things like failure counts for per-item rate limiting
  146. c.queue.Forget(key)
  147. return true
  148. }
  149. // there was a failure so be sure to report it. This method allows for pluggable error handling
  150. // which can be used for things like cluster-monitoring
  151. utilruntime.HandleError(fmt.Errorf("%v failed with : %v", key, err))
  152. // since we failed, we should requeue the item to work on later. This method will add a backoff
  153. // to avoid hotlooping on particular items (they're probably still not going to work right away)
  154. // and overall controller protection (everything I've done is broken, this controller needs to
  155. // calm down or it can starve other useful work) cases.
  156. c.queue.AddRateLimited(key)
  157. return true
  158. }
  159. func (c *crdRegistrationController) enqueueCRD(crd *apiextensions.CustomResourceDefinition) {
  160. for _, version := range crd.Spec.Versions {
  161. c.queue.Add(schema.GroupVersion{Group: crd.Spec.Group, Version: version.Name})
  162. }
  163. }
  164. func (c *crdRegistrationController) handleVersionUpdate(groupVersion schema.GroupVersion) error {
  165. apiServiceName := groupVersion.Version + "." + groupVersion.Group
  166. // check all CRDs. There shouldn't that many, but if we have problems later we can index them
  167. crds, err := c.crdLister.List(labels.Everything())
  168. if err != nil {
  169. return err
  170. }
  171. for _, crd := range crds {
  172. if crd.Spec.Group != groupVersion.Group {
  173. continue
  174. }
  175. for _, version := range crd.Spec.Versions {
  176. if version.Name != groupVersion.Version || !version.Served {
  177. continue
  178. }
  179. c.apiServiceRegistration.AddAPIServiceToSync(&apiregistration.APIService{
  180. ObjectMeta: metav1.ObjectMeta{Name: apiServiceName},
  181. Spec: apiregistration.APIServiceSpec{
  182. Group: groupVersion.Group,
  183. Version: groupVersion.Version,
  184. GroupPriorityMinimum: 1000, // CRDs should have relatively low priority
  185. VersionPriority: 100, // CRDs will be sorted by kube-like versions like any other APIService with the same VersionPriority
  186. },
  187. })
  188. return nil
  189. }
  190. }
  191. c.apiServiceRegistration.RemoveAPIServiceToSync(apiServiceName)
  192. return nil
  193. }