alloc.go 985 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package alloc
  2. import (
  3. "fmt"
  4. "net"
  5. "github.com/coreos/rudder/backend"
  6. "github.com/coreos/rudder/pkg/ip"
  7. "github.com/coreos/rudder/pkg/task"
  8. "github.com/coreos/rudder/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) (ip.IP4Net, int, 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 ip.IP4Net{}, 0, err
  28. } else {
  29. return ip.IP4Net{}, 0, fmt.Errorf("Failed to acquire lease: %v", err)
  30. }
  31. }
  32. return sn, extIface.MTU, nil
  33. }
  34. func (m *AllocBackend) Run() {
  35. m.sm.LeaseRenewer(m.stop)
  36. }
  37. func (m *AllocBackend) Stop() {
  38. close(m.stop)
  39. }
  40. func (m *AllocBackend) Name() string {
  41. return "allocation"
  42. }