1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- package alloc
- import (
- "fmt"
- "github.com/flannel-io/flannel/backend"
- "github.com/flannel-io/flannel/pkg/ip"
- "github.com/flannel-io/flannel/subnet"
- "golang.org/x/net/context"
- "sync"
- )
- func init() {
- backend.Register("alloc", New)
- }
- type AllocBackend struct {
- sm subnet.Manager
- extIface *backend.ExternalInterface
- }
- func New(sm subnet.Manager, extIface *backend.ExternalInterface) (backend.Backend, error) {
- be := AllocBackend{
- sm: sm,
- extIface: extIface,
- }
- return &be, nil
- }
- func (be *AllocBackend) RegisterNetwork(ctx context.Context, wg *sync.WaitGroup, config *subnet.Config) (backend.Network, error) {
- attrs := subnet.LeaseAttrs{
- PublicIP: ip.FromIP(be.extIface.ExtAddr),
- }
- l, err := be.sm.AcquireLease(ctx, &attrs)
- switch err {
- case nil:
- return &backend.SimpleNetwork{
- SubnetLease: l,
- ExtIface: be.extIface,
- }, nil
- case context.Canceled, context.DeadlineExceeded:
- return nil, err
- default:
- return nil, fmt.Errorf("failed to acquire lease: %v", err)
- }
- }
|