Skip to content
This repository has been archived by the owner on Nov 5, 2021. It is now read-only.

Commit

Permalink
Allow the server listening address to be changed
Browse files Browse the repository at this point in the history
It may be desired to change the listening address of the http server to
set it for a specific address or interface.

Allow this to be set with a "host" config or environment variable
CLOUDPROBER_HOST and listen on that hostname/address if given.

If not available, default to the existing setting of listening on all
addresses.

PiperOrigin-RevId: 239244493
  • Loading branch information
bekriebel authored and manugarg committed Mar 19, 2019
1 parent 16a6374 commit 667a6db
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 38 deletions.
16 changes: 14 additions & 2 deletions cloudprober.go
Expand Up @@ -50,9 +50,11 @@ const (
sysvarsModuleName = "sysvars"
)

// Constants defining the default server port.
// Constants defining the default server host and port.
const (
DefaultServerHost = ""
DefaultServerPort = 9313
ServerHostEnvVar = "CLOUDPROBER_HOST"
ServerPortEnvVar = "CLOUDPROBER_PORT"
)

Expand All @@ -74,6 +76,16 @@ type Prober struct {
}

func (pr *Prober) initDefaultServer() error {
serverHost := pr.c.GetHost()
if serverHost == "" {
serverHost = DefaultServerHost
// If ServerHostEnvVar is defined, it will override the default
// server host.
if host := os.Getenv(ServerHostEnvVar); host != "" {
serverHost = host
}
}

serverPort := int(pr.c.GetPort())
if serverPort == 0 {
serverPort = DefaultServerPort
Expand All @@ -89,7 +101,7 @@ func (pr *Prober) initDefaultServer() error {
}
}

ln, err := net.Listen("tcp", fmt.Sprintf(":%d", serverPort))
ln, err := net.Listen("tcp", fmt.Sprintf("%s:%d", serverHost, serverPort))
if err != nil {
return fmt.Errorf("error while creating listener for default HTTP server. Err: %v", err)
}
Expand Down
78 changes: 45 additions & 33 deletions config/proto/config.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 11 additions & 3 deletions config/proto/config.proto
@@ -1,14 +1,14 @@
syntax = "proto2";

package cloudprober;

import "github.com/google/cloudprober/probes/proto/config.proto";
import "github.com/google/cloudprober/servers/proto/config.proto";
import "github.com/google/cloudprober/targets/proto/targets.proto";
import "github.com/google/cloudprober/surfacers/proto/config.proto";
import "github.com/google/cloudprober/targets/proto/targets.proto";
import "github.com/google/cloudprober/targets/rds/server/proto/config.proto";
import "github.com/google/cloudprober/targets/rtc/rtcreporter/proto/rtcreporter.proto";

package cloudprober;

message ProberConfig {
// Probes to run.
repeated probes.ProbeDef probe = 1;
Expand All @@ -28,6 +28,9 @@ message ProberConfig {
// other probes.
repeated servers.ServerDef server = 3;

// Common services related options.
// Next tag: 102

// Resource discovery server
optional targets.rds.ServerConf rds_server = 95;

Expand All @@ -37,6 +40,11 @@ message ProberConfig {
// CLOUDPROBER_PORT.
optional int32 port = 96;

// Host for the default HTTP server. Default listens on all addresses. If not
// specified in the config, default port can be overridden by the environment
// variable CLOUDPROBER_HOST.
optional string host = 101;

// How often to export system variables. To learn more about system variables:
// http://godoc.org/github.com/google/cloudprober/sysvars.
optional int32 sysvars_interval_msec = 97 [default = 10000];
Expand Down

0 comments on commit 667a6db

Please sign in to comment.