2020-08-19 15:41:06 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
autoconf "github.com/hashicorp/consul/agent/auto-config"
|
|
|
|
"github.com/hashicorp/consul/agent/cache"
|
|
|
|
"github.com/hashicorp/consul/agent/config"
|
|
|
|
"github.com/hashicorp/consul/agent/pool"
|
2020-08-27 15:23:52 +00:00
|
|
|
"github.com/hashicorp/consul/agent/router"
|
2020-08-19 15:41:06 +00:00
|
|
|
"github.com/hashicorp/consul/agent/token"
|
|
|
|
"github.com/hashicorp/consul/ipaddr"
|
|
|
|
"github.com/hashicorp/consul/lib"
|
|
|
|
"github.com/hashicorp/consul/logging"
|
|
|
|
"github.com/hashicorp/consul/tlsutil"
|
|
|
|
"github.com/hashicorp/go-hclog"
|
2020-08-20 21:50:00 +00:00
|
|
|
"google.golang.org/grpc/grpclog"
|
2020-08-19 15:41:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// TODO: BaseDeps should be renamed in the future once more of Agent.Start
|
|
|
|
// has been moved out in front of Agent.New, and we can better see the setup
|
|
|
|
// dependencies.
|
|
|
|
type BaseDeps struct {
|
|
|
|
Logger hclog.InterceptLogger
|
|
|
|
TLSConfigurator *tlsutil.Configurator // TODO: use an interface
|
|
|
|
MetricsHandler MetricsHandler
|
|
|
|
RuntimeConfig *config.RuntimeConfig
|
|
|
|
Tokens *token.Store
|
|
|
|
Cache *cache.Cache
|
|
|
|
AutoConfig *autoconf.AutoConfig // TODO: use an interface
|
|
|
|
ConnPool *pool.ConnPool // TODO: use an interface
|
2020-08-27 15:23:52 +00:00
|
|
|
Router *router.Router
|
2020-08-19 15:41:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MetricsHandler provides an http.Handler for displaying metrics.
|
|
|
|
type MetricsHandler interface {
|
|
|
|
DisplayMetrics(resp http.ResponseWriter, req *http.Request) (interface{}, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type ConfigLoader func(source config.Source) (cfg *config.RuntimeConfig, warnings []string, err error)
|
|
|
|
|
|
|
|
func NewBaseDeps(configLoader ConfigLoader, logOut io.Writer) (BaseDeps, error) {
|
|
|
|
d := BaseDeps{}
|
|
|
|
cfg, warnings, err := configLoader(nil)
|
|
|
|
if err != nil {
|
|
|
|
return d, err
|
|
|
|
}
|
|
|
|
|
2020-08-20 21:50:00 +00:00
|
|
|
logConf := cfg.Logging
|
|
|
|
logConf.Name = logging.Agent
|
|
|
|
d.Logger, err = logging.Setup(logConf, logOut)
|
2020-08-19 15:41:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return d, err
|
|
|
|
}
|
2020-08-20 21:50:00 +00:00
|
|
|
grpclog.SetLoggerV2(logging.NewGRPCLogger(cfg.Logging.LogLevel, d.Logger))
|
2020-08-19 15:41:06 +00:00
|
|
|
|
|
|
|
for _, w := range warnings {
|
|
|
|
d.Logger.Warn(w)
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg.NodeID, err = newNodeIDFromConfig(cfg, d.Logger)
|
|
|
|
if err != nil {
|
|
|
|
return d, fmt.Errorf("failed to setup node ID: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.MetricsHandler, err = lib.InitTelemetry(cfg.Telemetry)
|
|
|
|
if err != nil {
|
|
|
|
return d, fmt.Errorf("failed to initialize telemetry: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.TLSConfigurator, err = tlsutil.NewConfigurator(cfg.ToTLSUtilConfig(), d.Logger)
|
|
|
|
if err != nil {
|
|
|
|
return d, err
|
|
|
|
}
|
|
|
|
|
|
|
|
d.RuntimeConfig = cfg
|
|
|
|
d.Tokens = new(token.Store)
|
|
|
|
// cache-types are not registered yet, but they won't be used until the components are started.
|
|
|
|
d.Cache = cache.New(cfg.Cache)
|
|
|
|
d.ConnPool = newConnPool(cfg, d.Logger, d.TLSConfigurator)
|
|
|
|
|
2020-08-27 15:23:52 +00:00
|
|
|
d.Router = router.NewRouter(d.Logger, cfg.Datacenter, fmt.Sprintf("%s.%s", cfg.NodeName, cfg.Datacenter))
|
|
|
|
|
2020-08-19 15:41:06 +00:00
|
|
|
acConf := autoconf.Config{
|
2020-08-31 20:46:37 +00:00
|
|
|
DirectRPC: d.ConnPool,
|
|
|
|
Logger: d.Logger,
|
|
|
|
Loader: configLoader,
|
|
|
|
ServerProvider: d.Router,
|
|
|
|
TLSConfigurator: d.TLSConfigurator,
|
|
|
|
Cache: d.Cache,
|
|
|
|
Tokens: d.Tokens,
|
2020-08-19 15:41:06 +00:00
|
|
|
}
|
|
|
|
d.AutoConfig, err = autoconf.New(acConf)
|
|
|
|
if err != nil {
|
|
|
|
return d, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return d, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func newConnPool(config *config.RuntimeConfig, logger hclog.Logger, tls *tlsutil.Configurator) *pool.ConnPool {
|
|
|
|
var rpcSrcAddr *net.TCPAddr
|
|
|
|
if !ipaddr.IsAny(config.RPCBindAddr) {
|
|
|
|
rpcSrcAddr = &net.TCPAddr{IP: config.RPCBindAddr.IP}
|
|
|
|
}
|
|
|
|
|
|
|
|
pool := &pool.ConnPool{
|
|
|
|
Server: config.ServerMode,
|
|
|
|
SrcAddr: rpcSrcAddr,
|
|
|
|
Logger: logger.StandardLogger(&hclog.StandardLoggerOptions{InferLevels: true}),
|
|
|
|
TLSConfigurator: tls,
|
|
|
|
Datacenter: config.Datacenter,
|
|
|
|
}
|
|
|
|
if config.ServerMode {
|
|
|
|
pool.MaxTime = 2 * time.Minute
|
|
|
|
pool.MaxStreams = 64
|
|
|
|
} else {
|
|
|
|
pool.MaxTime = 127 * time.Second
|
|
|
|
pool.MaxStreams = 32
|
|
|
|
}
|
|
|
|
return pool
|
|
|
|
}
|