diff --git a/test/integration/scheduler_perf/create.go b/test/integration/scheduler_perf/create.go index 0b279d57487..4678d4b1ec6 100644 --- a/test/integration/scheduler_perf/create.go +++ b/test/integration/scheduler_perf/create.go @@ -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)) +} \ No newline at end of file diff --git a/test/integration/scheduler_perf/dra/templates/pod-with-claim-ref.yaml b/test/integration/scheduler_perf/dra/templates/pod-with-claim-ref.yaml index 407109aff2b..0a26ce73629 100644 --- a/test/integration/scheduler_perf/dra/templates/pod-with-claim-ref.yaml +++ b/test/integration/scheduler_perf/dra/templates/pod-with-claim-ref.yaml @@ -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}} diff --git a/test/integration/scheduler_perf/dra/templates/pod-with-extended-resource-mod25.yaml b/test/integration/scheduler_perf/dra/templates/pod-with-extended-resource-mod25.yaml index 690762f3cda..cfb68c68369 100644 --- a/test/integration/scheduler_perf/dra/templates/pod-with-extended-resource-mod25.yaml +++ b/test/integration/scheduler_perf/dra/templates/pod-with-extended-resource-mod25.yaml @@ -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 diff --git a/test/integration/scheduler_perf/dra/templates/pod-with-extended-resource-mod50.yaml b/test/integration/scheduler_perf/dra/templates/pod-with-extended-resource-mod50.yaml index 2fd25530ea8..d0895a6102b 100644 --- a/test/integration/scheduler_perf/dra/templates/pod-with-extended-resource-mod50.yaml +++ b/test/integration/scheduler_perf/dra/templates/pod-with-extended-resource-mod50.yaml @@ -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 diff --git a/test/integration/scheduler_perf/dra/templates/pod-with-implicit-extended-resource-mod25.yaml b/test/integration/scheduler_perf/dra/templates/pod-with-implicit-extended-resource-mod25.yaml index 4dd57815aa4..a13f81a3c4f 100644 --- a/test/integration/scheduler_perf/dra/templates/pod-with-implicit-extended-resource-mod25.yaml +++ b/test/integration/scheduler_perf/dra/templates/pod-with-implicit-extended-resource-mod25.yaml @@ -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 diff --git a/test/integration/scheduler_perf/event_handling/templates/nodeupdate-pod-tainttoleration.yaml b/test/integration/scheduler_perf/event_handling/templates/nodeupdate-pod-tainttoleration.yaml index ef466be9751..a4f9aa639c6 100644 --- a/test/integration/scheduler_perf/event_handling/templates/nodeupdate-pod-tainttoleration.yaml +++ b/test/integration/scheduler_perf/event_handling/templates/nodeupdate-pod-tainttoleration.yaml @@ -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: diff --git a/test/integration/scheduler_perf/event_handling/templates/poddelete-pod-blocker-topology-ports-resources.yaml b/test/integration/scheduler_perf/event_handling/templates/poddelete-pod-blocker-topology-ports-resources.yaml index 06df0000b31..774f65f370b 100644 --- a/test/integration/scheduler_perf/event_handling/templates/poddelete-pod-blocker-topology-ports-resources.yaml +++ b/test/integration/scheduler_perf/event_handling/templates/poddelete-pod-blocker-topology-ports-resources.yaml @@ -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 diff --git a/test/integration/scheduler_perf/event_handling/templates/poddelete-pod-nodeports.yaml b/test/integration/scheduler_perf/event_handling/templates/poddelete-pod-nodeports.yaml index ec3372b72a6..7dc4682b322 100644 --- a/test/integration/scheduler_perf/event_handling/templates/poddelete-pod-nodeports.yaml +++ b/test/integration/scheduler_perf/event_handling/templates/poddelete-pod-nodeports.yaml @@ -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 }} diff --git a/test/integration/scheduler_perf/event_handling/templates/podupdate-pod-blocker-update.yaml b/test/integration/scheduler_perf/event_handling/templates/podupdate-pod-blocker-update.yaml index 69a3ee09815..9a47db7d9c6 100644 --- a/test/integration/scheduler_perf/event_handling/templates/podupdate-pod-blocker-update.yaml +++ b/test/integration/scheduler_perf/event_handling/templates/podupdate-pod-blocker-update.yaml @@ -9,5 +9,5 @@ spec: resources: requests: cpu: 0.0001 - memory: {{ div 30000 .Count }}Mi + memory: {{ DivideInt 30000 .Count }}Mi nodeName: scheduler-perf-node diff --git a/test/integration/scheduler_perf/event_handling/templates/podupdate-pod-blocker.yaml b/test/integration/scheduler_perf/event_handling/templates/podupdate-pod-blocker.yaml index f41cb2ac674..a0ce77d2eb5 100644 --- a/test/integration/scheduler_perf/event_handling/templates/podupdate-pod-blocker.yaml +++ b/test/integration/scheduler_perf/event_handling/templates/podupdate-pod-blocker.yaml @@ -12,4 +12,4 @@ spec: resources: requests: cpu: 0.0001 - memory: {{ div 30000 .Count }}Mi + memory: {{ DivideInt 30000 .Count }}Mi diff --git a/test/integration/scheduler_perf/event_handling/templates/podupdate-pod-noderesources.yaml b/test/integration/scheduler_perf/event_handling/templates/podupdate-pod-noderesources.yaml index bf8a51338de..3c5d02ed364 100644 --- a/test/integration/scheduler_perf/event_handling/templates/podupdate-pod-noderesources.yaml +++ b/test/integration/scheduler_perf/event_handling/templates/podupdate-pod-noderesources.yaml @@ -11,4 +11,4 @@ spec: resources: requests: cpu: 0.0001 - memory: {{ div 30000 .Count }}Mi + memory: {{ DivideInt 30000 .Count }}Mi