registry.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. "k8s.io/apimachinery/pkg/util/json"
  18. "sigs.k8s.io/yaml"
  19. )
  20. // PluginFactory is a function that builds a plugin.
  21. type PluginFactory = func(configuration *runtime.Unknown, f FrameworkHandle) (Plugin, error)
  22. // DecodeInto decodes configuration whose type is *runtime.Unknown to the interface into.
  23. func DecodeInto(configuration *runtime.Unknown, into interface{}) error {
  24. if configuration == nil || configuration.Raw == nil {
  25. return nil
  26. }
  27. switch configuration.ContentType {
  28. // If ContentType is empty, it means ContentTypeJSON by default.
  29. case runtime.ContentTypeJSON, "":
  30. return json.Unmarshal(configuration.Raw, into)
  31. case runtime.ContentTypeYAML:
  32. return yaml.Unmarshal(configuration.Raw, into)
  33. default:
  34. return fmt.Errorf("not supported content type %s", configuration.ContentType)
  35. }
  36. }
  37. // Registry is a collection of all available plugins. The framework uses a
  38. // registry to enable and initialize configured plugins.
  39. // All plugins must be in the registry before initializing the framework.
  40. type Registry map[string]PluginFactory
  41. // Register adds a new plugin to the registry. If a plugin with the same name
  42. // exists, it returns an error.
  43. func (r Registry) Register(name string, factory PluginFactory) error {
  44. if _, ok := r[name]; ok {
  45. return fmt.Errorf("a plugin named %v already exists", name)
  46. }
  47. r[name] = factory
  48. return nil
  49. }
  50. // Unregister removes an existing plugin from the registry. If no plugin with
  51. // the provided name exists, it returns an error.
  52. func (r Registry) Unregister(name string) error {
  53. if _, ok := r[name]; !ok {
  54. return fmt.Errorf("no plugin named %v exists", name)
  55. }
  56. delete(r, name)
  57. return nil
  58. }
  59. // Merge merges the provided registry to the current one.
  60. func (r Registry) Merge(in Registry) error {
  61. for name, factory := range in {
  62. if err := r.Register(name, factory); err != nil {
  63. return err
  64. }
  65. }
  66. return nil
  67. }