2019-06-11 23:59:25 +01:00
|
|
|
package router
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2019-06-19 18:01:48 +01:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/olekukonko/tablewriter"
|
2019-06-11 23:59:25 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// ErrWatcherStopped is returned when routing table watcher has been stopped
|
|
|
|
ErrWatcherStopped = errors.New("routing table watcher stopped")
|
|
|
|
)
|
|
|
|
|
2019-06-18 10:57:43 +01:00
|
|
|
// EventType defines routing table event
|
|
|
|
type EventType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
// CreateEvent is emitted when new route has been created
|
|
|
|
CreateEvent EventType = iota
|
|
|
|
// DeleteEvent is emitted when an existing route has been deleted
|
|
|
|
DeleteEvent
|
|
|
|
// UpdateEvent is emitted when a routing table has been updated
|
|
|
|
UpdateEvent
|
|
|
|
)
|
|
|
|
|
|
|
|
// String returns string representation of the event
|
|
|
|
func (et EventType) String() string {
|
|
|
|
switch et {
|
|
|
|
case CreateEvent:
|
|
|
|
return "CREATE"
|
|
|
|
case DeleteEvent:
|
|
|
|
return "DELETE"
|
|
|
|
case UpdateEvent:
|
|
|
|
return "UPDATE"
|
|
|
|
default:
|
|
|
|
return "UNKNOWN"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Event is returned by a call to Next on the watcher.
|
|
|
|
type Event struct {
|
|
|
|
// Type defines type of event
|
|
|
|
Type EventType
|
|
|
|
// Route is table rout
|
|
|
|
Route Route
|
|
|
|
}
|
|
|
|
|
2019-06-13 12:09:49 +01:00
|
|
|
// WatchOption is used to define what routes to watch in the table
|
|
|
|
type WatchOption func(*WatchOptions)
|
|
|
|
|
|
|
|
// Watcher defines routing table watcher interface
|
|
|
|
// Watcher returns updates to the routing table
|
2019-06-11 23:59:25 +01:00
|
|
|
type Watcher interface {
|
|
|
|
// Next is a blocking call that returns watch result
|
2019-06-18 10:57:43 +01:00
|
|
|
Next() (*Event, error)
|
2019-06-19 21:22:14 +01:00
|
|
|
// Chan returns event channel
|
|
|
|
Chan() (<-chan *Event, error)
|
2019-06-11 23:59:25 +01:00
|
|
|
// Stop stops watcher
|
|
|
|
Stop()
|
|
|
|
}
|
|
|
|
|
2019-06-13 23:28:47 +02:00
|
|
|
// WatchOptions are table watcher options
|
2019-06-11 23:59:25 +01:00
|
|
|
type WatchOptions struct {
|
|
|
|
// Specify destination address to watch
|
2019-06-19 21:22:14 +01:00
|
|
|
Destination string
|
2019-06-11 23:59:25 +01:00
|
|
|
// Specify network to watch
|
|
|
|
Network string
|
|
|
|
}
|
|
|
|
|
2019-06-19 21:22:14 +01:00
|
|
|
// WatchDestination sets what destination to watch
|
2019-06-11 23:59:25 +01:00
|
|
|
// Destination is usually microservice name
|
2019-06-19 21:22:14 +01:00
|
|
|
func WatchDestination(a string) WatchOption {
|
2019-06-11 23:59:25 +01:00
|
|
|
return func(o *WatchOptions) {
|
2019-06-19 21:22:14 +01:00
|
|
|
o.Destination = a
|
2019-06-11 23:59:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WatchNetwork sets what network to watch
|
|
|
|
func WatchNetwork(n string) WatchOption {
|
|
|
|
return func(o *WatchOptions) {
|
|
|
|
o.Network = n
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type tableWatcher struct {
|
|
|
|
opts WatchOptions
|
2019-06-18 10:57:43 +01:00
|
|
|
resChan chan *Event
|
2019-06-11 23:59:25 +01:00
|
|
|
done chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next returns the next noticed action taken on table
|
2019-06-17 19:51:13 +01:00
|
|
|
// TODO: this needs to be thought through properly
|
2019-06-19 18:01:48 +01:00
|
|
|
// we are aiming to provide the same options Query provides
|
2019-06-18 10:57:43 +01:00
|
|
|
func (w *tableWatcher) Next() (*Event, error) {
|
2019-06-11 23:59:25 +01:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case res := <-w.resChan:
|
2019-06-19 21:22:14 +01:00
|
|
|
switch w.opts.Destination {
|
2019-06-12 22:30:42 +01:00
|
|
|
case "*", "":
|
2019-06-19 21:22:14 +01:00
|
|
|
if w.opts.Network == "*" || w.opts.Network == res.Route.Network {
|
2019-06-11 23:59:25 +01:00
|
|
|
return res, nil
|
|
|
|
}
|
2019-06-19 21:22:14 +01:00
|
|
|
case res.Route.Destination:
|
|
|
|
if w.opts.Network == "*" || w.opts.Network == res.Route.Network {
|
2019-06-11 23:59:25 +01:00
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case <-w.done:
|
|
|
|
return nil, ErrWatcherStopped
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-19 21:22:14 +01:00
|
|
|
// Chan returns watcher events channel
|
|
|
|
func (w *tableWatcher) Chan() (<-chan *Event, error) {
|
|
|
|
return w.resChan, nil
|
|
|
|
}
|
|
|
|
|
2019-06-11 23:59:25 +01:00
|
|
|
// Stop stops routing table watcher
|
|
|
|
func (w *tableWatcher) Stop() {
|
|
|
|
select {
|
|
|
|
case <-w.done:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
close(w.done)
|
|
|
|
}
|
|
|
|
}
|
2019-06-19 18:01:48 +01:00
|
|
|
|
|
|
|
// String prints debug information
|
|
|
|
func (w *tableWatcher) String() string {
|
|
|
|
sb := &strings.Builder{}
|
|
|
|
|
|
|
|
table := tablewriter.NewWriter(sb)
|
2019-06-19 21:22:14 +01:00
|
|
|
table.SetHeader([]string{"Destination", "Network"})
|
2019-06-19 18:01:48 +01:00
|
|
|
|
|
|
|
data := []string{
|
2019-06-19 21:22:14 +01:00
|
|
|
w.opts.Destination,
|
2019-06-19 18:01:48 +01:00
|
|
|
w.opts.Network,
|
|
|
|
}
|
|
|
|
table.Append(data)
|
|
|
|
|
|
|
|
// render table into sb
|
|
|
|
table.Render()
|
|
|
|
|
|
|
|
return sb.String()
|
|
|
|
}
|