ent: move all license info into LicenseConfig{} (#16738)

and add new TestConfigForServer() to get a
valid nomad.Config to use in tests

Co-authored-by: Luiz Aoqui <luiz@hashicorp.com>
This commit is contained in:
Daniel Bennett
2023-03-30 16:15:05 -05:00
committed by GitHub
parent 5957880112
commit 967f6e9e04
7 changed files with 55 additions and 29 deletions

View File

@@ -170,7 +170,6 @@ func convertServerConfig(agentConfig *Config) (*nomad.Config, error) {
conf.EnableDebug = agentConfig.EnableDebug
conf.Build = agentConfig.Version.VersionNumber()
conf.BuildDate = agentConfig.Version.BuildDate
conf.Revision = agentConfig.Version.Revision
if agentConfig.Region != "" {
conf.Region = agentConfig.Region
@@ -550,9 +549,12 @@ func convertServerConfig(agentConfig *Config) (*nomad.Config, error) {
}
// Add Enterprise license configs
conf.LicenseEnv = agentConfig.Server.LicenseEnv
conf.LicensePath = agentConfig.Server.LicensePath
conf.LicenseConfig.AdditionalPubKeys = agentConfig.Server.licenseAdditionalPublicKeys
conf.LicenseConfig = &nomad.LicenseConfig{
BuildDate: agentConfig.Version.BuildDate,
AdditionalPubKeys: agentConfig.Server.licenseAdditionalPublicKeys,
LicenseEnvBytes: agentConfig.Server.LicenseEnv,
LicensePath: agentConfig.Server.LicensePath,
}
// Add the search configuration
if search := agentConfig.Server.Search; search != nil {

View File

@@ -127,9 +127,6 @@ type Config struct {
// operators track which versions are actively deployed
Build string
// BuildDate is the time of the git commit used to build the program.
BuildDate time.Time
// Revision is a string that carries the version.GitCommit of Nomad that
// was compiled.
Revision string
@@ -389,10 +386,8 @@ type Config struct {
// connections from a single IP address. nil/0 means no limit.
RPCMaxConnsPerClient int
// LicenseConfig is a tunable knob for enterprise license testing.
// LicenseConfig stores information about the Enterprise license loaded for the server.
LicenseConfig *LicenseConfig
LicenseEnv string
LicensePath string
// SearchConfig provides knobs for Search API.
SearchConfig *structs.SearchConfig

View File

@@ -3,7 +3,6 @@ package nomad
import (
"time"
"github.com/hashicorp/go-hclog"
"golang.org/x/exp/slices"
)
@@ -21,8 +20,6 @@ type LicenseConfig struct {
// AdditionalPubKeys is a set of public keys to
AdditionalPubKeys []string
Logger hclog.InterceptLogger
}
func (c *LicenseConfig) Copy() *LicenseConfig {

View File

@@ -0,0 +1,7 @@
//go:build !ent
package nomad
func (c *LicenseConfig) Validate() error {
return nil
}

View File

@@ -345,6 +345,11 @@ func NewServer(config *Config, consulCatalog consul.CatalogAPI, consulConfigEntr
// Create the logger
logger := config.Logger.ResetNamedIntercept("nomad")
// Validate enterprise license before anything stateful happens
if err = config.LicenseConfig.Validate(); err != nil {
return nil, err
}
// Create the server
s := &Server{
config: config,
@@ -868,8 +873,11 @@ func (s *Server) Reload(newConfig *Config) error {
}
}
if newConfig.LicenseEnv != "" || newConfig.LicensePath != "" {
s.EnterpriseState.ReloadLicense(newConfig)
if newConfig.LicenseConfig.LicenseEnvBytes != "" || newConfig.LicenseConfig.LicensePath != "" {
if err = s.EnterpriseState.ReloadLicense(newConfig); err != nil {
s.logger.Error("error reloading license", "error", err)
_ = multierror.Append(&mErr, err)
}
}
// Because this is a new configuration, we extract the worker pool arguments without acquiring a lock

View File

@@ -1,5 +1,4 @@
//go:build !ent
// +build !ent
package nomad

View File

@@ -1,7 +1,6 @@
package nomad
import (
"errors"
"fmt"
"math/rand"
"net"
@@ -43,7 +42,11 @@ func TestServer(t testing.T, cb func(*Config)) (*Server, func()) {
return s, c
}
func TestServerErr(t testing.T, cb func(*Config)) (*Server, func(), error) {
// TestConfigForServer provides a fully functional Config to pass to NewServer()
// It can be changed beforehand to induce different behavior such as specific errors.
func TestConfigForServer(t testing.T) *Config {
t.Helper()
// Setup the default settings
config := DefaultConfig()
@@ -95,6 +98,19 @@ func TestServerErr(t testing.T, cb func(*Config)) (*Server, func(), error) {
MinTermLength: 2,
}
// Get random ports for RPC and Serf
ports := ci.PortAllocator.Grab(2)
config.RPCAddr = &net.TCPAddr{
IP: []byte{127, 0, 0, 1},
Port: ports[0],
}
config.SerfConfig.MemberlistConfig.BindPort = ports[1]
return config
}
func TestServerErr(t testing.T, cb func(*Config)) (*Server, func(), error) {
config := TestConfigForServer(t)
// Invoke the callback if any
if cb != nil {
cb(config)
@@ -104,18 +120,12 @@ func TestServerErr(t testing.T, cb func(*Config)) (*Server, func(), error) {
cConfigs := consul.NewMockConfigsAPI(config.Logger)
cACLs := consul.NewMockACLsAPI(config.Logger)
var server *Server
var err error
for i := 10; i >= 0; i-- {
// Get random ports, need to cleanup later
ports := ci.PortAllocator.Grab(2)
config.RPCAddr = &net.TCPAddr{
IP: []byte{127, 0, 0, 1},
Port: ports[0],
}
config.SerfConfig.MemberlistConfig.BindPort = ports[1]
// Create server
server, err := NewServer(config, cCatalog, cConfigs, cACLs)
server, err = NewServer(config, cCatalog, cConfigs, cACLs)
if err == nil {
return server, func() {
ch := make(chan error)
@@ -145,9 +155,17 @@ func TestServerErr(t testing.T, cb func(*Config)) (*Server, func(), error) {
wait := time.Duration(rand.Int31n(2000)) * time.Millisecond
time.Sleep(wait)
}
// if it failed for port reasons, try new ones
ports := ci.PortAllocator.Grab(2)
config.RPCAddr = &net.TCPAddr{
IP: []byte{127, 0, 0, 1},
Port: ports[0],
}
config.SerfConfig.MemberlistConfig.BindPort = ports[1]
}
return nil, nil, errors.New("unable to acquire ports for test server")
return nil, nil, fmt.Errorf("error starting test server: %w", err)
}
func TestJoin(t testing.T, servers ...*Server) {