alloc.go 1016 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. network string
  13. lease *subnet.Lease
  14. }
  15. func New(sm subnet.Manager, network string) backend.Backend {
  16. return &AllocBackend{
  17. sm: sm,
  18. network: network,
  19. }
  20. }
  21. func (m *AllocBackend) Init(ctx context.Context, extIface *net.Interface, extIaddr net.IP, extEaddr net.IP) (*backend.SubnetDef, error) {
  22. attrs := subnet.LeaseAttrs{
  23. PublicIP: ip.FromIP(extEaddr),
  24. }
  25. l, err := m.sm.AcquireLease(ctx, m.network, &attrs)
  26. switch err {
  27. case nil:
  28. m.lease = l
  29. return &backend.SubnetDef{
  30. Lease: l,
  31. MTU: extIface.MTU,
  32. }, nil
  33. case context.Canceled, context.DeadlineExceeded:
  34. return nil, err
  35. default:
  36. return nil, fmt.Errorf("failed to acquire lease: %v", err)
  37. }
  38. }
  39. func (m *AllocBackend) Run(ctx context.Context) {
  40. }