windowsipam.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package windowsipam
  2. import (
  3. "net"
  4. "github.com/Sirupsen/logrus"
  5. "github.com/docker/libnetwork/discoverapi"
  6. "github.com/docker/libnetwork/ipamapi"
  7. "github.com/docker/libnetwork/netlabel"
  8. "github.com/docker/libnetwork/types"
  9. )
  10. const (
  11. localAddressSpace = "LocalDefault"
  12. globalAddressSpace = "GlobalDefault"
  13. )
  14. // DefaultIPAM defines the default ipam-driver for local-scoped windows networks
  15. const DefaultIPAM = "windows"
  16. var (
  17. defaultPool, _ = types.ParseCIDR("0.0.0.0/0")
  18. )
  19. type allocator struct {
  20. }
  21. // GetInit registers the built-in ipam service with libnetwork
  22. func GetInit(ipamName string) func(ic ipamapi.Callback, l, g interface{}) error {
  23. return func(ic ipamapi.Callback, l, g interface{}) error {
  24. return ic.RegisterIpamDriver(ipamName, &allocator{})
  25. }
  26. }
  27. func (a *allocator) GetDefaultAddressSpaces() (string, string, error) {
  28. return localAddressSpace, globalAddressSpace, nil
  29. }
  30. // RequestPool returns an address pool along with its unique id. This is a null ipam driver. It allocates the
  31. // subnet user asked and does not validate anything. Doesn't support subpool allocation
  32. func (a *allocator) RequestPool(addressSpace, pool, subPool string, options map[string]string, v6 bool) (string, *net.IPNet, map[string]string, error) {
  33. logrus.Debugf("RequestPool(%s, %s, %s, %v, %t)", addressSpace, pool, subPool, options, v6)
  34. if subPool != "" || v6 {
  35. return "", nil, nil, types.InternalErrorf("This request is not supported by null ipam driver")
  36. }
  37. var ipNet *net.IPNet
  38. var err error
  39. if pool != "" {
  40. _, ipNet, err = net.ParseCIDR(pool)
  41. if err != nil {
  42. return "", nil, nil, err
  43. }
  44. } else {
  45. ipNet = defaultPool
  46. }
  47. return ipNet.String(), ipNet, nil, nil
  48. }
  49. // ReleasePool releases the address pool - always succeeds
  50. func (a *allocator) ReleasePool(poolID string) error {
  51. logrus.Debugf("ReleasePool(%s)", poolID)
  52. return nil
  53. }
  54. // RequestAddress returns an address from the specified pool ID.
  55. // Always allocate the 0.0.0.0/32 ip if no preferred address was specified
  56. func (a *allocator) RequestAddress(poolID string, prefAddress net.IP, opts map[string]string) (*net.IPNet, map[string]string, error) {
  57. logrus.Debugf("RequestAddress(%s, %v, %v)", poolID, prefAddress, opts)
  58. _, ipNet, err := net.ParseCIDR(poolID)
  59. if err != nil {
  60. return nil, nil, err
  61. }
  62. // TODO Windows: Remove this once the bug in docker daemon is fixed
  63. // that causes it to throw an exception on nil gateway
  64. if prefAddress != nil {
  65. return &net.IPNet{IP: prefAddress, Mask: ipNet.Mask}, nil, nil
  66. } else if opts[ipamapi.RequestAddressType] == netlabel.Gateway {
  67. return ipNet, nil, nil
  68. } else {
  69. return nil, nil, nil
  70. }
  71. }
  72. // ReleaseAddress releases the address - always succeeds
  73. func (a *allocator) ReleaseAddress(poolID string, address net.IP) error {
  74. logrus.Debugf("ReleaseAddress(%s, %v)", poolID, address)
  75. return nil
  76. }
  77. // DiscoverNew informs the allocator about a new global scope datastore
  78. func (a *allocator) DiscoverNew(dType discoverapi.DiscoveryType, data interface{}) error {
  79. return nil
  80. }
  81. // DiscoverDelete is a notification of no interest for the allocator
  82. func (a *allocator) DiscoverDelete(dType discoverapi.DiscoveryType, data interface{}) error {
  83. return nil
  84. }
  85. func (a *allocator) IsBuiltIn() bool {
  86. return true
  87. }