Skip to content

Commit

Permalink
feat: add reporting config with reload
Browse files Browse the repository at this point in the history
  • Loading branch information
JadhavPoonam committed Apr 11, 2023
1 parent 1cfed68 commit 8c07f54
Show file tree
Hide file tree
Showing 14 changed files with 248 additions and 0 deletions.
8 changes: 8 additions & 0 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -1360,7 +1360,10 @@ func newConsulConfig(runtimeCfg *config.RuntimeConfig, logger hclog.Logger) (*co
cfg.PeeringEnabled = runtimeCfg.PeeringEnabled
cfg.PeeringTestAllowPeerRegistrations = runtimeCfg.PeeringTestAllowPeerRegistrations

cfg.Reporting.License.Enabled = runtimeCfg.Reporting.License.Enabled

enterpriseConsulConfig(cfg, runtimeCfg)

return cfg, nil
}

Expand Down Expand Up @@ -4007,6 +4010,11 @@ func (a *Agent) reloadConfigInternal(newCfg *config.RuntimeConfig) error {
HeartbeatTimeout: newCfg.ConsulRaftHeartbeatTimeout,
ElectionTimeout: newCfg.ConsulRaftElectionTimeout,
RaftTrailingLogs: newCfg.RaftTrailingLogs,
Reporting: consul.Reporting{
License: consul.License{
Enabled: newCfg.Reporting.License.Enabled,
},
},
}
if err := a.delegate.ReloadConfig(cc); err != nil {
return err
Expand Down
46 changes: 46 additions & 0 deletions agent/agent_oss_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

//go:build !consulent
// +build !consulent

package agent

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestAgent_consulConfig_Reporting(t *testing.T) {
if testing.Short() {
t.Skip("too slow for testing.Short")
}

t.Parallel()
hcl := `
reporting {
license {
enabled = true
}
}
`
a := NewTestAgent(t, hcl)
defer a.Shutdown()
require.Equal(t, false, a.consulConfig().Reporting.License.Enabled)
}

func TestAgent_consulConfig_Reporting_Default(t *testing.T) {
if testing.Short() {
t.Skip("too slow for testing.Short")
}

t.Parallel()
hcl := `
reporting {
}
`
a := NewTestAgent(t, hcl)
defer a.Shutdown()
require.Equal(t, false, a.consulConfig().Reporting.License.Enabled)
}
4 changes: 4 additions & 0 deletions agent/config/builder_oss.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ func validateEnterpriseConfigKeys(config *Config) []error {
add("license_path")
config.LicensePath = nil
}
if config.Reporting.License.Enabled != nil {
add("reporting.license.enabled")
config.Reporting.License.Enabled = nil
}

return result
}
Expand Down
13 changes: 13 additions & 0 deletions agent/config/builder_oss_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,19 @@ func TestValidateEnterpriseConfigKeys(t *testing.T) {
require.Empty(t, c.LicensePath)
},
},
"reporting.license.enabled": {
config: Config{
Reporting: Reporting{
License: License{
Enabled: &boolVal,
},
},
},
badKeys: []string{"reporting.license.enabled"},
check: func(t *testing.T, c *Config) {
require.Nil(t, c.Reporting.License.Enabled)
},
},
"multi": {
config: Config{
ReadReplica: &boolVal,
Expand Down
11 changes: 11 additions & 0 deletions agent/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,9 @@ type Config struct {
LicensePollMaxTime *string `mapstructure:"license_poll_max_time"`
LicenseUpdateBaseTime *string `mapstructure:"license_update_base_time"`
LicenseUpdateMaxTime *string `mapstructure:"license_update_max_time"`

// license reporting
Reporting Reporting `mapstructure:"reporting"`
}

type GossipLANConfig struct {
Expand Down Expand Up @@ -924,3 +927,11 @@ type Peering struct {
// This always gets overridden in NonUserSource()
TestAllowPeerRegistrations *bool `mapstructure:"test_allow_peer_registrations"`
}

type License struct {
Enabled *bool `mapstructure:"enabled"`
}

type Reporting struct {
License License `mapstructure:"license"`
}
10 changes: 10 additions & 0 deletions agent/config/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -1429,9 +1429,19 @@ type RuntimeConfig struct {
// AutoReloadConfigCoalesceInterval Coalesce Interval for auto reload config
AutoReloadConfigCoalesceInterval time.Duration

Reporting ReportingConfig

EnterpriseRuntimeConfig
}

type LicenseConfig struct {
Enabled bool
}

type ReportingConfig struct {
License LicenseConfig
}

type AutoConfig struct {
Enabled bool
IntroToken string
Expand Down
81 changes: 81 additions & 0 deletions agent/config/runtime_oss_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"testing"

"github.com/hashicorp/consul/sdk/testutil"
"github.com/stretchr/testify/require"
)

var testRuntimeConfigSanitizeExpectedFilename = "TestRuntimeConfig_Sanitize.golden"
Expand All @@ -27,6 +28,7 @@ var enterpriseConfigKeyWarnings = []string{
enterpriseConfigKeyError{key: "acl.msp_disable_bootstrap"}.Error(),
enterpriseConfigKeyError{key: "acl.tokens.managed_service_provider"}.Error(),
enterpriseConfigKeyError{key: "audit"}.Error(),
enterpriseConfigKeyError{key: "reporting.license.enabled"}.Error(),
}

// OSS-only equivalent of TestConfigFlagsAndEdgecases
Expand Down Expand Up @@ -80,3 +82,82 @@ func TestLoad_IntegrationWithFlags_OSS(t *testing.T) {
}
}
}

func TestLoad_ReportingConfig(t *testing.T) {
dir := testutil.TempDir(t, t.Name())

t.Run("load from JSON defaults to false", func(t *testing.T) {
content := `{
"reporting": {}
}`

opts := LoadOpts{
FlagValues: Config{
DataDir: &dir,
},
Overrides: []Source{
FileSource{
Name: "reporting.json",
Format: "json",
Data: content,
},
},
}
patchLoadOptsShims(&opts)
result, err := Load(opts)
require.NoError(t, err)
require.Len(t, result.Warnings, 0)
require.Equal(t, false, result.RuntimeConfig.Reporting.License.Enabled)
})

t.Run("load from HCL defaults to false", func(t *testing.T) {
content := `
reporting {}
`

opts := LoadOpts{
FlagValues: Config{
DataDir: &dir,
},
Overrides: []Source{
FileSource{
Name: "reporting.hcl",
Format: "hcl",
Data: content,
},
},
}
patchLoadOptsShims(&opts)
result, err := Load(opts)
require.NoError(t, err)
require.Len(t, result.Warnings, 0)
require.Equal(t, false, result.RuntimeConfig.Reporting.License.Enabled)
})

t.Run("with value set returns warning and defaults to false", func(t *testing.T) {
content := `reporting {
license {
enabled = true
}
}`

opts := LoadOpts{
FlagValues: Config{
DataDir: &dir,
},
Overrides: []Source{
FileSource{
Name: "reporting.hcl",
Format: "hcl",
Data: content,
},
},
}
patchLoadOptsShims(&opts)
result, err := Load(opts)
require.NoError(t, err)
require.Len(t, result.Warnings, 1)
require.Contains(t, result.Warnings[0], "\"reporting.license.enabled\" is a Consul Enterprise configuration and will have no effect")
require.Equal(t, false, result.RuntimeConfig.Reporting.License.Enabled)
})
}
5 changes: 5 additions & 0 deletions agent/config/testdata/TestRuntimeConfig_Sanitize.golden
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,11 @@
"ReconnectTimeoutLAN": "0s",
"ReconnectTimeoutWAN": "0s",
"RejoinAfterLeave": false,
"Reporting": {
"License": {
"Enabled": false
}
},
"RetryJoinIntervalLAN": "0s",
"RetryJoinIntervalWAN": "0s",
"RetryJoinLAN": [
Expand Down
5 changes: 5 additions & 0 deletions agent/config/testdata/full-config.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,11 @@ reconnect_timeout = "23739s"
reconnect_timeout_wan = "26694s"
recursors = [ "63.38.39.58", "92.49.18.18" ]
rejoin_after_leave = true
reporting = {
license = {
enabled = false
}
}
retry_interval = "8067s"
retry_interval_wan = "28866s"
retry_join = [ "pbsSFY7U", "l0qLtWij" ]
Expand Down
5 changes: 5 additions & 0 deletions agent/config/testdata/full-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,11 @@
"reconnect_timeout_wan": "26694s",
"recursors": [ "63.38.39.58", "92.49.18.18" ],
"rejoin_after_leave": true,
"reporting": {
"license": {
"enabled": false
}
},
"retry_interval": "8067s",
"retry_interval_wan": "28866s",
"retry_join": [ "pbsSFY7U", "l0qLtWij" ],
Expand Down
11 changes: 11 additions & 0 deletions agent/consul/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,8 @@ type Config struct {

PeeringTestAllowPeerRegistrations bool

Reporting Reporting

// Embedded Consul Enterprise specific configuration
*EnterpriseConfig
}
Expand Down Expand Up @@ -630,8 +632,17 @@ type ReloadableConfig struct {
RaftTrailingLogs int
HeartbeatTimeout time.Duration
ElectionTimeout time.Duration
Reporting Reporting
}

type RaftBoltDBConfig struct {
NoFreelistSync bool
}

type License struct {
Enabled bool
}

type Reporting struct {
License License
}
2 changes: 2 additions & 0 deletions agent/consul/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1618,6 +1618,8 @@ func (s *Server) ReloadConfig(config ReloadableConfig) error {
return err
}

s.updateReportingConfig(config)

s.rpcLimiter.Store(rate.NewLimiter(config.RPCRateLimit, config.RPCMaxBurst))
s.rpcConnLimiter.SetConfig(connlimit.Config{
MaxConnsPerClientIP: config.RPCMaxConnsPerClient,
Expand Down
4 changes: 4 additions & 0 deletions agent/consul/server_oss.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,7 @@ func addSerfMetricsLabels(conf *serf.Config, wan bool, segment string, partition

conf.MetricLabels = append(conf.MetricLabels, networkMetric)
}

func (s *Server) updateReportingConfig(config ReloadableConfig) {
// no-op
}
43 changes: 43 additions & 0 deletions agent/consul/server_oss_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//go:build !consulent
// +build !consulent

package consul

import (
"os"
"testing"

"github.com/stretchr/testify/require"

"github.com/hashicorp/consul/testrpc"
)

func TestAgent_ReloadConfig_Reporting(t *testing.T) {
if testing.Short() {
t.Skip("too slow for testing.Short")
}
t.Parallel()

dir1, s := testServerWithConfig(t, func(c *Config) {
c.Reporting.License.Enabled = false
})
defer os.RemoveAll(dir1)
defer s.Shutdown()

testrpc.WaitForTestAgent(t, s.RPC, "dc1")

require.Equal(t, false, s.config.Reporting.License.Enabled)

rc := ReloadableConfig{
Reporting: Reporting{
License: License{
Enabled: true,
},
},
}

require.NoError(t, s.ReloadConfig(rc))

// Check config reload is no-op
require.Equal(t, false, s.config.Reporting.License.Enabled)
}

0 comments on commit 8c07f54

Please sign in to comment.