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

Custom pongo2 filters for loading haproxy data. #1

Merged
merged 4 commits into from Dec 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions pkg/controller/ingress/parser.go
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/flosch/pongo2"
kapi "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/util/intstr"
"github.com/appscode/voyager/pkg/controller/ingress/template"
)

func (lbc *EngressController) parse() error {
Expand Down Expand Up @@ -97,13 +98,13 @@ func (lbc *EngressController) getEndpoints(s *kapi.Service, servicePort *kapi.Se
}

func (lbc *EngressController) generateTemplate() error {
log.Infoln("Generating Ingress templates.")
log.Infoln("Generating Ingress template.")
ctx, err := Context(lbc.Parsed)
if err != nil {
return errors.New().WithCause(err).Internal()
}

tpl, err := pongo2.FromString(haproxyTemplate)
tpl, err := pongo2.FromString(template.HAProxyTemplate)
if err != nil {
return errors.New().WithCause(err).Internal()
}
Expand Down
39 changes: 39 additions & 0 deletions pkg/controller/ingress/template/filters.go
@@ -0,0 +1,39 @@
package template

import (
"strings"

"github.com/flosch/pongo2"
)

// custom pongo2 filters to works with
func init() {
pongo2.RegisterFilter("header_name", HeaderNameFilter)
pongo2.RegisterFilter("host_name", HostNameFilter)
}

func HeaderNameFilter(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
v := strings.TrimSpace(in.String())
if v == "" {
return pongo2.AsValue(v), nil
}
index := strings.Index(v, " ")
if index < 0 {
return pongo2.AsValue(""), nil
}
name := v[:index]
return pongo2.AsValue(name), nil
}

func HostNameFilter(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
v := in.String()
v = strings.TrimSpace(v)
if v == "" {
return pongo2.AsValue(v), nil
}
if strings.HasPrefix(v, "*") {
v = v[1:]
return pongo2.AsValue("hdr_end(host) -i " + v), nil
}
return pongo2.AsValue("hdr(host) -i " + v), nil
}
57 changes: 57 additions & 0 deletions pkg/controller/ingress/template/filters_test.go
@@ -0,0 +1,57 @@
package template

import (
"fmt"
"testing"

"github.com/flosch/pongo2"
"github.com/stretchr/testify/assert"
)

func TestHeaderNameFilter(t *testing.T) {
temp := `
{{ val|header_name }}
{{ val2|header_name }}
`
ctx := &pongo2.Context{
"val": "hello world",
"val2": "hello world",
}
res, _ := render(ctx, temp)
exp := `
hello
hello
`
assert.Equal(t, res, exp)
fmt.Println(res)
}

func TestHostNameFilter(t *testing.T) {
temp := `
{{ val|host_name }}
{{ val2|host_name }}
`
ctx := &pongo2.Context{
"val": "appscode.com",
"val2": "*.appscode.com",
}
res, _ := render(ctx, temp)
exp := `
hdr(host) -i appscode.com
hdr_end(host) -i .appscode.com
`
assert.Equal(t, res, exp)
fmt.Println(res)
}

func render(ctx *pongo2.Context, temp string) (string, error) {
tpl, err := pongo2.FromString(temp)
if err != nil {
return "", err
}
out, err := tpl.Execute(*ctx)
if err != nil {
return "", err
}
return out, nil
}
@@ -1,6 +1,6 @@
package ingress
package template

const haproxyTemplate = `# This file uses golang pongo2 templates to
const HAProxyTemplate = `# This file uses golang pongo2 template to
# dynamically configure the haproxy loadbalancer.
global
daemon
Expand Down