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.
This commit is contained in:
yyforyongyu 2024-01-31 14:58:10 +08:00
parent 7d74165296
commit 157c84cc4d
No known key found for this signature in database
GPG Key ID: 9BCD95C4FF296868
4 changed files with 45 additions and 8 deletions

View File

@ -813,6 +813,9 @@ func LoadConfig(interceptor signal.Interceptor) (*Config, error) {
ltndLog.Warnf("%v", configFileError) ltndLog.Warnf("%v", configFileError)
} }
// Finally, log warnings for deprecated config options if they are set.
logWarningsForDeprecation(*cleanCfg)
return cleanCfg, nil return cleanCfg, nil
} }
@ -2112,12 +2115,20 @@ func checkEstimateMode(estimateMode string) error {
bitcoindEstimateModes[:]) bitcoindEstimateModes[:])
} }
// configToFlatMap converts the given config struct into a flat map of key/value // configToFlatMap converts the given config struct into a flat map of
// pairs using the dot notation we are used to from the config file or command // key/value pairs using the dot notation we are used to from the config file
// line flags. // or command line flags. It also returns a map containing deprecated config
func configToFlatMap(cfg Config) (map[string]string, error) { // options.
func configToFlatMap(cfg Config) (map[string]string,
map[string]struct{}, error) {
result := make(map[string]string) 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 // redact is the helper function that redacts sensitive values like
// passwords. // passwords.
redact := func(key, value string) string { redact := func(key, value string) string {
@ -2159,6 +2170,8 @@ func configToFlatMap(cfg Config) (map[string]string, error) {
longName := fieldType.Tag.Get("long") longName := fieldType.Tag.Get("long")
namespace := fieldType.Tag.Get("namespace") namespace := fieldType.Tag.Get("namespace")
group := fieldType.Tag.Get("group") group := fieldType.Tag.Get("group")
hidden := fieldType.Tag.Get("hidden")
switch { switch {
// We have a long name defined, this is a config value. // We have a long name defined, this is a config value.
case longName != "": case longName != "":
@ -2172,6 +2185,11 @@ func configToFlatMap(cfg Config) (map[string]string, error) {
"%v", field.Interface(), "%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 // We have no long name but a namespace, this is a
// nested struct. // nested struct.
case longName == "" && namespace != "": case longName == "" && namespace != "":
@ -2202,5 +2220,18 @@ func configToFlatMap(cfg Config) (map[string]string, error) {
// Turn the whole config struct into a flat map. // Turn the whole config struct into a flat map.
printConfig(reflect.ValueOf(cfg), "") 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)
}
} }

View File

@ -24,9 +24,15 @@ func TestConfigToFlatMap(t *testing.T) {
cfg.DB.Etcd.Pass = testPassword cfg.DB.Etcd.Pass = testPassword
cfg.DB.Postgres.Dsn = 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) 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. // Pick a couple of random values to check.
require.Equal(t, DefaultLndDir, result["lnddir"]) require.Equal(t, DefaultLndDir, result["lnddir"])
require.Equal( require.Equal(

View File

@ -10,7 +10,7 @@ import (
// //
//nolint:lll //nolint:lll
type Chain struct { 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."` 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"` Node string `long:"node" description:"The blockchain interface to use." choice:"btcd" choice:"bitcoind" choice:"neutrino" choice:"nochainbackend"`

View File

@ -3054,7 +3054,7 @@ func (r *rpcServer) GetInfo(_ context.Context,
func (r *rpcServer) GetDebugInfo(_ context.Context, func (r *rpcServer) GetDebugInfo(_ context.Context,
_ *lnrpc.GetDebugInfoRequest) (*lnrpc.GetDebugInfoResponse, error) { _ *lnrpc.GetDebugInfoRequest) (*lnrpc.GetDebugInfoResponse, error) {
flatConfig, err := configToFlatMap(*r.cfg) flatConfig, _, err := configToFlatMap(*r.cfg)
if err != nil { if err != nil {
return nil, fmt.Errorf("error converting config to flat map: "+ return nil, fmt.Errorf("error converting config to flat map: "+
"%w", err) "%w", err)