From 99f28e601c98c5e6877e0cdd0a661b8868c4ca46 Mon Sep 17 00:00:00 2001 From: "A.Unger" Date: Fri, 5 Nov 2021 14:10:25 +0100 Subject: [PATCH] migrate store to the new config scheme --- ocis-pkg/config/config.go | 4 +- ocis/pkg/command/store.go | 2 - store/cmd/store/main.go | 2 +- store/pkg/command/root.go | 50 ++++------------------ store/pkg/command/server.go | 10 ++--- store/pkg/config/config.go | 83 +++++++++++++++++++++++++++++++++++- store/pkg/config/env.go | 80 ++++++++++++++++++++++++++++++++++ store/pkg/flagset/flagset.go | 31 -------------- 8 files changed, 178 insertions(+), 84 deletions(-) create mode 100644 store/pkg/config/env.go diff --git a/ocis-pkg/config/config.go b/ocis-pkg/config/config.go index b379d6a2e..da41c2b77 100644 --- a/ocis-pkg/config/config.go +++ b/ocis-pkg/config/config.go @@ -123,10 +123,10 @@ func New() *Config { OCS: ocs.DefaultConfig(), Settings: settings.DefaultConfig(), Web: web.DefaultConfig(), - Storage: storage.New(), - Store: store.New(), + Store: store.DefaultConfig(), Thumbnails: thumbnails.DefaultConfig(), WebDAV: webdav.DefaultConfig(), + Storage: storage.New(), } } diff --git a/ocis/pkg/command/store.go b/ocis/pkg/command/store.go index 74d2a84fe..b37d10a76 100644 --- a/ocis/pkg/command/store.go +++ b/ocis/pkg/command/store.go @@ -9,7 +9,6 @@ import ( "github.com/owncloud/ocis/ocis/pkg/register" "github.com/owncloud/ocis/store/pkg/command" svcconfig "github.com/owncloud/ocis/store/pkg/config" - "github.com/owncloud/ocis/store/pkg/flagset" "github.com/urfave/cli/v2" ) @@ -19,7 +18,6 @@ func StoreCommand(cfg *config.Config) *cli.Command { Name: "store", Usage: "Start a go-micro store", Category: "Extensions", - Flags: flagset.ServerWithConfig(cfg.Store), Subcommands: []*cli.Command{ command.PrintVersion(cfg.Store), }, diff --git a/store/cmd/store/main.go b/store/cmd/store/main.go index ad2549012..f20f59258 100644 --- a/store/cmd/store/main.go +++ b/store/cmd/store/main.go @@ -8,7 +8,7 @@ import ( ) func main() { - if err := command.Execute(config.New()); err != nil { + if err := command.Execute(config.DefaultConfig()); err != nil { os.Exit(1) } } diff --git a/store/pkg/command/root.go b/store/pkg/command/root.go index b16eae65f..882607891 100644 --- a/store/pkg/command/root.go +++ b/store/pkg/command/root.go @@ -3,15 +3,11 @@ package command import ( "context" "os" - "strings" - - "github.com/owncloud/ocis/ocis-pkg/sync" ociscfg "github.com/owncloud/ocis/ocis-pkg/config" "github.com/owncloud/ocis/ocis-pkg/log" "github.com/owncloud/ocis/ocis-pkg/version" "github.com/owncloud/ocis/store/pkg/config" - "github.com/spf13/viper" "github.com/thejerf/suture/v4" "github.com/urfave/cli/v2" ) @@ -31,8 +27,6 @@ func Execute(cfg *config.Config) error { }, }, - //Flags: flagset.RootWithConfig(cfg), - Before: func(c *cli.Context) error { cfg.Service.Version = version.String return ParseConfig(c, cfg) @@ -69,46 +63,18 @@ func NewLogger(cfg *config.Config) log.Logger { ) } -// ParseConfig loads store configuration from Viper known paths. +// ParseConfig loads idp configuration from known paths. func ParseConfig(c *cli.Context, cfg *config.Config) error { - sync.ParsingViperConfig.Lock() - defer sync.ParsingViperConfig.Unlock() - logger := NewLogger(cfg) - - viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) - viper.SetEnvPrefix("STORE") - viper.AutomaticEnv() - - if c.IsSet("config-file") { - viper.SetConfigFile(c.String("config-file")) - } else { - viper.SetConfigName("store") - - viper.AddConfigPath("/etc/ocis") - viper.AddConfigPath("$HOME/.ocis") - viper.AddConfigPath("./config") + conf, err := ociscfg.BindSourcesToStructs("store", cfg) + if err != nil { + return err } - if err := viper.ReadInConfig(); err != nil { - switch err.(type) { - case viper.ConfigFileNotFoundError: - logger.Debug(). - Msg("no config found on preconfigured location") - case viper.UnsupportedConfigError: - logger.Fatal(). - Err(err). - Msg("Unsupported config type") - default: - logger.Fatal(). - Err(err). - Msg("Failed to read config") - } - } + // load all env variables relevant to the config in the current context. + conf.LoadOSEnv(config.GetEnv(), false) - if err := viper.Unmarshal(&cfg); err != nil { - logger.Fatal(). - Err(err). - Msg("Failed to parse config") + if err = cfg.UnmapEnv(conf); err != nil { + return err } return nil diff --git a/store/pkg/command/server.go b/store/pkg/command/server.go index 1fec4e195..311ceb453 100644 --- a/store/pkg/command/server.go +++ b/store/pkg/command/server.go @@ -23,12 +23,12 @@ func Server(cfg *config.Config) *cli.Command { Usage: "Start integrated server", Flags: flagset.ServerWithConfig(cfg), Before: func(ctx *cli.Context) error { - logger := NewLogger(cfg) - // When running on single binary mode the before hook from the root command won't get called. We manually - // call this before hook from ocis command, so the configuration can be loaded. - if !cfg.Supervised { - return ParseConfig(ctx, cfg) + + if err := ParseConfig(ctx, cfg); err != nil { + return err } + + logger := NewLogger(cfg) logger.Debug().Str("service", "store").Msg("ignoring config file parsing when running supervised") return nil }, diff --git a/store/pkg/config/config.go b/store/pkg/config/config.go index ce9c85252..eabf1d058 100644 --- a/store/pkg/config/config.go +++ b/store/pkg/config/config.go @@ -1,6 +1,14 @@ package config -import "context" +import ( + "context" + "fmt" + "path" + "reflect" + + gofig "github.com/gookit/config/v2" + "github.com/owncloud/ocis/ocis-pkg/config/defaults" +) // Log defines the available logging configuration. type Log struct { @@ -58,3 +66,76 @@ type Config struct { func New() *Config { return &Config{} } + +func DefaultConfig() *Config { + return &Config{ + Log: Log{}, + Debug: Debug{ + Addr: "127.0.0.1:9464", + Token: "", + Pprof: false, + Zpages: false, + }, + GRPC: GRPC{ + Addr: "127.0.0.1:9460", + }, + Tracing: Tracing{ + Enabled: false, + Type: "jaeger", + Endpoint: "", + Collector: "", + Service: "store", + }, + Datapath: path.Join(defaults.BaseDataPath(), "store"), + Service: Service{ + Name: "store", + Namespace: "com.owncloud.api", + }, + } +} + +// GetEnv fetches a list of known env variables for this extension. It is to be used by gookit, as it provides a list +// with all the environment variables an extension supports. +func GetEnv() []string { + var r = make([]string, len(structMappings(&Config{}))) + for i := range structMappings(&Config{}) { + r = append(r, structMappings(&Config{})[i].EnvVars...) + } + + return r +} + +// UnmapEnv loads values from the gooconf.Config argument and sets them in the expected destination. +func (c *Config) UnmapEnv(gooconf *gofig.Config) error { + vals := structMappings(c) + for i := range vals { + for j := range vals[i].EnvVars { + // we need to guard against v != "" because this is the condition that checks that the value is set from the environment. + // the `ok` guard is not enough, apparently. + if v, ok := gooconf.GetValue(vals[i].EnvVars[j]); ok && v != "" { + + // get the destination type from destination + switch reflect.ValueOf(vals[i].Destination).Type().String() { + case "*bool": + r := gooconf.Bool(vals[i].EnvVars[j]) + *vals[i].Destination.(*bool) = r + case "*string": + r := gooconf.String(vals[i].EnvVars[j]) + *vals[i].Destination.(*string) = r + case "*int": + r := gooconf.Int(vals[i].EnvVars[j]) + *vals[i].Destination.(*int) = r + case "*float64": + // defaults to float64 + r := gooconf.Float(vals[i].EnvVars[j]) + *vals[i].Destination.(*float64) = r + default: + // it is unlikely we will ever get here. Let this serve more as a runtime check for when debugging. + return fmt.Errorf("invalid type for env var: `%v`", vals[i].EnvVars[j]) + } + } + } + } + + return nil +} diff --git a/store/pkg/config/env.go b/store/pkg/config/env.go new file mode 100644 index 000000000..42e09aedc --- /dev/null +++ b/store/pkg/config/env.go @@ -0,0 +1,80 @@ +package config + +type mapping struct { + EnvVars []string // name of the EnvVars var. + Destination interface{} // memory address of the original config value to modify. +} + +// structMappings binds a set of environment variables to a destination on cfg. +func structMappings(cfg *Config) []mapping { + return []mapping{ + { + EnvVars: []string{"STORE_LOG_LEVEL", "OCIS_LOG_LEVEL"}, + Destination: &cfg.Log.Level, + }, + { + EnvVars: []string{"STORE_LOG_PRETTY", "OCIS_LOG_PRETTY"}, + Destination: &cfg.Log.Pretty, + }, + { + EnvVars: []string{"STORE_LOG_COLOR", "OCIS_LOG_COLOR"}, + Destination: &cfg.Log.Color, + }, + { + EnvVars: []string{"STORE_LOG_FILE", "OCIS_LOG_FILE"}, + Destination: &cfg.Log.File, + }, + { + EnvVars: []string{"STORE_TRACING_ENABLED", "OCIS_TRACING_ENABLED"}, + Destination: &cfg.Tracing.Enabled, + }, + { + EnvVars: []string{"STORE_TRACING_TYPE", "OCIS_TRACING_TYPE"}, + Destination: &cfg.Tracing.Type, + }, + { + EnvVars: []string{"STORE_TRACING_ENDPOINT", "OCIS_TRACING_ENDPOINT"}, + Destination: &cfg.Tracing.Endpoint, + }, + { + EnvVars: []string{"STORE_TRACING_COLLECTOR", "OCIS_TRACING_COLLECTOR"}, + Destination: &cfg.Tracing.Collector, + }, + { + EnvVars: []string{"STORE_TRACING_SERVICE"}, + Destination: &cfg.Tracing.Service, + }, + { + EnvVars: []string{"STORE_DEBUG_ADDR"}, + Destination: &cfg.Debug.Addr, + }, + { + EnvVars: []string{"STORE_DEBUG_TOKEN"}, + Destination: &cfg.Debug.Token, + }, + { + EnvVars: []string{"STORE_DEBUG_PPROF"}, + Destination: &cfg.Debug.Pprof, + }, + { + EnvVars: []string{"STORE_DEBUG_ZPAGES"}, + Destination: &cfg.Debug.Zpages, + }, + { + EnvVars: []string{"STORE_GRPC_NAMESPACE"}, + Destination: &cfg.Service.Namespace, + }, + { + EnvVars: []string{"STORE_GRPC_ADDR"}, + Destination: &cfg.GRPC.Addr, + }, + { + EnvVars: []string{"STORE_NAME"}, + Destination: &cfg.Service.Name, + }, + { + EnvVars: []string{"STORE_DATA_PATH"}, + Destination: &cfg.Datapath, + }, + } +} diff --git a/store/pkg/flagset/flagset.go b/store/pkg/flagset/flagset.go index 2651bbf98..1f9e5e7cd 100644 --- a/store/pkg/flagset/flagset.go +++ b/store/pkg/flagset/flagset.go @@ -9,37 +9,6 @@ import ( "github.com/urfave/cli/v2" ) -// RootWithConfig applies cfg to the root flagset -func RootWithConfig(cfg *config.Config) []cli.Flag { - return []cli.Flag{ - &cli.StringFlag{ - Name: "config-file", - Value: "", - Usage: "Path to config file", - EnvVars: []string{"STORE_CONFIG_FILE"}, - Destination: &cfg.File, - }, - &cli.StringFlag{ - Name: "log-level", - Usage: "Set logging level", - EnvVars: []string{"STORE_LOG_LEVEL", "OCIS_LOG_LEVEL"}, - Destination: &cfg.Log.Level, - }, - &cli.BoolFlag{ - Name: "log-pretty", - Usage: "Enable pretty logging", - EnvVars: []string{"STORE_LOG_PRETTY", "OCIS_LOG_PRETTY"}, - Destination: &cfg.Log.Pretty, - }, - &cli.BoolFlag{ - Name: "log-color", - Usage: "Enable colored logging", - EnvVars: []string{"STORE_LOG_COLOR", "OCIS_LOG_COLOR"}, - Destination: &cfg.Log.Color, - }, - } -} - // HealthWithConfig applies cfg to the root flagset func HealthWithConfig(cfg *config.Config) []cli.Flag { return []cli.Flag{