Skip to content

Commit

Permalink
fixup! feature(config): add sockaddr template support
Browse files Browse the repository at this point in the history
  • Loading branch information
Hugo Vieira committed Nov 18, 2021
1 parent 174f144 commit 1459fb1
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 16 deletions.
36 changes: 36 additions & 0 deletions internal/cmd/base/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,24 @@ func TestSetupControllerPublicClusterAddress(t *testing.T) {
expErrStr: "",
expPublicClusterAddress: "127.0.0.1:8080",
},
{
name: "setting public cluster address to env var that points to template",
inputConfig: &config.Config{
SharedConfig: &configutil.SharedConfig{
Listeners: []*listenerutil.ListenerConfig{},
},
Controller: &config.Controller{
PublicClusterAddr: "env://TEST_ENV_VAR_FOR_CONTROLLER_ADDR",
},
},
inputFlagValue: "",
stateFn: func(t *testing.T) {
t.Setenv("TEST_ENV_VAR_FOR_CONTROLLER_ADDR", `{{ GetAllInterfaces | include "flags" "loopback" | include "type" "IPV4" | join "address" " " }}`)
},
expErr: false,
expErrStr: "",
expPublicClusterAddress: "127.0.0.1:9201",
},
{
name: "setting public cluster address to ip template",
inputConfig: &config.Config{
Expand Down Expand Up @@ -577,6 +595,24 @@ func TestSetupWorkerPublicAddress(t *testing.T) {
expErrStr: "",
expPublicAddress: "127.0.0.1:8080",
},
{
name: "setting public address to env var that points to template",
inputConfig: &config.Config{
SharedConfig: &configutil.SharedConfig{
Listeners: []*listenerutil.ListenerConfig{},
},
Worker: &config.Worker{
PublicAddr: "env://TEST_ENV_VAR_FOR_WORKER_ADDR",
},
},
inputFlagValue: "",
stateFn: func(t *testing.T) {
t.Setenv("TEST_ENV_VAR_FOR_WORKER_ADDR", `{{ GetAllInterfaces | include "flags" "loopback" | include "type" "IPV4" | join "address" " " }}`)
},
expErr: false,
expErrStr: "",
expPublicAddress: "127.0.0.1:9202",
},
{
name: "setting public address to ip template",
inputConfig: &config.Config{
Expand Down
26 changes: 10 additions & 16 deletions internal/cmd/base/servers.go
Original file line number Diff line number Diff line change
Expand Up @@ -636,18 +636,17 @@ func (b *Server) SetupControllerPublicClusterAddress(conf *config.Config, flagVa
}
}
}
} else if looksLikeTemplate(conf.Controller.PublicClusterAddr) {
var err error
conf.Controller.PublicClusterAddr, err = listenerutil.ParseSingleIPTemplate(conf.Controller.PublicClusterAddr)
if err != nil {
return fmt.Errorf("Error parsing IP template on controller public cluster addr: %w", err)
}
} else {
var err error
conf.Controller.PublicClusterAddr, err = parseutil.ParsePath(conf.Controller.PublicClusterAddr)
if err != nil && !errors.Is(err, parseutil.ErrNotAUrl) {
return fmt.Errorf("Error parsing public cluster addr: %w", err)
}

conf.Controller.PublicClusterAddr, err = listenerutil.ParseSingleIPTemplate(conf.Controller.PublicClusterAddr)
if err != nil {
return fmt.Errorf("Error parsing IP template on controller public cluster addr: %w", err)
}
}

host, port, err := net.SplitHostPort(conf.Controller.PublicClusterAddr)
Expand Down Expand Up @@ -680,18 +679,17 @@ func (b *Server) SetupWorkerPublicAddress(conf *config.Config, flagValue string)
}
}
}
} else if looksLikeTemplate(conf.Worker.PublicAddr) {
var err error
conf.Worker.PublicAddr, err = listenerutil.ParseSingleIPTemplate(conf.Worker.PublicAddr)
if err != nil {
return fmt.Errorf("Error parsing IP template on worker public addr: %w", err)
}
} else {
var err error
conf.Worker.PublicAddr, err = parseutil.ParsePath(conf.Worker.PublicAddr)
if err != nil && !errors.Is(err, parseutil.ErrNotAUrl) {
return fmt.Errorf("Error parsing public addr: %w", err)
}

conf.Worker.PublicAddr, err = listenerutil.ParseSingleIPTemplate(conf.Worker.PublicAddr)
if err != nil {
return fmt.Errorf("Error parsing IP template on worker public addr: %w", err)
}
}

host, port, err := net.SplitHostPort(conf.Worker.PublicAddr)
Expand All @@ -707,10 +705,6 @@ func (b *Server) SetupWorkerPublicAddress(conf *config.Config, flagValue string)
return nil
}

func looksLikeTemplate(s string) bool {
return strings.HasPrefix(s, "{{") && strings.HasSuffix(s, "}}")
}

// MakeSighupCh returns a channel that can be used for SIGHUP
// reloading. This channel will send a message for every
// SIGHUP received.
Expand Down

0 comments on commit 1459fb1

Please sign in to comment.