plugin.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. Copyright The containerd 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 plugin
  14. import (
  15. "fmt"
  16. "sync"
  17. "github.com/pkg/errors"
  18. "google.golang.org/grpc"
  19. )
  20. var (
  21. // ErrNoType is returned when no type is specified
  22. ErrNoType = errors.New("plugin: no type")
  23. // ErrNoPluginID is returned when no id is specified
  24. ErrNoPluginID = errors.New("plugin: no id")
  25. // ErrSkipPlugin is used when a plugin is not initialized and should not be loaded,
  26. // this allows the plugin loader differentiate between a plugin which is configured
  27. // not to load and one that fails to load.
  28. ErrSkipPlugin = errors.New("skip plugin")
  29. // ErrInvalidRequires will be thrown if the requirements for a plugin are
  30. // defined in an invalid manner.
  31. ErrInvalidRequires = errors.New("invalid requires")
  32. )
  33. // IsSkipPlugin returns true if the error is skipping the plugin
  34. func IsSkipPlugin(err error) bool {
  35. return errors.Cause(err) == ErrSkipPlugin
  36. }
  37. // Type is the type of the plugin
  38. type Type string
  39. func (t Type) String() string { return string(t) }
  40. const (
  41. // InternalPlugin implements an internal plugin to containerd
  42. InternalPlugin Type = "io.containerd.internal.v1"
  43. // RuntimePlugin implements a runtime
  44. RuntimePlugin Type = "io.containerd.runtime.v1"
  45. // RuntimePluginV2 implements a runtime v2
  46. RuntimePluginV2 Type = "io.containerd.runtime.v2"
  47. // ServicePlugin implements a internal service
  48. ServicePlugin Type = "io.containerd.service.v1"
  49. // GRPCPlugin implements a grpc service
  50. GRPCPlugin Type = "io.containerd.grpc.v1"
  51. // SnapshotPlugin implements a snapshotter
  52. SnapshotPlugin Type = "io.containerd.snapshotter.v1"
  53. // TaskMonitorPlugin implements a task monitor
  54. TaskMonitorPlugin Type = "io.containerd.monitor.v1"
  55. // DiffPlugin implements a differ
  56. DiffPlugin Type = "io.containerd.differ.v1"
  57. // MetadataPlugin implements a metadata store
  58. MetadataPlugin Type = "io.containerd.metadata.v1"
  59. // ContentPlugin implements a content store
  60. ContentPlugin Type = "io.containerd.content.v1"
  61. // GCPlugin implements garbage collection policy
  62. GCPlugin Type = "io.containerd.gc.v1"
  63. )
  64. // Registration contains information for registering a plugin
  65. type Registration struct {
  66. // Type of the plugin
  67. Type Type
  68. // ID of the plugin
  69. ID string
  70. // Config specific to the plugin
  71. Config interface{}
  72. // Requires is a list of plugins that the registered plugin requires to be available
  73. Requires []Type
  74. // InitFn is called when initializing a plugin. The registration and
  75. // context are passed in. The init function may modify the registration to
  76. // add exports, capabilities and platform support declarations.
  77. InitFn func(*InitContext) (interface{}, error)
  78. }
  79. // Init the registered plugin
  80. func (r *Registration) Init(ic *InitContext) *Plugin {
  81. p, err := r.InitFn(ic)
  82. return &Plugin{
  83. Registration: r,
  84. Config: ic.Config,
  85. Meta: ic.Meta,
  86. instance: p,
  87. err: err,
  88. }
  89. }
  90. // URI returns the full plugin URI
  91. func (r *Registration) URI() string {
  92. return fmt.Sprintf("%s.%s", r.Type, r.ID)
  93. }
  94. // Service allows GRPC services to be registered with the underlying server
  95. type Service interface {
  96. Register(*grpc.Server) error
  97. }
  98. var register = struct {
  99. sync.RWMutex
  100. r []*Registration
  101. }{}
  102. // Load loads all plugins at the provided path into containerd
  103. func Load(path string) (err error) {
  104. defer func() {
  105. if v := recover(); v != nil {
  106. rerr, ok := v.(error)
  107. if !ok {
  108. rerr = fmt.Errorf("%s", v)
  109. }
  110. err = rerr
  111. }
  112. }()
  113. return loadPlugins(path)
  114. }
  115. // Register allows plugins to register
  116. func Register(r *Registration) {
  117. register.Lock()
  118. defer register.Unlock()
  119. if r.Type == "" {
  120. panic(ErrNoType)
  121. }
  122. if r.ID == "" {
  123. panic(ErrNoPluginID)
  124. }
  125. var last bool
  126. for _, requires := range r.Requires {
  127. if requires == "*" {
  128. last = true
  129. }
  130. }
  131. if last && len(r.Requires) != 1 {
  132. panic(ErrInvalidRequires)
  133. }
  134. register.r = append(register.r, r)
  135. }
  136. // Graph returns an ordered list of registered plugins for initialization.
  137. // Plugins in disableList specified by id will be disabled.
  138. func Graph(disableList []string) (ordered []*Registration) {
  139. register.RLock()
  140. defer register.RUnlock()
  141. for _, d := range disableList {
  142. for i, r := range register.r {
  143. if r.ID == d {
  144. register.r = append(register.r[:i], register.r[i+1:]...)
  145. break
  146. }
  147. }
  148. }
  149. added := map[*Registration]bool{}
  150. for _, r := range register.r {
  151. children(r.ID, r.Requires, added, &ordered)
  152. if !added[r] {
  153. ordered = append(ordered, r)
  154. added[r] = true
  155. }
  156. }
  157. return ordered
  158. }
  159. func children(id string, types []Type, added map[*Registration]bool, ordered *[]*Registration) {
  160. for _, t := range types {
  161. for _, r := range register.r {
  162. if r.ID != id && (t == "*" || r.Type == t) {
  163. children(r.ID, r.Requires, added, ordered)
  164. if !added[r] {
  165. *ordered = append(*ordered, r)
  166. added[r] = true
  167. }
  168. }
  169. }
  170. }
  171. }