Skip to content

Commit

Permalink
make it possible to access config server over a different network int…
Browse files Browse the repository at this point in the history
…erface so we can make assertions with docker services
  • Loading branch information
atoulme committed Jan 10, 2025
1 parent 9badcfe commit 96ed032
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 24 deletions.
27 changes: 19 additions & 8 deletions internal/configconverter/config_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,13 @@ import (
)

const (
configServerEnabledEnvVar = "SPLUNK_DEBUG_CONFIG_SERVER"
configServerPortEnvVar = "SPLUNK_DEBUG_CONFIG_SERVER_PORT"
defaultConfigServerPort = "55554"
defaultConfigServerEndpoint = "localhost:" + defaultConfigServerPort
effectivePath = "/debug/configz/effective"
initialPath = "/debug/configz/initial"
configServerEnabledEnvVar = "SPLUNK_DEBUG_CONFIG_SERVER"
configServerPortEnvVar = "SPLUNK_DEBUG_CONFIG_SERVER_PORT"
configServerNetworkInterfaceEnvVar = "SPLUNK_DEBUG_CONFIG_SERVER_NETWORK_INTERFACE"
defaultConfigServerPort = "55554"
defaultConfigServerNetworkInterface = "localhost"
effectivePath = "/debug/configz/effective"
initialPath = "/debug/configz/initial"
)

type ConfigType int
Expand Down Expand Up @@ -138,16 +139,26 @@ func (cs *ConfigServer) start() {

cs.once.Do(
func() {
endpoint := defaultConfigServerEndpoint
port := defaultConfigServerPort
if portOverride, ok := os.LookupEnv(configServerPortEnvVar); ok {
if portOverride == "" {
// If explicitly set to empty do not start the server.
return
}

endpoint = "localhost:" + portOverride
port = portOverride
}

networkInterface := defaultConfigServerNetworkInterface
if networkInterfaceOverride, ok := os.LookupEnv(configServerNetworkInterfaceEnvVar); ok {
if networkInterfaceOverride != "" {
networkInterface = networkInterfaceOverride
}
}

endpoint := net.JoinHostPort(networkInterface, port)
log.Printf("Starting config server at %q\n", endpoint)

listener, err := net.Listen("tcp", endpoint)
if err != nil {
if errors.Is(err, syscall.EADDRINUSE) {
Expand Down
36 changes: 20 additions & 16 deletions internal/configconverter/config_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,22 @@ func TestConfigServer_RequireEnvVar(t *testing.T) {

client := &http.Client{}
path := "/debug/configz/initial"
_, err := client.Get("http://" + defaultConfigServerEndpoint + path)
_, err := client.Get("http://localhost:55554" + path)
assert.Error(t, err)
}

func TestConfigServer_EnvVar(t *testing.T) {
alternativePort := strconv.FormatUint(uint64(testutils.GetAvailablePort(t)), 10)
require.NoError(t, os.Setenv(configServerEnabledEnvVar, "true"))
t.Cleanup(func() {
assert.NoError(t, os.Unsetenv(configServerEnabledEnvVar))
})
alternativeNetworkInterface := "0.0.0.0"
t.Setenv(configServerEnabledEnvVar, "true")

tests := []struct {
name string
portEnvVar string
endpoint string
setPortEnvVar bool
serverDown bool
name string
portEnvVar string
networkInterfaceEnvVar string
endpoint string
setPortEnvVar bool
serverDown bool
}{
{
name: "default",
Expand All @@ -82,6 +81,11 @@ func TestConfigServer_EnvVar(t *testing.T) {
portEnvVar: alternativePort,
endpoint: "http://localhost:" + alternativePort,
},
{
name: "change_networkInterface",
networkInterfaceEnvVar: alternativeNetworkInterface,
endpoint: "http://0.0.0.0:55554",
},
}

for _, tt := range tests {
Expand All @@ -97,10 +101,10 @@ func TestConfigServer_EnvVar(t *testing.T) {
}

if tt.portEnvVar != "" || tt.setPortEnvVar {
require.NoError(t, os.Setenv(configServerPortEnvVar, tt.portEnvVar))
defer func() {
assert.NoError(t, os.Unsetenv(configServerPortEnvVar))
}()
t.Setenv(configServerPortEnvVar, tt.portEnvVar)
}
if tt.portEnvVar != "" || tt.setPortEnvVar {
t.Setenv(configServerNetworkInterfaceEnvVar, tt.networkInterfaceEnvVar)
}

cs := NewConfigServer()
Expand All @@ -115,7 +119,7 @@ func TestConfigServer_EnvVar(t *testing.T) {

endpoint := tt.endpoint
if endpoint == "" {
endpoint = "http://" + defaultConfigServerEndpoint
endpoint = "http://localhost:55554"
}

path := "/debug/configz/initial"
Expand Down Expand Up @@ -180,7 +184,7 @@ func TestConfigServer_Serve(t *testing.T) {
}

func assertValidYAMLPages(t *testing.T, expected map[string]any, path string) {
url := "http://" + defaultConfigServerEndpoint + path
url := "http://localhost:55554" + path

client := &http.Client{}

Expand Down

0 comments on commit 96ed032

Please sign in to comment.