Skip to content

Commit

Permalink
Add several custom functions for more dynamic gotemplates
Browse files Browse the repository at this point in the history
  • Loading branch information
akrzos authored and rsevilla87 committed Mar 24, 2021
1 parent 23f0e4f commit 07493f8
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 11 deletions.
45 changes: 42 additions & 3 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ It's possible to use `go-template` syntax within this configuration file, also i
This feature can be very useful at the time of defining secrets such as the user and password of our indexer, or a token to use in pprof collection.
# Global
# Global
In this section is described global job configuration, it holds the following parameters:
| Option | Description | Type | Example | Default |
|------------------|----------------------------------------------------------------------------------------------------------|----------------|----------------|-------------|
| kubeconfig | Points to a valid kubeconfig file. Can be omitted if using the KUBECONFIG environment variable, or running from a pod | String | ~/mykubeconfig | in-cluster | |
| writeToFile | Whether to dump collected metrics to files | Boolean | true | true |
| metricsDirectory | Directory where collected metrics will be dumped into. It will be created if it doesn't exist previously | String | ./metrics | ./collected-metrics |
| metricsDirectory | Directory where collected metrics will be dumped into. It will be created if it doesn't exist previously | String | ./metrics | ./collected-metrics |
| measurements | List of measurements. Detailed in the [measurements section] | List | - | [] |
| indexerConfig | Holds the indexer configuration. Detailed in the [indexers section] | Object | - | - |
| requestTimeout | Client-go request timeout | Duration | 5s | 15s |
Expand Down Expand Up @@ -176,6 +176,30 @@ spec:
# Template functions

Apart from the default [golang template semantics](https://golang.org/pkg/text/template/), Kube-burner ships several functions to provide higher dynamism to the template language:
- add: Add two integers

```yaml
apiVersion: v1
data:
two: {{add 1 1}}
anotherInt: {{add .inputIntVariable 1}}
kind: ConfigMap
metadata:
name: configmap-{{.Replica}}
```

- multiply: Multiply two integers

```yaml
apiVersion: v1
data:
eight: {{multiply 2 4}}
anotherInt: {{multiply .inputIntVariable 5}}
kind: ConfigMap
metadata:
name: configmap-{{.Replica}}
```

- rand: This function can be used to generate a random string with the given length. i.e

```yaml
Expand All @@ -188,5 +212,20 @@ metadata:
name: configmap-{{.Replica}}
```

[measurements section]: ../measurements/
- sequence: This function can be used to generate an array with elements to loop over

```yaml
apiVersion: v1
data:
blah: "This has many labels"
kind: ConfigMap
metadata:
name: configmap-{{.Replica}}
labels:
{{ range $index, $element := sequence 1 10 }}
label-{{ $element }}: "true"
{{ end }}
```

[measurements section]: ../measurements/
[indexers section]: ../indexers/
31 changes: 23 additions & 8 deletions pkg/burner/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,29 @@ func prepareTemplate(original []byte) ([]byte, error) {

func renderTemplate(original []byte, data interface{}, options templateOption) ([]byte, error) {
var rendered bytes.Buffer
funcMap := template.FuncMap{"rand": func(length int) string {
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
b := make([]rune, length)
for i := range b {
b[i] = letterRunes[rand.Intn(len(letterRunes))]
}
return string(b)
}}
funcMap := template.FuncMap{
"add": func(a int, b int) int {
return a + b
},
"multiply": func(a int, b int) int {
return a * b
},
"rand": func(length int) string {
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
b := make([]rune, length)
for i := range b {
b[i] = letterRunes[rand.Intn(len(letterRunes))]
}
return string(b)
},
"sequence": func(start int, end int) []int {
var sequence = []int{}
for i := start; i <= end; i++ {
sequence = append(sequence, i)
}
return sequence
},
}
t, err := template.New("").Option(string(options)).Funcs(funcMap).Parse(string(original))
if err != nil {
return nil, fmt.Errorf("Parsing error: %s", err)
Expand Down

0 comments on commit 07493f8

Please sign in to comment.