Skip to content

Commit

Permalink
Merge pull request #1260 from MondayCha/main
Browse files Browse the repository at this point in the history
Fix quantity scaling in cel calculations
  • Loading branch information
k8s-ci-robot authored Nov 8, 2024
2 parents 4039962 + 5e2d202 commit d2a420f
Show file tree
Hide file tree
Showing 4 changed files with 384 additions and 12 deletions.
7 changes: 5 additions & 2 deletions pkg/kwok/metrics/evaluator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package metrics

import (
"context"
"math"
"testing"
"time"

Expand Down Expand Up @@ -112,7 +113,9 @@ func TestResourceEvaluation(t *testing.T) {
t.Fatalf("evaluation failed: %v", err)
}

if actual != 18 {
t.Errorf("expected %v, got %v", 18, actual)
const epsilon = 1e-9
expected := 1.8
if math.Abs(actual-expected) > epsilon {
t.Errorf("expected %v, got %v", expected, actual)
}
}
72 changes: 72 additions & 0 deletions pkg/utils/cel/funcs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
Copyright 2024 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cel

import (
"testing"
"time"

"github.com/google/cel-go/common/types"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestTimeNow(t *testing.T) {
refVal := runAndCheckExpression(t, "Now()", types.TimestampType)

actual := refVal.Value().(time.Time)

if !expectedTime.Equal(actual) {
t.Errorf("expected %v, got %v", expectedTime, actual)
}
}

func TestMathRand(t *testing.T) {
refVal := runAndCheckExpression(t, "Rand()", types.DoubleType)
actual := refVal.Value().(float64)

if actual < 0 || actual > 1 {
t.Errorf("expected value between 0 and 1, got %v", refVal.Value())
}
}

func TestUnixSecond(t *testing.T) {
refVal := runAndCheckExpressionWithData(t, "UnixSecond(time)", map[string]any{
"time": expectedTime,
}, types.DoubleType)

actual := refVal.Value().(float64)
expected := float64(expectedTime.Unix())

compareFloat64(t, expected, actual)
}

func TestSinceSecond(t *testing.T) {
n := &corev1.Node{
ObjectMeta: metav1.ObjectMeta{
CreationTimestamp: metav1.Time{Time: expectedTime},
},
}
refVal := runAndCheckExpressionWithData(t, "SinceSecond(node)", map[string]any{
"node": n,
}, types.DoubleType)

actual := refVal.Value().(float64)
if actual < 0 {
t.Errorf("expected positive value, got %v", actual)
}
}
20 changes: 10 additions & 10 deletions pkg/utils/cel/quantity.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type Quantity struct {
// NewQuantity creates a new Quantity
func NewQuantity(q *resource.Quantity) Quantity {
if q == nil {
q = resource.NewScaledQuantity(0, resource.Nano)
q = resource.NewMilliQuantity(0, resource.DecimalSI)
}
return Quantity{Quantity: q}
}
Expand All @@ -61,18 +61,18 @@ func NewQuantityFromString(s string) (Quantity, error) {
return NewQuantity(&r), nil
}

func newQuantityFromNanoInt64(v int64) Quantity {
r := resource.NewScaledQuantity(v, resource.Nano)
func newQuantityFromMilliInt64(v int64) Quantity {
r := resource.NewMilliQuantity(v, resource.DecimalSI)
return NewQuantity(r)
}

func newQuantityFromFloat64(v float64) Quantity {
r := resource.NewScaledQuantity(int64(v*10e9), resource.Nano)
r := resource.NewMilliQuantity(int64(v*1e3), resource.DecimalSI)
return NewQuantity(r)
}

func (q Quantity) nano() int64 {
return q.Quantity.ScaledValue(resource.Nano)
func (q Quantity) milli() int64 {
return q.Quantity.MilliValue()
}

func (q Quantity) float() float64 {
Expand Down Expand Up @@ -157,10 +157,10 @@ func (q Quantity) Divide(other ref.Val) ref.Val {
switch other.Type() {
case types.IntType:
otherInt := other.(types.Int)
return newQuantityFromNanoInt64(q.nano() / int64(otherInt))
return newQuantityFromMilliInt64(q.milli() / int64(otherInt))
case types.UintType:
otherUint := other.(types.Uint)
return newQuantityFromNanoInt64(q.nano() / int64(otherUint))
return newQuantityFromMilliInt64(q.milli() / int64(otherUint))
case types.DoubleType:
otherDouble := other.(types.Double)
return newQuantityFromFloat64(q.float() / float64(otherDouble))
Expand All @@ -174,10 +174,10 @@ func (q Quantity) Multiply(other ref.Val) ref.Val {
switch other.Type() {
case types.IntType:
otherInt := other.(types.Int)
return newQuantityFromNanoInt64(q.nano() * int64(otherInt))
return newQuantityFromMilliInt64(q.milli() * int64(otherInt))
case types.UintType:
otherUint := other.(types.Uint)
return newQuantityFromNanoInt64(q.nano() * int64(otherUint))
return newQuantityFromMilliInt64(q.milli() * int64(otherUint))
case types.DoubleType:
otherDouble := other.(types.Double)
return newQuantityFromFloat64(q.float() * float64(otherDouble))
Expand Down
Loading

0 comments on commit d2a420f

Please sign in to comment.