Skip to content

Commit

Permalink
Fixes #323
Browse files Browse the repository at this point in the history
  • Loading branch information
vfarcic committed Sep 6, 2017
1 parent dd8a3b5 commit 0db6b9d
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ ENV CERTS="" \
PROXY_INSTANCE_NAME="docker-flow" \
RELOAD_INTERVAL="5000" REPEAT_RELOAD=false \
SERVICE_NAME="proxy" SERVICE_DOMAIN_ALGO="hdr(host)" \
STATS_USER="" STATS_USER_ENV="STATS_USER" STATS_PASS="" STATS_PASS_ENV="STATS_PASS" STATS_URI="" STATS_URI_ENV="STATS_URI" \
STATS_USER="" STATS_USER_ENV="STATS_USER" STATS_PASS="" STATS_PASS_ENV="STATS_PASS" STATS_URI="" STATS_URI_ENV="STATS_URI" STATS_PORT="" \
TIMEOUT_HTTP_REQUEST="5" TIMEOUT_HTTP_KEEP_ALIVE="15" TIMEOUT_CLIENT="20" TIMEOUT_CONNECT="5" TIMEOUT_QUEUE="30" TIMEOUT_SERVER="20" TIMEOUT_TUNNEL="3600" \
USERS="" \
SKIP_ADDRESS_VALIDATION="true" \
Expand Down
1 change: 1 addition & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ PROXY_INSTANCE_NAME|The name of the proxy instance. Useful if multiple proxies a
|STATS_USER_ENV |The name of the environment variable that holds the username for the statistics page.<br>**Example:** MY_USER<br>**Default value:** `STATS_USER`|
|STATS_PASS |Password for the statistics page. If not set, stats will not be available. If both `STATS_USER` and `STATS_PASS` are set to `none`, statistics will be available without authentication.<br>**Example:** my-pass<br>**Default value:** `admin`|
|STATS_PASS_ENV |The name of the environment variable that holds the password for the statistics page.<br>**Example:** MY_PASS|STATS_PASS|
|STATS_PORT |The port for the statistics page.<br>**Example:** `81`<br>**Default value:** `80`|
|STATS_URI |URI for the statistics page.<br>**Example:** `/proxyStats`<br>**Default value:** `/admin?stats`|
|STATS_URI_ENV |The name of the environment variable that holds the URI for the statistics page.<br>**Example:** `MY_URI`<br>**Default value:** `STATS_URI`|
|TERMINATE_ON_RELOAD|Whether to terminate the proxy process every time a reload request is received. If set to `false`, a new process will spawn and all the existing requests will terminate through the old process. The downside of this approach is that the system might end up with zombie processes. If set to `true`, zombie processes will be removed but the existing requests to the proxy might be cut.<br>**Example:** `true`<br>**Default value:** `false`|
Expand Down
2 changes: 1 addition & 1 deletion haproxy.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ defaults
timeout tunnel {{.TimeoutTunnel}}s
timeout http-request {{.TimeoutHttpRequest}}s
timeout http-keep-alive {{.TimeoutHttpKeepAlive}}s
{{.Stats}}
{{.UserList}}
{{.Stats}}
frontend services{{.DefaultBinds}}
mode {{.DefaultReqMode}}
{{.ExtraFrontend}}{{.ContentFrontend}}{{.ContentFrontendTcp}}{{.ContentFrontendSNI}}
4 changes: 2 additions & 2 deletions integration_tests/integration_swarm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,6 @@ func (s IntegrationSwarmTestSuite) Test_HeaderAcls() {

func (s IntegrationSwarmTestSuite) Test_AddHeaders() {
s.reconfigureGoDemo("&addResHeader=my-res-header%20my-res-value")
time.Sleep(2 * time.Second)

resp, err := http.Get(fmt.Sprintf("http://%s/demo/hello", s.hostIP))

Expand Down Expand Up @@ -756,6 +755,7 @@ func (s *IntegrationSwarmTestSuite) reconfigureRedis() {
}

func (s *IntegrationSwarmTestSuite) reconfigureService(params string) {
time.Sleep(1 * time.Second)
url := fmt.Sprintf(
"http://%s:8080/v1/docker-flow-proxy/reconfigure?%s",
s.hostIP,
Expand All @@ -775,7 +775,7 @@ CONFIGURATION:
s.getProxyConf(""))
s.Equal(200, resp.StatusCode, msg)
}
time.Sleep(3 * time.Second)
time.Sleep(5 * time.Second)
}

func (s *IntegrationSwarmTestSuite) reloadService(params string) {
Expand Down
6 changes: 5 additions & 1 deletion metrics/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ var (
func SetupHandler(creds string) {
if !isInitialized {
statsUri := getSecretOrEnvVar(os.Getenv("STATS_URI_ENV"), "/admin?stats")
uri := fmt.Sprintf("http://%slocalhost%s;csv", creds, statsUri)
statsPort := getSecretOrEnvVar("STATS_PORT", "")
if len(statsPort) > 0 {
statsPort = ":" + statsPort
}
uri := fmt.Sprintf("http://%slocalhost%s%s;csv", creds, statsPort, statsUri)

selectedServerMetrics, err := filterServerMetrics(serverMetrics.String())
if err != nil {
Expand Down
13 changes: 12 additions & 1 deletion proxy/ha_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,8 +364,19 @@ func (m *HaProxy) putStats(data *configData) {
statsUser := getSecretOrEnvVar(os.Getenv("STATS_USER_ENV"), "")
statsPass := getSecretOrEnvVar(os.Getenv("STATS_PASS_ENV"), "")
statsUri := getSecretOrEnvVar(os.Getenv("STATS_URI_ENV"), "/admin?stats")
statsPort := getSecretOrEnvVar("STATS_PORT", "")
if statsPort != "80" && len(statsPort) > 0 {
data.Stats += fmt.Sprintf(`
frontend stats
bind *:%s
default_backend stats
backend stats
mode http`,
statsPort)
}
if len(statsUser) > 0 && len(statsPass) > 0 {
data.Stats = fmt.Sprintf(`
data.Stats += fmt.Sprintf(`
stats enable
stats refresh 30s
stats realm Strictly\ Private
Expand Down
46 changes: 46 additions & 0 deletions proxy/ha_proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,52 @@ frontend services`
s.Equal(expectedData, actualData)
}

func (s HaProxyTestSuite) Test_CreateConfigFromTemplates_CreatesSeparateFrontEndAndBackEnd_WhenStatsPortIsNot80() {
var actualData string
statUserOrig := os.Getenv("STATS_USER")
statPassOrig := os.Getenv("STATS_PASS")
statUserEnvOrig := os.Getenv("STATS_USER_ENV")
statPassEnvOrig := os.Getenv("STATS_PASS_ENV")
defer func() {
os.Setenv("STATS_USER", statUserOrig)
os.Setenv("STATS_PASS", statPassOrig)
os.Setenv("STATS_USER_ENV", statUserEnvOrig)
os.Setenv("STATS_PASS_ENV", statPassEnvOrig)
os.Unsetenv("STATS_PORT")
}()
os.Setenv("STATS_USER", "my-user")
os.Setenv("STATS_PASS", "my-pass")
os.Setenv("STATS_USER_ENV", "STATS_USER")
os.Setenv("STATS_PASS_ENV", "STATS_PASS")
os.Setenv("STATS_PORT", "81")
stats := `frontend stats
bind *:81
default_backend stats
backend stats
mode http
stats enable
stats refresh 30s
stats realm Strictly\ Private
stats uri /admin?stats
stats auth my-user:my-pass
frontend services`
expectedData := fmt.Sprintf(
"%s%s",
strings.Replace(s.TemplateContent, "\nfrontend services", stats, -1),
s.ServicesContent,
)
writeFile = func(filename string, data []byte, perm os.FileMode) error {
actualData = string(data)
return nil
}

NewHaProxy(s.TemplatesPath, s.ConfigsPath).CreateConfigFromTemplates()

s.Equal(expectedData, actualData)
}

func (s HaProxyTestSuite) Test_CreateConfigFromTemplates_RemovesStatsAuth_WhenUserIsNone() {
var actualData string
statUserOrig := os.Getenv("STATS_USER")
Expand Down
6 changes: 5 additions & 1 deletion server/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ type metrics struct {
func NewMetrics(metricsUrl string) metricer {
if len(metricsUrl) == 0 {
statsUri := getSecretOrEnvVar(os.Getenv("STATS_URI_ENV"), "/admin?stats")
metricsUrl = fmt.Sprintf("http://%slocalhost%s;csv", GetCreds(), statsUri)
statsPort := getSecretOrEnvVar("STATS_PORT", "")
if len(statsPort) > 0 {
statsPort = ":" + statsPort
}
metricsUrl = fmt.Sprintf("http://%slocalhost%s%s;csv", GetCreds(), statsPort, statsUri)
}
return &metrics{metricsUrl: metricsUrl}
}
Expand Down
4 changes: 3 additions & 1 deletion server/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,16 @@ func (s *MetricsTestSuite) Test_NewMetrics_SetsMetricsUrlWithCredentials() {
os.Unsetenv("STATS_PASS_ENV")
os.Unsetenv("STATS_URI")
os.Unsetenv("STATS_URI_ENV")
os.Unsetenv("STATS_PORT")
}()
os.Setenv("STATS_USER_ENV", "STATS_USER")
os.Setenv("STATS_USER", "my-user")
os.Setenv("STATS_PASS_ENV", "STATS_PASS")
os.Setenv("STATS_PASS", "my-pass")
os.Setenv("STATS_URI", "/myStats")
os.Setenv("STATS_URI_ENV", "STATS_URI")
expected := "http://my-user:my-pass@localhost/myStats;csv"
os.Setenv("STATS_PORT", "81")
expected := "http://my-user:my-pass@localhost:81/myStats;csv"
m := NewMetrics("")

s.Equal(expected, m.getMetricsUrl())
Expand Down

0 comments on commit 0db6b9d

Please sign in to comment.