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.
This commit is contained in:
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,
})
}
b.Tail = append(b.Tail, NonUserSource(), DefaultConsulSource(), OverrideEnterpriseSource(), DefaultVersionSource())
b.Tail = append(b.Tail, NonUserSource(), DefaultConsulSource(), OverrideEnterpriseSource(), defaultVersionSource())
if b.boolVal(opts.DevMode) {
b.Tail = append(b.Tail, DevConsulSource())
}

View File

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

View File

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

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.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
rt, err := b.Build()