Skip to content

Commit

Permalink
Enable TCP mode in port 80 (#664)
Browse files Browse the repository at this point in the history
  • Loading branch information
tamalsaha committed Oct 25, 2017
1 parent 4f89df1 commit d59491e
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 3 deletions.
1 change: 1 addition & 0 deletions apis/voyager/v1beta1/port.go
Expand Up @@ -94,6 +94,7 @@ func (r Ingress) explicitPodPorts() sets.Int {
}
}
}
// If Ingress does not use any HTTP rule but defined a default backend, we need to open port 80
if !usesHTTPRule && r.Spec.Backend != nil {
ports.Insert(80)
}
Expand Down
1 change: 1 addition & 0 deletions apis/voyager/v1beta1/validator.go
Expand Up @@ -196,6 +196,7 @@ func (r Ingress) IsValid(cloudProvider string) error {
}
}

// If Ingress does not use any HTTP rule but defined a default backend, we need to open port 80
if !usesHTTPRule && r.Spec.Backend != nil {
addrs[80] = &address{Protocol: "http", PodPort: 80}
}
Expand Down
2 changes: 1 addition & 1 deletion hack/docker/voyager/templates/haproxy.cfg
Expand Up @@ -23,7 +23,7 @@
{{ template "tcp-backend.cfg" $svc }}
{{ end }}

{{- if and (not .HTTPService) .DefaultBackend }}
{{- if .DefaultFrontend }}
{{ template "default-frontend.cfg" .SharedInfo }}
{{ end }}

Expand Down
1 change: 1 addition & 0 deletions pkg/haproxy/types.go
Expand Up @@ -13,6 +13,7 @@ type TemplateData struct {
OptionsDefaults map[string]bool
Stats *StatsInfo
DNSResolvers []*api.DNSResolver
DefaultFrontend bool
HTTPService []*HTTPService
TCPService []*TCPService
ErrorFiles []*ErrorFile
Expand Down
17 changes: 15 additions & 2 deletions pkg/ingress/parser.go
Expand Up @@ -322,6 +322,7 @@ func (c *controller) generateConfig() error {

td.HTTPService = make([]*haproxy.HTTPService, 0)
td.TCPService = make([]*haproxy.TCPService, 0)
tp80 := false

type httpKey struct {
Port int
Expand Down Expand Up @@ -395,6 +396,10 @@ func (c *controller) generateConfig() error {
httpServices[key] = httpPaths
}
} else if rule.TCP != nil {
if rule.TCP.Port.IntValue() == 80 {
tp80 = true // Port 80 used in TCP mode
}

bk, err := c.serviceEndpoints(dnsResolvers, userLists, rule.TCP.Backend.ServiceName, rule.TCP.Backend.ServicePort, rule.TCP.Backend.HostNames)
if err != nil {
return err
Expand Down Expand Up @@ -445,12 +450,12 @@ func (c *controller) generateConfig() error {
}
}

tp80 := false
for key := range httpServices {
value := httpServices[key]
if key.Port == 80 {
tp80 = true
tp80 = true // Port 80 used in HTTP mode
}

fr := getFrontendRulesForPort(c.Ingress.Spec.FrontendRules, key.Port)
srv := &haproxy.HTTPService{
SharedInfo: si,
Expand Down Expand Up @@ -493,6 +498,7 @@ func (c *controller) generateConfig() error {
}
td.HTTPService = append(td.HTTPService, srv)
}
// Port 80 is not used explicitly but port 443 is used (HTTP/TCP mode), so auto redirect from 80 -> 443
if !tp80 && c.Ingress.SSLRedirect() {
td.HTTPService = append(td.HTTPService, &haproxy.HTTPService{
SharedInfo: si,
Expand All @@ -501,6 +507,13 @@ func (c *controller) generateConfig() error {
})
}

// Must be checked after `ssl-redirect` annotation is processed
if len(td.HTTPService) == 0 && // No HTTP rule used
!tp80 && // Port 80 is not used in either HTTP or TCP mode
td.DefaultBackend != nil { // Default backend is provided
td.DefaultFrontend = true
}

td.DNSResolvers = make([]*api.DNSResolver, 0, len(dnsResolvers))
for k := range dnsResolvers {
td.DNSResolvers = append(td.DNSResolvers, dnsResolvers[k])
Expand Down

0 comments on commit d59491e

Please sign in to comment.