registry.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 v1alpha1
  14. import (
  15. "fmt"
  16. "k8s.io/apimachinery/pkg/runtime"
  17. )
  18. // PluginFactory is a function that builds a plugin.
  19. type PluginFactory = func(configuration *runtime.Unknown, f FrameworkHandle) (Plugin, error)
  20. // Registry is a collection of all available plugins. The framework uses a
  21. // registry to enable and initialize configured plugins.
  22. // All plugins must be in the registry before initializing the framework.
  23. type Registry map[string]PluginFactory
  24. // Register adds a new plugin to the registry. If a plugin with the same name
  25. // exists, it returns an error.
  26. func (r Registry) Register(name string, factory PluginFactory) error {
  27. if _, ok := r[name]; ok {
  28. return fmt.Errorf("a plugin named %v already exists", name)
  29. }
  30. r[name] = factory
  31. return nil
  32. }
  33. // Unregister removes an existing plugin from the registry. If no plugin with
  34. // the provided name exists, it returns an error.
  35. func (r Registry) Unregister(name string) error {
  36. if _, ok := r[name]; !ok {
  37. return fmt.Errorf("no plugin named %v exists", name)
  38. }
  39. delete(r, name)
  40. return nil
  41. }
  42. // NewRegistry builds a default registry with all the default plugins.
  43. // This is the registry that Kubernetes default scheduler uses. A scheduler that
  44. // runs custom plugins, can pass a different Registry and when initializing the
  45. // scheduler.
  46. func NewRegistry() Registry {
  47. return Registry{
  48. // FactoryMap:
  49. // New plugins are registered here.
  50. // example:
  51. // {
  52. // stateful_plugin.Name: stateful.NewStatefulMultipointExample,
  53. // fooplugin.Name: fooplugin.New,
  54. // }
  55. }
  56. }