alloc.go 1009 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package alloc
  2. import (
  3. "fmt"
  4. "net"
  5. "github.com/coreos/flannel/backend"
  6. "github.com/coreos/flannel/pkg/ip"
  7. "github.com/coreos/flannel/pkg/task"
  8. "github.com/coreos/flannel/subnet"
  9. )
  10. type AllocBackend struct {
  11. sm *subnet.SubnetManager
  12. stop chan bool
  13. }
  14. func New(sm *subnet.SubnetManager) backend.Backend {
  15. return &AllocBackend{
  16. sm: sm,
  17. stop: make(chan bool),
  18. }
  19. }
  20. func (m *AllocBackend) Init(extIface *net.Interface, extIP net.IP, ipMasq bool) (*backend.SubnetDef, error) {
  21. attrs := subnet.BaseAttrs{
  22. PublicIP: ip.FromIP(extIP),
  23. }
  24. sn, err := m.sm.AcquireLease(ip.FromIP(extIP), &attrs, m.stop)
  25. if err != nil {
  26. if err == task.ErrCanceled {
  27. return nil, err
  28. } else {
  29. return nil, fmt.Errorf("failed to acquire lease: %v", err)
  30. }
  31. }
  32. return &backend.SubnetDef{
  33. Net: sn,
  34. MTU: extIface.MTU,
  35. }, nil
  36. }
  37. func (m *AllocBackend) Run() {
  38. m.sm.LeaseRenewer(m.stop)
  39. }
  40. func (m *AllocBackend) Stop() {
  41. close(m.stop)
  42. }
  43. func (m *AllocBackend) Name() string {
  44. return "allocation"
  45. }