diff --git a/CHANGELOG.md b/CHANGELOG.md index ee0dca5c0ac..766d92b268e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## Unreleased - XXXX-XX-XX + - Add search locations to load lakeFS configuration. More information on https://docs.lakefs.io/reference/configuration (#2355) ## v0.48.0 - 2021-08-22 diff --git a/cmd/lakefs/cmd/root.go b/cmd/lakefs/cmd/root.go index 8edd20adda1..cab1693ab1a 100644 --- a/cmd/lakefs/cmd/root.go +++ b/cmd/lakefs/cmd/root.go @@ -1,8 +1,10 @@ package cmd import ( + "errors" "fmt" "os" + "path" "strings" "sync" @@ -50,22 +52,15 @@ func init() { func initConfig() { logger := logging.Default().WithField("phase", "startup") if cfgFile != "" { - logger.WithField("file", cfgFile).Info("configuration file") + logger.WithField("file", cfgFile).Info("Configuration file") // Use config file from the flag. viper.SetConfigFile(cfgFile) } else { - logger.Info("search for configuration file .lakefs") - // Find home directory. - home, err := homedir.Dir() - if err != nil { - fmt.Println(err) - os.Exit(1) - } - - // Search config in home directory with name ".lakefs" (without extension). - viper.AddConfigPath(home) viper.SetConfigType("yaml") - viper.SetConfigName(".lakefs") + viper.SetConfigName("config") + viper.AddConfigPath(".") + viper.AddConfigPath(path.Join(getHomeDir(), ".lakefs")) + viper.AddConfigPath("/etc/lakefs") } viper.SetEnvPrefix("LAKEFS") @@ -73,23 +68,46 @@ func initConfig() { // read in environment variables viper.AutomaticEnv() - // If a config file is found, read it in. + // read configuration file err := viper.ReadInConfig() - logger = logger.WithField("file", viper.ConfigFileUsed()) - - if err == nil { - logger.Info("loaded configuration from file") - } else if _, ok := err.(viper.ConfigFileNotFoundError); !ok { - logger.WithError(err).Fatal("failed to read config file") + logger = logger.WithField("file", viper.ConfigFileUsed()) // should be called after SetConfigFile + var errFileNotFound viper.ConfigFileNotFoundError + if err != nil && !errors.As(err, &errFileNotFound) { + logger.WithError(err).Fatal("Failed to find a config file") + } + // fallback - try to load the previous supported $HOME/.lakefs.yaml + // if err is set it will be file-not-found based on previous check + if err != nil { + fallbackCfgFile := path.Join(getHomeDir(), ".lakefs.yaml") + if cfgFile != fallbackCfgFile { + viper.SetConfigFile(fallbackCfgFile) + logger = logger.WithField("file", viper.ConfigFileUsed()) // should be called after SetConfigFile + err = viper.ReadInConfig() + if err != nil && !os.IsNotExist(err) { + logger.WithError(err).Fatal("Failed to read config file") + } + } } // setup config used by the executed command cfg, err = config.NewConfig() if err != nil { - logger.WithError(err).Fatal("load config") + logger.WithError(err).Fatal("Load config") + } else { + logger.Info("Config loaded") } err = cfg.Validate() if err != nil { - logger.WithError(err).Fatal("invalid config") + logger.WithError(err).Fatal("Invalid config") + } +} + +// getHomeDir find and return the home directory +func getHomeDir() string { + home, err := homedir.Dir() + if err != nil { + fmt.Println("Get home directory -", err) + os.Exit(1) } + return home } diff --git a/docs/deploy/docker.md b/docs/deploy/docker.md index b85fce3960b..09d55905432 100644 --- a/docs/deploy/docker.md +++ b/docs/deploy/docker.md @@ -28,7 +28,7 @@ Here is a minimal example, but you can see the [reference](../reference/configur
  • Google Cloud
  • Microsoft Azure
  • -
    +
    {% include_relative includes/aws-docker-config.md %}
    @@ -45,8 +45,8 @@ Save the configuration file locally as `lakefs-config.yaml` and run the followin docker run \ --name lakefs \ -p 8000:8000 \ - -v $(pwd)/lakefs-config.yaml:/home/lakefs/.lakefs.yaml \ - treeverse/lakefs:latest run + -v $(pwd)/lakefs-config.yaml:/etc/lakefs/config.yaml \ + treeverse/lakefs:latest run --config /etc/lakefs/config.yaml ``` ## Load balancing diff --git a/docs/reference/configuration.md b/docs/reference/configuration.md index f63e538b81f..c4549d1cff2 100644 --- a/docs/reference/configuration.md +++ b/docs/reference/configuration.md @@ -12,7 +12,16 @@ has_children: false {% include toc.html %} -Configuring lakeFS is done using a yaml configuration file. +Configuring lakeFS is done using a yaml configuration file and/or environment variable. +The configuration file location can be set with the '--config' flag. If not specified, the the first file found in the following order will be used: +1. ./config.yaml +1. `$HOME`/lakefs/config.yaml +1. /etc/lakefs/config.yaml +1. `$HOME`/.lakefs.yaml + +Configuration items can each be controlled by an environment variable. The variable name will have a prefix of *LAKEFS_*, followed by the name of the configuration, replacing every '.' with a '_'. +Example: `LAKEFS_LOGGING_LEVEL` controls `logging.format`. + This reference uses `.` to denote the nesting of values. ## Reference