Skip to content
This repository has been archived by the owner on Apr 19, 2024. It is now read-only.

Commit

Permalink
feat: SetupDaemonConfig no longer needs a file (#214)
Browse files Browse the repository at this point in the history
  • Loading branch information
miparnisari authored Feb 19, 2024
1 parent d61bf5c commit 452c5b5
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 15 deletions.
6 changes: 5 additions & 1 deletion cmd/gubernator-cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

This comment has been minimized.

Copy link
@pbennett

pbennett Mar 22, 2024

This appears to make not defining a config file a hard error. Something never previously needed and a breaking change from the last version.
Trying to upgrade from 2.3.2 to 2.4.0 in my images is a hard fail now.

This comment has been minimized.

Copy link
@Baliedge

Baliedge Mar 22, 2024

Contributor

Fair observation and was probably not intended. Could you log an issue on this at the new repo: https://github.com/gubernator-io/gubernator/

if err != nil {
return fmt.Errorf("while opening config file: %s", err)
}
conf, err := guber.SetupDaemonConfig(log, configFileReader)
if err != nil {
return err
}
Expand Down
6 changes: 5 additions & 1 deletion cmd/gubernator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
21 changes: 8 additions & 13 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,17 +263,17 @@ 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
var logFormat string
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
Expand Down Expand Up @@ -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)
}
Expand Down
40 changes: 40 additions & 0 deletions config_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 452c5b5

Please sign in to comment.