alloc.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package alloc
  2. import (
  3. "fmt"
  4. "net"
  5. "github.com/coreos/flannel/Godeps/_workspace/src/golang.org/x/net/context"
  6. "github.com/coreos/flannel/backend"
  7. "github.com/coreos/flannel/pkg/ip"
  8. "github.com/coreos/flannel/subnet"
  9. )
  10. type AllocBackend struct {
  11. sm subnet.Manager
  12. publicIP ip.IP4
  13. mtu int
  14. lease *subnet.Lease
  15. }
  16. func New(sm subnet.Manager, extIface *net.Interface, extIaddr net.IP, extEaddr net.IP) (backend.Backend, error) {
  17. be := AllocBackend{
  18. sm: sm,
  19. publicIP: ip.FromIP(extEaddr),
  20. mtu: extIface.MTU,
  21. }
  22. return &be, nil
  23. }
  24. func (m *AllocBackend) RegisterNetwork(ctx context.Context, network string, config *subnet.Config) (*backend.SubnetDef, error) {
  25. attrs := subnet.LeaseAttrs{
  26. PublicIP: m.publicIP,
  27. }
  28. l, err := m.sm.AcquireLease(ctx, network, &attrs)
  29. switch err {
  30. case nil:
  31. m.lease = l
  32. return &backend.SubnetDef{
  33. Lease: l,
  34. MTU: m.mtu,
  35. }, nil
  36. case context.Canceled, context.DeadlineExceeded:
  37. return nil, err
  38. default:
  39. return nil, fmt.Errorf("failed to acquire lease: %v", err)
  40. }
  41. }
  42. func (m *AllocBackend) Run(ctx context.Context) {
  43. }
  44. func (m *AllocBackend) UnregisterNetwork(ctx context.Context, name string) {
  45. }