From 2eeb754454a64026c85280ff2e55af37d04c8e0d Mon Sep 17 00:00:00 2001 From: Adam Harrison Date: Mon, 14 Mar 2016 17:27:14 +0000 Subject: [PATCH] refactor: Introduce IPAM config object --- ipam/allocator.go | 26 ++++++++++++++++++-------- ipam/testutils_test.go | 6 ++++-- prog/weaver/main.go | 10 +++++++++- 3 files changed, 31 insertions(+), 11 deletions(-) diff --git a/ipam/allocator.go b/ipam/allocator.go index 0b151c67..819b1a49 100644 --- a/ipam/allocator.go +++ b/ipam/allocator.go @@ -66,17 +66,27 @@ type Allocator struct { 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 -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{ - ourName: ourName, - universe: universe, - ring: ring.New(universe.Start, universe.End, ourName), + ourName: config.OurName, + universe: config.Universe, + ring: ring.New(config.Universe.Start, config.Universe.End, config.OurName), owned: make(map[string][]address.CIDR), - db: db, - paxos: paxos.NewParticipant(ourName, ourUID, quorum), - nicknames: map[mesh.PeerName]string{ourName: ourNickname}, - isKnownPeer: isKnownPeer, + db: config.Db, + paxos: paxos.NewParticipant(config.OurName, config.OurUID, config.Quorum), + nicknames: map[mesh.PeerName]string{config.OurName: config.OurNickname}, + isKnownPeer: config.IsKnownPeer, dead: make(map[string]time.Time), now: time.Now, } diff --git a/ipam/testutils_test.go b/ipam/testutils_test.go index fbb4df45..40dcf634 100644 --- a/ipam/testutils_test.go +++ b/ipam/testutils_test.go @@ -139,8 +139,10 @@ func makeAllocator(name string, cidrStr string, quorum uint) (*Allocator, addres panic(err) } - alloc := NewAllocator(peername, mesh.PeerUID(rand.Int63()), - "nick-"+name, cidr.Range(), quorum, new(mockDB), func(mesh.PeerName) bool { return true }) + config := Config{peername, mesh.PeerUID(rand.Int63()), + "nick-" + name, cidr.Range(), quorum, new(mockDB), func(mesh.PeerName) bool { return true }} + + alloc := NewAllocator(config) return alloc, cidr } diff --git a/prog/weaver/main.go b/prog/weaver/main.go index 511d017d..9833477d 100644 --- a/prog/weaver/main.go +++ b/prog/weaver/main.go @@ -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) } } - 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.Start()