2019-07-08 18:51:55 +03:00
|
|
|
package table
|
2019-06-07 01:29:24 +03:00
|
|
|
|
2019-06-26 18:03:19 +03:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/olekukonko/tablewriter"
|
|
|
|
)
|
|
|
|
|
2019-06-10 01:09:38 +03:00
|
|
|
// LookupPolicy defines query policy
|
|
|
|
type LookupPolicy int
|
2019-06-07 01:29:24 +03:00
|
|
|
|
|
|
|
const (
|
2019-06-26 18:03:19 +03:00
|
|
|
// DiscardIfNone discards query when no route is found
|
|
|
|
DiscardIfNone LookupPolicy = iota
|
2019-06-10 01:09:38 +03:00
|
|
|
// ClosestMatch returns closest match to supplied query
|
2019-06-07 01:29:24 +03:00
|
|
|
ClosestMatch
|
|
|
|
)
|
|
|
|
|
2019-06-18 20:33:05 +03:00
|
|
|
// String returns human representation of LookupPolicy
|
|
|
|
func (lp LookupPolicy) String() string {
|
|
|
|
switch lp {
|
2019-06-26 18:03:19 +03:00
|
|
|
case DiscardIfNone:
|
2019-06-18 20:33:05 +03:00
|
|
|
return "DISCARD"
|
|
|
|
case ClosestMatch:
|
|
|
|
return "CLOSEST"
|
|
|
|
default:
|
|
|
|
return "UNKNOWN"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-14 00:28:47 +03:00
|
|
|
// QueryOption sets routing table query options
|
2019-06-13 14:09:49 +03:00
|
|
|
type QueryOption func(*QueryOptions)
|
|
|
|
|
2019-06-14 00:28:47 +03:00
|
|
|
// QueryOptions are routing table query options
|
2019-06-07 01:29:24 +03:00
|
|
|
type QueryOptions struct {
|
2019-07-09 17:45:42 +03:00
|
|
|
// Service is destination service name
|
|
|
|
Service string
|
|
|
|
// Gateway is route gateway
|
|
|
|
Gateway string
|
2019-06-19 23:22:14 +03:00
|
|
|
// Network is network address
|
2019-06-13 00:30:42 +03:00
|
|
|
Network string
|
2019-06-14 00:28:47 +03:00
|
|
|
// Policy is query lookup policy
|
2019-06-10 01:09:38 +03:00
|
|
|
Policy LookupPolicy
|
2019-06-07 01:29:24 +03:00
|
|
|
}
|
|
|
|
|
2019-07-09 17:45:42 +03:00
|
|
|
// QueryService sets destination address
|
|
|
|
func QueryService(s string) QueryOption {
|
2019-06-07 01:29:24 +03:00
|
|
|
return func(o *QueryOptions) {
|
2019-07-09 17:45:42 +03:00
|
|
|
o.Service = s
|
2019-06-07 01:29:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-09 17:45:42 +03:00
|
|
|
// QueryGateway sets route gateway
|
|
|
|
func QueryGateway(g string) QueryOption {
|
2019-06-07 01:29:24 +03:00
|
|
|
return func(o *QueryOptions) {
|
2019-07-09 17:45:42 +03:00
|
|
|
o.Gateway = g
|
2019-06-07 01:29:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-09 17:45:42 +03:00
|
|
|
// QueryNetwork sets route network address
|
|
|
|
func QueryNetwork(n string) QueryOption {
|
2019-06-17 01:09:59 +03:00
|
|
|
return func(o *QueryOptions) {
|
2019-07-09 17:45:42 +03:00
|
|
|
o.Network = n
|
2019-06-19 23:22:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-14 00:28:47 +03:00
|
|
|
// QueryPolicy sets query policy
|
2019-06-19 23:22:14 +03:00
|
|
|
// NOTE: this might be renamed to filter or some such
|
2019-06-13 00:30:42 +03:00
|
|
|
func QueryPolicy(p LookupPolicy) QueryOption {
|
2019-06-10 01:19:56 +03:00
|
|
|
return func(o *QueryOptions) {
|
2019-06-13 00:30:42 +03:00
|
|
|
o.Policy = p
|
2019-06-10 01:19:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-14 00:28:47 +03:00
|
|
|
// Query is routing table query
|
2019-06-07 01:29:24 +03:00
|
|
|
type Query interface {
|
|
|
|
// Options returns query options
|
|
|
|
Options() QueryOptions
|
|
|
|
}
|
|
|
|
|
2019-06-14 00:28:47 +03:00
|
|
|
// query is a basic implementation of Query
|
2019-06-07 01:29:24 +03:00
|
|
|
type query struct {
|
|
|
|
opts QueryOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewQuery creates new query and returns it
|
|
|
|
func NewQuery(opts ...QueryOption) Query {
|
2019-06-13 00:30:42 +03:00
|
|
|
// default options
|
2019-06-19 23:22:14 +03:00
|
|
|
// NOTE: by default we use DefaultNetworkMetric
|
2019-06-13 00:30:42 +03:00
|
|
|
qopts := QueryOptions{
|
2019-07-09 17:45:42 +03:00
|
|
|
Service: "*",
|
|
|
|
Gateway: "*",
|
|
|
|
Network: "*",
|
|
|
|
Policy: DiscardIfNone,
|
2019-06-13 00:30:42 +03:00
|
|
|
}
|
2019-06-07 01:29:24 +03:00
|
|
|
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&qopts)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &query{
|
|
|
|
opts: qopts,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Options returns query options
|
|
|
|
func (q *query) Options() QueryOptions {
|
|
|
|
return q.opts
|
|
|
|
}
|
2019-06-26 18:03:19 +03:00
|
|
|
|
|
|
|
// String prints routing table query in human readable form
|
|
|
|
func (q query) String() string {
|
|
|
|
// this will help us build routing table string
|
|
|
|
sb := &strings.Builder{}
|
|
|
|
|
|
|
|
// create nice table printing structure
|
|
|
|
table := tablewriter.NewWriter(sb)
|
2019-07-09 17:45:42 +03:00
|
|
|
table.SetHeader([]string{"Service", "Gateway", "Network", "Policy"})
|
2019-06-26 18:03:19 +03:00
|
|
|
|
|
|
|
strQuery := []string{
|
2019-07-09 17:45:42 +03:00
|
|
|
q.opts.Service,
|
|
|
|
q.opts.Gateway,
|
2019-06-26 18:03:19 +03:00
|
|
|
q.opts.Network,
|
|
|
|
fmt.Sprintf("%s", q.opts.Policy),
|
|
|
|
}
|
|
|
|
table.Append(strQuery)
|
|
|
|
|
|
|
|
// render table into sb
|
|
|
|
table.Render()
|
|
|
|
|
|
|
|
return sb.String()
|
|
|
|
}
|