Skip to content

Commit f73268c

Browse files
authored
Enhance Kubernetes Resource Quantity Comparisons in Templating System (#1147)
* support kubernetes resources quantity compaisons Signed-off-by: YuChen <[email protected]> * add kubernetes resources quantity tests Signed-off-by: YuChen <[email protected]> * update the document for templating system Signed-off-by: YuChen <[email protected]> * polish the dynamic configuration with templating Signed-off-by: YuChen <[email protected]> * removed nil error Signed-off-by: YuChen <[email protected]> * added use cases Signed-off-by: YuChen <[email protected]> --------- Signed-off-by: YuChen <[email protected]>
1 parent a783430 commit f73268c

File tree

3 files changed

+388
-130
lines changed

3 files changed

+388
-130
lines changed

controllers/operator/manager.go

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
authorizationv1 "k8s.io/api/authorization/v1"
3333
corev1 "k8s.io/api/core/v1"
3434
apierrors "k8s.io/apimachinery/pkg/api/errors"
35+
"k8s.io/apimachinery/pkg/api/resource"
3536
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
3637
"k8s.io/apimachinery/pkg/runtime"
3738
"k8s.io/apimachinery/pkg/types"
@@ -961,21 +962,47 @@ func (m *ODLMOperator) EvaluateExpression(ctx context.Context, expr *util.Logica
961962
return leftVal, rightVal, nil
962963
}
963964

965+
// Helper function to compare values with support for Kubernetes resource units
966+
compareValues := func(leftVal, rightVal string) (leftIsGreater bool, rightIsEqual bool, rightIsGreater bool) {
967+
// Check if both are resource quantities
968+
leftQty, leftErr := resource.ParseQuantity(strings.TrimSpace(leftVal))
969+
rightQty, rightErr := resource.ParseQuantity(strings.TrimSpace(rightVal))
970+
971+
// If both are valid Kubernetes quantities, compare their values
972+
if leftErr == nil && rightErr == nil {
973+
cmp := leftQty.Cmp(rightQty)
974+
return cmp > 0, cmp == 0, cmp < 0
975+
}
976+
977+
// Fall back to standard float parsing
978+
leftNum, leftFloatErr := strconv.ParseFloat(strings.TrimSpace(leftVal), 64)
979+
rightNum, rightFloatErr := strconv.ParseFloat(strings.TrimSpace(rightVal), 64)
980+
981+
if leftFloatErr == nil && rightFloatErr == nil {
982+
return leftNum > rightNum, leftNum == rightNum, leftNum < rightNum
983+
}
984+
985+
// Fall back to string comparison
986+
return leftVal > rightVal, leftVal == rightVal, leftVal < rightVal
987+
}
988+
964989
// Handle basic comparison operators
965990
if expr.Equal != nil {
966991
leftVal, rightVal, err := getComparisonValues(expr.Equal.Left, expr.Equal.Right)
967992
if err != nil {
968993
return false, err
969994
}
970-
return leftVal == rightVal, nil
995+
_, isEqual, _ := compareValues(leftVal, rightVal)
996+
return isEqual, nil
971997
}
972998

973999
if expr.NotEqual != nil {
9741000
leftVal, rightVal, err := getComparisonValues(expr.NotEqual.Left, expr.NotEqual.Right)
9751001
if err != nil {
9761002
return false, err
9771003
}
978-
return leftVal != rightVal, nil
1004+
_, isEqual, _ := compareValues(leftVal, rightVal)
1005+
return !isEqual, nil
9791006
}
9801007

9811008
if expr.GreaterThan != nil {
@@ -984,13 +1011,8 @@ func (m *ODLMOperator) EvaluateExpression(ctx context.Context, expr *util.Logica
9841011
return false, err
9851012
}
9861013

987-
leftNum, leftErr := strconv.ParseFloat(strings.TrimSpace(leftVal), 64)
988-
rightNum, rightErr := strconv.ParseFloat(strings.TrimSpace(rightVal), 64)
989-
990-
if leftErr == nil && rightErr == nil {
991-
return leftNum > rightNum, nil
992-
}
993-
return leftVal > rightVal, nil
1014+
leftGreater, _, _ := compareValues(leftVal, rightVal)
1015+
return leftGreater, nil
9941016
}
9951017

9961018
if expr.LessThan != nil {
@@ -999,13 +1021,8 @@ func (m *ODLMOperator) EvaluateExpression(ctx context.Context, expr *util.Logica
9991021
return false, err
10001022
}
10011023

1002-
leftNum, leftErr := strconv.ParseFloat(strings.TrimSpace(leftVal), 64)
1003-
rightNum, rightErr := strconv.ParseFloat(strings.TrimSpace(rightVal), 64)
1004-
1005-
if leftErr == nil && rightErr == nil {
1006-
return leftNum < rightNum, nil
1007-
}
1008-
return leftVal < rightVal, nil
1024+
_, _, leftLess := compareValues(leftVal, rightVal)
1025+
return leftLess, nil
10091026
}
10101027

10111028
// Handle logical operators

controllers/operator/manager_test.go

Lines changed: 124 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ func TestEvaluateExpression(t *testing.T) {
561561
expectError: false,
562562
},
563563
{
564-
name: "GreaterThan comparison with strings",
564+
name: "GreaterThan comparison with strings - true",
565565
expr: &util.LogicalExpression{
566566
GreaterThan: &util.ValueComparison{
567567
Left: &util.ValueSource{Literal: "xyz"},
@@ -594,7 +594,7 @@ func TestEvaluateExpression(t *testing.T) {
594594
expectError: false,
595595
},
596596
{
597-
name: "LessThan comparison with strings",
597+
name: "LessThan comparison with strings - true",
598598
expr: &util.LogicalExpression{
599599
LessThan: &util.ValueComparison{
600600
Left: &util.ValueSource{Literal: "abc"},
@@ -604,6 +604,128 @@ func TestEvaluateExpression(t *testing.T) {
604604
expectedResult: true,
605605
expectError: false,
606606
},
607+
// Kubernetes resource quantity tests
608+
{
609+
name: "GreaterThan comparison with memory quantities - true",
610+
expr: &util.LogicalExpression{
611+
GreaterThan: &util.ValueComparison{
612+
Left: &util.ValueSource{Literal: "10Gi"},
613+
Right: &util.ValueSource{Literal: "2Gi"},
614+
},
615+
},
616+
expectedResult: true,
617+
expectError: false,
618+
},
619+
{
620+
name: "GreaterThan comparison with memory quantities - false",
621+
expr: &util.LogicalExpression{
622+
GreaterThan: &util.ValueComparison{
623+
Left: &util.ValueSource{Literal: "100Mi"},
624+
Right: &util.ValueSource{Literal: "1Gi"},
625+
},
626+
},
627+
expectedResult: false,
628+
expectError: false,
629+
},
630+
{
631+
name: "LessThan comparison with memory quantities - true",
632+
expr: &util.LogicalExpression{
633+
LessThan: &util.ValueComparison{
634+
Left: &util.ValueSource{Literal: "500Mi"},
635+
Right: &util.ValueSource{Literal: "1Gi"},
636+
},
637+
},
638+
expectedResult: true,
639+
expectError: false,
640+
},
641+
{
642+
name: "LessThan comparison with memory quantities - false",
643+
expr: &util.LogicalExpression{
644+
LessThan: &util.ValueComparison{
645+
Left: &util.ValueSource{Literal: "5Gi"},
646+
Right: &util.ValueSource{Literal: "2Gi"},
647+
},
648+
},
649+
expectedResult: false,
650+
expectError: false,
651+
},
652+
{
653+
name: "Equal comparison with CPU quantities",
654+
expr: &util.LogicalExpression{
655+
Equal: &util.ValueComparison{
656+
Left: &util.ValueSource{Literal: "1000m"},
657+
Right: &util.ValueSource{Literal: "1"},
658+
},
659+
},
660+
expectedResult: true,
661+
expectError: false,
662+
},
663+
{
664+
name: "Equal comparison with different unit formats",
665+
expr: &util.LogicalExpression{
666+
Equal: &util.ValueComparison{
667+
Left: &util.ValueSource{Literal: "1Gi"},
668+
Right: &util.ValueSource{Literal: "1024Mi"},
669+
},
670+
},
671+
expectedResult: true,
672+
expectError: false,
673+
},
674+
{
675+
name: "GreaterThan comparison with CPU quantities",
676+
expr: &util.LogicalExpression{
677+
GreaterThan: &util.ValueComparison{
678+
Left: &util.ValueSource{Literal: "2"},
679+
Right: &util.ValueSource{Literal: "1500m"},
680+
},
681+
},
682+
expectedResult: true,
683+
expectError: false,
684+
},
685+
{
686+
name: "GreaterThan comparison with mixed format units",
687+
expr: &util.LogicalExpression{
688+
GreaterThan: &util.ValueComparison{
689+
Left: &util.ValueSource{Literal: "1Mi"},
690+
Right: &util.ValueSource{Literal: "1024Ki"},
691+
},
692+
},
693+
expectedResult: false,
694+
expectError: false,
695+
},
696+
{
697+
name: "Mixed resource types - comparing memory and CPU",
698+
expr: &util.LogicalExpression{
699+
Equal: &util.ValueComparison{
700+
Left: &util.ValueSource{Literal: "100m"},
701+
Right: &util.ValueSource{Literal: "100Mi"},
702+
},
703+
},
704+
expectedResult: false,
705+
expectError: false,
706+
},
707+
{
708+
name: "NotEqual comparison with memory quantities - true",
709+
expr: &util.LogicalExpression{
710+
NotEqual: &util.ValueComparison{
711+
Left: &util.ValueSource{Literal: "2Gi"},
712+
Right: &util.ValueSource{Literal: "1Gi"},
713+
},
714+
},
715+
expectedResult: true,
716+
expectError: false,
717+
},
718+
{
719+
name: "NotEqual comparison with memory quantities - false",
720+
expr: &util.LogicalExpression{
721+
NotEqual: &util.ValueComparison{
722+
Left: &util.ValueSource{Literal: "1Gi"},
723+
Right: &util.ValueSource{Literal: "1024Mi"},
724+
},
725+
},
726+
expectedResult: false,
727+
expectError: false,
728+
},
607729
{
608730
name: "And operator - all true",
609731
expr: &util.LogicalExpression{

0 commit comments

Comments
 (0)