crdregistration_controller.go 8.2 KB

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