drvregistry.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package drvregistry
  2. import (
  3. "fmt"
  4. "github.com/docker/docker/libnetwork/driverapi"
  5. "github.com/docker/docker/libnetwork/ipamapi"
  6. "github.com/docker/docker/pkg/plugingetter"
  7. )
  8. // DrvRegistry holds the registry of all network drivers and IPAM drivers that it knows about.
  9. type DrvRegistry struct {
  10. Networks
  11. IPAMs
  12. pluginGetter plugingetter.PluginGetter
  13. }
  14. var _ driverapi.DriverCallback = (*DrvRegistry)(nil)
  15. var _ ipamapi.Callback = (*DrvRegistry)(nil)
  16. // InitFunc defines the driver initialization function signature.
  17. type InitFunc func(driverapi.DriverCallback, map[string]interface{}) error
  18. // Placeholder is a type for function arguments which need to be present for Swarmkit
  19. // to compile, but for which the only acceptable value is nil.
  20. type Placeholder *struct{}
  21. // New returns a new legacy driver registry.
  22. //
  23. // Deprecated: use the separate [Networks] and [IPAMs] registries.
  24. func New(lDs, gDs Placeholder, dfn DriverNotifyFunc, ifn Placeholder, pg plugingetter.PluginGetter) (*DrvRegistry, error) {
  25. return &DrvRegistry{
  26. Networks: Networks{Notify: dfn},
  27. pluginGetter: pg,
  28. }, nil
  29. }
  30. // AddDriver adds a network driver to the registry.
  31. //
  32. // Deprecated: call fn(r, config) directly.
  33. func (r *DrvRegistry) AddDriver(_ string, fn InitFunc, config map[string]interface{}) error {
  34. return fn(r, config)
  35. }
  36. // IPAMDefaultAddressSpaces returns the default address space strings for the passed IPAM driver name.
  37. //
  38. // Deprecated: call GetDefaultAddressSpaces() on the IPAM driver.
  39. func (r *DrvRegistry) IPAMDefaultAddressSpaces(name string) (string, string, error) {
  40. d, _ := r.IPAM(name)
  41. if d == nil {
  42. return "", "", fmt.Errorf("ipam %s not found", name)
  43. }
  44. return d.GetDefaultAddressSpaces()
  45. }
  46. // GetPluginGetter returns the plugingetter
  47. func (r *DrvRegistry) GetPluginGetter() plugingetter.PluginGetter {
  48. return r.pluginGetter
  49. }
  50. // Driver returns the network driver instance registered under name, and its capability.
  51. func (r *DrvRegistry) Driver(name string) (driverapi.Driver, *driverapi.Capability) {
  52. d, c := r.Networks.Driver(name)
  53. if c == (driverapi.Capability{}) {
  54. return d, nil
  55. }
  56. return d, &c
  57. }