extpoint.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. //go:generate pluginrpc-gen -i $GOFILE -o proxy.go -type volumeDriver -name VolumeDriver
  2. package drivers // import "github.com/docker/docker/volume/drivers"
  3. import (
  4. "context"
  5. "fmt"
  6. "sort"
  7. "sync"
  8. "github.com/containerd/log"
  9. "github.com/docker/docker/errdefs"
  10. getter "github.com/docker/docker/pkg/plugingetter"
  11. "github.com/docker/docker/pkg/plugins"
  12. "github.com/docker/docker/volume"
  13. "github.com/moby/locker"
  14. "github.com/pkg/errors"
  15. )
  16. const extName = "VolumeDriver"
  17. // volumeDriver defines the available functions that volume plugins must implement.
  18. // This interface is only defined to generate the proxy objects.
  19. // It's not intended to be public or reused.
  20. //
  21. //nolint:unused
  22. type volumeDriver interface {
  23. // Create a volume with the given name
  24. Create(name string, opts map[string]string) (err error)
  25. // Remove the volume with the given name
  26. Remove(name string) (err error)
  27. // Path returns the mountpoint of the given volume.
  28. Path(name string) (mountpoint string, err error)
  29. // Mount the given volume and return the mountpoint
  30. Mount(name, id string) (mountpoint string, err error)
  31. // Unmount the given volume
  32. Unmount(name, id string) (err error)
  33. // List lists all the volumes known to the driver
  34. List() (volumes []*proxyVolume, err error)
  35. // Get retrieves the volume with the requested name
  36. Get(name string) (volume *proxyVolume, err error)
  37. // Capabilities gets the list of capabilities of the driver
  38. Capabilities() (capabilities volume.Capability, err error)
  39. }
  40. // Store is an in-memory store for volume drivers
  41. type Store struct {
  42. extensions map[string]volume.Driver
  43. mu sync.Mutex
  44. driverLock *locker.Locker
  45. pluginGetter getter.PluginGetter
  46. }
  47. // NewStore creates a new volume driver store
  48. func NewStore(pg getter.PluginGetter) *Store {
  49. return &Store{
  50. extensions: make(map[string]volume.Driver),
  51. driverLock: locker.New(),
  52. pluginGetter: pg,
  53. }
  54. }
  55. type driverNotFoundError string
  56. func (e driverNotFoundError) Error() string {
  57. return "volume driver not found: " + string(e)
  58. }
  59. func (driverNotFoundError) NotFound() {}
  60. // lookup returns the driver associated with the given name. If a
  61. // driver with the given name has not been registered it checks if
  62. // there is a VolumeDriver plugin available with the given name.
  63. func (s *Store) lookup(name string, mode int) (volume.Driver, error) {
  64. if name == "" {
  65. return nil, errdefs.InvalidParameter(errors.New("driver name cannot be empty"))
  66. }
  67. s.driverLock.Lock(name)
  68. defer s.driverLock.Unlock(name)
  69. s.mu.Lock()
  70. ext, ok := s.extensions[name]
  71. s.mu.Unlock()
  72. if ok {
  73. return ext, nil
  74. }
  75. if s.pluginGetter != nil {
  76. p, err := s.pluginGetter.Get(name, extName, mode)
  77. if err != nil {
  78. return nil, errors.Wrap(err, "error looking up volume plugin "+name)
  79. }
  80. d, err := makePluginAdapter(p)
  81. if err != nil {
  82. return nil, errors.Wrap(err, "error making plugin client")
  83. }
  84. if err := validateDriver(d); err != nil {
  85. if mode > 0 {
  86. // Undo any reference count changes from the initial `Get`
  87. if _, err := s.pluginGetter.Get(name, extName, mode*-1); err != nil {
  88. log.G(context.TODO()).WithError(err).WithField("action", "validate-driver").WithField("plugin", name).Error("error releasing reference to plugin")
  89. }
  90. }
  91. return nil, err
  92. }
  93. if p.IsV1() {
  94. s.mu.Lock()
  95. s.extensions[name] = d
  96. s.mu.Unlock()
  97. }
  98. return d, nil
  99. }
  100. return nil, driverNotFoundError(name)
  101. }
  102. func validateDriver(vd volume.Driver) error {
  103. scope := vd.Scope()
  104. if scope != volume.LocalScope && scope != volume.GlobalScope {
  105. return fmt.Errorf("Driver %q provided an invalid capability scope: %s", vd.Name(), scope)
  106. }
  107. return nil
  108. }
  109. // Register associates the given driver to the given name, checking if
  110. // the name is already associated
  111. func (s *Store) Register(d volume.Driver, name string) bool {
  112. if name == "" {
  113. return false
  114. }
  115. s.mu.Lock()
  116. defer s.mu.Unlock()
  117. if _, exists := s.extensions[name]; exists {
  118. return false
  119. }
  120. if err := validateDriver(d); err != nil {
  121. return false
  122. }
  123. s.extensions[name] = d
  124. return true
  125. }
  126. // GetDriver returns a volume driver by its name.
  127. // If the driver is empty, it looks for the local driver.
  128. func (s *Store) GetDriver(name string) (volume.Driver, error) {
  129. return s.lookup(name, getter.Lookup)
  130. }
  131. // CreateDriver returns a volume driver by its name and increments RefCount.
  132. // If the driver is empty, it looks for the local driver.
  133. func (s *Store) CreateDriver(name string) (volume.Driver, error) {
  134. return s.lookup(name, getter.Acquire)
  135. }
  136. // ReleaseDriver returns a volume driver by its name and decrements RefCount..
  137. // If the driver is empty, it looks for the local driver.
  138. func (s *Store) ReleaseDriver(name string) (volume.Driver, error) {
  139. return s.lookup(name, getter.Release)
  140. }
  141. // GetDriverList returns list of volume drivers registered.
  142. // If no driver is registered, empty string list will be returned.
  143. func (s *Store) GetDriverList() []string {
  144. var driverList []string
  145. s.mu.Lock()
  146. defer s.mu.Unlock()
  147. for driverName := range s.extensions {
  148. driverList = append(driverList, driverName)
  149. }
  150. sort.Strings(driverList)
  151. return driverList
  152. }
  153. // GetAllDrivers lists all the registered drivers
  154. func (s *Store) GetAllDrivers() ([]volume.Driver, error) {
  155. var plugins []getter.CompatPlugin
  156. if s.pluginGetter != nil {
  157. var err error
  158. plugins, err = s.pluginGetter.GetAllByCap(extName)
  159. if err != nil {
  160. return nil, fmt.Errorf("error listing plugins: %v", err)
  161. }
  162. }
  163. var ds []volume.Driver
  164. s.mu.Lock()
  165. defer s.mu.Unlock()
  166. for _, d := range s.extensions {
  167. ds = append(ds, d)
  168. }
  169. for _, p := range plugins {
  170. name := p.Name()
  171. if _, ok := s.extensions[name]; ok {
  172. continue
  173. }
  174. ext, err := makePluginAdapter(p)
  175. if err != nil {
  176. return nil, errors.Wrap(err, "error making plugin client")
  177. }
  178. if p.IsV1() {
  179. s.extensions[name] = ext
  180. }
  181. ds = append(ds, ext)
  182. }
  183. return ds, nil
  184. }
  185. func makePluginAdapter(p getter.CompatPlugin) (*volumeDriverAdapter, error) {
  186. if pc, ok := p.(getter.PluginWithV1Client); ok {
  187. return &volumeDriverAdapter{name: p.Name(), scopePath: p.ScopedPath, proxy: &volumeDriverProxy{pc.Client()}}, nil
  188. }
  189. pa, ok := p.(getter.PluginAddr)
  190. if !ok {
  191. return nil, errdefs.System(errors.Errorf("got unknown plugin instance %T", p))
  192. }
  193. if pa.Protocol() != plugins.ProtocolSchemeHTTPV1 {
  194. return nil, errors.Errorf("plugin protocol not supported: %s", p)
  195. }
  196. addr := pa.Addr()
  197. client, err := plugins.NewClientWithTimeout(addr.Network()+"://"+addr.String(), nil, pa.Timeout())
  198. if err != nil {
  199. return nil, errors.Wrap(err, "error creating plugin client")
  200. }
  201. return &volumeDriverAdapter{name: p.Name(), scopePath: p.ScopedPath, proxy: &volumeDriverProxy{client}}, nil
  202. }