Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow proxy to accept a listen address. fixes #1220 #1222

Merged
merged 2 commits into from
Sep 9, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion cmd/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ var (
configFile = flag.String("configfile", "/tmp/proxy_config", "Configuration file for the proxy")
master = flag.String("master", "", "The address of the Kubernetes API server (optional)")
etcdServerList util.StringList
bindAddress = flag.String("bindaddress", "0.0.0.0", "The address for the proxy server to serve on (set to 0.0.0.0 or \"\" for all interfaces)")
)

func init() {
Expand Down Expand Up @@ -84,7 +85,7 @@ func main() {
glog.Infof("Using configuration file %s", *configFile)

loadBalancer := proxy.NewLoadBalancerRR()
proxier := proxy.NewProxier(loadBalancer)
proxier := proxy.NewProxier(loadBalancer, *bindAddress)
// Wire proxier to handle changes to services
serviceConfig.RegisterHandler(proxier)
// And wire loadBalancer to handle changes to endpoints to services
Expand Down
11 changes: 7 additions & 4 deletions pkg/proxy/proxier.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,16 @@ type Proxier struct {
loadBalancer LoadBalancer
mu sync.Mutex // protects serviceMap
serviceMap map[string]*serviceInfo
address string
}

// NewProxier returns a new Proxier given a LoadBalancer.
func NewProxier(loadBalancer LoadBalancer) *Proxier {
// NewProxier returns a new Proxier given a LoadBalancer and an
// address on which to listen
func NewProxier(loadBalancer LoadBalancer, address string) *Proxier {
return &Proxier{
loadBalancer: loadBalancer,
serviceMap: make(map[string]*serviceInfo),
address: address,
}
}

Expand Down Expand Up @@ -152,7 +155,7 @@ var unusedPortLock sync.Mutex
func (proxier *Proxier) addServiceOnUnusedPort(service string) (string, error) {
unusedPortLock.Lock()
defer unusedPortLock.Unlock()
l, err := net.Listen("tcp", ":0")
l, err := net.Listen("tcp", net.JoinHostPort(proxier.address, "0"))
if err != nil {
return "", err
}
Expand Down Expand Up @@ -194,7 +197,7 @@ func (proxier *Proxier) OnUpdate(services []api.Service) {
proxier.StopProxy(service.ID)
}
glog.Infof("Adding a new service %s on port %d", service.ID, service.Port)
listener, err := net.Listen("tcp", fmt.Sprintf(":%d", service.Port))
listener, err := net.Listen("tcp", net.JoinHostPort(proxier.address, strconv.Itoa(service.Port)))
if err != nil {
glog.Infof("Failed to start listening for %s on %d", service.ID, service.Port)
continue
Expand Down
12 changes: 6 additions & 6 deletions pkg/proxy/proxier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func TestProxy(t *testing.T) {
lb.OnUpdate([]api.Endpoints{
{JSONBase: api.JSONBase{ID: "echo"}, Endpoints: []string{net.JoinHostPort("127.0.0.1", port)}}})

p := NewProxier(lb)
p := NewProxier(lb, "127.0.0.1")

proxyPort, err := p.addServiceOnUnusedPort("echo")
if err != nil {
Expand All @@ -92,7 +92,7 @@ func TestProxyStop(t *testing.T) {
lb := NewLoadBalancerRR()
lb.OnUpdate([]api.Endpoints{{JSONBase: api.JSONBase{ID: "echo"}, Endpoints: []string{net.JoinHostPort("127.0.0.1", port)}}})

p := NewProxier(lb)
p := NewProxier(lb, "127.0.0.1")

proxyPort, err := p.addServiceOnUnusedPort("echo")
if err != nil {
Expand All @@ -115,7 +115,7 @@ func TestProxyUpdateDelete(t *testing.T) {
lb := NewLoadBalancerRR()
lb.OnUpdate([]api.Endpoints{{JSONBase: api.JSONBase{ID: "echo"}, Endpoints: []string{net.JoinHostPort("127.0.0.1", port)}}})

p := NewProxier(lb)
p := NewProxier(lb, "127.0.0.1")

proxyPort, err := p.addServiceOnUnusedPort("echo")
if err != nil {
Expand All @@ -137,7 +137,7 @@ func TestProxyUpdateDeleteUpdate(t *testing.T) {
lb := NewLoadBalancerRR()
lb.OnUpdate([]api.Endpoints{{JSONBase: api.JSONBase{ID: "echo"}, Endpoints: []string{net.JoinHostPort("127.0.0.1", port)}}})

p := NewProxier(lb)
p := NewProxier(lb, "127.0.0.1")

proxyPort, err := p.addServiceOnUnusedPort("echo")
if err != nil {
Expand All @@ -164,7 +164,7 @@ func TestProxyUpdatePort(t *testing.T) {
lb := NewLoadBalancerRR()
lb.OnUpdate([]api.Endpoints{{JSONBase: api.JSONBase{ID: "echo"}, Endpoints: []string{net.JoinHostPort("127.0.0.1", port)}}})

p := NewProxier(lb)
p := NewProxier(lb, "127.0.0.1")

proxyPort, err := p.addServiceOnUnusedPort("echo")
if err != nil {
Expand Down Expand Up @@ -197,7 +197,7 @@ func TestProxyUpdatePortLetsGoOfOldPort(t *testing.T) {
lb := NewLoadBalancerRR()
lb.OnUpdate([]api.Endpoints{{JSONBase: api.JSONBase{ID: "echo"}, Endpoints: []string{net.JoinHostPort("127.0.0.1", port)}}})

p := NewProxier(lb)
p := NewProxier(lb, "127.0.0.1")

proxyPort, err := p.addServiceOnUnusedPort("echo")
if err != nil {
Expand Down