Merge pull request #9251 from hashicorp/dnephin/config-cleanup-flags-and-opts

config: move testing shims and cleanup some defaults
This commit is contained in:
Daniel Nephin 2021-01-07 19:11:22 -05:00 committed by hashicorp-ci
parent 223b85f89e
commit 156c14ae14
7 changed files with 53 additions and 77 deletions

View File

@ -92,8 +92,7 @@ func Load(opts BuilderOpts, extraHead Source, overrides ...Source) (*RuntimeConf
// since not all pre-conditions have to be satisfied when performing // since not all pre-conditions have to be satisfied when performing
// syntactical tests. // syntactical tests.
type Builder struct { type Builder struct {
// devMode stores the value of the -dev flag, and enables development mode. opts BuilderOpts
devMode *bool
// Head, Sources, and Tail are used to manage the order of the // Head, Sources, and Tail are used to manage the order of the
// config sources, as described in the comments above. // config sources, as described in the comments above.
@ -105,15 +104,6 @@ type Builder struct {
// parsing the configuration. // parsing the configuration.
Warnings []string Warnings []string
// hostname is a shim for testing, allowing tests to specify a replacement
// for os.Hostname.
hostname func() (string, error)
// getPrivateIPv4 and getPublicIPv6 are shims for testing, allowing tests to
// specify a replacement for ipaddr.GetPrivateIPv4 and ipaddr.GetPublicIPv6.
getPrivateIPv4 func() ([]*net.IPAddr, error)
getPublicIPv6 func() ([]*net.IPAddr, error)
// err contains the first error that occurred during // err contains the first error that occurred during
// building the runtime configuration. // building the runtime configuration.
err error err error
@ -135,8 +125,8 @@ func NewBuilder(opts BuilderOpts) (*Builder, error) {
} }
b := &Builder{ b := &Builder{
devMode: opts.DevMode, opts: opts,
Head: []Source{DefaultSource(), DefaultEnterpriseSource()}, Head: []Source{DefaultSource(), DefaultEnterpriseSource()},
} }
if b.boolVal(opts.DevMode) { if b.boolVal(opts.DevMode) {
@ -165,7 +155,7 @@ func NewBuilder(opts BuilderOpts) (*Builder, error) {
Data: s, Data: s,
}) })
} }
b.Tail = append(b.Tail, NonUserSource(), DefaultConsulSource(), OverrideEnterpriseSource(), DefaultVersionSource()) b.Tail = append(b.Tail, NonUserSource(), DefaultConsulSource(), OverrideEnterpriseSource(), defaultVersionSource())
if b.boolVal(opts.DevMode) { if b.boolVal(opts.DevMode) {
b.Tail = append(b.Tail, DevConsulSource()) b.Tail = append(b.Tail, DevConsulSource())
} }
@ -467,14 +457,14 @@ func (b *Builder) Build() (rt RuntimeConfig, err error) {
switch { switch {
case ipaddr.IsAnyV4(advertiseAddr): case ipaddr.IsAnyV4(advertiseAddr):
addrtyp = "private IPv4" addrtyp = "private IPv4"
detect = b.getPrivateIPv4 detect = b.opts.getPrivateIPv4
if detect == nil { if detect == nil {
detect = ipaddr.GetPrivateIPv4 detect = ipaddr.GetPrivateIPv4
} }
case ipaddr.IsAnyV6(advertiseAddr): case ipaddr.IsAnyV6(advertiseAddr):
addrtyp = "public IPv6" addrtyp = "public IPv6"
detect = b.getPublicIPv6 detect = b.opts.getPublicIPv6
if detect == nil { if detect == nil {
detect = ipaddr.GetPublicIPv6 detect = ipaddr.GetPublicIPv6
} }
@ -995,7 +985,7 @@ func (b *Builder) Build() (rt RuntimeConfig, err error) {
DataDir: dataDir, DataDir: dataDir,
Datacenter: datacenter, Datacenter: datacenter,
DefaultQueryTime: b.durationVal("default_query_time", c.DefaultQueryTime), DefaultQueryTime: b.durationVal("default_query_time", c.DefaultQueryTime),
DevMode: b.boolVal(b.devMode), DevMode: b.boolVal(b.opts.DevMode),
DisableAnonymousSignature: b.boolVal(c.DisableAnonymousSignature), DisableAnonymousSignature: b.boolVal(c.DisableAnonymousSignature),
DisableCoordinates: b.boolVal(c.DisableCoordinates), DisableCoordinates: b.boolVal(c.DisableCoordinates),
DisableHostNodeID: b.boolVal(c.DisableHostNodeID), DisableHostNodeID: b.boolVal(c.DisableHostNodeID),
@ -1901,7 +1891,7 @@ func (b *Builder) tlsCipherSuites(name string, v *string) []uint16 {
func (b *Builder) nodeName(v *string) string { func (b *Builder) nodeName(v *string) string {
nodeName := b.stringVal(v) nodeName := b.stringVal(v)
if nodeName == "" { if nodeName == "" {
fn := b.hostname fn := b.opts.hostname
if fn == nil { if fn == nil {
fn = os.Hostname fn = os.Hostname
} }

View File

@ -172,13 +172,13 @@ func TestBuilder_BuildAndValidate_NodeName(t *testing.T) {
} }
func patchBuilderShims(b *Builder) { func patchBuilderShims(b *Builder) {
b.hostname = func() (string, error) { b.opts.hostname = func() (string, error) {
return "thehostname", nil return "thehostname", nil
} }
b.getPrivateIPv4 = func() ([]*net.IPAddr, error) { b.opts.getPrivateIPv4 = func() ([]*net.IPAddr, error) {
return []*net.IPAddr{ipAddr("10.0.0.1")}, nil return []*net.IPAddr{ipAddr("10.0.0.1")}, nil
} }
b.getPublicIPv6 = func() ([]*net.IPAddr, error) { b.opts.getPublicIPv6 = func() ([]*net.IPAddr, error) {
return []*net.IPAddr{ipAddr("dead:beef::1")}, nil return []*net.IPAddr{ipAddr("dead:beef::1")}, nil
} }
} }

View File

@ -10,11 +10,6 @@ import (
"github.com/hashicorp/consul/lib/decode" "github.com/hashicorp/consul/lib/decode"
) )
const (
SerfLANKeyring = "serf/local.keyring"
SerfWANKeyring = "serf/remote.keyring"
)
// Source parses configuration from some source. // Source parses configuration from some source.
type Source interface { type Source interface {
// Source returns an identifier for the Source that can be used in error message // Source returns an identifier for the Source that can be used in error message

View File

@ -1,13 +1,13 @@
package config package config
import ( import (
"fmt"
"strconv" "strconv"
"github.com/hashicorp/raft"
"github.com/hashicorp/consul/agent/checks" "github.com/hashicorp/consul/agent/checks"
"github.com/hashicorp/consul/agent/consul" "github.com/hashicorp/consul/agent/consul"
"github.com/hashicorp/consul/version" "github.com/hashicorp/consul/version"
"github.com/hashicorp/raft"
) )
// DefaultSource is the default agent configuration. // DefaultSource is the default agent configuration.
@ -205,22 +205,24 @@ func NonUserSource() Source {
} }
} }
// VersionSource creates a config source for the version parameters. // versionSource creates a config source for the version parameters.
// This should be merged in the tail since these values are not // This should be merged in the tail since these values are not
// user configurable. // user configurable.
// TODO: return a LiteralSource (no decoding) instead of a FileSource func versionSource(rev, ver, verPre string) Source {
func VersionSource(rev, ver, verPre string) Source { return LiteralSource{
return FileSource{ Name: "version",
Name: "version", Config: Config{
Format: "hcl", Revision: &rev,
Data: fmt.Sprintf(`revision = %q version = %q version_prerelease = %q`, rev, ver, verPre), Version: &ver,
VersionPrerelease: &verPre,
},
} }
} }
// DefaultVersionSource returns the version config source for the embedded // defaultVersionSource returns the version config source for the embedded
// version numbers. // version numbers.
func DefaultVersionSource() Source { func defaultVersionSource() Source {
return VersionSource(version.GitCommit, version.Version, version.VersionPrerelease) return versionSource(version.GitCommit, version.Version, version.VersionPrerelease)
} }
// DefaultConsulSource returns the default configuration for the consul agent. // DefaultConsulSource returns the default configuration for the consul agent.
@ -254,27 +256,18 @@ func DefaultConsulSource() Source {
// DevConsulSource returns the consul agent configuration for the dev mode. // DevConsulSource returns the consul agent configuration for the dev mode.
// This should be merged in the tail after the DefaultConsulSource. // This should be merged in the tail after the DefaultConsulSource.
// TODO: return a LiteralSource (no decoding) instead of a FileSource
func DevConsulSource() Source { func DevConsulSource() Source {
return FileSource{ c := Config{}
Name: "consul-dev", c.Consul.Coordinate.UpdatePeriod = strPtr("100ms")
Format: "hcl", c.Consul.Raft.ElectionTimeout = strPtr("52ms")
Data: ` c.Consul.Raft.HeartbeatTimeout = strPtr("35ms")
consul = { c.Consul.Raft.LeaderLeaseTimeout = strPtr("20ms")
coordinate = { c.Consul.Server.HealthInterval = strPtr("10ms")
update_period = "100ms" return LiteralSource{Name: "consul-dev", Config: c}
} }
raft = {
election_timeout = "52ms" func strPtr(v string) *string {
heartbeat_timeout = "35ms" return &v
leader_lease_timeout = "20ms"
}
server = {
health_interval = "10ms"
}
}
`,
}
} }
func DefaultRuntimeConfig(hcl string) *RuntimeConfig { func DefaultRuntimeConfig(hcl string) *RuntimeConfig {

View File

@ -5,22 +5,12 @@ package config
// DefaultEnterpriseSource returns the consul agent configuration for enterprise mode. // DefaultEnterpriseSource returns the consul agent configuration for enterprise mode.
// These can be overridden by the user and therefore this source should be merged in the // These can be overridden by the user and therefore this source should be merged in the
// head and processed before user configuration. // head and processed before user configuration.
// TODO: return a LiteralSource (no decoding) instead of a FileSource
func DefaultEnterpriseSource() Source { func DefaultEnterpriseSource() Source {
return FileSource{ return LiteralSource{Name: "enterprise-defaults"}
Name: "enterprise-defaults",
Format: "hcl",
Data: ``,
}
} }
// OverrideEnterpriseSource returns the consul agent configuration for the enterprise mode. // OverrideEnterpriseSource returns the consul agent configuration for the enterprise mode.
// This should be merged in the tail after the DefaultConsulSource. // This should be merged in the tail after the DefaultConsulSource.
// TODO: return a LiteralSource (no decoding) instead of a FileSource
func OverrideEnterpriseSource() Source { func OverrideEnterpriseSource() Source {
return FileSource{ return LiteralSource{Name: "enterprise-overrides"}
Name: "enterprise-overrides",
Format: "hcl",
Data: ``,
}
} }

View File

@ -3,6 +3,7 @@ package config
import ( import (
"flag" "flag"
"fmt" "fmt"
"net"
"time" "time"
) )
@ -26,6 +27,15 @@ type BuilderOpts struct {
// HCL contains an arbitrary config in hcl format. // HCL contains an arbitrary config in hcl format.
HCL []string HCL []string
// hostname is a shim for testing, allowing tests to specify a replacement
// for os.Hostname.
hostname func() (string, error)
// getPrivateIPv4 and getPublicIPv6 are shims for testing, allowing tests to
// specify a replacement for ipaddr.GetPrivateIPv4 and ipaddr.GetPublicIPv6.
getPrivateIPv4 func() ([]*net.IPAddr, error)
getPublicIPv6 func() ([]*net.IPAddr, error)
} }
// AddFlags adds the command line flags for the agent. // AddFlags adds the command line flags for the agent.

View File

@ -4886,13 +4886,13 @@ func testConfig(t *testing.T, tests []configTest, dataDir string) {
patchBuilderShims(b) patchBuilderShims(b)
if tt.hostname != nil { if tt.hostname != nil {
b.hostname = tt.hostname b.opts.hostname = tt.hostname
} }
if tt.privatev4 != nil { if tt.privatev4 != nil {
b.getPrivateIPv4 = tt.privatev4 b.opts.getPrivateIPv4 = tt.privatev4
} }
if tt.publicv6 != nil { if tt.publicv6 != nil {
b.getPublicIPv6 = tt.publicv6 b.opts.getPublicIPv6 = tt.publicv6
} }
// read the source fragements // read the source fragements
@ -4937,9 +4937,7 @@ func testConfig(t *testing.T, tests []configTest, dataDir string) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
x.hostname = b.hostname patchBuilderShims(x)
x.getPrivateIPv4 = func() ([]*net.IPAddr, error) { return []*net.IPAddr{ipAddr("10.0.0.1")}, nil }
x.getPublicIPv6 = func() ([]*net.IPAddr, error) { return []*net.IPAddr{ipAddr("dead:beef::1")}, nil }
expected, err := x.Build() expected, err := x.Build()
if err != nil { if err != nil {
t.Fatalf("build default failed: %s", err) t.Fatalf("build default failed: %s", err)
@ -7208,7 +7206,7 @@ func TestFullConfig(t *testing.T) {
} }
b.Sources = append(b.Sources, FileSource{Name: "full." + format, Data: data, Format: format}) b.Sources = append(b.Sources, FileSource{Name: "full." + format, Data: data, Format: format})
b.Tail = append(b.Tail, tail[format]...) b.Tail = append(b.Tail, tail[format]...)
b.Tail = append(b.Tail, VersionSource("JNtPSav3", "R909Hblt", "ZT1JOQLn")) b.Tail = append(b.Tail, versionSource("JNtPSav3", "R909Hblt", "ZT1JOQLn"))
// construct the runtime config // construct the runtime config
rt, err := b.Build() rt, err := b.Build()