From 452c5b5e102c740f2f404007a343f1811d713637 Mon Sep 17 00:00:00 2001 From: Maria Ines Parnisari Date: Mon, 19 Feb 2024 17:55:55 -0300 Subject: [PATCH] feat: SetupDaemonConfig no longer needs a file (#214) --- cmd/gubernator-cli/main.go | 6 +++++- cmd/gubernator/main.go | 6 +++++- config.go | 21 ++++++++------------ config_test.go | 40 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 58 insertions(+), 15 deletions(-) create mode 100644 config_test.go diff --git a/cmd/gubernator-cli/main.go b/cmd/gubernator-cli/main.go index 4e0a96b2..f75753bd 100644 --- a/cmd/gubernator-cli/main.go +++ b/cmd/gubernator-cli/main.go @@ -89,7 +89,11 @@ func main() { cmdLine := strings.Join(os.Args[1:], " ") logrus.WithContext(ctx).Info("Command line: " + cmdLine) - conf, err := guber.SetupDaemonConfig(log, configFile) + configFileReader, err := os.Open(configFile) + if err != nil { + return fmt.Errorf("while opening config file: %s", err) + } + conf, err := guber.SetupDaemonConfig(log, configFileReader) if err != nil { return err } diff --git a/cmd/gubernator/main.go b/cmd/gubernator/main.go index 2d3d6fe8..d556ba88 100644 --- a/cmd/gubernator/main.go +++ b/cmd/gubernator/main.go @@ -74,7 +74,11 @@ func main() { } // Read our config from the environment or optional environment config file - conf, err := gubernator.SetupDaemonConfig(logrus.StandardLogger(), configFile) + configFileReader, err := os.Open(configFile) + if err != nil { + log.WithError(err).Fatal("while opening config file") + } + conf, err := gubernator.SetupDaemonConfig(logrus.StandardLogger(), configFileReader) checkErr(err, "while getting config") ctx, cancel := context.WithTimeout(ctx, clock.Second*10) diff --git a/config.go b/config.go index 14c9b592..122ffa22 100644 --- a/config.go +++ b/config.go @@ -263,9 +263,9 @@ func (d *DaemonConfig) ServerTLS() *tls.Config { return nil } -// SetupDaemonConfig returns a DaemonConfig object as configured by reading the provided config file -// and environment. -func SetupDaemonConfig(logger *logrus.Logger, configFile string) (DaemonConfig, error) { +// SetupDaemonConfig returns a DaemonConfig object that is the result of merging the lines +// in the provided configFile and the environment variables. See `example.conf` for all available config options and their descriptions. +func SetupDaemonConfig(logger *logrus.Logger, configFile io.Reader) (DaemonConfig, error) { log := logrus.NewEntry(logger) var conf DaemonConfig var logLevel string @@ -273,7 +273,7 @@ func SetupDaemonConfig(logger *logrus.Logger, configFile string) (DaemonConfig, var advAddr, advPort string var err error - if configFile != "" { + if configFile != nil { log.Infof("Loading env config: %s", configFile) if err := fromEnvFile(log, configFile); err != nil { return conf, err @@ -628,15 +628,10 @@ func getEnvSlice(name string) []string { return strings.Split(v, ",") } -// Take values from a file in the format `GUBER_CONF_ITEM=my-value` and put them into the environment -// lines that begin with `#` are ignored -func fromEnvFile(log logrus.FieldLogger, configFile string) error { - fd, err := os.Open(configFile) - if err != nil { - return fmt.Errorf("while opening config file: %s", err) - } - - contents, err := io.ReadAll(fd) +// Take values from a file in the format `GUBER_CONF_ITEM=my-value` and sets them as environment variables. +// Lines that begin with `#` are ignored +func fromEnvFile(log logrus.FieldLogger, configFile io.Reader) error { + contents, err := io.ReadAll(configFile) if err != nil { return fmt.Errorf("while reading config file '%s': %s", configFile, err) } diff --git a/config_test.go b/config_test.go new file mode 100644 index 00000000..290b329f --- /dev/null +++ b/config_test.go @@ -0,0 +1,40 @@ +package gubernator + +import ( + "fmt" + "os" + "strings" + "testing" + + "github.com/sirupsen/logrus" + "github.com/stretchr/testify/require" +) + +func TestParsesGrpcAddress(t *testing.T) { + os.Clearenv() + s := ` +# a comment +GUBER_GRPC_ADDRESS=10.10.10.10:9000` + daemonConfig, err := SetupDaemonConfig(logrus.StandardLogger(), strings.NewReader(s)) + require.NoError(t, err) + require.Equal(t, "10.10.10.10:9000", daemonConfig.GRPCListenAddress) + require.NotEmpty(t, daemonConfig.InstanceID) +} + +func TestDefaultGrpcAddress(t *testing.T) { + os.Clearenv() + s := ` +# a comment` + daemonConfig, err := SetupDaemonConfig(logrus.StandardLogger(), strings.NewReader(s)) + require.NoError(t, err) + require.Equal(t, fmt.Sprintf("%s:81", LocalHost()), daemonConfig.GRPCListenAddress) + require.NotEmpty(t, daemonConfig.InstanceID) +} + +func TestDefaultInstanceId(t *testing.T) { + os.Clearenv() + s := `` + daemonConfig, err := SetupDaemonConfig(logrus.StandardLogger(), strings.NewReader(s)) + require.NoError(t, err) + require.NotEmpty(t, daemonConfig.InstanceID) +}