Skip to content

Commit

Permalink
hubble: Remove serveroption.WithListeners
Browse files Browse the repository at this point in the history
Now that Hubble has 1 unix domain socket and at most 1 TCP socket to
serve, we can simply use `With{UnixSocket,TCP}Listener` options.

Signed-off-by: Michi Mutsuzaki <michi@isovalent.com>
  • Loading branch information
michi-covalent committed May 8, 2020
1 parent 26a98b7 commit 5b6be3e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 46 deletions.
7 changes: 2 additions & 5 deletions daemon/cmd/hubble.go
Expand Up @@ -16,7 +16,6 @@ package cmd

import (
"context"
"strings"
"time"

"github.com/cilium/cilium/api/v1/models"
Expand Down Expand Up @@ -127,11 +126,9 @@ func (d *Daemon) launchHubble() {
address := option.Config.HubbleListenAddress
if address != "" {
// TODO: remove warning once mutual TLS has been implemented
if !strings.HasPrefix(address, "unix://") {
logger.WithField("address", address).Warn("Hubble server will be exposing its API insecurely on this address")
}
logger.WithField("address", address).Warn("Hubble server will be exposing its API insecurely on this address")
srv, err := server.NewServer(logger,
serveroption.WithListeners([]string{address}),
serveroption.WithTCPListener(address),
serveroption.WithHealthService(),
serveroption.WithObserverService(d.hubbleObserver),
)
Expand Down
20 changes: 11 additions & 9 deletions pkg/hubble/server/server.go
Expand Up @@ -38,9 +38,7 @@ type Server struct {

// NewServer creates a new hubble gRPC server.
func NewServer(log *logrus.Entry, options ...serveroption.Option) (*Server, error) {
opts := serveroption.Options{
Listeners: make(map[string]net.Listener),
}
opts := serveroption.Options{}
for _, opt := range options {
if err := opt(&opts); err != nil {
return nil, fmt.Errorf("failed to apply option: %v", err)
Expand Down Expand Up @@ -68,12 +66,16 @@ func (s *Server) initGRPCServer() {
// listeners. Stop should be called to stop the server.
func (s *Server) Serve() error {
s.initGRPCServer()
for name, listener := range s.opts.Listeners {
go func(name string, listener net.Listener) {
if err := s.srv.Serve(listener); err != nil {
s.log.WithError(err).Error("failed to close grpc server")
}
}(name, listener)
for _, listener := range []net.Listener{s.opts.UnixSocketListener, s.opts.TCPListener} {
if listener != nil {
go func(listener net.Listener) {
if err := s.srv.Serve(listener); err != nil {
s.log.WithError(err).
WithField("address", listener.Addr().String()).
Error("failed to start grpc server")
}
}(listener)
}
}
return nil
}
Expand Down
41 changes: 9 additions & 32 deletions pkg/hubble/server/serveroption/option.go
Expand Up @@ -33,51 +33,28 @@ import (

// Options stores all the configuration values for the hubble server.
type Options struct {
Listeners map[string]net.Listener
HealthService healthpb.HealthServer
ObserverService observerpb.ObserverServer
PeerService peerpb.PeerServer
TCPListener net.Listener
UnixSocketListener net.Listener
HealthService healthpb.HealthServer
ObserverService observerpb.ObserverServer
PeerService peerpb.PeerServer
}

// Option customizes then configuration of the hubble server.
type Option func(o *Options) error

// WithListeners configures listeners. Addresses that are prefixed with
// 'unix://' are assumed to be UNIX domain sockets, in which case appropriate
// permissions are tentatively set and the group owner is set to socketGroup.
// Otherwise, the address is assumed to be TCP.
func WithListeners(addresses []string) Option {
return func(o *Options) error {
var opt Option
for _, address := range addresses {
if strings.HasPrefix(address, "unix://") {
opt = WithUnixSocketListener(address)
} else {
opt = WithTCPListener(address)
}
if err := opt(o); err != nil {
for _, l := range o.Listeners {
l.Close()
}
return err
}
}
return nil
}
}

// WithTCPListener configures a TCP listener with the address.
func WithTCPListener(address string) Option {
return func(o *Options) error {
socket, err := net.Listen("tcp", address)
if err != nil {
return err
}
if _, exist := o.Listeners[address]; exist {
if o.TCPListener != nil {
socket.Close()
return fmt.Errorf("listener already configured: %s", address)
}
o.Listeners[address] = socket
o.TCPListener = socket
return nil
}
}
Expand All @@ -98,12 +75,12 @@ func WithUnixSocketListener(path string) Option {
return err
}
}
if _, exist := o.Listeners[path]; exist {
if o.UnixSocketListener != nil {
socket.Close()
unix.Unlink(socketPath)
return fmt.Errorf("listener already configured: %s", path)
}
o.Listeners[path] = socket
o.UnixSocketListener = socket
return nil
}
}
Expand Down

0 comments on commit 5b6be3e

Please sign in to comment.