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

support for default_backend #222

Merged
merged 10 commits into from
Apr 29, 2017
Merged
Show file tree
Hide file tree
Changes from 6 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
1 change: 1 addition & 0 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ The following query parameters can be used to send a *reconfigure* request to *D
|delReqHeader |Additional headers that will be deleted in the request before forwarding it to the service. Multiple headers should be separated with comma (`,`). Please consult [Delete a header in the request](https://www.haproxy.com/doc/aloha/7.0/haproxy/http_rewriting.html#delete-a-header-in-the-request) for more info.<br>Example: `X-Forwarded-For,Cookie`|No| |
|delResHeader |Additional headers that will be deleted in the response before forwarding it to the client. Multiple headers should be separated with comma (`,`). Please consult [Delete a header in the response](https://www.haproxy.com/doc/aloha/7.0/haproxy/http_rewriting.html#delete-a-header-in-the-response) for more info.<br>Example: `X-Varnish,X-Cache`|No| |
|httpsPort |The internal HTTPS port of a service that should be reconfigured. The port is used only in the `swarm` mode. If not specified, the `port` parameter will be used instead.<br>Example: `443`|No| |
|isDefaultBackend | If true, the service will be set to the default_backend rule, meaning it will catch all requests not matching any other rules.<br>Example: `true`|No| |
Copy link
Owner

Choose a reason for hiding this comment

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

Can you also add an entry in the Environment Variables section below?

|port |The internal port of a service that should be reconfigured. The port is used only in the `swarm` mode. The parameter can be prefixed with an index thus allowing definition of multiple destinations for a single service (e.g. `port.1`, `port.2`, and so on).<br>Example: `8080`|Only in `swarm` mode.| |
|reqMode |The request mode. The proxy should be able to work with any mode supported by HAProxy. However, actively supported and tested modes are `http`, `tcp`, and `sni`. The `sni` mode implies TCP with an SNI-based routing.<br>Example: `tcp`|No|http|
|reqPathReplace |A regular expression to apply the modification. If specified, `reqPathSearch` needs to be set as well.<br>Example: `/demo/`|No| |
Expand Down
5 changes: 5 additions & 0 deletions proxy/ha_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,11 @@ func (m *HaProxy) getFrontTemplate(s Service) string {
} else {
tmplString += `use_backend {{$.ServiceName}}-be{{.Port}} if url_{{$.AclName}}{{.Port}}{{$.AclCondition}}{{.SrcPortAclName}}`
}
if s.IsDefaultBackend {
tmplString += fmt.Sprintf(
`
default_backend {{$.ServiceName}}-be{{.Port}}`)
}
tmplString += "{{end}}"
return m.templateToString(tmplString, s)
}
Expand Down
32 changes: 32 additions & 0 deletions proxy/ha_proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -877,6 +877,38 @@ func (s HaProxyTestSuite) Test_CreateConfigFromTemplates_AddsContentFrontEndWith
s.Equal(expectedData, actualData)
}


func (s HaProxyTestSuite) Test_CreateConfigFromTemplates_AddsContentFrontEndWithDefaultBackend_WhenIsDefaultBackendIsTrue() {
var actualData string
tmpl := s.TemplateContent
expectedData := fmt.Sprintf(
`%s
acl url_my-service1111 path_beg /path
use_backend my-service-be1111 if url_my-service1111
default_backend my-service-be1111%s`,
tmpl,
s.ServicesContent,
)
writeFile = func(filename string, data []byte, perm os.FileMode) error {
actualData = string(data)
return nil
}
p := NewHaProxy(s.TemplatesPath, s.ConfigsPath)
data.Services["my-service"] = Service{
ServiceName: "my-service",
IsDefaultBackend: true,
AclName: "my-service",
PathType: "path_beg",
ServiceDest: []ServiceDest{
{Port: "1111", ServicePath: []string{"/path"}},
},
}

p.CreateConfigFromTemplates()

s.Equal(expectedData, actualData)
}

