From 0a44906fe384069d31bb8ce9ed5bcdfe167ca852 Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Fri, 20 Nov 2020 18:14:17 -0500 Subject: [PATCH] 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. --- agent/config/builder.go | 2 +- agent/config/default.go | 57 ++++++++++++++++-------------------- agent/config/default_oss.go | 14 ++------- agent/config/runtime_test.go | 2 +- 4 files changed, 29 insertions(+), 46 deletions(-) diff --git a/agent/config/builder.go b/agent/config/builder.go index 48a2c55bec..9ca6092ab4 100644 --- a/agent/config/builder.go +++ b/agent/config/builder.go @@ -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()) } diff --git a/agent/config/default.go b/agent/config/default.go index 72888250d6..736ceeedb6 100644 --- a/agent/config/default.go +++ b/agent/config/default.go @@ -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 { diff --git a/agent/config/default_oss.go b/agent/config/default_oss.go index 8f5d61aaaf..57f52d9275 100644 --- a/agent/config/default_oss.go +++ b/agent/config/default_oss.go @@ -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"} } diff --git a/agent/config/runtime_test.go b/agent/config/runtime_test.go index a989add2f7..092e75b6a3 100644 --- a/agent/config/runtime_test.go +++ b/agent/config/runtime_test.go @@ -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()