Skip to content

Commit

Permalink
Check all jsonConfig fields for zero values
Browse files Browse the repository at this point in the history
* don't set corresponding config fields if they are zero values
* this is needed to avoid overwriting default/json file values with zero values
when applying environment config variables

License: MIT
Signed-off-by: Robert Ignat <robert.ignat91@gmail.com>
  • Loading branch information
roignpar committed Feb 13, 2019
1 parent aa5d545 commit 166aeec
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 67 deletions.
23 changes: 14 additions & 9 deletions api/ipfsproxy/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,20 +227,25 @@ func (cfg *Config) LoadJSON(raw []byte) error {
}

func (cfg *Config) applyJSONConfig(jcfg *jsonConfig) error {
proxyAddr, err := ma.NewMultiaddr(jcfg.ListenMultiaddress)
if err != nil {
return fmt.Errorf("error parsing proxy listen_multiaddress: %s", err)
if jcfg.ListenMultiaddress != "" {
proxyAddr, err := ma.NewMultiaddr(jcfg.ListenMultiaddress)
if err != nil {
return fmt.Errorf("error parsing proxy listen_multiaddress: %s", err)
}
cfg.ListenAddr = proxyAddr
}
nodeAddr, err := ma.NewMultiaddr(jcfg.NodeMultiaddress)
if err != nil {
return fmt.Errorf("error parsing ipfs node_multiaddress: %s", err)

if jcfg.NodeMultiaddress != "" {
nodeAddr, err := ma.NewMultiaddr(jcfg.NodeMultiaddress)
if err != nil {
return fmt.Errorf("error parsing ipfs node_multiaddress: %s", err)
}
cfg.NodeAddr = nodeAddr
}

cfg.ListenAddr = proxyAddr
cfg.NodeAddr = nodeAddr
config.SetIfNotDefault(jcfg.NodeHTTPS, &cfg.NodeHTTPS)

err = config.ParseDurations(
err := config.ParseDurations(
"ipfsproxy",
&config.DurationOpt{Duration: jcfg.ReadTimeout, Dst: &cfg.ReadTimeout, Name: "read_timeout"},
&config.DurationOpt{Duration: jcfg.ReadHeaderTimeout, Dst: &cfg.ReadHeaderTimeout, Name: "read_header_timeout"},
Expand Down
26 changes: 19 additions & 7 deletions api/rest/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,12 @@ func (cfg *Config) applyJSONConfig(jcfg *jsonConfig) error {
}

// Other options
cfg.BasicAuthCreds = jcfg.BasicAuthCreds
cfg.Headers = jcfg.Headers
if cfg.BasicAuthCreds != nil {
cfg.BasicAuthCreds = jcfg.BasicAuthCreds
}
if jcfg.Headers != nil {
cfg.Headers = jcfg.Headers
}

return cfg.Validate()
}
Expand Down Expand Up @@ -288,11 +292,19 @@ func (cfg *Config) loadHTTPOptions(jcfg *jsonConfig) error {
}

// CORS
cfg.CORSAllowedOrigins = jcfg.CORSAllowedOrigins
cfg.CORSAllowedMethods = jcfg.CORSAllowedMethods
cfg.CORSAllowedHeaders = jcfg.CORSAllowedHeaders
cfg.CORSExposedHeaders = jcfg.CORSExposedHeaders
cfg.CORSAllowCredentials = jcfg.CORSAllowCredentials
if jcfg.CORSAllowedOrigins != nil {
cfg.CORSAllowedOrigins = jcfg.CORSAllowedOrigins
}
if jcfg.CORSAllowedMethods != nil {
cfg.CORSAllowedMethods = jcfg.CORSAllowedMethods
}
if jcfg.CORSAllowedHeaders != nil {
cfg.CORSAllowedHeaders = jcfg.CORSAllowedHeaders
}
if jcfg.CORSExposedHeaders != nil {
cfg.CORSExposedHeaders = jcfg.CORSExposedHeaders
}
config.SetIfNotDefault(jcfg.CORSAllowCredentials, &cfg.CORSAllowCredentials)
if jcfg.CORSMaxAge == "" { // compatibility
jcfg.CORSMaxAge = "0s"
}
Expand Down
64 changes: 36 additions & 28 deletions cluster_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,40 +324,48 @@ for more information.`)
func (cfg *Config) applyConfigJSON(jcfg *configJSON) error {
config.SetIfNotDefault(jcfg.PeerstoreFile, &cfg.PeerstoreFile)

id, err := peer.IDB58Decode(jcfg.ID)
if err != nil {
err = fmt.Errorf("error decoding cluster ID: %s", err)
return err
if jcfg.ID != "" {
id, err := peer.IDB58Decode(jcfg.ID)
if err != nil {
err = fmt.Errorf("error decoding cluster ID: %s", err)
return err
}
cfg.ID = id
}
cfg.ID = id

config.SetIfNotDefault(jcfg.Peername, &cfg.Peername)

pkb, err := base64.StdEncoding.DecodeString(jcfg.PrivateKey)
if err != nil {
err = fmt.Errorf("error decoding private_key: %s", err)
return err
}
pKey, err := crypto.UnmarshalPrivateKey(pkb)
if err != nil {
err = fmt.Errorf("error parsing private_key ID: %s", err)
return err
if jcfg.PrivateKey != "" {
pkb, err := base64.StdEncoding.DecodeString(jcfg.PrivateKey)
if err != nil {
err = fmt.Errorf("error decoding private_key: %s", err)
return err
}
pKey, err := crypto.UnmarshalPrivateKey(pkb)
if err != nil {
err = fmt.Errorf("error parsing private_key ID: %s", err)
return err
}
cfg.PrivateKey = pKey
}
cfg.PrivateKey = pKey

clusterSecret, err := DecodeClusterSecret(jcfg.Secret)
if err != nil {
err = fmt.Errorf("error loading cluster secret from config: %s", err)
return err
if jcfg.Secret != "" {
clusterSecret, err := DecodeClusterSecret(jcfg.Secret)
if err != nil {
err = fmt.Errorf("error loading cluster secret from config: %s", err)
return err
}
cfg.Secret = clusterSecret
}
cfg.Secret = clusterSecret

clusterAddr, err := ma.NewMultiaddr(jcfg.ListenMultiaddress)
if err != nil {
err = fmt.Errorf("error parsing cluster_listen_multiaddress: %s", err)
return err
if jcfg.ListenMultiaddress != "" {
clusterAddr, err := ma.NewMultiaddr(jcfg.ListenMultiaddress)
if err != nil {
err = fmt.Errorf("error parsing cluster_listen_multiaddress: %s", err)
return err
}
cfg.ListenAddr = clusterAddr
}
cfg.ListenAddr = clusterAddr

rplMin := jcfg.ReplicationFactorMin
rplMax := jcfg.ReplicationFactorMax
Expand All @@ -368,7 +376,7 @@ func (cfg *Config) applyConfigJSON(jcfg *configJSON) error {
config.SetIfNotDefault(rplMin, &cfg.ReplicationFactorMin)
config.SetIfNotDefault(rplMax, &cfg.ReplicationFactorMax)

err = config.ParseDurations("cluster",
err := config.ParseDurations("cluster",
&config.DurationOpt{Duration: jcfg.StateSyncInterval, Dst: &cfg.StateSyncInterval, Name: "state_sync_interval"},
&config.DurationOpt{Duration: jcfg.IPFSSyncInterval, Dst: &cfg.IPFSSyncInterval, Name: "ipfs_sync_interval"},
&config.DurationOpt{Duration: jcfg.MonitorPingInterval, Dst: &cfg.MonitorPingInterval, Name: "monitor_ping_interval"},
Expand All @@ -378,8 +386,8 @@ func (cfg *Config) applyConfigJSON(jcfg *configJSON) error {
return err
}

cfg.LeaveOnShutdown = jcfg.LeaveOnShutdown
cfg.DisableRepinning = jcfg.DisableRepinning
config.SetIfNotDefault(jcfg.LeaveOnShutdown, &cfg.LeaveOnShutdown)
config.SetIfNotDefault(jcfg.DisableRepinning, &cfg.DisableRepinning)

return cfg.Validate()
}
Expand Down
2 changes: 1 addition & 1 deletion consensus/raft/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ func (cfg *Config) applyJSONConfig(jcfg *jsonConfig) error {
config.SetIfNotDefault(jcfg.DataFolder, &cfg.DataFolder)
config.SetIfNotDefault(waitForLeaderTimeout, &cfg.WaitForLeaderTimeout)
config.SetIfNotDefault(networkTimeout, &cfg.NetworkTimeout)
cfg.CommitRetries = jcfg.CommitRetries
config.SetIfNotDefault(jcfg.CommitRetries, &cfg.CommitRetries)
config.SetIfNotDefault(commitRetryDelay, &cfg.CommitRetryDelay)
config.SetIfNotDefault(jcfg.BackupsRotate, &cfg.BackupsRotate)

Expand Down
4 changes: 3 additions & 1 deletion informer/disk/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,15 @@ func (cfg *Config) LoadJSON(raw []byte) error {

func (cfg *Config) applyJSONConfig(jcfg *jsonConfig) error {
t, _ := time.ParseDuration(jcfg.MetricTTL)
cfg.MetricTTL = t
config.SetIfNotDefault(t, &cfg.MetricTTL)

switch jcfg.Type {
case "reposize":
cfg.Type = MetricRepoSize
case "freespace":
cfg.Type = MetricFreeSpace
case "":
// ignore zero value
default:
return errors.New("disk.metric_type is invalid")
}
Expand Down
2 changes: 1 addition & 1 deletion informer/numpin/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (cfg *Config) LoadJSON(raw []byte) error {

func (cfg *Config) applyJSONConfig(jcfg *jsonConfig) error {
t, _ := time.ParseDuration(jcfg.MetricTTL)
cfg.MetricTTL = t
config.SetIfNotDefault(t, &cfg.MetricTTL)

return cfg.Validate()
}
Expand Down
14 changes: 8 additions & 6 deletions ipfsconn/ipfshttp/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,16 @@ func (cfg *Config) LoadJSON(raw []byte) error {
}

func (cfg *Config) applyJSONConfig(jcfg *jsonConfig) error {
nodeAddr, err := ma.NewMultiaddr(jcfg.NodeMultiaddress)
if err != nil {
return fmt.Errorf("error parsing ipfs_node_multiaddress: %s", err)
}
if jcfg.NodeMultiaddress != "" {
nodeAddr, err := ma.NewMultiaddr(jcfg.NodeMultiaddress)
if err != nil {
return fmt.Errorf("error parsing ipfs_node_multiaddress: %s", err)
}

cfg.NodeAddr = nodeAddr
cfg.NodeAddr = nodeAddr
}

err = config.ParseDurations(
err := config.ParseDurations(
"ipfshttp",
&config.DurationOpt{Duration: jcfg.ConnectSwarmsDelay, Dst: &cfg.ConnectSwarmsDelay, Name: "connect_swarms_delay"},
&config.DurationOpt{Duration: jcfg.IPFSRequestTimeout, Dst: &cfg.IPFSRequestTimeout, Name: "ipfs_request_timeout"},
Expand Down
2 changes: 1 addition & 1 deletion monitor/basic/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (cfg *Config) LoadJSON(raw []byte) error {

func (cfg *Config) applyJSONConfig(jcfg *jsonConfig) error {
interval, _ := time.ParseDuration(jcfg.CheckInterval)
cfg.CheckInterval = interval
config.SetIfNotDefault(interval, &cfg.CheckInterval)

return cfg.Validate()
}
Expand Down
2 changes: 1 addition & 1 deletion monitor/pubsubmon/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (cfg *Config) LoadJSON(raw []byte) error {

func (cfg *Config) applyJSONConfig(jcfg *jsonConfig) error {
interval, _ := time.ParseDuration(jcfg.CheckInterval)
cfg.CheckInterval = interval
config.SetIfNotDefault(interval, &cfg.CheckInterval)

return cfg.Validate()
}
Expand Down
28 changes: 16 additions & 12 deletions observations/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,14 @@ func (cfg *MetricsConfig) applyJSONConfig(jcfg *jsonMetricsConfig) error {
}

func (cfg *MetricsConfig) loadMetricsOptions(jcfg *jsonMetricsConfig) error {
cfg.EnableStats = jcfg.EnableStats
endpointAddr, err := ma.NewMultiaddr(jcfg.PrometheusEndpoint)
if err != nil {
return fmt.Errorf("loadMetricsOptions: PrometheusEndpoint multiaddr: %v", err)
config.SetIfNotDefault(jcfg.EnableStats, &cfg.EnableStats)
if jcfg.PrometheusEndpoint != "" {
endpointAddr, err := ma.NewMultiaddr(jcfg.PrometheusEndpoint)
if err != nil {
return fmt.Errorf("loadMetricsOptions: PrometheusEndpoint multiaddr: %v", err)
}
cfg.PrometheusEndpoint = endpointAddr
}
cfg.PrometheusEndpoint = endpointAddr

return config.ParseDurations(
metricsConfigKey,
Expand Down Expand Up @@ -223,14 +225,16 @@ func (cfg *TracingConfig) applyJSONConfig(jcfg *jsonTracingConfig) error {
}

func (cfg *TracingConfig) loadTracingOptions(jcfg *jsonTracingConfig) error {
cfg.EnableTracing = jcfg.EnableTracing
agentAddr, err := ma.NewMultiaddr(jcfg.JaegerAgentEndpoint)
if err != nil {
return fmt.Errorf("loadTracingOptions: JaegerAgentEndpoint multiaddr: %v", err)
config.SetIfNotDefault(jcfg.EnableTracing, &cfg.EnableTracing)
if jcfg.JaegerAgentEndpoint != "" {
agentAddr, err := ma.NewMultiaddr(jcfg.JaegerAgentEndpoint)
if err != nil {
return fmt.Errorf("loadTracingOptions: JaegerAgentEndpoint multiaddr: %v", err)
}
cfg.JaegerAgentEndpoint = agentAddr
}
cfg.JaegerAgentEndpoint = agentAddr
cfg.TracingSamplingProb = jcfg.TracingSamplingProb
cfg.TracingServiceName = jcfg.TracingServiceName
config.SetIfNotDefault(jcfg.TracingSamplingProb, &cfg.TracingSamplingProb)
config.SetIfNotDefault(jcfg.TracingServiceName, &cfg.TracingServiceName)

return nil
}
Expand Down

0 comments on commit 166aeec

Please sign in to comment.