func (s HaProxyTestSuite) Test_CreateConfigFromTemplates_AddsContentFrontEndWithHdrDom_WhenServiceDomainMatchAllIsSet() {
var actualData string
tmpl := s.TemplateContent
Expand Down
2 changes: 2 additions & 0 deletions proxy/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ type Service struct {
// The port is used only in the swarm mode.
// If not specified, the `port` parameter will be used instead.
HttpsPort int `split_words:"true"`
// If set to true, it will be the default_backend service.
IsDefaultBackend bool `envconfig:"default_backend" split_words:"true"`
// The hostname where the service is running, for instance on a separate swarm.
// If specified, the proxy will dispatch requests to that domain.
OutboundHostname string `split_words:"true"`
Expand Down
2 changes: 2 additions & 0 deletions proxy/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ func (s *TypesTestSuite) Test_GetServiceFromMap_ReturnsProxyService() {
Distribute: true,
HttpsOnly: true,
HttpsPort: 1234,
IsDefaultBackend: true,
OutboundHostname: "outboundHostname",
PathType: "pathType",
RedirectWhenHttpProto: true,
Expand Down Expand Up @@ -235,6 +236,7 @@ func (s *TypesTestSuite) Test_GetServiceFromMap_ReturnsProxyService() {
"delResHeader": strings.Join(expected.DelResHeader, ","),
"port": expected.ServiceDest[0].Port,
"servicePath": strings.Join(expected.ServiceDest[0].ServicePath, ","),
"isDefaultBackend": strconv.FormatBool(expected.IsDefaultBackend),
}
actual := GetServiceFromMap(&serviceMap)
s.Equal(expected, *actual)
Expand Down
3 changes: 3 additions & 0 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,7 @@ func (s *ServerTestSuite) Test_GetServicesFromEnvVars_ReturnsServices() {
Distribute: true,
HttpsOnly: true,
HttpsPort: 1234,
IsDefaultBackend: true,
OutboundHostname: "my-OutboundHostname",
PathType: "my-PathType",
RedirectWhenHttpProto: true,
Expand Down Expand Up @@ -759,6 +760,7 @@ func (s *ServerTestSuite) Test_GetServicesFromEnvVars_ReturnsServices() {
os.Setenv("DFP_SERVICE_DISTRIBUTE", strconv.FormatBool(service.Distribute))
os.Setenv("DFP_SERVICE_HTTPS_ONLY", strconv.FormatBool(service.HttpsOnly))
os.Setenv("DFP_SERVICE_HTTPS_PORT", strconv.Itoa(service.HttpsPort))
os.Setenv("DFP_SERVICE_DEFAULT_BACKEND", strconv.FormatBool(service.IsDefaultBackend))
Copy link
Owner

Choose a reason for hiding this comment

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

Why not call it DFP_SERVICE_IS_DEFAULT_BACKEND? That would make it consistent and easy to remember that env. var. names are the same as request params with the DFP_SERVICE_ prefix.

os.Setenv("DFP_SERVICE_OUTBOUND_HOSTNAME", service.OutboundHostname)
os.Setenv("DFP_SERVICE_PATH_TYPE", service.PathType)
os.Setenv("DFP_SERVICE_REDIRECT_WHEN_HTTP_PROTO", strconv.FormatBool(service.RedirectWhenHttpProto))
Expand Down Expand Up @@ -789,6 +791,7 @@ func (s *ServerTestSuite) Test_GetServicesFromEnvVars_ReturnsServices() {
os.Unsetenv("DFP_SERVICE_CONNECTION_MODE")
os.Unsetenv("DFP_SERVICE_CONSUL_TEMPLATE_BE_PATH")
os.Unsetenv("DFP_SERVICE_CONSUL_TEMPLATE_FE_PATH")
os.Unsetenv("DFP_SERVICE_DEFAULT_BACKEND")
os.Unsetenv("DFP_SERVICE_DEL_REQ_HEADER")
os.Unsetenv("DFP_SERVICE_DEL_RES_HEADER")
os.Unsetenv("DFP_SERVICE_DISTRIBUTE")
Expand Down