Implement ApplyEnvVars for all ComponentConfigs

License: MIT
Signed-off-by: Robert Ignat <robert.ignat91@gmail.com>
This commit is contained in:
Robert Ignat 2019-02-08 23:57:16 +02:00
parent ed30ac1ab4
commit 032f02802f
10 changed files with 148 additions and 37 deletions

View File

@ -157,8 +157,14 @@ func (cfg *Config) Default() error {
// ApplyEnvVars fills in any Config fields found // ApplyEnvVars fills in any Config fields found
// as environment variables. // as environment variables.
func (cfg *Config) ApplyEnvVars() error { func (cfg *Config) ApplyEnvVars() error {
// doesn't read any config from env jcfg := &jsonConfig{}
return nil
err := envconfig.Process(envConfigKey, jcfg)
if err != nil {
return err
}
return cfg.applyJSONConfig(jcfg)
} }
// Validate checks that the fields of this Config have sensible values, // Validate checks that the fields of this Config have sensible values,
@ -217,12 +223,10 @@ func (cfg *Config) LoadJSON(raw []byte) error {
return fmt.Errorf("error setting config to default values: %s", err) return fmt.Errorf("error setting config to default values: %s", err)
} }
// override json config with env var return cfg.applyJSONConfig(jcfg)
err = envconfig.Process("cluster_ipfsproxy", jcfg) }
if err != nil {
return err
}
func (cfg *Config) applyJSONConfig(jcfg *jsonConfig) error {
proxyAddr, err := ma.NewMultiaddr(jcfg.ListenMultiaddress) proxyAddr, err := ma.NewMultiaddr(jcfg.ListenMultiaddress)
if err != nil { if err != nil {
return fmt.Errorf("error parsing proxy listen_multiaddress: %s", err) return fmt.Errorf("error parsing proxy listen_multiaddress: %s", err)

View File

@ -9,6 +9,7 @@ import (
"github.com/ipfs/ipfs-cluster/api" "github.com/ipfs/ipfs-cluster/api"
"github.com/ipfs/ipfs-cluster/config" "github.com/ipfs/ipfs-cluster/config"
"github.com/kelseyhightower/envconfig"
hraft "github.com/hashicorp/raft" hraft "github.com/hashicorp/raft"
peer "github.com/libp2p/go-libp2p-peer" peer "github.com/libp2p/go-libp2p-peer"
@ -17,6 +18,7 @@ import (
// ConfigKey is the default configuration key for holding this component's // ConfigKey is the default configuration key for holding this component's
// configuration section. // configuration section.
var configKey = "raft" var configKey = "raft"
var envConfigKey = "cluster_raft"
// Configuration defaults // Configuration defaults
var ( var (
@ -185,6 +187,10 @@ func (cfg *Config) LoadJSON(raw []byte) error {
cfg.Default() cfg.Default()
return cfg.applyJSONConfig(jcfg)
}
func (cfg *Config) applyJSONConfig(jcfg *jsonConfig) error {
parseDuration := func(txt string) time.Duration { parseDuration := func(txt string) time.Duration {
d, _ := time.ParseDuration(txt) d, _ := time.ParseDuration(txt)
if txt != "" && d == 0 { if txt != "" && d == 0 {
@ -275,8 +281,14 @@ func (cfg *Config) Default() error {
// ApplyEnvVars fills in any Config fields found // ApplyEnvVars fills in any Config fields found
// as environment variables. // as environment variables.
func (cfg *Config) ApplyEnvVars() error { func (cfg *Config) ApplyEnvVars() error {
// doesn't read any config from env jcfg := &jsonConfig{}
return nil
err := envconfig.Process(envConfigKey, jcfg)
if err != nil {
return err
}
return cfg.applyJSONConfig(jcfg)
} }
// GetDataFolder returns the Raft data folder that we are using. // GetDataFolder returns the Raft data folder that we are using.

View File

@ -6,9 +6,11 @@ import (
"time" "time"
"github.com/ipfs/ipfs-cluster/config" "github.com/ipfs/ipfs-cluster/config"
"github.com/kelseyhightower/envconfig"
) )
const configKey = "disk" const configKey = "disk"
const envConfigKey = "cluster_disk"
// Default values for disk Config // Default values for disk Config
const ( const (
@ -56,8 +58,14 @@ func (cfg *Config) Default() error {
// ApplyEnvVars fills in any Config fields found // ApplyEnvVars fills in any Config fields found
// as environment variables. // as environment variables.
func (cfg *Config) ApplyEnvVars() error { func (cfg *Config) ApplyEnvVars() error {
// doesn't read any config from env jcfg := &jsonConfig{}
return nil
err := envconfig.Process(envConfigKey, jcfg)
if err != nil {
return err
}
return cfg.applyJSONConfig(jcfg)
} }
// Validate checks that the fields of this Config have working values, // Validate checks that the fields of this Config have working values,
@ -83,6 +91,12 @@ func (cfg *Config) LoadJSON(raw []byte) error {
return err return err
} }
cfg.Default()
return cfg.applyJSONConfig(jcfg)
}
func (cfg *Config) applyJSONConfig(jcfg *jsonConfig) error {
t, _ := time.ParseDuration(jcfg.MetricTTL) t, _ := time.ParseDuration(jcfg.MetricTTL)
cfg.MetricTTL = t cfg.MetricTTL = t

View File

@ -6,9 +6,11 @@ import (
"time" "time"
"github.com/ipfs/ipfs-cluster/config" "github.com/ipfs/ipfs-cluster/config"
"github.com/kelseyhightower/envconfig"
) )
const configKey = "numpin" const configKey = "numpin"
const envConfigKey = "cluster_numpin"
// These are the default values for a Config. // These are the default values for a Config.
const ( const (
@ -41,8 +43,14 @@ func (cfg *Config) Default() error {
// ApplyEnvVars fills in any Config fields found // ApplyEnvVars fills in any Config fields found
// as environment variables. // as environment variables.
func (cfg *Config) ApplyEnvVars() error { func (cfg *Config) ApplyEnvVars() error {
// doesn't read any config from env jcfg := &jsonConfig{}
return nil
err := envconfig.Process(envConfigKey, jcfg)
if err != nil {
return err
}
return cfg.applyJSONConfig(jcfg)
} }
// Validate checks that the fields of this configuration have // Validate checks that the fields of this configuration have
@ -63,6 +71,12 @@ func (cfg *Config) LoadJSON(raw []byte) error {
return err return err
} }
cfg.Default()
return cfg.applyJSONConfig(jcfg)
}
func (cfg *Config) applyJSONConfig(jcfg *jsonConfig) error {
t, _ := time.ParseDuration(jcfg.MetricTTL) t, _ := time.ParseDuration(jcfg.MetricTTL)
cfg.MetricTTL = t cfg.MetricTTL = t

View File

@ -12,6 +12,7 @@ import (
) )
const configKey = "ipfshttp" const configKey = "ipfshttp"
const envConfigKey = "cluster_ipfshttp"
// Default values for Config. // Default values for Config.
const ( const (
@ -140,6 +141,10 @@ func (cfg *Config) LoadJSON(raw []byte) error {
cfg.Default() cfg.Default()
return cfg.applyJSONConfig(jcfg)
}
func (cfg *Config) applyJSONConfig(jcfg *jsonConfig) error {
nodeAddr, err := ma.NewMultiaddr(jcfg.NodeMultiaddress) nodeAddr, err := ma.NewMultiaddr(jcfg.NodeMultiaddress)
if err != nil { if err != nil {
return fmt.Errorf("error parsing ipfs_node_multiaddress: %s", err) return fmt.Errorf("error parsing ipfs_node_multiaddress: %s", err)

View File

@ -6,9 +6,11 @@ import (
"time" "time"
"github.com/ipfs/ipfs-cluster/config" "github.com/ipfs/ipfs-cluster/config"
"github.com/kelseyhightower/envconfig"
) )
const configKey = "monbasic" const configKey = "monbasic"
const envConfigKey = "cluster_monbasic"
// Default values for this Config. // Default values for this Config.
const ( const (
@ -40,8 +42,14 @@ func (cfg *Config) Default() error {
// ApplyEnvVars fills in any Config fields found // ApplyEnvVars fills in any Config fields found
// as environment variables. // as environment variables.
func (cfg *Config) ApplyEnvVars() error { func (cfg *Config) ApplyEnvVars() error {
// doesn't read any config from env jcfg := &jsonConfig{}
return nil
err := envconfig.Process(envConfigKey, jcfg)
if err != nil {
return err
}
return cfg.applyJSONConfig(jcfg)
} }
// Validate checks that the fields of this Config have working values, // Validate checks that the fields of this Config have working values,
@ -63,6 +71,12 @@ func (cfg *Config) LoadJSON(raw []byte) error {
return err return err
} }
cfg.Default()
return cfg.applyJSONConfig(jcfg)
}
func (cfg *Config) applyJSONConfig(jcfg *jsonConfig) error {
interval, _ := time.ParseDuration(jcfg.CheckInterval) interval, _ := time.ParseDuration(jcfg.CheckInterval)
cfg.CheckInterval = interval cfg.CheckInterval = interval

View File

@ -6,9 +6,11 @@ import (
"time" "time"
"github.com/ipfs/ipfs-cluster/config" "github.com/ipfs/ipfs-cluster/config"
"github.com/kelseyhightower/envconfig"
) )
const configKey = "pubsubmon" const configKey = "pubsubmon"
const envConfigKey = "cluster_pubsubmon"
// Default values for this Config. // Default values for this Config.
const ( const (
@ -40,8 +42,14 @@ func (cfg *Config) Default() error {
// ApplyEnvVars fills in any Config fields found // ApplyEnvVars fills in any Config fields found
// as environment variables. // as environment variables.
func (cfg *Config) ApplyEnvVars() error { func (cfg *Config) ApplyEnvVars() error {
// doesn't read any config from env jcfg := &jsonConfig{}
return nil
err := envconfig.Process(envConfigKey, jcfg)
if err != nil {
return err
}
return cfg.applyJSONConfig(jcfg)
} }
// Validate checks that the fields of this Config have working values, // Validate checks that the fields of this Config have working values,
@ -63,6 +71,12 @@ func (cfg *Config) LoadJSON(raw []byte) error {
return err return err
} }
cfg.Default()
return cfg.applyJSONConfig(jcfg)
}
func (cfg *Config) applyJSONConfig(jcfg *jsonConfig) error {
interval, _ := time.ParseDuration(jcfg.CheckInterval) interval, _ := time.ParseDuration(jcfg.CheckInterval)
cfg.CheckInterval = interval cfg.CheckInterval = interval

View File

@ -61,8 +61,14 @@ func (cfg *MetricsConfig) Default() error {
// ApplyEnvVars fills in any Config fields found // ApplyEnvVars fills in any Config fields found
// as environment variables. // as environment variables.
func (cfg *MetricsConfig) ApplyEnvVars() error { func (cfg *MetricsConfig) ApplyEnvVars() error {
// doesn't read any config from env jcfg := &jsonMetricsConfig{}
return nil
err := envconfig.Process(envConfigKey, jcfg)
if err != nil {
return err
}
return cfg.applyJSONConfig(jcfg)
} }
// Validate checks that the fields of this Config have working values, // Validate checks that the fields of this Config have working values,
@ -91,13 +97,11 @@ func (cfg *MetricsConfig) LoadJSON(raw []byte) error {
cfg.Default() cfg.Default()
// override json config with env var return cfg.applyJSONConfig(jcfg)
err = envconfig.Process(envConfigKey, jcfg) }
if err != nil {
return err
}
err = cfg.loadMetricsOptions(jcfg) func (cfg *MetricsConfig) applyJSONConfig(jcfg *jsonMetricsConfig) error {
err := cfg.loadMetricsOptions(jcfg)
if err != nil { if err != nil {
return err return err
} }
@ -169,8 +173,14 @@ func (cfg *TracingConfig) Default() error {
// ApplyEnvVars fills in any Config fields found // ApplyEnvVars fills in any Config fields found
// as environment variables. // as environment variables.
func (cfg *TracingConfig) ApplyEnvVars() error { func (cfg *TracingConfig) ApplyEnvVars() error {
// doesn't read any config from env jcfg := &jsonTracingConfig{}
return nil
err := envconfig.Process(envConfigKey, jcfg)
if err != nil {
return err
}
return cfg.applyJSONConfig(jcfg)
} }
// Validate checks that the fields of this Config have working values, // Validate checks that the fields of this Config have working values,
@ -199,13 +209,11 @@ func (cfg *TracingConfig) LoadJSON(raw []byte) error {
cfg.Default() cfg.Default()
// override json config with env var return cfg.applyJSONConfig(jcfg)
err = envconfig.Process(envConfigKey, jcfg) }
if err != nil {
return err
}
err = cfg.loadTracingOptions(jcfg) func (cfg *TracingConfig) applyJSONConfig(jcfg *jsonTracingConfig) error {
err := cfg.loadTracingOptions(jcfg)
if err != nil { if err != nil {
return err return err
} }

View File

@ -4,10 +4,13 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"github.com/kelseyhightower/envconfig"
"github.com/ipfs/ipfs-cluster/config" "github.com/ipfs/ipfs-cluster/config"
) )
const configKey = "maptracker" const configKey = "maptracker"
const envConfigKey = "cluster_maptracker"
// Default values for this Config. // Default values for this Config.
const ( const (
@ -47,8 +50,14 @@ func (cfg *Config) Default() error {
// ApplyEnvVars fills in any Config fields found // ApplyEnvVars fills in any Config fields found
// as environment variables. // as environment variables.
func (cfg *Config) ApplyEnvVars() error { func (cfg *Config) ApplyEnvVars() error {
// doesn't read any config from env jcfg := &jsonConfig{}
return nil
err := envconfig.Process(envConfigKey, jcfg)
if err != nil {
return err
}
return cfg.applyJSONConfig(jcfg)
} }
// Validate checks that the fields of this Config have working values, // Validate checks that the fields of this Config have working values,
@ -76,6 +85,10 @@ func (cfg *Config) LoadJSON(raw []byte) error {
cfg.Default() cfg.Default()
return cfg.applyJSONConfig(jcfg)
}
func (cfg *Config) applyJSONConfig(jcfg *jsonConfig) error {
config.SetIfNotDefault(jcfg.MaxPinQueueSize, &cfg.MaxPinQueueSize) config.SetIfNotDefault(jcfg.MaxPinQueueSize, &cfg.MaxPinQueueSize)
config.SetIfNotDefault(jcfg.ConcurrentPins, &cfg.ConcurrentPins) config.SetIfNotDefault(jcfg.ConcurrentPins, &cfg.ConcurrentPins)

View File

@ -4,10 +4,13 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"github.com/kelseyhightower/envconfig"
"github.com/ipfs/ipfs-cluster/config" "github.com/ipfs/ipfs-cluster/config"
) )
const configKey = "stateless" const configKey = "stateless"
const envConfigKey = "cluster_stateless"
// Default values for this Config. // Default values for this Config.
const ( const (
@ -47,8 +50,14 @@ func (cfg *Config) Default() error {
// ApplyEnvVars fills in any Config fields found // ApplyEnvVars fills in any Config fields found
// as environment variables. // as environment variables.
func (cfg *Config) ApplyEnvVars() error { func (cfg *Config) ApplyEnvVars() error {
// doesn't read any config from env jcfg := &jsonConfig{}
return nil
err := envconfig.Process(envConfigKey, jcfg)
if err != nil {
return err
}
return cfg.applyJSONConfig(jcfg)
} }
// Validate checks that the fields of this Config have working values, // Validate checks that the fields of this Config have working values,
@ -76,6 +85,10 @@ func (cfg *Config) LoadJSON(raw []byte) error {
cfg.Default() cfg.Default()
return cfg.applyJSONConfig(jcfg)
}
func (cfg *Config) applyJSONConfig(jcfg *jsonConfig) error {
config.SetIfNotDefault(jcfg.MaxPinQueueSize, &cfg.MaxPinQueueSize) config.SetIfNotDefault(jcfg.MaxPinQueueSize, &cfg.MaxPinQueueSize)
config.SetIfNotDefault(jcfg.ConcurrentPins, &cfg.ConcurrentPins) config.SetIfNotDefault(jcfg.ConcurrentPins, &cfg.ConcurrentPins)