-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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. | | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
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). |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package version1 | ||
|
||
import ( | ||
"strings" | ||
"text/template" | ||
) | ||
|
||
// splitinput splits the input from "," and returns an array of strings | ||
func splitinput(s string, delim string) []string { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For multiword names, the convention is to use mixed caps. Like "splitInput". See https://golang.org/doc/effective_go.html#mixed-caps There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The documentation for the function is not correct. I suggest removing it complety - the function is private and also strait-forward. |
||
return strings.Split(s, delim) | ||
} | ||
|
||
// triminput trims the leading and trailing spaces in the string | ||
func triminput(s string) string { | ||
return strings.TrimSpace(s) | ||
} | ||
|
||
var helperFunctions = template.FuncMap{ | ||
"split": splitinput, //returns array of strings | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suggest removing these comments completely. The comment for split s already outdated. Having the docs in the doc is enough. |
||
"trim": triminput, //returns string with trimmed leading and trailing spaces | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
} | ||
|
@@ -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) | ||
} | ||
|
@@ -169,3 +169,51 @@ 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) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 " | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
t.Log(buf.String()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we need to print the buffer? Same applies for TestTrimHelperFunctions There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The other tests are following same pattern to print buf if it fails. So did same here. Let me know if it shoud be removed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider removing it here. |
||
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) | ||
t.Log(buf.String()) | ||
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) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.