refactor: Introduce IPAM config object

This commit is contained in:
Adam Harrison committed 2016-03-14 17:41:20 +00:00
1 parent 2a10182cf6
commit 2eeb754454
3 files changed
+31 -11

No files matched your search

+18 -8
View File
@@ -66,17 +66,27 @@ type Allocator struct {
now func() time.Time now func() time.Time
} }
type Config struct {
OurName mesh.PeerName
OurUID mesh.PeerUID
OurNickname string
Universe address.Range
Quorum uint
Db db.DB
IsKnownPeer func(name mesh.PeerName) bool
}
// NewAllocator creates and initialises a new Allocator // NewAllocator creates and initialises a new Allocator
func NewAllocator(ourName mesh.PeerName, ourUID mesh.PeerUID, ourNickname string, universe address.Range, quorum uint, db db.DB, isKnownPeer func(name mesh.PeerName) bool) *Allocator { func NewAllocator(config Config) *Allocator {
return &Allocator{ return &Allocator{
ourName: ourName, ourName: config.OurName,
universe: universe, universe: config.Universe,
ring: ring.New(universe.Start, universe.End, ourName), ring: ring.New(config.Universe.Start, config.Universe.End, config.OurName),
owned: make(map[string][]address.CIDR), owned: make(map[string][]address.CIDR),
db: db, db: config.Db,
paxos: paxos.NewParticipant(ourName, ourUID, quorum), paxos: paxos.NewParticipant(config.OurName, config.OurUID, config.Quorum),
nicknames: map[mesh.PeerName]string{ourName: ourNickname}, nicknames: map[mesh.PeerName]string{config.OurName: config.OurNickname},
isKnownPeer: isKnownPeer, isKnownPeer: config.IsKnownPeer,
dead: make(map[string]time.Time), dead: make(map[string]time.Time),
now: time.Now, now: time.Now,
} }
+4 -2
View File
@@ -139,8 +139,10 @@ func makeAllocator(name string, cidrStr string, quorum uint) (*Allocator, addres
panic(err) panic(err)
} }
alloc := NewAllocator(peername, mesh.PeerUID(rand.Int63()), config := Config{peername, mesh.PeerUID(rand.Int63()),
"nick-"+name, cidr.Range(), quorum, new(mockDB), func(mesh.PeerName) bool { return true }) "nick-" + name, cidr.Range(), quorum, new(mockDB), func(mesh.PeerName) bool { return true }}
alloc := NewAllocator(config)
return alloc, cidr return alloc, cidr
} }
+9 -1
View File
@@ -366,7 +366,15 @@ func createAllocator(router *mesh.Router, ipRangeStr string, defaultSubnetStr st
Log.Fatalf("IP address allocation default subnet %s does not overlap with allocation range %s", defaultSubnet, ipRange) Log.Fatalf("IP address allocation default subnet %s does not overlap with allocation range %s", defaultSubnet, ipRange)
} }
} }
allocator := ipam.NewAllocator(router.Ourself.Peer.Name, router.Ourself.Peer.UID, router.Ourself.Peer.NickName, ipRange.Range(), quorum, db, isKnownPeer)
config := ipam.Config{
router.Ourself.Peer.Name,
router.Ourself.Peer.UID,
router.Ourself.Peer.NickName,
ipRange.Range(),
quorum, db, isKnownPeer}
allocator := ipam.NewAllocator(config)
allocator.SetInterfaces(router.NewGossip("IPallocation", allocator)) allocator.SetInterfaces(router.NewGossip("IPallocation", allocator))
allocator.Start() allocator.Start()