Give AddressManager responsibility for its host
You no longer have to pass a hostname to initialise the addressmanager. The DefaultAddressManager always listens on localhost. If you want to listen on some other interface, you can use a different AddressManager.
This commit is contained in:
parent
57c5c42731
commit
08cb5b2ee7
|
|
@ -8,7 +8,7 @@ import (
|
||||||
// AddressManager knows how to generate and remember a single address on some
|
// AddressManager knows how to generate and remember a single address on some
|
||||||
// local interface for a service to listen on.
|
// local interface for a service to listen on.
|
||||||
type AddressManager interface {
|
type AddressManager interface {
|
||||||
Initialize(host string) (port int, resolvedAddress string, err error)
|
Initialize() (port int, resolvedAddress string, err error)
|
||||||
Host() (string, error)
|
Host() (string, error)
|
||||||
Port() (int, error)
|
Port() (int, error)
|
||||||
}
|
}
|
||||||
|
|
@ -22,13 +22,13 @@ type DefaultAddressManager struct {
|
||||||
host string
|
host string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize returns a address a process can listen on. Given a hostname it returns an address,
|
// Initialize returns a address a process can listen on. It returns
|
||||||
// a tuple consisting of a free port and the hostname resolved to its IP.
|
// a tuple consisting of a free port and the hostname resolved to its IP.
|
||||||
func (d *DefaultAddressManager) Initialize(host string) (port int, resolvedHost string, err error) {
|
func (d *DefaultAddressManager) Initialize() (port int, resolvedHost string, err error) {
|
||||||
if d.port != 0 {
|
if d.port != 0 {
|
||||||
return 0, "", fmt.Errorf("this DefaultAddressManager is already initialized")
|
return 0, "", fmt.Errorf("this DefaultAddressManager is already initialized")
|
||||||
}
|
}
|
||||||
addr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf("%s:0", host))
|
addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ var _ = Describe("DefaultAddressManager", func() {
|
||||||
|
|
||||||
Describe("Initialize", func() {
|
Describe("Initialize", func() {
|
||||||
It("returns a free port and an address to bind to", func() {
|
It("returns a free port and an address to bind to", func() {
|
||||||
port, host, err := defaultAddressManager.Initialize("localhost")
|
port, host, err := defaultAddressManager.Initialize()
|
||||||
|
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
Expect(host).To(Equal("127.0.0.1"))
|
Expect(host).To(Equal("127.0.0.1"))
|
||||||
|
|
@ -33,32 +33,11 @@ var _ = Describe("DefaultAddressManager", func() {
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
})
|
})
|
||||||
|
|
||||||
Context("when given an invalid hostname", func() {
|
|
||||||
It("propagates the error", func() {
|
|
||||||
_, _, err := defaultAddressManager.Initialize("this is not a hostname")
|
|
||||||
|
|
||||||
Expect(err).To(MatchError(ContainSubstring("no such host")))
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
Context("when given a hostname that we don't have permission to listen on", func() {
|
|
||||||
It("propagates the error", func() {
|
|
||||||
_, _, err := defaultAddressManager.Initialize("example.com")
|
|
||||||
|
|
||||||
Expect(err).To(SatisfyAny(
|
|
||||||
// Linux
|
|
||||||
MatchError(ContainSubstring("bind: cannot assign requested address")),
|
|
||||||
// Darwin
|
|
||||||
MatchError(ContainSubstring("bind: can't assign requested address")),
|
|
||||||
))
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
Context("initialized multiple times", func() {
|
Context("initialized multiple times", func() {
|
||||||
It("fails", func() {
|
It("fails", func() {
|
||||||
_, _, err := defaultAddressManager.Initialize("localhost")
|
_, _, err := defaultAddressManager.Initialize()
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
_, _, err = defaultAddressManager.Initialize("localhost")
|
_, _, err = defaultAddressManager.Initialize()
|
||||||
Expect(err).To(MatchError(ContainSubstring("already initialized")))
|
Expect(err).To(MatchError(ContainSubstring("already initialized")))
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
@ -69,7 +48,7 @@ var _ = Describe("DefaultAddressManager", func() {
|
||||||
Expect(err).To(MatchError(ContainSubstring("not initialized yet")))
|
Expect(err).To(MatchError(ContainSubstring("not initialized yet")))
|
||||||
})
|
})
|
||||||
It("returns the same port as previously allocated by Initialize", func() {
|
It("returns the same port as previously allocated by Initialize", func() {
|
||||||
expectedPort, _, err := defaultAddressManager.Initialize("localhost")
|
expectedPort, _, err := defaultAddressManager.Initialize()
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
actualPort, err := defaultAddressManager.Port()
|
actualPort, err := defaultAddressManager.Port()
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
|
@ -82,7 +61,7 @@ var _ = Describe("DefaultAddressManager", func() {
|
||||||
Expect(err).To(MatchError(ContainSubstring("not initialized yet")))
|
Expect(err).To(MatchError(ContainSubstring("not initialized yet")))
|
||||||
})
|
})
|
||||||
It("returns the same port as previously allocated by Initialize", func() {
|
It("returns the same port as previously allocated by Initialize", func() {
|
||||||
_, expectedHost, err := defaultAddressManager.Initialize("localhost")
|
_, expectedHost, err := defaultAddressManager.Initialize()
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
actualHost, err := defaultAddressManager.Host()
|
actualHost, err := defaultAddressManager.Host()
|
||||||
Expect(err).NotTo(HaveOccurred())
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
|
|
||||||
|
|
@ -51,7 +51,7 @@ func (s *APIServer) URL() (string, error) {
|
||||||
func (s *APIServer) Start() error {
|
func (s *APIServer) Start() error {
|
||||||
s.ensureInitialized()
|
s.ensureInitialized()
|
||||||
|
|
||||||
port, addr, err := s.AddressManager.Initialize("localhost")
|
port, addr, err := s.AddressManager.Initialize()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -88,7 +88,6 @@ var _ = Describe("Apiserver", func() {
|
||||||
|
|
||||||
By("...in turn calling the AddressManager")
|
By("...in turn calling the AddressManager")
|
||||||
Expect(fakeAddressManager.InitializeCallCount()).To(Equal(1))
|
Expect(fakeAddressManager.InitializeCallCount()).To(Equal(1))
|
||||||
Expect(fakeAddressManager.InitializeArgsForCall(0)).To(Equal("localhost"))
|
|
||||||
|
|
||||||
By("...in turn calling the CertDirManager")
|
By("...in turn calling the CertDirManager")
|
||||||
Expect(fakeCertDirManager.CreateCallCount()).To(Equal(1))
|
Expect(fakeCertDirManager.CreateCallCount()).To(Equal(1))
|
||||||
|
|
|
||||||
|
|
@ -61,7 +61,7 @@ func (e *Etcd) URL() (string, error) {
|
||||||
func (e *Etcd) Start() error {
|
func (e *Etcd) Start() error {
|
||||||
e.ensureInitialized()
|
e.ensureInitialized()
|
||||||
|
|
||||||
port, host, err := e.AddressManager.Initialize("localhost")
|
port, host, err := e.AddressManager.Initialize()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -76,7 +76,6 @@ var _ = Describe("Etcd", func() {
|
||||||
|
|
||||||
By("...in turn calling using the AddressManager")
|
By("...in turn calling using the AddressManager")
|
||||||
Expect(fakeAddressManager.InitializeCallCount()).To(Equal(1))
|
Expect(fakeAddressManager.InitializeCallCount()).To(Equal(1))
|
||||||
Expect(fakeAddressManager.InitializeArgsForCall(0)).To(Equal("localhost"))
|
|
||||||
|
|
||||||
By("...in turn using the DataDirManager")
|
By("...in turn using the DataDirManager")
|
||||||
Expect(fakeDataDirManager.CreateCallCount()).To(Equal(1))
|
Expect(fakeDataDirManager.CreateCallCount()).To(Equal(1))
|
||||||
|
|
|
||||||
|
|
@ -8,11 +8,9 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type FakeAddressManager struct {
|
type FakeAddressManager struct {
|
||||||
InitializeStub func(host string) (port int, resolvedAddress string, err error)
|
InitializeStub func() (port int, resolvedAddress string, err error)
|
||||||
initializeMutex sync.RWMutex
|
initializeMutex sync.RWMutex
|
||||||
initializeArgsForCall []struct {
|
initializeArgsForCall []struct{}
|
||||||
host string
|
|
||||||
}
|
|
||||||
initializeReturns struct {
|
initializeReturns struct {
|
||||||
result1 int
|
result1 int
|
||||||
result2 string
|
result2 string
|
||||||
|
|
@ -49,16 +47,14 @@ type FakeAddressManager struct {
|
||||||
invocationsMutex sync.RWMutex
|
invocationsMutex sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func (fake *FakeAddressManager) Initialize(host string) (port int, resolvedAddress string, err error) {
|
func (fake *FakeAddressManager) Initialize() (port int, resolvedAddress string, err error) {
|
||||||
fake.initializeMutex.Lock()
|
fake.initializeMutex.Lock()
|
||||||
ret, specificReturn := fake.initializeReturnsOnCall[len(fake.initializeArgsForCall)]
|
ret, specificReturn := fake.initializeReturnsOnCall[len(fake.initializeArgsForCall)]
|
||||||
fake.initializeArgsForCall = append(fake.initializeArgsForCall, struct {
|
fake.initializeArgsForCall = append(fake.initializeArgsForCall, struct{}{})
|
||||||
host string
|
fake.recordInvocation("Initialize", []interface{}{})
|
||||||
}{host})
|
|
||||||
fake.recordInvocation("Initialize", []interface{}{host})
|
|
||||||
fake.initializeMutex.Unlock()
|
fake.initializeMutex.Unlock()
|
||||||
if fake.InitializeStub != nil {
|
if fake.InitializeStub != nil {
|
||||||
return fake.InitializeStub(host)
|
return fake.InitializeStub()
|
||||||
}
|
}
|
||||||
if specificReturn {
|
if specificReturn {
|
||||||
return ret.result1, ret.result2, ret.result3
|
return ret.result1, ret.result2, ret.result3
|
||||||
|
|
@ -72,12 +68,6 @@ func (fake *FakeAddressManager) InitializeCallCount() int {
|
||||||
return len(fake.initializeArgsForCall)
|
return len(fake.initializeArgsForCall)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (fake *FakeAddressManager) InitializeArgsForCall(i int) string {
|
|
||||||
fake.initializeMutex.RLock()
|
|
||||||
defer fake.initializeMutex.RUnlock()
|
|
||||||
return fake.initializeArgsForCall[i].host
|
|
||||||
}
|
|
||||||
|
|
||||||
func (fake *FakeAddressManager) InitializeReturns(result1 int, result2 string, result3 error) {
|
func (fake *FakeAddressManager) InitializeReturns(result1 int, result2 string, result3 error) {
|
||||||
fake.InitializeStub = nil
|
fake.InitializeStub = nil
|
||||||
fake.initializeReturns = struct {
|
fake.initializeReturns = struct {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue