Skip to content

Commit

Permalink
Add method to import custom template rendering functions (kube-burner…
Browse files Browse the repository at this point in the history
…#727)

## Type of change

- [ ] Refactor
- [x] New feature
- [ ] Bug fix
- [ ] Optimization
- [ ] Documentation Update

## Description

New method to import custom rendering template functions from external
implementations.

## Related Tickets & Documents

- Related Issue #
- Closes #

---------

Signed-off-by: Raul Sevilla <[email protected]>
  • Loading branch information
rsevilla87 authored Nov 19, 2024
1 parent da1ae46 commit 14015b0
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
25 changes: 25 additions & 0 deletions docs/wrappers/wrappers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Kube-burner wrappers

It's possible to extend and take advantage of the kube-burner's features by developing wrappers. A kube-burner wrapper is basically a new binary that uses some of the exposed functions and interfaces of kube-burner.

## Helper functions

There're some helper functions meant to be consumed by wrappers in the `workloads` package. These functions provide some shortcuts to incorporate workloads in golang embedded filesystems.

## Customizing template rendering

It's possible to provide custom template rendering functions from a wrapper, this can be done by calling the function `AddRenderingFunction()` of the util package. For example:

```golang
util.AddRenderingFunction("isEven", func(n int) bool {
return n%2 == 0
})
```

With the above code snippet we're provisioning a new template rendering function `isEven`, that basically returns true or false depending if the provided number is even. This function can be consumed in any of the kube-burner configuration files, for example

```yaml
jobs:
- name: job
podWait: {{ isEven 5 }}
```
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ nav:
- contributing/pullrequest.md
- contributing/release.md
- Tests: contributing/tests.md
- Wrappers: wrappers/index.md
site_name: Kube-burner
plugins:
- search
Expand Down
26 changes: 17 additions & 9 deletions pkg/util/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,17 @@ const (
MissingKeyZero templateOption = "missingkey=zero"
)

// RenderTemplate renders a go-template
func RenderTemplate(original []byte, inputData interface{}, options templateOption) ([]byte, error) {
var rendered bytes.Buffer
funcMap := sprig.GenericFuncMap()
funcMap["Binomial"] = combin.Binomial
funcMap["IndexToCombination"] = combin.IndexToCombination
funcMap["GetSubnet24"] = func(subnetIdx int) string {
var funcMap = sprig.GenericFuncMap()

func init() {
AddRenderingFunction("Binomial", combin.Binomial)
AddRenderingFunction("IndexToCombination", combin.IndexToCombination)
funcMap["GetSubnet24"] = func(subnetIdx int) string { // TODO Document this function
return netip.AddrFrom4([4]byte{byte(subnetIdx>>16 + 1), byte(subnetIdx >> 8), byte(subnetIdx), 0}).String() + "/24"
}
// This function returns number of addresses requested per iteration from the list of total provided addresses
funcMap["GetIPAddress"] = func(Addresses string, iteration int, addressesPerIteration int) string {
funcMap["GetIPAddress"] = func(Addresses string, iteration int, addressesPerIteration int) string { // TODO Move this function to kube-burner-ocp
var retAddrs []string

addrSlice := strings.Split(Addresses, " ")
for i := 0; i < addressesPerIteration; i++ {
// For example, if iteration=6 and addressesPerIteration=2, return 12th address from list.
Expand All @@ -55,6 +53,16 @@ func RenderTemplate(original []byte, inputData interface{}, options templateOpti
}
return strings.Join(retAddrs, " ")
}
}

func AddRenderingFunction(name string, function any) {
log.Debugf("Importing template function: %s", name)
funcMap[name] = function
}

// RenderTemplate renders a go-template
func RenderTemplate(original []byte, inputData interface{}, options templateOption) ([]byte, error) {
var rendered bytes.Buffer
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 14015b0

Please sign in to comment.