From 82f5cb3f08f3d8e192a63d48165b6ebf7d5ed6d9 Mon Sep 17 00:00:00 2001 From: Matt Keeler Date: Tue, 11 May 2021 10:50:03 -0400 Subject: [PATCH] Preparation for changing where license management is done. --- agent/agent.go | 19 +++++++++++++++++++ agent/agent_oss.go | 15 +++++++++++++++ agent/agent_test.go | 11 +++++++++++ agent/consul/client.go | 19 ++++--------------- agent/consul/client_test.go | 1 + agent/consul/enterprise_client_oss.go | 6 +++++- agent/consul/enterprise_server_oss.go | 6 +----- agent/consul/enterprise_server_oss_test.go | 14 ++++++++++++++ agent/consul/options.go | 1 + agent/consul/options_oss.go | 5 +++++ agent/consul/server.go | 12 +----------- agent/routine-leak-checker/leak_test.go | 2 +- agent/setup.go | 2 +- agent/setup_oss.go | 13 +++++++++++++ sdk/testutil/context.go | 13 +++++++++++++ 15 files changed, 105 insertions(+), 34 deletions(-) create mode 100644 agent/consul/enterprise_server_oss_test.go create mode 100644 agent/consul/options_oss.go create mode 100644 agent/setup_oss.go create mode 100644 sdk/testutil/context.go diff --git a/agent/agent.go b/agent/agent.go index 0425612dc..19e6a01bd 100644 --- a/agent/agent.go +++ b/agent/agent.go @@ -457,6 +457,12 @@ func (a *Agent) Start(ctx context.Context) error { return fmt.Errorf("Failed to load TLS configurations after applying auto-config settings: %w", err) } + // we cannot use the context passed into this method as that context will be cancelled after the + // agent finishes starting up which would cause the license manager to stop + if err := a.startLicenseManager(&lib.StopChannelContext{StopCh: a.shutdownCh}); err != nil { + return err + } + // create the local state a.State = local.NewState(LocalConfig(c), a.logger, a.tokens) @@ -1339,6 +1345,8 @@ func (a *Agent) ShutdownAgent() error { // Stop the watches to avoid any notification/state change during shutdown a.stopAllWatches() + a.stopLicenseManager() + // this would be cancelled anyways (by the closing of the shutdown ch) but // this should help them to be stopped more quickly a.baseDeps.AutoConfig.Stop() @@ -3071,6 +3079,17 @@ func (a *Agent) Stats() map[string]map[string]string { "version": a.config.Version, "prerelease": a.config.VersionPrerelease, } + + for outerKey, outerValue := range a.enterpriseStats() { + if _, ok := stats[outerKey]; ok { + for innerKey, innerValue := range outerValue { + stats[outerKey][innerKey] = innerValue + } + } else { + stats[outerKey] = outerValue + } + } + return stats } diff --git a/agent/agent_oss.go b/agent/agent_oss.go index 705205fb3..4be00ed80 100644 --- a/agent/agent_oss.go +++ b/agent/agent_oss.go @@ -3,6 +3,8 @@ package agent import ( + "context" + "github.com/hashicorp/consul/agent/config" "github.com/hashicorp/consul/agent/consul" "github.com/hashicorp/consul/agent/structs" @@ -35,3 +37,16 @@ func enterpriseConsulConfig(_ *consul.Config, _ *config.RuntimeConfig) { // WriteEvent is a noop stub for the func defined agent_ent.go func (a *Agent) WriteEvent(eventType string, payload interface{}) { } + +// startLicenseManager is used to start the license management process +func (a *Agent) startLicenseManager(_ context.Context) error { + return nil +} + +// stopLicenseManager is used to stop the license management go routines +func (a *Agent) stopLicenseManager() {} + +// enterpriseStats outputs all the Agent stats specific to Consul Enterprise +func (a *Agent) enterpriseStats() map[string]map[string]string { + return nil +} diff --git a/agent/agent_test.go b/agent/agent_test.go index b006aa7ae..3726edbc8 100644 --- a/agent/agent_test.go +++ b/agent/agent_test.go @@ -318,9 +318,14 @@ func TestAgent_HTTPMaxHeaderBytes(t *testing.T) { }, Cache: cache.New(cache.Options{}), } + bd, err = initEnterpriseBaseDeps(bd, nil) + require.NoError(t, err) + a, err := New(bd) require.NoError(t, err) + a.startLicenseManager(testutil.TestContext(t)) + srvs, err := a.listenHTTP() require.NoError(t, err) @@ -5182,9 +5187,15 @@ func TestAgent_ListenHTTP_MultipleAddresses(t *testing.T) { }, Cache: cache.New(cache.Options{}), } + + bd, err = initEnterpriseBaseDeps(bd, nil) + require.NoError(t, err) + agent, err := New(bd) require.NoError(t, err) + agent.startLicenseManager(testutil.TestContext(t)) + srvs, err := agent.listenHTTP() require.NoError(t, err) defer func() { diff --git a/agent/consul/client.go b/agent/consul/client.go index f341f9abc..78db6fe72 100644 --- a/agent/consul/client.go +++ b/agent/consul/client.go @@ -10,10 +10,6 @@ import ( "github.com/armon/go-metrics" "github.com/armon/go-metrics/prometheus" - "github.com/hashicorp/go-hclog" - "github.com/hashicorp/serf/serf" - "golang.org/x/time/rate" - "github.com/hashicorp/consul/agent/pool" "github.com/hashicorp/consul/agent/router" "github.com/hashicorp/consul/agent/structs" @@ -21,6 +17,9 @@ import ( "github.com/hashicorp/consul/logging" "github.com/hashicorp/consul/tlsutil" "github.com/hashicorp/consul/types" + "github.com/hashicorp/go-hclog" + "github.com/hashicorp/serf/serf" + "golang.org/x/time/rate" ) var ClientCounters = []prometheus.CounterDefinition{ @@ -116,7 +115,7 @@ func NewClient(config *Config, deps Deps) (*Client, error) { c.rpcLimiter.Store(rate.NewLimiter(config.RPCRateLimit, config.RPCMaxBurst)) - if err := c.initEnterprise(); err != nil { + if err := c.initEnterprise(deps); err != nil { c.Shutdown() return nil, err } @@ -381,16 +380,6 @@ func (c *Client) Stats() map[string]map[string]string { stats["consul"]["acl"] = "disabled" } - for outerKey, outerValue := range c.enterpriseStats() { - if _, ok := stats[outerKey]; ok { - for innerKey, innerValue := range outerValue { - stats[outerKey][innerKey] = innerValue - } - } else { - stats[outerKey] = outerValue - } - } - return stats } diff --git a/agent/consul/client_test.go b/agent/consul/client_test.go index 8253c2512..bb398ac98 100644 --- a/agent/consul/client_test.go +++ b/agent/consul/client_test.go @@ -515,6 +515,7 @@ func newDefaultDeps(t *testing.T, c *Config) Deps { Tokens: new(token.Store), Router: r, ConnPool: connPool, + EnterpriseDeps: newDefaultDepsEnterprise(t, logger, c), } } diff --git a/agent/consul/enterprise_client_oss.go b/agent/consul/enterprise_client_oss.go index 290077ea8..bedbe8734 100644 --- a/agent/consul/enterprise_client_oss.go +++ b/agent/consul/enterprise_client_oss.go @@ -8,7 +8,7 @@ import ( type EnterpriseClient struct{} -func (c *Client) initEnterprise() error { +func (c *Client) initEnterprise(_ Deps) error { return nil } @@ -20,6 +20,10 @@ func (c *Client) handleEnterpriseUserEvents(event serf.UserEvent) bool { return false } +func (_ *Client) addEnterpriseSerfTags(_ map[string]string) { + // do nothing +} + func (c *Client) enterpriseStats() map[string]map[string]string { return nil } diff --git a/agent/consul/enterprise_server_oss.go b/agent/consul/enterprise_server_oss.go index b1833b9cd..097b94b4b 100644 --- a/agent/consul/enterprise_server_oss.go +++ b/agent/consul/enterprise_server_oss.go @@ -22,7 +22,7 @@ var ( type EnterpriseServer struct{} -func (s *Server) initEnterprise() error { +func (s *Server) initEnterprise(_ Deps) error { return nil } @@ -46,10 +46,6 @@ func (s *Server) handleEnterpriseLeave() { return } -func (s *Server) enterpriseStats() map[string]map[string]string { - return nil -} - func (s *Server) establishEnterpriseLeadership() error { return nil } diff --git a/agent/consul/enterprise_server_oss_test.go b/agent/consul/enterprise_server_oss_test.go new file mode 100644 index 000000000..a92e12684 --- /dev/null +++ b/agent/consul/enterprise_server_oss_test.go @@ -0,0 +1,14 @@ +// +build !consulent + +package consul + +import ( + "testing" + + hclog "github.com/hashicorp/go-hclog" +) + +func newDefaultDepsEnterprise(t *testing.T, _ hclog.Logger, _ *Config) EnterpriseDeps { + t.Helper() + return EnterpriseDeps{} +} diff --git a/agent/consul/options.go b/agent/consul/options.go index 9c75a7b33..f2d536cc5 100644 --- a/agent/consul/options.go +++ b/agent/consul/options.go @@ -17,6 +17,7 @@ type Deps struct { Router *router.Router ConnPool *pool.ConnPool GRPCConnPool GRPCClientConner + EnterpriseDeps } type GRPCClientConner interface { diff --git a/agent/consul/options_oss.go b/agent/consul/options_oss.go new file mode 100644 index 000000000..7e33e6cd7 --- /dev/null +++ b/agent/consul/options_oss.go @@ -0,0 +1,5 @@ +// +build !consulent + +package consul + +type EnterpriseDeps struct{} diff --git a/agent/consul/server.go b/agent/consul/server.go index 470ca9c42..e73a4208d 100644 --- a/agent/consul/server.go +++ b/agent/consul/server.go @@ -391,7 +391,7 @@ func NewServer(config *Config, flat Deps) (*Server, error) { } // Initialize enterprise specific server functionality - if err := s.initEnterprise(); err != nil { + if err := s.initEnterprise(flat); err != nil { s.Shutdown() return nil, err } @@ -1353,16 +1353,6 @@ func (s *Server) Stats() map[string]map[string]string { stats["serf_wan"] = s.serfWAN.Stats() } - for outerKey, outerValue := range s.enterpriseStats() { - if _, ok := stats[outerKey]; ok { - for innerKey, innerValue := range outerValue { - stats[outerKey][innerKey] = innerValue - } - } else { - stats[outerKey] = outerValue - } - } - return stats } diff --git a/agent/routine-leak-checker/leak_test.go b/agent/routine-leak-checker/leak_test.go index 8eac5be91..abc6ab6db 100644 --- a/agent/routine-leak-checker/leak_test.go +++ b/agent/routine-leak-checker/leak_test.go @@ -80,7 +80,7 @@ func setupPrimaryServer(t *testing.T) *agent.TestAgent { return a } -func TestTestAgentLeaks_Server(t *testing.T) { +func TestAgentLeaks_Server(t *testing.T) { if testing.Short() { t.Skip("too slow for testing.Short") } diff --git a/agent/setup.go b/agent/setup.go index 55299c49a..c35a54c6c 100644 --- a/agent/setup.go +++ b/agent/setup.go @@ -124,7 +124,7 @@ func NewBaseDeps(configLoader ConfigLoader, logOut io.Writer) (BaseDeps, error) return d, err } - return d, nil + return initEnterpriseBaseDeps(d, cfg) } // grpcLogInitOnce because the test suite will call NewBaseDeps in many tests and diff --git a/agent/setup_oss.go b/agent/setup_oss.go new file mode 100644 index 000000000..d16494133 --- /dev/null +++ b/agent/setup_oss.go @@ -0,0 +1,13 @@ +// +build !consulent + +package agent + +import ( + "github.com/hashicorp/consul/agent/config" +) + +// initEnterpriseBaseDeps is responsible for initializing the enterprise dependencies that +// will be utilized throughout the whole Consul Agent. +func initEnterpriseBaseDeps(d BaseDeps, _ *config.RuntimeConfig) (BaseDeps, error) { + return d, nil +} diff --git a/sdk/testutil/context.go b/sdk/testutil/context.go new file mode 100644 index 000000000..77fc53c06 --- /dev/null +++ b/sdk/testutil/context.go @@ -0,0 +1,13 @@ +package testutil + +import ( + "context" + "testing" +) + +func TestContext(t *testing.T) context.Context { + t.Helper() + ctx, cancel := context.WithCancel(context.Background()) + t.Cleanup(cancel) + return ctx +}