plugin_manager.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. deprecatedSockDir string,
  47. recorder record.EventRecorder) PluginManager {
  48. asw := cache.NewActualStateOfWorld()
  49. dsw := cache.NewDesiredStateOfWorld()
  50. reconciler := reconciler.NewReconciler(
  51. operationexecutor.NewOperationExecutor(
  52. operationexecutor.NewOperationGenerator(
  53. recorder,
  54. ),
  55. ),
  56. loopSleepDuration,
  57. dsw,
  58. asw,
  59. )
  60. pm := &pluginManager{
  61. desiredStateOfWorldPopulator: pluginwatcher.NewWatcher(
  62. sockDir,
  63. deprecatedSockDir,
  64. dsw,
  65. ),
  66. reconciler: reconciler,
  67. desiredStateOfWorld: dsw,
  68. actualStateOfWorld: asw,
  69. }
  70. return pm
  71. }
  72. // pluginManager implements the PluginManager interface
  73. type pluginManager struct {
  74. // desiredStateOfWorldPopulator (the plugin watcher) runs an asynchronous
  75. // periodic loop to populate the desiredStateOfWorld.
  76. desiredStateOfWorldPopulator *pluginwatcher.Watcher
  77. // reconciler runs an asynchronous periodic loop to reconcile the
  78. // desiredStateOfWorld with the actualStateOfWorld by triggering register
  79. // and unregister operations using the operationExecutor.
  80. reconciler reconciler.Reconciler
  81. // actualStateOfWorld is a data structure containing the actual state of
  82. // the world according to the manager: i.e. which plugins are registered.
  83. // The data structure is populated upon successful completion of register
  84. // and unregister actions triggered by the reconciler.
  85. actualStateOfWorld cache.ActualStateOfWorld
  86. // desiredStateOfWorld is a data structure containing the desired state of
  87. // the world according to the plugin manager: i.e. what plugins are registered.
  88. // The data structure is populated by the desired state of the world
  89. // populator (plugin watcher).
  90. desiredStateOfWorld cache.DesiredStateOfWorld
  91. }
  92. var _ PluginManager = &pluginManager{}
  93. func (pm *pluginManager) Run(sourcesReady config.SourcesReady, stopCh <-chan struct{}) {
  94. defer runtime.HandleCrash()
  95. pm.desiredStateOfWorldPopulator.Start(stopCh)
  96. klog.V(2).Infof("The desired_state_of_world populator (plugin watcher) starts")
  97. klog.Infof("Starting Kubelet Plugin Manager")
  98. go pm.reconciler.Run(stopCh)
  99. metrics.Register(pm.actualStateOfWorld, pm.desiredStateOfWorld)
  100. <-stopCh
  101. klog.Infof("Shutting down Kubelet Plugin Manager")
  102. }
  103. func (pm *pluginManager) AddHandler(pluginType string, handler cache.PluginHandler) {
  104. pm.reconciler.AddHandler(pluginType, handler)
  105. }