plugin.go 5.3 KB

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