From 157c84cc4de28f02341c09ae2a754d8b197b6994 Mon Sep 17 00:00:00 2001 From: yyforyongyu Date: Wed, 31 Jan 2024 14:58:10 +0800 Subject: [PATCH] multi: log warnings if deprecated config options are used This commit adds warning logs if the user uses deprecated config options, paving the way for future removal. --- config.go | 41 ++++++++++++++++++++++++++++++++++++----- config_test.go | 8 +++++++- lncfg/chain.go | 2 +- rpcserver.go | 2 +- 4 files changed, 45 insertions(+), 8 deletions(-) diff --git a/config.go b/config.go index e89f37bae..d510b7cf5 100644 --- a/config.go +++ b/config.go @@ -813,6 +813,9 @@ func LoadConfig(interceptor signal.Interceptor) (*Config, error) { ltndLog.Warnf("%v", configFileError) } + // Finally, log warnings for deprecated config options if they are set. + logWarningsForDeprecation(*cleanCfg) + return cleanCfg, nil } @@ -2112,12 +2115,20 @@ func checkEstimateMode(estimateMode string) error { bitcoindEstimateModes[:]) } -// configToFlatMap converts the given config struct into a flat map of key/value -// pairs using the dot notation we are used to from the config file or command -// line flags. -func configToFlatMap(cfg Config) (map[string]string, error) { +// configToFlatMap converts the given config struct into a flat map of +// key/value pairs using the dot notation we are used to from the config file +// or command line flags. It also returns a map containing deprecated config +// options. +func configToFlatMap(cfg Config) (map[string]string, + map[string]struct{}, error) { + result := make(map[string]string) + // deprecated stores a map of deprecated options found in the config + // that are set by the users. A config option is considered as + // deprecated if it has a `hidden` flag. + deprecated := make(map[string]struct{}) + // redact is the helper function that redacts sensitive values like // passwords. redact := func(key, value string) string { @@ -2159,6 +2170,8 @@ func configToFlatMap(cfg Config) (map[string]string, error) { longName := fieldType.Tag.Get("long") namespace := fieldType.Tag.Get("namespace") group := fieldType.Tag.Get("group") + hidden := fieldType.Tag.Get("hidden") + switch { // We have a long name defined, this is a config value. case longName != "": @@ -2172,6 +2185,11 @@ func configToFlatMap(cfg Config) (map[string]string, error) { "%v", field.Interface(), )) + // If there's a hidden flag, it's deprecated. + if hidden == "true" && !field.IsZero() { + deprecated[key] = struct{}{} + } + // We have no long name but a namespace, this is a // nested struct. case longName == "" && namespace != "": @@ -2202,5 +2220,18 @@ func configToFlatMap(cfg Config) (map[string]string, error) { // Turn the whole config struct into a flat map. printConfig(reflect.ValueOf(cfg), "") - return result, nil + return result, deprecated, nil +} + +// logWarningsForDeprecation logs a warning if a deprecated config option is +// set. +func logWarningsForDeprecation(cfg Config) { + _, deprecated, err := configToFlatMap(cfg) + if err != nil { + ltndLog.Errorf("Convert configs to map: %v", err) + } + + for k := range deprecated { + ltndLog.Warnf("Config '%s' is deprecated, please remove it", k) + } } diff --git a/config_test.go b/config_test.go index 1f1740a0d..0c82db28d 100644 --- a/config_test.go +++ b/config_test.go @@ -24,9 +24,15 @@ func TestConfigToFlatMap(t *testing.T) { cfg.DB.Etcd.Pass = testPassword cfg.DB.Postgres.Dsn = testPassword - result, err := configToFlatMap(cfg) + // Set a deprecated field. + cfg.Bitcoin.Active = true + + result, deprecated, err := configToFlatMap(cfg) require.NoError(t, err) + // Check that the deprecated option has been parsed out. + require.Contains(t, deprecated, "bitcoin.active") + // Pick a couple of random values to check. require.Equal(t, DefaultLndDir, result["lnddir"]) require.Equal( diff --git a/lncfg/chain.go b/lncfg/chain.go index 645236336..e84cadb7b 100644 --- a/lncfg/chain.go +++ b/lncfg/chain.go @@ -10,7 +10,7 @@ import ( // //nolint:lll type Chain struct { - Active bool `long:"active" description:"DEPRECATED: If the chain should be active or not. This field is now ignored since only the Bitcoin chain is supported"` + Active bool `long:"active" description:"DEPRECATED: If the chain should be active or not. This field is now ignored since only the Bitcoin chain is supported" hidden:"true"` ChainDir string `long:"chaindir" description:"The directory to store the chain's data within."` Node string `long:"node" description:"The blockchain interface to use." choice:"btcd" choice:"bitcoind" choice:"neutrino" choice:"nochainbackend"` diff --git a/rpcserver.go b/rpcserver.go index acce910df..25140c4d0 100644 --- a/rpcserver.go +++ b/rpcserver.go @@ -3054,7 +3054,7 @@ func (r *rpcServer) GetInfo(_ context.Context, func (r *rpcServer) GetDebugInfo(_ context.Context, _ *lnrpc.GetDebugInfoRequest) (*lnrpc.GetDebugInfoResponse, error) { - flatConfig, err := configToFlatMap(*r.cfg) + flatConfig, _, err := configToFlatMap(*r.cfg) if err != nil { return nil, fmt.Errorf("error converting config to flat map: "+ "%w", err)