forked from flannel-io/flannel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alloc.go
63 lines (51 loc) · 1.22 KB
/
alloc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package alloc
import (
"fmt"
"net"
"github.com/coreos/flannel/Godeps/_workspace/src/golang.org/x/net/context"
"github.com/coreos/flannel/backend"
"github.com/coreos/flannel/pkg/ip"
"github.com/coreos/flannel/subnet"
)
type AllocBackend struct {
sm subnet.Manager
network string
lease *subnet.Lease
ctx context.Context
cancel context.CancelFunc
}
func New(sm subnet.Manager, network string) backend.Backend {
ctx, cancel := context.WithCancel(context.Background())
return &AllocBackend{
sm: sm,
network: network,
ctx: ctx,
cancel: cancel,
}
}
func (m *AllocBackend) Init(extIface *net.Interface, extIP net.IP) (*backend.SubnetDef, error) {
attrs := subnet.LeaseAttrs{
PublicIP: ip.FromIP(extIP),
}
l, err := m.sm.AcquireLease(m.ctx, m.network, &attrs)
switch err {
case nil:
return &backend.SubnetDef{
Net: l.Subnet,
MTU: extIface.MTU,
}, nil
case context.Canceled, context.DeadlineExceeded:
return nil, err
default:
return nil, fmt.Errorf("failed to acquire lease: %v", err)
}
}
func (m *AllocBackend) Run() {
subnet.LeaseRenewer(m.ctx, m.sm, m.network, m.lease)
}
func (m *AllocBackend) Stop() {
m.cancel()
}
func (m *AllocBackend) Name() string {
return "allocation"
}