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 2 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 @@ -25,6 +25,7 @@ The following query parameters can be used to send a *reconfigure* request to *D
|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| |
|reqPathSearch |A regular expression to search the content to be replaced. If specified, `reqPathReplace` needs to be set as well.<br>Example: `/something/`|No| |
|ServiceDefaultBackend | 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.

ServiceDefaultBackend might be confusing. People might expect to put some value other than true. Maybe it would be better to name it isDefaultBackend?

Copy link
Contributor Author

@d-h1 d-h1 Apr 27, 2017

Choose a reason for hiding this comment

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

I agree, isDefaultBackend sounds much better, I'll update my PR

|serviceDomain |The domain of the service. If set, the proxy will allow access only to requests coming to that domain. Multiple domains should be separated with comma (`,`).**This parameter cannot be used with TCP.**<br>Example: ecme.com|No| |
|serviceDomainMatchAll|Whether to include subdomains and FDQN domains in the match. If set to false, and, for example, `serviceDomain` is set to `acme.com`, `something.acme.com` would not be considered a match unless this parameter is set to `true`. If this option is used, it is recommended to put any subdomains higher in the list using `aclName`.<br>Example: `true`|No|false|
|serviceName |The name of the service. It must match the name of the Swarm service.<br>Example: `go-demo`|Yes| |
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.ServiceDefaultBackend {
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_WhenServiceDefaultBackendIsTrue() {
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",
ServiceDefaultBackend: 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 @@ -87,6 +87,8 @@ type Service struct {
ReqPathSearch string `split_words:"true"`
// Content of the PEM-encoded certificate to be used by the proxy when serving traffic over SSL.
ServiceCert string `split_words:"true"`
// If set to true, it will be the default_backend service.
ServiceDefaultBackend bool `envconfig:"default_backend" split_words:"true"`
// The domain of the service.
// If set, the proxy will allow access only to requests coming to that domain.
ServiceDomain []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 @@ -184,6 +184,7 @@ func (s *TypesTestSuite) Test_GetServiceFromMap_ReturnsProxyService() {
ReqPathSearch: "reqPathSearch",
ServiceCert: "serviceCert",
ServiceColor: "serviceColor",
ServiceDefaultBackend: true,
ServiceDest: []ServiceDest{{ServicePath: []string{"/"}, Port: "1234", ReqMode: "reqMode",}},
ServiceDomain: []string{"domain1", "domain2"},
ServiceDomainMatchAll: 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, ","),
"serviceDefaultBackend": strconv.FormatBool(expected.ServiceDefaultBackend),
}
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 @@ -721,6 +721,7 @@ func (s *ServerTestSuite) Test_GetServicesFromEnvVars_ReturnsServices() {
ReqPathReplace: "my-ReqPathReplace",
ReqPathSearch: "my-ReqPathSearch",
ServiceCert: "my-ServiceCert",
ServiceDefaultBackend: true,
ServiceDomain: []string{"my-domain-1.com", "my-domain-2.com"},
ServiceDomainMatchAll: true,
ServiceName: "my-ServiceName",
Expand All @@ -743,6 +744,7 @@ func (s *ServerTestSuite) Test_GetServicesFromEnvVars_ReturnsServices() {
os.Setenv("DFP_SERVICE_CONNECTION_MODE", service.ConnectionMode)
os.Setenv("DFP_SERVICE_CONSUL_TEMPLATE_FE_PATH", service.ConsulTemplateFePath)
os.Setenv("DFP_SERVICE_CONSUL_TEMPLATE_BE_PATH", service.ConsulTemplateBePath)
os.Setenv("DFP_SERVICE_DEFAULT_BACKEND", strconv.FormatBool(service.ServiceDefaultBackend))
os.Setenv("DFP_SERVICE_DEL_REQ_HEADER", strings.Join(service.DelReqHeader, ","))
os.Setenv("DFP_SERVICE_DEL_RES_HEADER", strings.Join(service.DelResHeader, ","))
os.Setenv("DFP_SERVICE_DISTRIBUTE", strconv.FormatBool(service.Distribute))
Expand Down Expand Up @@ -778,6 +780,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