Skip to content

Commit

Permalink
Fix backend name checking for haproxy template
Browse files Browse the repository at this point in the history
  • Loading branch information
tamalsaha committed Dec 20, 2017
1 parent b95d301 commit b7f5fd3
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 19 deletions.
2 changes: 1 addition & 1 deletion hack/docker/voyager/templates/http-backend.cfg
@@ -1,6 +1,6 @@
{{ range $host := .Hosts }}
{{ range $path := $host.Paths }}
{{ if $path.Backend.Name }}
{{ if $path.Backend }}
backend {{ $path.Backend.Name }}
{{ if $path.Backend.BasicAuth }}
{{ range $name := $path.Backend.BasicAuth.UserLists }}
Expand Down
2 changes: 1 addition & 1 deletion hack/docker/voyager/templates/http-frontend.cfg
Expand Up @@ -82,7 +82,7 @@ frontend {{ .FrontendName }}
{{ if $path.SSLRedirect }}
redirect scheme https code 301 if ! is_proxy_https {{ if $host.Host }}host_acl_{{ $host.Host | acl_name }}{{ end }}{{ if $path.Path }} url_acl_{{ $host.Host | acl_name }}_{{ $path.Path | acl_name }}{{ end }}
{{ end }}
{{ if $path.Backend.Name }}
{{ if $path.Backend }}
use_backend {{ $path.Backend.Name }} {{ if or $host.Host $path.Path }}if {{ end }}{{ if $host.Host }}host_acl_{{ $host.Host | acl_name }}{{ end }}{{ if $path.Path }} url_acl_{{ $host.Host | acl_name }}_{{ $path.Path | acl_name }}{{ end }}
{{ end }}
{{ end }}
Expand Down
2 changes: 2 additions & 0 deletions hack/docker/voyager/templates/tcp-backend.cfg
@@ -1,3 +1,4 @@
{{ if .Backend }}
backend {{ .Backend.Name }}
mode tcp

Expand All @@ -17,3 +18,4 @@ backend {{ .Backend.Name }}
server {{ $e.Name }} {{ $e.IP }}:{{ $e.Port }} {{ if $e.MaxConnections }} maxconn {{ $e.MaxConnections }} {{ end }} {{ if $e.Weight }} weight {{ $e.Weight }}{{ end }} {{ if $e.TLSOption }} {{ $e.TLSOption }} {{ end }} {{ if $e.CheckHealth }} check {{ if $e.CheckHealthPort }} port {{ $e.CheckHealthPort }} {{ end }} {{ end }} {{ if $e.SendProxy }}{{ $e.SendProxy }}{{ end }}
{{ end }}
{{ end }}
{{ end }}
2 changes: 2 additions & 0 deletions hack/docker/voyager/templates/tcp-frontend.cfg
Expand Up @@ -24,4 +24,6 @@ frontend {{ .FrontendName }}
{{ $rule }}
{{ end }}

{{ if .Backend }}
default_backend {{ .Backend.Name }}
{{ end }}
31 changes: 22 additions & 9 deletions pkg/haproxy/renderer.go
Expand Up @@ -2,6 +2,7 @@ package haproxy

