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

Helper Functions for custom annotations #691

Merged
merged 3 commits into from
Sep 17, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions docs/custom-annotations.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,37 @@ If you'd like to use custom annotations with Mergeable Ingress resources, please

* Minions do not inherent custom annotations of the master.

### Helper Functions

Helper functions can be used in the Ingress template to parse the values of custom annotations.

| Function | Input Arguments| Return Arguments | Description |
| -------- | -------------- | ---------------- | ----------- |
| `split` | `s, sep string` | `[]string` | Splits the string `s` into a slice of strings separated by the `sep`. |
| `trim` | `s string` | `string` | Trims the trailing and leading whitespace from the string `s`. |

Consider the following custom annotation `custom.nginx.org/allowed-ips`, which expects a comma-separated list of IP addresses:
```
annotations:
custom.nginx.org/allowed-ips: "192.168.1.3, 10.0.0.13"
```

The helper functions can parse the value of the `custom.nginx.org/allowed-ips` annotation, so that in the template you can use each IP address separately. Consider the following template excerpt:

```
{{range $ip := split (index $.Ingress.Annotations "custom.nginx.org/allowed-ips") ","}}
allow {{trim $ip}};
{{end}}
deny all;
```

The template excerpt will generate the following configuration:
```
allow 192.168.1.3;
allow 10.0.0.13;
deny all;
```

## Example

See the [custom annotations example](../examples/custom-annotations).
4 changes: 2 additions & 2 deletions internal/configs/version1/template_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func NewTemplateExecutor(mainTemplatePath string, ingressTemplatePath string) (*
return nil, err
}

ingressTemplate, err := template.New(path.Base(ingressTemplatePath)).ParseFiles(ingressTemplatePath)
ingressTemplate, err := template.New(path.Base(ingressTemplatePath)).Funcs(helperFunctions).ParseFiles(ingressTemplatePath)
if err != nil {
return nil, err
}
Expand All @@ -45,7 +45,7 @@ func (te *TemplateExecutor) UpdateMainTemplate(templateString *string) error {

// UpdateIngressTemplate updates the ingress template.
func (te *TemplateExecutor) UpdateIngressTemplate(templateString *string) error {
newTemplate, err := template.New("ingressTemplate").Parse(*templateString)
newTemplate, err := template.New("ingressTemplate").Funcs(helperFunctions).Parse(*templateString)
if err != nil {
return err
}
Expand Down
19 changes: 19 additions & 0 deletions internal/configs/version1/template_helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package version1

import (
"strings"
"text/template"
)

func split(s string, delim string) []string {
return strings.Split(s, delim)
}

func trim(s string) string {
return strings.TrimSpace(s)
}

var helperFunctions = template.FuncMap{
"split": split,
"trim": trim,
}
50 changes: 48 additions & 2 deletions internal/configs/version1/templates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ var mainCfg = MainConfig{
}

func TestIngressForNGINXPlus(t *testing.T) {
tmpl, err := template.New(nginxPlusIngressTmpl).ParseFiles(nginxPlusIngressTmpl)
tmpl, err := template.New(nginxPlusIngressTmpl).Funcs(helperFunctions).ParseFiles(nginxPlusIngressTmpl)
if err != nil {
t.Fatalf("Failed to parse template file: %v", err)
}
Expand All @@ -126,7 +126,7 @@ func TestIngressForNGINXPlus(t *testing.T) {
}

func TestIngressForNGINX(t *testing.T) {
tmpl, err := template.New(nginxIngressTmpl).ParseFiles(nginxIngressTmpl)
tmpl, err := template.New(nginxIngressTmpl).Funcs(helperFunctions).ParseFiles(nginxIngressTmpl)
if err != nil {
t.Fatalf("Failed to parse template file: %v", err)
}
Expand Down Expand Up @@ -169,3 +169,49 @@ func TestMainForNGINX(t *testing.T) {
t.Fatalf("Failed to write template %v", err)
}
}

func TestSplitHelperFunction(t *testing.T) {
const tpl = `{{range $n := split . ","}}{{$n}} {{end}}`

tmpl, err := template.New("testTemplate").Funcs(helperFunctions).Parse(tpl)
if err != nil {
t.Fatalf("Failed to parse template: %v", err)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

consider adding a blank line here to improve readability. Same applies for TestTrimHelperFunctions


var buf bytes.Buffer

input := "foo,bar"
expected := "foo bar "
Copy link
Contributor

Choose a reason for hiding this comment

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

consider adding a blank line here to improve readability. Same applies for TestTrimHelperFunctions


err = tmpl.Execute(&buf, input)
if err != nil {
t.Fatalf("Failed to execute the template %v", err)
}

if buf.String() != expected {
t.Fatalf("Template generated wrong config, got %v but expected %v.", buf.String(), expected)
}
}

func TestTrimHelperFunction(t *testing.T) {
const tpl = `{{trim .}}`

tmpl, err := template.New("testTemplate").Funcs(helperFunctions).Parse(tpl)
if err != nil {
t.Fatalf("Failed to parse template: %v", err)
}

var buf bytes.Buffer

input := " foobar "
expected := "foobar"

err = tmpl.Execute(&buf, input)
if err != nil {
t.Fatalf("Failed to execute the template %v", err)
}

if buf.String() != expected {
t.Fatalf("Template generated wrong config, got %v but expected %v.", buf.String(), expected)
}
}