plugin_manager.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. Copyright 2019 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 pluginmanager
  14. import (
  15. "time"
  16. "k8s.io/apimachinery/pkg/util/runtime"
  17. "k8s.io/client-go/tools/record"
  18. "k8s.io/klog"
  19. "k8s.io/kubernetes/pkg/kubelet/config"
  20. "k8s.io/kubernetes/pkg/kubelet/pluginmanager/cache"
  21. "k8s.io/kubernetes/pkg/kubelet/pluginmanager/metrics"
  22. "k8s.io/kubernetes/pkg/kubelet/pluginmanager/operationexecutor"
  23. "k8s.io/kubernetes/pkg/kubelet/pluginmanager/pluginwatcher"
  24. "k8s.io/kubernetes/pkg/kubelet/pluginmanager/reconciler"
  25. )
  26. // PluginManager runs a set of asynchronous loops that figure out which plugins
  27. // need to be registered/deregistered and makes it so.
  28. type PluginManager interface {
  29. // Starts the plugin manager and all the asynchronous loops that it controls
  30. Run(sourcesReady config.SourcesReady, stopCh <-chan struct{})
  31. // AddHandler adds the given plugin handler for a specific plugin type, which
  32. // will be added to the actual state of world cache so that it can be passed to
  33. // the desired state of world cache in order to be used during plugin
  34. // registration/deregistration
  35. AddHandler(pluginType string, pluginHandler cache.PluginHandler)
  36. }
  37. const (
  38. // loopSleepDuration is the amount of time the reconciler loop waits
  39. // between successive executions
  40. loopSleepDuration = 1 * time.Second
  41. )
  42. // NewPluginManager returns a new concrete instance implementing the
  43. // PluginManager interface.
  44. func NewPluginManager(
  45. sockDir string,
  46. recorder record.EventRecorder) PluginManager {
  47. asw := cache.NewActualStateOfWorld()
  48. dsw := cache.NewDesiredStateOfWorld()
  49. reconciler := reconciler.NewReconciler(
  50. operationexecutor.NewOperationExecutor(
  51. operationexecutor.NewOperationGenerator(
  52. recorder,
  53. ),
  54. ),
  55. loopSleepDuration,
  56. dsw,
  57. asw,
  58. )
  59. pm := &pluginManager{
  60. desiredStateOfWorldPopulator: pluginwatcher.NewWatcher(
  61. sockDir,
  62. dsw,
  63. ),
  64. reconciler: reconciler,
  65. desiredStateOfWorld: dsw,
  66. actualStateOfWorld: asw,
  67. }
  68. return pm
  69. }
  70. // pluginManager implements the PluginManager interface
  71. type pluginManager struct {
  72. // desiredStateOfWorldPopulator (the plugin watcher) runs an asynchronous
  73. // periodic loop to populate the desiredStateOfWorld.
  74. desiredStateOfWorldPopulator *pluginwatcher.Watcher
  75. // reconciler runs an asynchronous periodic loop to reconcile the
  76. // desiredStateOfWorld with the actualStateOfWorld by triggering register
  77. // and unregister operations using the operationExecutor.
  78. reconciler reconciler.Reconciler
  79. // actualStateOfWorld is a data structure containing the actual state of
  80. // the world according to the manager: i.e. which plugins are registered.
  81. // The data structure is populated upon successful completion of register
  82. // and unregister actions triggered by the reconciler.
  83. actualStateOfWorld cache.ActualStateOfWorld
  84. // desiredStateOfWorld is a data structure containing the desired state of
  85. // the world according to the plugin manager: i.e. what plugins are registered.
  86. // The data structure is populated by the desired state of the world
  87. // populator (plugin watcher).
  88. desiredStateOfWorld cache.DesiredStateOfWorld
  89. }
  90. var _ PluginManager = &pluginManager{}
  91. func (pm *pluginManager) Run(sourcesReady config.SourcesReady, stopCh <-chan struct{}) {
  92. defer runtime.HandleCrash()
  93. pm.desiredStateOfWorldPopulator.Start(stopCh)
  94. klog.V(2).Infof("The desired_state_of_world populator (plugin watcher) starts")
  95. klog.Infof("Starting Kubelet Plugin Manager")
  96. go pm.reconciler.Run(stopCh)
  97. metrics.Register(pm.actualStateOfWorld, pm.desiredStateOfWorld)
  98. <-stopCh
  99. klog.Infof("Shutting down Kubelet Plugin Manager")
  100. }
  101. func (pm *pluginManager) AddHandler(pluginType string, handler cache.PluginHandler) {
  102. pm.reconciler.AddHandler(pluginType, handler)
  103. }