config: Use LiteralSource for some defaults

Using the LiteralSource makes it much easier to find default values, because an IDE reports
the location of a default. With an HCL string they are harder to discover.

Also removes unnecessary mapstructure.Decodes of constant values.
pull/9251/head
Daniel Nephin 2020-11-20 18:14:17 -05:00
parent b564eb4e81
commit 0a44906fe3
4 changed files with 29 additions and 46 deletions

View File

@ -165,7 +165,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())
} }

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

@ -7205,7 +7205,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()