forked from flynn-archive/flannel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alloc.go
56 lines (45 loc) · 992 Bytes
/
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
package alloc
import (
"fmt"
"net"
"github.com/coreos/flannel/backend"
"github.com/coreos/flannel/pkg/ip"
"github.com/coreos/flannel/pkg/task"
"github.com/coreos/flannel/subnet"
)
type AllocBackend struct {
sm *subnet.SubnetManager
stop chan bool
}
func New(sm *subnet.SubnetManager) backend.Backend {
return &AllocBackend{
sm: sm,
stop: make(chan bool),
}
}
func (m *AllocBackend) Init(extIface *net.Interface, extIP net.IP, ipMasq bool) (*backend.SubnetDef, error) {
attrs := subnet.LeaseAttrs{
PublicIP: ip.FromIP(extIP),
}
sn, err := m.sm.AcquireLease(&attrs, m.stop)
if err != nil {
if err == task.ErrCanceled {
return nil, err
} else {
return nil, fmt.Errorf("failed to acquire lease: %v", err)
}
}
return &backend.SubnetDef{
Net: sn,
MTU: extIface.MTU,
}, nil
}
func (m *AllocBackend) Run() {
m.sm.LeaseRenewer(m.stop)
}
func (m *AllocBackend) Stop() {
close(m.stop)
}
func (m *AllocBackend) Name() string {
return "allocation"
}