Skip to content

Commit

Permalink
Modularize config factory
Browse files Browse the repository at this point in the history
  • Loading branch information
N-o-Z committed Jan 15, 2025
1 parent 06312c7 commit 4b3f9e4
Show file tree
Hide file tree
Showing 15 changed files with 793 additions and 102 deletions.
6 changes: 4 additions & 2 deletions cmd/lakefs-loadtest/cmd/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
nanoid "github.com/matoous/go-nanoid/v2"
"github.com/schollz/progressbar/v3"
"github.com/spf13/cobra"
configfactory "github.com/treeverse/lakefs/modules/config/factory"
"github.com/treeverse/lakefs/pkg/catalog"
"github.com/treeverse/lakefs/pkg/config"
"github.com/treeverse/lakefs/pkg/kv"
"github.com/treeverse/lakefs/pkg/kv/kvparams"
"github.com/treeverse/lakefs/pkg/logging"
Expand Down Expand Up @@ -50,10 +50,12 @@ var entryCmd = &cobra.Command{

ctx := cmd.Context()

conf, err := config.NewConfig("")
confInterface, err := configfactory.BuildConfig("")
if err != nil {
fmt.Printf("config: %s\n", err)
}
conf := confInterface.BaseConfig()

err = conf.Validate()
if err != nil {
fmt.Printf("invalid config: %s\n", err)
Expand Down
2 changes: 1 addition & 1 deletion cmd/lakefs/cmd/flare.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var flareCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
flare.SetBaselinePermissions(flare.FlareUmask)
now := strings.ReplaceAll(time.Now().String(), " ", "")
cfg := loadConfig()
cfg := loadConfig().BaseConfig()
envVarBlacklist := addAppEnvVarPrefix(config.GetSecureStringKeyPaths(cfg))
flr, err := flare.NewFlare(flare.WithEnvVarBlacklist(envVarBlacklist))
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions cmd/lakefs/cmd/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var kvGetCmd = &cobra.Command{
Short: "Return the value for the given path under the given partition",
Args: cobra.ExactArgs(GetCmdNumArgs),
RunE: func(cmd *cobra.Command, args []string) error {
cfg := loadConfig()
cfg := loadConfig().BaseConfig()

pretty, err := cmd.Flags().GetBool("pretty")
if err != nil {
Expand Down Expand Up @@ -83,7 +83,7 @@ var kvScanCmd = &cobra.Command{
Short: "Scan through keys and values under the given partition. An optional path can be specified as a starting point (inclusive)",
Args: cobra.RangeArgs(ScanCmdMinArgs, ScanCmdMaxArgs),
RunE: func(cmd *cobra.Command, args []string) error {
cfg := loadConfig()
cfg := loadConfig().BaseConfig()

limit, err := cmd.Flags().GetInt("limit")
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions cmd/lakefs/cmd/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var versionCmd = &cobra.Command{
Use: "version",
Short: "Print current migration version and available version",
Run: func(cmd *cobra.Command, args []string) {
cfg := loadConfig()
cfg := loadConfig().BaseConfig()
kvParams, err := kvparams.NewConfig(&cfg.Database)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "KV params: %s\n", err)
Expand Down Expand Up @@ -63,7 +63,7 @@ var upCmd = &cobra.Command{
Use: "up",
Short: "Apply all up migrations",
Run: func(cmd *cobra.Command, args []string) {
cfg := loadConfig()
cfg := loadConfig().BaseConfig()
kvParams, err := kvparams.NewConfig(&cfg.Database)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "KV params: %s\n", err)
Expand Down Expand Up @@ -136,7 +136,7 @@ var gotoCmd = &cobra.Command{
Use: "goto",
Short: "Migrate to version V.",
Run: func(cmd *cobra.Command, args []string) {
cfg := loadConfig()
cfg := loadConfig().BaseConfig()
kvParams, err := kvparams.NewConfig(&cfg.Database)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "KV params: %s\n", err)
Expand Down
13 changes: 8 additions & 5 deletions cmd/lakefs/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/spf13/viper"
configfactory "github.com/treeverse/lakefs/modules/config/factory"
"github.com/treeverse/lakefs/pkg/block"
"github.com/treeverse/lakefs/pkg/config"
"github.com/treeverse/lakefs/pkg/kv/local"
Expand Down Expand Up @@ -52,6 +53,8 @@ func init() {
rootCmd.PersistentFlags().Bool(config.QuickstartConfiguration, false, "Use lakeFS quickstart configuration")
}

// TODO (niro): All this validation logic should be in the OSS config package

func validateQuickstartEnv(cfg *config.Config) {
if (cfg.Database.Type != local.DriverName && cfg.Database.Type != mem.DriverName) || cfg.Blockstore.Type != block.BlockstoreTypeLocal {
_, _ = fmt.Fprint(os.Stderr, "\nFATAL: quickstart mode can only run with local settings\n")
Expand All @@ -78,25 +81,25 @@ func useConfig(flagName string) bool {
return res
}

func newConfig() (*config.Config, error) {
func newConfig() (config.Interface, error) {
name := ""
configurations := []string{config.QuickstartConfiguration, config.UseLocalConfiguration}
if idx := slices.IndexFunc(configurations, useConfig); idx != -1 {
name = configurations[idx]
}

cfg, err := config.NewConfig(name)
cfg, err := configfactory.BuildConfig(name)
if err != nil {
return nil, err
}

if name == config.QuickstartConfiguration {
validateQuickstartEnv(cfg)
validateQuickstartEnv(cfg.BaseConfig())
}
return cfg, nil
return cfg.BaseConfig(), nil
}

func loadConfig() *config.Config {
func loadConfig() config.Interface {
initOnce.Do(initConfig)
cfg, err := newConfig()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/lakefs/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ var runCmd = &cobra.Command{
Short: "Run lakeFS",
Run: func(cmd *cobra.Command, args []string) {
logger := logging.ContextUnavailable()
cfg := loadConfig()
cfg := loadConfig().BaseConfig()
viper.WatchConfig()
viper.OnConfigChange(func(in fsnotify.Event) {
var c config.Config
Expand Down
2 changes: 1 addition & 1 deletion cmd/lakefs/cmd/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var setupCmd = &cobra.Command{
Aliases: []string{"init"},
Short: "Setup a new lakeFS instance with initial credentials",
Run: func(cmd *cobra.Command, args []string) {
cfg := loadConfig()
cfg := loadConfig().BaseConfig()

ctx := cmd.Context()
kvParams, err := kvparams.NewConfig(&cfg.Database)
Expand Down
2 changes: 1 addition & 1 deletion cmd/lakefs/cmd/superuser.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ To do that provide the user name as well as the access key ID to import.
If the wrong user or credentials were chosen it is possible to delete the user and perform the action again.
`,
Run: func(cmd *cobra.Command, args []string) {
cfg := loadConfig()
cfg := loadConfig().BaseConfig()
if cfg.Auth.UIConfig.RBAC == config.AuthRBACExternal {
fmt.Printf("Can't create additional admin while using external auth API - auth.api.endpoint is configured.\n")
os.Exit(1)
Expand Down
1 change: 1 addition & 0 deletions go.work
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ go 1.23
use (
.
./webui
./modules/config/factory
)
25 changes: 25 additions & 0 deletions modules/config/factory/build.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package factory

import (
"github.com/treeverse/lakefs/pkg/config"
)

func BuildConfig(cfgType string) (config.Interface, error) {
return newConfig(cfgType)
}

func newConfig(cfgType string) (*config.Config, error) {
c := &config.Config{}
c, err := config.NewConfig(cfgType, c)
if err != nil {
return nil, err
}

// OSS specific validation
err = c.ValidateDomainNames()
if err != nil {
return nil, err
}

return c, nil
}
3 changes: 3 additions & 0 deletions modules/config/factory/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/treeverse/lakefs/modules/config/factory

go 1.23
6 changes: 6 additions & 0 deletions modules/config/factory/go.work
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
go 1.23

use (
.
../../..
)
Loading

0 comments on commit 4b3f9e4

Please sign in to comment.