Skip to content

Commit

Permalink
Deprecate custom template functions (kube-burner#302)
Browse files Browse the repository at this point in the history
* Remove custom template functions

Signed-off-by: Raul Sevilla <[email protected]>

* Add TerminationGracePeriodSeconds for preload namespace

Signed-off-by: Raul Sevilla <[email protected]>

---------

Signed-off-by: Raul Sevilla <[email protected]>
  • Loading branch information
rsevilla87 authored Apr 28, 2023
1 parent 0e788d3 commit 410079b
Show file tree
Hide file tree
Showing 14 changed files with 31 additions and 118 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ kind: ConfigMap
metadata:
name: {{.JobName}}-{{.Replica}}
data:
key1: "{{rand 2048}}"
key1: "{{randAlphaNum 2048}}"
8 changes: 4 additions & 4 deletions cmd/kube-burner/ocp-config/cluster-density-ms/deployment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ spec:
name: cluster-density
env:
- name: ENVVAR1
value: "{{rand 250}}"
value: "{{randAlphaNum 250}}"
- name: ENVVAR2
value: "{{rand 250}}"
value: "{{randAlphaNum 250}}"
- name: ENVVAR3
value: "{{rand 250}}"
value: "{{randAlphaNum 250}}"
- name: ENVVAR4
value: "{{rand 250}}"
value: "{{randAlphaNum 250}}"
volumes:
- name: secret-1
secret:
Expand Down
2 changes: 1 addition & 1 deletion cmd/kube-burner/ocp-config/cluster-density-ms/secret.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ kind: Secret
metadata:
name: {{.JobName}}-{{.Replica}}
data:
top-secret: "{{rand 2048}}"
top-secret: "{{randAlphaNum 2048}}"
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ kind: ConfigMap
metadata:
name: {{.JobName}}-{{.Replica}}
data:
key1: "{{rand 2048}}"
key1: "{{randAlphaNum 2048}}"
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,17 @@ spec:
mountPath: /etc/podlabels
env:
- name: ENVVAR1
value: "{{rand 250}}"
value: "{{randAlphaNum 250}}"
- name: ENVVAR2
value: "{{rand 250}}"
value: "{{randAlphaNum 250}}"
- name: ENVVAR3
value: "{{rand 250}}"
value: "{{randAlphaNum 250}}"
- name: ENVVAR4
value: "{{rand 250}}"
value: "{{randAlphaNum 250}}"
- name: ROUTE_ENDPOINT
value: "https://cluster-density-{{.Replica}}-cluster-density-v2-{{.Iteration}}.{{ .ingressDomain }}/256.html"
- name: SERVICE_ENDPOINT
value: "http://cluster-density-{{randInteger 1 5}}/256.html"
value: "http://cluster-density-{{randInt 1 6}}/256.html"
volumes:
- name: secret-1
secret:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ spec:
name: cluster-density
env:
- name: ENVVAR1
value: "{{rand 250}}"
value: "{{randAlphaNum 250}}"
- name: ENVVAR2
value: "{{rand 250}}"
value: "{{randAlphaNum 250}}"
- name: ENVVAR3
value: "{{rand 250}}"
value: "{{randAlphaNum 250}}"
- name: ENVVAR4
value: "{{rand 250}}"
value: "{{randAlphaNum 250}}"
volumes:
- name: secret-1
secret:
Expand Down
2 changes: 1 addition & 1 deletion cmd/kube-burner/ocp-config/cluster-density-v2/secret.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ kind: Secret
metadata:
name: {{.JobName}}-{{.Replica}}
data:
top-secret: "{{rand 2048}}"
top-secret: "{{randAlphaNum 2048}}"
2 changes: 1 addition & 1 deletion cmd/kube-burner/ocp-config/cluster-density/configmap.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ kind: ConfigMap
metadata:
name: {{.JobName}}-{{.Replica}}
data:
key1: "{{rand 2048}}"
key1: "{{randAlphaNum 2048}}"
8 changes: 4 additions & 4 deletions cmd/kube-burner/ocp-config/cluster-density/deployment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ spec:
name: cluster-density
env:
- name: ENVVAR1
value: "{{rand 250}}"
value: "{{randAlphaNum 250}}"
- name: ENVVAR2
value: "{{rand 250}}"
value: "{{randAlphaNum 250}}"
- name: ENVVAR3
value: "{{rand 250}}"
value: "{{randAlphaNum 250}}"
- name: ENVVAR4
value: "{{rand 250}}"
value: "{{randAlphaNum 250}}"
volumes:
- name: secret-1
secret:
Expand Down
2 changes: 1 addition & 1 deletion cmd/kube-burner/ocp-config/cluster-density/secret.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ kind: Secret
metadata:
name: {{.JobName}}-{{.Replica}}
data:
top-secret: "{{rand 2048}}"
top-secret: "{{randAlphaNum 2048}}"
51 changes: 0 additions & 51 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,57 +245,6 @@ spec:
## Template functions

Apart from the default [golang template semantics](https://golang.org/pkg/text/template/), Kube-burner is compiled with the library [sprig](http://masterminds.github.io/sprig/), which adds over 70 template functions for Go’s template language.
In addition, kube-burner ships some several extra functions:

- multiply: Multiply two integers

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

- randInteger: Generates a positive random integer between two numbers.

```yaml
apiVersion: v1
data:
number: {{randInteger 0 100}}
kind: ConfigMap
metadata:
name: configmap-{{.Replica}}
```

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

```yaml
apiVersion: v1
data:
myfile: {{rand 512}}
myOtherFile: {{rand .inputIntVariable}}
kind: ConfigMap
metadata:
name: configmap-{{.Replica}}
```

- 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/
4 changes: 3 additions & 1 deletion pkg/burner/pre_load.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/utils/pointer"
)

const preLoadNs = "preload-kube-burner"
Expand Down Expand Up @@ -123,7 +124,8 @@ func createDSs(imageList []string, namespaceLabels map[string]string) error {
Labels: map[string]string{"app": dsName},
},
Spec: corev1.PodSpec{
InitContainers: []corev1.Container{container},
TerminationGracePeriodSeconds: pointer.Int64(0),
InitContainers: []corev1.Container{container},
// Only Always restart policy is supported
Containers: []corev1.Container{
{
Expand Down
43 changes: 2 additions & 41 deletions pkg/util/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,12 @@ package util
import (
"bytes"
"fmt"
"math/rand"
"os"
"strings"
"text/template"
"time"

sprig "github.com/Masterminds/sprig/v3"
log "github.com/sirupsen/logrus"
"github.com/spf13/cast"
)

type templateOption string
Expand All @@ -35,46 +32,10 @@ const (
MissingKeyZero templateOption = "missingkey=zero"
)

func init() {
rand.Seed(time.Now().UnixNano())
}

// RenderTemplate renders a go-template and adds several custom functions
// RenderTemplate renders a go-template
func RenderTemplate(original []byte, inputData interface{}, options templateOption) ([]byte, error) {
var rendered bytes.Buffer
funcMap := sprig.GenericFuncMap()
extraFuncs := map[string]interface{}{
"multiply": func(a interface{}, b ...interface{}) int {
res := cast.ToInt(a)
for _, v := range b {
res = res * cast.ToInt(v)
}
return res
},
"rand": func(length interface{}) string {
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
b := make([]rune, cast.ToInt(length))
for i := range b {
b[i] = letterRunes[rand.Intn(len(letterRunes))]
}
return string(b)
},
"sequence": func(start, end interface{}) []int {
var sequence = []int{}
for i := cast.ToInt(start); i <= cast.ToInt(end); i++ {
sequence = append(sequence, i)
}
return sequence
},
"randInteger": func(a, b interface{}) int {
return rand.Intn(cast.ToInt(b)) + cast.ToInt(a)
},
}
for k, v := range extraFuncs {
funcMap[k] = v
}
sprig.GenericFuncMap()
t, err := template.New("").Option(string(options)).Funcs(funcMap).Parse(string(original))
t, err := template.New("").Option(string(options)).Funcs(sprig.GenericFuncMap()).Parse(string(original))
if err != nil {
return nil, fmt.Errorf("parsing error: %s", err)
}
Expand Down
5 changes: 3 additions & 2 deletions test/kube-burner-delete.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ global:
jobs:
- name: not-namespaced
jobType: create
jobIterations: {{randInteger 1 2}}
qps: {{randInt 5 6 }}
jobIterations: {{randInt 1 3}}
qps: {{randInt 5 10 }}
burst: 15
namespacedIterations: false
cleanup: true
Expand All @@ -33,6 +33,7 @@ jobs:
replicas: 1
inputVars:
containerImage: gcr.io/google_containers/pause-amd64:3.0
wait: true


- name: delete-job
Expand Down

0 comments on commit 410079b

Please sign in to comment.