Extend template functions supported in scheduler perf

This commit is contained in:
Maciej Wyrzuc
2026-02-11 09:24:03 +00:00
parent 158364332a
commit fa36069c7f
11 changed files with 109 additions and 22 deletions

View File

@@ -22,6 +22,7 @@ import (
"fmt"
"html/template"
"os"
"strconv"
"time"
"k8s.io/apimachinery/pkg/api/meta"
@@ -152,13 +153,7 @@ func getSpecFromTextTemplateFile(path string, env map[string]any, spec interface
if err != nil {
return err
}
fm := template.FuncMap{"div": func(a, b int) int {
return a / b
}}
modFn := template.FuncMap{"mod": func(a, b int) int {
return a % b
}}
tmpl, err := template.New("object").Funcs(fm).Funcs(modFn).Parse(string(content))
tmpl, err := template.New("object").Funcs(getTemplateFuncs()).Parse(string(content))
if err != nil {
return err
}
@@ -187,3 +182,95 @@ func restMappingFromUnstructuredObj(tCtx ktesting.TContext, obj *unstructured.Un
}
return mapping, nil
}
func getTemplateFuncs() template.FuncMap {
return template.FuncMap{
"AddFloat": addFloat,
"AddInt": addInt,
"DivideFloat": divideFloat,
"DivideInt": divideInt,
"Mod": mod,
"MultiplyFloat": multiplyFloat,
"MultiplyInt": multiplyInt,
"SubtractFloat": subtractFloat,
"SubtractInt": subtractInt,
}
}
func toFloat64(val interface{}) float64 {
switch i := val.(type) {
case float64:
return i
case float32:
return float64(i)
case int64:
return float64(i)
case int32:
return float64(i)
case int:
return float64(i)
case uint64:
return float64(i)
case uint32:
return float64(i)
case uint:
return float64(i)
case string:
f, err := strconv.ParseFloat(i, 64)
if err == nil {
return f
}
}
panic(fmt.Sprintf("cannot cast %v to float64", val))
}
func addInt(numbers ...interface{}) int {
return int(addFloat(numbers...))
}
func subtractInt(i, j interface{}) int {
return int(subtractFloat(i, j))
}
func multiplyInt(numbers ...interface{}) int {
return int(multiplyFloat(numbers...))
}
func divideInt(i, j interface{}) int {
return int(divideFloat(i, j))
}
func addFloat(numbers ...interface{}) float64 {
sum := 0.0
for _, number := range numbers {
sum += toFloat64(number)
}
return sum
}
func subtractFloat(i, j interface{}) float64 {
typedI := toFloat64(i)
typedJ := toFloat64(j)
return typedI - typedJ
}
func multiplyFloat(numbers ...interface{}) float64 {
product := 1.0
for _, number := range numbers {
product *= toFloat64(number)
}
return product
}
func divideFloat(i, j interface{}) float64 {
typedI := toFloat64(i)
typedJ := toFloat64(j)
if typedJ == 0 {
panic("division by zero")
}
return typedI / typedJ
}
func mod(a interface{}, b interface{}) int {
return int(toFloat64(a)) % int(toFloat64(b))
}

View File

@@ -12,4 +12,4 @@ spec:
resourceClaims:
- name: resource
# Five pods share access to the same claim.
resourceClaimName: test-claim-{{div .Index 5}}
resourceClaimName: test-claim-{{DivideInt .Index 5}}

View File

@@ -10,7 +10,7 @@ spec:
name: pause
resources:
requests:
example.com/gpu-{{mod .Index 25}}: 1
example.com/gpu-{{Mod .Index 25}}: 1
limits:
example.com/gpu-{{mod .Index 25}}: 1
example.com/gpu-{{Mod .Index 25}}: 1

View File

@@ -10,7 +10,7 @@ spec:
name: pause
resources:
requests:
example.com/gpu-{{mod .Index 50}}: 1
example.com/gpu-{{Mod .Index 50}}: 1
limits:
example.com/gpu-{{mod .Index 50}}: 1
example.com/gpu-{{Mod .Index 50}}: 1

View File

@@ -11,6 +11,6 @@ spec:
name: pause
resources:
requests:
deviceclass.resource.kubernetes.io/test-class-{{mod .Index 25}}: 1
deviceclass.resource.kubernetes.io/test-class-{{Mod .Index 25}}: 1
limits:
deviceclass.resource.kubernetes.io/test-class-{{mod .Index 25}}: 1
deviceclass.resource.kubernetes.io/test-class-{{Mod .Index 25}}: 1

View File

@@ -4,7 +4,7 @@ metadata:
generateName: pod-unsched-
spec:
tolerations:
- key: toleration-{{ div .Index 10 }}
- key: toleration-{{ DivideInt .Index 10 }}
operator: Exists
effect: NoSchedule
containers:

View File

@@ -18,8 +18,8 @@ spec:
- image: registry.k8s.io/pause:3.10.1
name: pause
ports:
- hostPort: 8{{ mod .Index 12 }}
containerPort: 8{{ mod .Index 12 }}
- hostPort: 8{{ Mod .Index 12 }}
containerPort: 8{{ Mod .Index 12 }}
resources:
requests:
cpu: 0.35

View File

@@ -9,5 +9,5 @@ spec:
- image: registry.k8s.io/pause:3.10.1
name: pause
ports:
- hostPort: 8{{ mod .Index 12 }}
containerPort: 8{{ mod .Index 12 }}
- hostPort: 8{{ Mod .Index 12 }}
containerPort: 8{{ Mod .Index 12 }}

View File

@@ -9,5 +9,5 @@ spec:
resources:
requests:
cpu: 0.0001
memory: {{ div 30000 .Count }}Mi
memory: {{ DivideInt 30000 .Count }}Mi
nodeName: scheduler-perf-node

View File

@@ -12,4 +12,4 @@ spec:
resources:
requests:
cpu: 0.0001
memory: {{ div 30000 .Count }}Mi
memory: {{ DivideInt 30000 .Count }}Mi

View File

@@ -11,4 +11,4 @@ spec:
resources:
requests:
cpu: 0.0001
memory: {{ div 30000 .Count }}Mi
memory: {{ DivideInt 30000 .Count }}Mi