import (
"bytes"
"encoding/json"
"fmt"
"sort"
"strings"
Expand All @@ -15,6 +16,7 @@ func RenderConfig(data TemplateData) (string, error) {
return "", err
}
data.canonicalize()

var buf bytes.Buffer
err := haproxyTemplate.ExecuteTemplate(&buf, "haproxy.cfg", data)
if err != nil {
Expand All @@ -31,6 +33,11 @@ func RenderConfig(data TemplateData) (string, error) {
return strings.Join(result, "\n"), nil
}

func (td TemplateData) String() string {
data, _ := json.MarshalIndent(td, "", " ")
return string(data)
}

func (td *TemplateData) canonicalize() {
if td.DefaultBackend != nil {
td.DefaultBackend.canonicalize()
Expand All @@ -57,7 +64,9 @@ func (td *TemplateData) canonicalize() {
for y := range svc.Hosts {
host := svc.Hosts[y]
for z := range host.Paths {
host.Paths[z].Backend.canonicalize()
if host.Paths[z].Backend != nil {
host.Paths[z].Backend.canonicalize()
}
}

sort.Slice(host.Paths, func(i, j int) bool {
Expand Down Expand Up @@ -105,10 +114,12 @@ func (td *TemplateData) isValid() error {

for _, host := range svc.Hosts {
for _, path := range host.Paths {
if backends.Has(path.Backend.Name) {
return fmt.Errorf("HAProxy backend name %s is reused.", path.Backend.Name)
} else {
backends.Insert(path.Backend.Name)
if path.Backend != nil {
if backends.Has(path.Backend.Name) {
return fmt.Errorf("HAProxy backend name %s is reused.", path.Backend.Name)
} else {
backends.Insert(path.Backend.Name)
}
}
}
}
Expand All @@ -121,10 +132,12 @@ func (td *TemplateData) isValid() error {
frontends.Insert(svc.FrontendName)
}

if backends.Has(svc.Backend.Name) {
return fmt.Errorf("HAProxy backend name %s is reused.", svc.Backend.Name)
} else {
backends.Insert(svc.Backend.Name)
if svc.Backend != nil {
if backends.Has(svc.Backend.Name) {
return fmt.Errorf("HAProxy backend name %s is reused.", svc.Backend.Name)
} else {
backends.Insert(svc.Backend.Name)
}
}
}
return nil
Expand Down
4 changes: 2 additions & 2 deletions pkg/haproxy/types.go
Expand Up @@ -84,7 +84,7 @@ type HTTPHost struct {
type HTTPPath struct {
//Host string
Path string
Backend Backend
Backend *Backend
SSLRedirect bool
}

Expand All @@ -98,7 +98,7 @@ type TCPService struct {
FrontendRules []string
CertFile string
PEMName string
Backend Backend
Backend *Backend
ALPNOptions string
TLSAuth *TLSAuth
SSLRedirect bool
Expand Down
9 changes: 3 additions & 6 deletions pkg/ingress/parser.go
Expand Up @@ -3,7 +3,6 @@ package ingress
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"net/url"
"strconv"
Expand Down Expand Up @@ -432,7 +431,7 @@ func (c *controller) generateConfig() error {
if len(bk.Endpoints) > 0 {
httpPaths = append(httpPaths, &haproxy.HTTPPath{
Path: path.Path,
Backend: haproxy.Backend{
Backend: &haproxy.Backend{
Name: getBackendName(c.Ingress, path.Backend.IngressBackend),
BasicAuth: bk.BasicAuth,
Endpoints: bk.Endpoints,
Expand Down Expand Up @@ -462,7 +461,7 @@ func (c *controller) generateConfig() error {
Port: rule.TCP.Port.String(),
ALPNOptions: parseALPNOptions(rule.TCP.ALPN),
FrontendRules: fr.Rules,
Backend: haproxy.Backend{
Backend: &haproxy.Backend{
Name: getBackendName(c.Ingress, rule.TCP.Backend),
BackendRules: rule.TCP.Backend.BackendRule,
Endpoints: bk.Endpoints,
Expand Down Expand Up @@ -748,9 +747,7 @@ func (c *controller) generateConfig() error {
td.UserLists = append(td.UserLists, userLists[k])
}

if jb, err := json.MarshalIndent(&td, "", " "); err != nil {
c.logger.Debugf("Rendering haproxy.cfg for Ingress %s@%s using data:", c.Ingress.Name, c.Ingress.Namespace, string(jb))
}
c.logger.Infof("Rendering haproxy.cfg for Ingress %s/%s using data: %s", c.Ingress.Namespace, c.Ingress.Name, td)
if cfg, err := haproxy.RenderConfig(td); err != nil {
return err
} else {
Expand Down

0 comments on commit b7f5fd3

Please sign in to comment.