Add BindNetwork to GenericApiServer.Config

This commit is contained in:
Dr. Stefan Schimanski 2016-10-21 16:03:50 +02:00
parent 07f78836ea
commit 0b7b613bba
2 changed files with 11 additions and 4 deletions

View File

@ -177,6 +177,9 @@ type Config struct {
type ServingInfo struct {
// BindAddress is the ip:port to serve on
BindAddress string
// BindNetwork is the type of network to bind to - defaults to "tcp", accepts "tcp",
// "tcp4", and "tcp6".
BindNetwork string
}
type SecureServingInfo struct {

View File

@ -89,7 +89,7 @@ func (s *GenericAPIServer) serveSecurely(stopCh <-chan struct{}) error {
}
glog.Infof("Serving securely on %s", s.SecureServingInfo.BindAddress)
s.effectiveSecurePort, err = runServer(secureServer, stopCh)
s.effectiveSecurePort, err = runServer(secureServer, s.SecureServingInfo.BindNetwork, stopCh)
return err
}
@ -104,19 +104,23 @@ func (s *GenericAPIServer) serveInsecurely(stopCh <-chan struct{}) error {
}
glog.Infof("Serving insecurely on %s", s.InsecureServingInfo.BindAddress)
var err error
s.effectiveInsecurePort, err = runServer(insecureServer, stopCh)
s.effectiveInsecurePort, err = runServer(insecureServer, s.InsecureServingInfo.BindNetwork, stopCh)
return err
}
// runServer listens on the given port, then spawns a go-routine continuously serving
// until the stopCh is closed. The port is returned. This function does not block.
func runServer(server *http.Server, stopCh <-chan struct{}) (int, error) {
func runServer(server *http.Server, network string, stopCh <-chan struct{}) (int, error) {
if len(server.Addr) == 0 {
return 0, errors.New("address cannot be empty")
}
if len(network) == 0 {
network = "tcp"
}
// first listen is synchronous (fail early!)
ln, err := net.Listen("tcp", server.Addr)
ln, err := net.Listen(network, server.Addr)
if err != nil {
return 0, fmt.Errorf("failed to listen on %v: %v", server.Addr, err)
}