Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Network interface for discovery config server #5772

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we update the readme/docs to mention this new env var option? I see we have a blurb in our readme about the config server https://github.com/signalfx/splunk-otel-collector/blob/main/README.md?plain=1#L157-L160. We should make a changelog entry also i think

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. This deserves more attention than I gave it. I only wanted this for testing purposes.

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
Loading