2014-06-06 23:40:48 +00:00
|
|
|
/*
|
|
|
|
Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package proxy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net"
|
2014-07-30 13:52:03 +00:00
|
|
|
"strconv"
|
2014-09-10 20:44:20 +00:00
|
|
|
"strings"
|
2014-07-30 13:52:03 +00:00
|
|
|
"sync"
|
2014-06-06 23:40:48 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
2014-07-30 13:52:03 +00:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
2014-06-25 03:51:57 +00:00
|
|
|
"github.com/golang/glog"
|
2014-06-06 23:40:48 +00:00
|
|
|
)
|
|
|
|
|
2014-07-30 13:52:03 +00:00
|
|
|
type serviceInfo struct {
|
2014-08-03 19:23:15 +00:00
|
|
|
name string
|
2014-07-30 13:52:03 +00:00
|
|
|
port int
|
2014-09-10 20:44:20 +00:00
|
|
|
protocol string
|
|
|
|
socket proxySocket
|
2014-09-11 16:50:20 +00:00
|
|
|
timeout time.Duration
|
2014-08-03 19:23:15 +00:00
|
|
|
mu sync.Mutex // protects active
|
|
|
|
active bool
|
2014-07-30 13:52:03 +00:00
|
|
|
}
|
|
|
|
|
2014-09-11 23:08:25 +00:00
|
|
|
func (si *serviceInfo) isActive() bool {
|
|
|
|
si.mu.Lock()
|
|
|
|
defer si.mu.Unlock()
|
|
|
|
return si.active
|
|
|
|
}
|
|
|
|
|
|
|
|
func (si *serviceInfo) setActive(val bool) bool {
|
|
|
|
si.mu.Lock()
|
|
|
|
defer si.mu.Unlock()
|
|
|
|
tmp := si.active
|
|
|
|
si.active = val
|
|
|
|
return tmp
|
|
|
|
}
|
|
|
|
|
2014-09-11 16:00:06 +00:00
|
|
|
// How long we wait for a connection to a backend.
|
|
|
|
const endpointDialTimeout = 5 * time.Second
|
|
|
|
|
2014-09-10 20:44:20 +00:00
|
|
|
// Abstraction over TCP/UDP sockets which are proxied.
|
|
|
|
type proxySocket interface {
|
|
|
|
// Addr gets the net.Addr for a proxySocket.
|
|
|
|
Addr() net.Addr
|
2014-09-11 16:00:06 +00:00
|
|
|
// Close stops the proxySocket from accepting incoming connections. Each implementation should comment
|
|
|
|
// on the impact of calling Close while sessions are active.
|
2014-09-10 20:44:20 +00:00
|
|
|
Close() error
|
|
|
|
// ProxyLoop proxies incoming connections for the specified service to the service endpoints.
|
|
|
|
ProxyLoop(service string, proxier *Proxier)
|
|
|
|
}
|
|
|
|
|
2014-09-11 16:00:06 +00:00
|
|
|
// tcpProxySocket implements proxySocket. Close() is implemented by net.Listener. When Close() is called,
|
|
|
|
// no new connections are allowed but existing connections are left untouched.
|
2014-09-10 20:44:20 +00:00
|
|
|
type tcpProxySocket struct {
|
|
|
|
net.Listener
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tcp *tcpProxySocket) ProxyLoop(service string, proxier *Proxier) {
|
|
|
|
info, found := proxier.getServiceInfo(service)
|
|
|
|
if !found {
|
|
|
|
glog.Errorf("Failed to find service: %s", service)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for {
|
2014-09-11 23:08:25 +00:00
|
|
|
if !info.isActive() {
|
2014-09-10 20:44:20 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// Block until a connection is made.
|
|
|
|
inConn, err := tcp.Accept()
|
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("Accept failed: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
2014-09-11 16:00:06 +00:00
|
|
|
glog.Infof("Accepted TCP connection from %v to %v", inConn.RemoteAddr(), inConn.LocalAddr())
|
2014-09-10 20:44:20 +00:00
|
|
|
endpoint, err := proxier.loadBalancer.NextEndpoint(service, inConn.RemoteAddr())
|
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("Couldn't find an endpoint for %s %v", service, err)
|
|
|
|
inConn.Close()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
glog.Infof("Mapped service %s to endpoint %s", service, endpoint)
|
|
|
|
// TODO: This could spin up a new goroutine to make the outbound connection,
|
|
|
|
// and keep accepting inbound traffic.
|
2014-09-11 16:00:06 +00:00
|
|
|
outConn, err := net.DialTimeout("tcp", endpoint, endpointDialTimeout)
|
2014-09-10 20:44:20 +00:00
|
|
|
if err != nil {
|
|
|
|
// TODO: Try another endpoint?
|
|
|
|
glog.Errorf("Dial failed: %v", err)
|
|
|
|
inConn.Close()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Spin up an async copy loop.
|
|
|
|
proxyTCP(inConn.(*net.TCPConn), outConn.(*net.TCPConn))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// proxyTCP proxies data bi-directionally between in and out.
|
|
|
|
func proxyTCP(in, out *net.TCPConn) {
|
|
|
|
glog.Infof("Creating proxy between %v <-> %v <-> %v <-> %v",
|
|
|
|
in.RemoteAddr(), in.LocalAddr(), out.LocalAddr(), out.RemoteAddr())
|
|
|
|
go copyBytes(in, out)
|
|
|
|
go copyBytes(out, in)
|
|
|
|
}
|
|
|
|
|
2014-09-11 16:00:06 +00:00
|
|
|
// udpProxySocket implements proxySocket. Close() is implemented by net.UDPConn. When Close() is called,
|
|
|
|
// no new connections are allowed and existing connections are broken.
|
|
|
|
// TODO: We could lame-duck this ourselves, if it becomes important.
|
|
|
|
type udpProxySocket struct {
|
|
|
|
*net.UDPConn
|
|
|
|
}
|
|
|
|
|
|
|
|
func (udp *udpProxySocket) Addr() net.Addr {
|
|
|
|
return udp.LocalAddr()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Holds all the known UDP clients that have not timed out.
|
|
|
|
type clientCache struct {
|
|
|
|
mu sync.Mutex
|
|
|
|
clients map[string]net.Conn // addr string -> connection
|
|
|
|
}
|
|
|
|
|
|
|
|
func newClientCache() *clientCache {
|
|
|
|
return &clientCache{clients: map[string]net.Conn{}}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (udp *udpProxySocket) ProxyLoop(service string, proxier *Proxier) {
|
|
|
|
info, found := proxier.getServiceInfo(service)
|
|
|
|
if !found {
|
|
|
|
glog.Errorf("Failed to find service: %s", service)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
activeClients := newClientCache()
|
|
|
|
var buffer [4096]byte // 4KiB should be enough for most whole-packets
|
|
|
|
for {
|
2014-09-11 23:08:25 +00:00
|
|
|
if !info.isActive() {
|
2014-09-11 16:00:06 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// Block until data arrives.
|
|
|
|
// TODO: Accumulate a histogram of n or something, to fine tune the buffer size.
|
|
|
|
n, cliAddr, err := udp.ReadFrom(buffer[0:])
|
|
|
|
if err != nil {
|
|
|
|
if e, ok := err.(net.Error); ok {
|
|
|
|
if e.Temporary() {
|
|
|
|
glog.Infof("ReadFrom had a temporary failure: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
glog.Errorf("ReadFrom failed, exiting ProxyLoop: %v", err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
// If this is a client we know already, reuse the connection and goroutine.
|
2014-09-11 23:08:25 +00:00
|
|
|
svrConn, err := udp.getBackendConn(activeClients, cliAddr, proxier, service, info.timeout)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
2014-09-11 16:00:06 +00:00
|
|
|
}
|
|
|
|
// TODO: It would be nice to let the goroutine handle this write, but we don't
|
|
|
|
// really want to copy the buffer. We could do a pool of buffers or something.
|
|
|
|
_, err = svrConn.Write(buffer[0:n])
|
|
|
|
if err != nil {
|
|
|
|
if !logTimeout(err) {
|
|
|
|
glog.Errorf("Write failed: %v", err)
|
|
|
|
// TODO: Maybe tear down the goroutine for this client/server pair?
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2014-09-11 16:50:20 +00:00
|
|
|
svrConn.SetDeadline(time.Now().Add(info.timeout))
|
2014-09-11 16:00:06 +00:00
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("SetDeadline failed: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-11 23:08:25 +00:00
|
|
|
func (udp *udpProxySocket) getBackendConn(activeClients *clientCache, cliAddr net.Addr, proxier *Proxier, service string, timeout time.Duration) (net.Conn, error) {
|
|
|
|
activeClients.mu.Lock()
|
|
|
|
defer activeClients.mu.Unlock()
|
|
|
|
|
|
|
|
svrConn, found := activeClients.clients[cliAddr.String()]
|
|
|
|
if !found {
|
|
|
|
// TODO: This could spin up a new goroutine to make the outbound connection,
|
|
|
|
// and keep accepting inbound traffic.
|
|
|
|
glog.Infof("New UDP connection from %s", cliAddr)
|
|
|
|
endpoint, err := proxier.loadBalancer.NextEndpoint(service, cliAddr)
|
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("Couldn't find an endpoint for %s %v", service, err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
glog.Infof("Mapped service %s to endpoint %s", service, endpoint)
|
|
|
|
svrConn, err = net.DialTimeout("udp", endpoint, endpointDialTimeout)
|
|
|
|
if err != nil {
|
|
|
|
// TODO: Try another endpoint?
|
|
|
|
glog.Errorf("Dial failed: %v", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
activeClients.clients[cliAddr.String()] = svrConn
|
2014-09-11 23:21:00 +00:00
|
|
|
go func(cliAddr net.Addr, svrConn net.Conn, activeClients *clientCache, timeout time.Duration) {
|
|
|
|
defer util.HandleCrash()
|
|
|
|
udp.proxyClient(cliAddr, svrConn, activeClients, timeout)
|
|
|
|
}(cliAddr, svrConn, activeClients, timeout)
|
2014-09-11 23:08:25 +00:00
|
|
|
}
|
|
|
|
return svrConn, nil
|
|
|
|
}
|
|
|
|
|
2014-09-11 16:00:06 +00:00
|
|
|
// This function is expected to be called as a goroutine.
|
2014-09-11 16:50:20 +00:00
|
|
|
func (udp *udpProxySocket) proxyClient(cliAddr net.Addr, svrConn net.Conn, activeClients *clientCache, timeout time.Duration) {
|
2014-09-11 16:00:06 +00:00
|
|
|
defer svrConn.Close()
|
|
|
|
var buffer [4096]byte
|
|
|
|
for {
|
|
|
|
n, err := svrConn.Read(buffer[0:])
|
|
|
|
if err != nil {
|
|
|
|
if !logTimeout(err) {
|
|
|
|
glog.Errorf("Read failed: %v", err)
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
2014-09-11 16:50:20 +00:00
|
|
|
svrConn.SetDeadline(time.Now().Add(timeout))
|
2014-09-11 16:00:06 +00:00
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("SetDeadline failed: %v", err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
n, err = udp.WriteTo(buffer[0:n], cliAddr)
|
|
|
|
if err != nil {
|
|
|
|
if !logTimeout(err) {
|
|
|
|
glog.Errorf("WriteTo failed: %v", err)
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
activeClients.mu.Lock()
|
|
|
|
delete(activeClients.clients, cliAddr.String())
|
|
|
|
activeClients.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func logTimeout(err error) bool {
|
|
|
|
if e, ok := err.(net.Error); ok {
|
|
|
|
if e.Timeout() {
|
|
|
|
glog.Infof("connection to endpoint closed due to inactivity")
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func newProxySocket(protocol string, host string, port int) (proxySocket, error) {
|
2014-09-10 20:44:20 +00:00
|
|
|
switch strings.ToUpper(protocol) {
|
|
|
|
case "TCP":
|
2014-09-11 16:00:06 +00:00
|
|
|
listener, err := net.Listen("tcp", net.JoinHostPort(host, strconv.Itoa(port)))
|
2014-09-10 20:44:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &tcpProxySocket{listener}, nil
|
2014-09-11 16:00:06 +00:00
|
|
|
case "UDP":
|
|
|
|
addr, err := net.ResolveUDPAddr("udp", net.JoinHostPort(host, strconv.Itoa(port)))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
conn, err := net.ListenUDP("udp", addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &udpProxySocket{conn}, nil
|
2014-09-10 20:44:20 +00:00
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("Unknown protocol %q", protocol)
|
|
|
|
}
|
|
|
|
|
2014-08-03 19:23:15 +00:00
|
|
|
// Proxier is a simple proxy for TCP connections between a localhost:lport
|
|
|
|
// and services that provide the actual implementations.
|
2014-06-06 23:40:48 +00:00
|
|
|
type Proxier struct {
|
|
|
|
loadBalancer LoadBalancer
|
2014-08-03 19:23:15 +00:00
|
|
|
mu sync.Mutex // protects serviceMap
|
2014-07-30 13:52:03 +00:00
|
|
|
serviceMap map[string]*serviceInfo
|
2014-09-08 06:27:43 +00:00
|
|
|
address string
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
|
2014-09-08 06:27:43 +00:00
|
|
|
// NewProxier returns a new Proxier given a LoadBalancer and an
|
|
|
|
// address on which to listen
|
|
|
|
func NewProxier(loadBalancer LoadBalancer, address string) *Proxier {
|
2014-08-03 19:23:15 +00:00
|
|
|
return &Proxier{
|
|
|
|
loadBalancer: loadBalancer,
|
|
|
|
serviceMap: make(map[string]*serviceInfo),
|
2014-09-08 06:27:43 +00:00
|
|
|
address: address,
|
2014-08-03 19:23:15 +00:00
|
|
|
}
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
|
2014-07-11 11:48:18 +00:00
|
|
|
func copyBytes(in, out *net.TCPConn) {
|
2014-06-25 03:51:57 +00:00
|
|
|
glog.Infof("Copying from %v <-> %v <-> %v <-> %v",
|
2014-06-06 23:40:48 +00:00
|
|
|
in.RemoteAddr(), in.LocalAddr(), out.LocalAddr(), out.RemoteAddr())
|
2014-08-03 19:23:15 +00:00
|
|
|
if _, err := io.Copy(in, out); err != nil {
|
2014-06-25 03:51:57 +00:00
|
|
|
glog.Errorf("I/O error: %v", err)
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
in.CloseRead()
|
|
|
|
out.CloseWrite()
|
|
|
|
}
|
|
|
|
|
2014-08-03 19:23:15 +00:00
|
|
|
// StopProxy stops the proxy for the named service.
|
2014-07-30 13:52:03 +00:00
|
|
|
func (proxier *Proxier) StopProxy(service string) error {
|
|
|
|
// TODO: delete from map here?
|
|
|
|
info, found := proxier.getServiceInfo(service)
|
|
|
|
if !found {
|
|
|
|
return fmt.Errorf("unknown service: %s", service)
|
|
|
|
}
|
|
|
|
return proxier.stopProxyInternal(info)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (proxier *Proxier) stopProxyInternal(info *serviceInfo) error {
|
2014-09-11 23:08:25 +00:00
|
|
|
if !info.setActive(false) {
|
2014-08-03 19:23:15 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
glog.Infof("Removing service: %s", info.name)
|
2014-09-10 20:44:20 +00:00
|
|
|
return info.socket.Close()
|
2014-07-30 13:52:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (proxier *Proxier) getServiceInfo(service string) (*serviceInfo, bool) {
|
2014-08-03 19:23:15 +00:00
|
|
|
proxier.mu.Lock()
|
|
|
|
defer proxier.mu.Unlock()
|
2014-07-30 13:52:03 +00:00
|
|
|
info, ok := proxier.serviceMap[service]
|
|
|
|
return info, ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func (proxier *Proxier) setServiceInfo(service string, info *serviceInfo) {
|
2014-08-03 19:23:15 +00:00
|
|
|
proxier.mu.Lock()
|
|
|
|
defer proxier.mu.Unlock()
|
|
|
|
info.name = service
|
2014-07-30 13:52:03 +00:00
|
|
|
proxier.serviceMap[service] = info
|
|
|
|
}
|
|
|
|
|
2014-08-03 19:23:15 +00:00
|
|
|
// used to globally lock around unused ports. Only used in testing.
|
2014-07-30 13:56:42 +00:00
|
|
|
var unusedPortLock sync.Mutex
|
|
|
|
|
2014-08-26 04:38:16 +00:00
|
|
|
// addServiceOnUnusedPort starts listening for a new service, returning the
|
2014-09-11 16:50:20 +00:00
|
|
|
// port it's using. For testing on a system with unknown ports used. The timeout only applies to UDP
|
|
|
|
// connections, for now.
|
|
|
|
func (proxier *Proxier) addServiceOnUnusedPort(service, protocol string, timeout time.Duration) (string, error) {
|
2014-07-30 13:56:42 +00:00
|
|
|
unusedPortLock.Lock()
|
|
|
|
defer unusedPortLock.Unlock()
|
2014-09-10 20:44:20 +00:00
|
|
|
sock, err := newProxySocket(protocol, proxier.address, 0)
|
2014-06-13 00:18:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2014-09-10 20:44:20 +00:00
|
|
|
_, port, err := net.SplitHostPort(sock.Addr().String())
|
2014-07-30 13:52:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
portNum, err := strconv.Atoi(port)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
proxier.setServiceInfo(service, &serviceInfo{
|
|
|
|
port: portNum,
|
2014-09-10 20:44:20 +00:00
|
|
|
protocol: protocol,
|
2014-07-30 13:52:03 +00:00
|
|
|
active: true,
|
2014-09-10 20:44:20 +00:00
|
|
|
socket: sock,
|
2014-09-11 16:50:20 +00:00
|
|
|
timeout: timeout,
|
2014-07-30 13:52:03 +00:00
|
|
|
})
|
2014-09-10 20:44:20 +00:00
|
|
|
proxier.startAccepting(service, sock)
|
2014-06-13 00:18:19 +00:00
|
|
|
return port, nil
|
|
|
|
}
|
|
|
|
|
2014-09-10 20:44:20 +00:00
|
|
|
func (proxier *Proxier) startAccepting(service string, sock proxySocket) {
|
2014-09-11 16:00:06 +00:00
|
|
|
glog.Infof("Listening for %s on %s:%s", service, sock.Addr().Network(), sock.Addr().String())
|
2014-09-11 23:21:00 +00:00
|
|
|
go func(service string, proxier *Proxier) {
|
|
|
|
defer util.HandleCrash()
|
|
|
|
sock.ProxyLoop(service, proxier)
|
|
|
|
}(service, proxier)
|
2014-06-13 00:18:19 +00:00
|
|
|
}
|
|
|
|
|
2014-09-11 16:50:20 +00:00
|
|
|
// How long we leave idle UDP connections open.
|
|
|
|
const udpIdleTimeout = 1 * time.Minute
|
|
|
|
|
2014-08-03 19:23:15 +00:00
|
|
|
// OnUpdate manages the active set of service proxies.
|
|
|
|
// Active service proxies are reinitialized if found in the update set or
|
|
|
|
// shutdown if missing from the update set.
|
2014-08-05 19:34:54 +00:00
|
|
|
func (proxier *Proxier) OnUpdate(services []api.Service) {
|
2014-06-25 03:51:57 +00:00
|
|
|
glog.Infof("Received update notice: %+v", services)
|
2014-08-03 19:23:15 +00:00
|
|
|
activeServices := util.StringSet{}
|
2014-06-06 23:40:48 +00:00
|
|
|
for _, service := range services {
|
2014-08-03 19:23:15 +00:00
|
|
|
activeServices.Insert(service.ID)
|
2014-07-30 13:52:03 +00:00
|
|
|
info, exists := proxier.getServiceInfo(service.ID)
|
2014-09-11 16:00:06 +00:00
|
|
|
// TODO: check health of the socket? What if ProxyLoop exited?
|
2014-09-11 23:08:25 +00:00
|
|
|
if exists && info.isActive() && info.port == service.Port {
|
2014-07-15 11:55:04 +00:00
|
|
|
continue
|
|
|
|
}
|
2014-08-19 19:24:10 +00:00
|
|
|
if exists && info.port != service.Port {
|
2014-09-11 16:00:06 +00:00
|
|
|
err := proxier.stopProxyInternal(info)
|
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("error stopping %s: %v", info.name, err)
|
|
|
|
}
|
2014-07-30 13:52:03 +00:00
|
|
|
}
|
2014-09-10 20:44:20 +00:00
|
|
|
glog.Infof("Adding a new service %s on %s port %d", service.ID, service.Protocol, service.Port)
|
|
|
|
sock, err := newProxySocket(service.Protocol, proxier.address, service.Port)
|
2014-07-15 11:55:04 +00:00
|
|
|
if err != nil {
|
2014-09-10 20:44:20 +00:00
|
|
|
glog.Errorf("Failed to get a socket for %s: %+v", service.ID, err)
|
2014-07-15 11:55:04 +00:00
|
|
|
continue
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
2014-07-30 13:52:03 +00:00
|
|
|
proxier.setServiceInfo(service.ID, &serviceInfo{
|
|
|
|
port: service.Port,
|
2014-09-10 20:44:20 +00:00
|
|
|
protocol: service.Protocol,
|
2014-07-30 13:52:03 +00:00
|
|
|
active: true,
|
2014-09-10 20:44:20 +00:00
|
|
|
socket: sock,
|
2014-09-11 16:50:20 +00:00
|
|
|
timeout: udpIdleTimeout,
|
2014-07-30 13:52:03 +00:00
|
|
|
})
|
2014-09-10 20:44:20 +00:00
|
|
|
proxier.startAccepting(service.ID, sock)
|
2014-07-30 13:52:03 +00:00
|
|
|
}
|
2014-08-03 19:23:15 +00:00
|
|
|
proxier.mu.Lock()
|
|
|
|
defer proxier.mu.Unlock()
|
2014-07-30 13:52:03 +00:00
|
|
|
for name, info := range proxier.serviceMap {
|
2014-08-03 19:23:15 +00:00
|
|
|
if !activeServices.Has(name) {
|
2014-09-11 16:00:06 +00:00
|
|
|
err := proxier.stopProxyInternal(info)
|
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("error stopping %s: %v", info.name, err)
|
|
|
|
}
|
2014-07-30 13:52:03 +00:00
|
|
|
}
|
2014-06-06 23:40:48 +00:00
|
|
|
}
|
|
|
|
}
|